
    h^!                         d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlZd dlZddlmZmZmZmZ ddlmZ  edg d	      Z G d
 de      Zy)    N)
namedtuple)Path)AnyCallableOptionalUnion   )check_integritydownload_file_from_google_driveextract_archiveverify_str_arg)VisionDatasetCSV)headerindexdatac                        e Zd ZdZdZg dZ	 	 	 	 	 ddeeef   dedee	e   ef   de
e   d	e
e   d
eddf fdZ	 ddede
e   defdZdefdZddZdedeeef   fdZdefdZdefdZ xZS )CelebAa  `Large-scale CelebFaces Attributes (CelebA) Dataset <http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory where images are downloaded to.
        split (string): One of {'train', 'valid', 'test', 'all'}.
            Accordingly dataset is selected.
        target_type (string or list, optional): Type of target to use, ``attr``, ``identity``, ``bbox``,
            or ``landmarks``. Can also be a list to output a tuple with all specified target types.
            The targets represent:

                - ``attr`` (Tensor shape=(40,) dtype=int): binary (0, 1) labels for attributes
                - ``identity`` (int): label for each person (data points with the same identity are the same person)
                - ``bbox`` (Tensor shape=(4,) dtype=int): bounding box (x, y, width, height)
                - ``landmarks`` (Tensor shape=(10,) dtype=int): landmark points (lefteye_x, lefteye_y, righteye_x,
                  righteye_y, nose_x, nose_y, leftmouth_x, leftmouth_y, rightmouth_x, rightmouth_y)

            Defaults to ``attr``. If empty, ``None`` will be returned as target.

        transform (callable, optional): A function/transform that takes in a PIL image
            and returns a transformed version. E.g, ``transforms.PILToTensor``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        download (bool, optional): If true, downloads the dataset from the internet and
            puts it in root directory. If dataset is already downloaded, it is not
            downloaded again.

            .. warning::

                To download the dataset `gdown <https://github.com/wkentaro/gdown>`_ is required.
    celeba))0B7EVK8r0v71pZjFTYXZWM3FlRnM 00d2c5bc6d35e252742224ab0c1e8fcbimg_align_celeba.zip)0B7EVK8r0v71pblRyaVFSWGxPY0U 75e246fa4810816ffd6ee81facbd244clist_attr_celeba.txt)z!1_ee_0u7vcNLOfNLegJRHmolfH5ICW-XS 32bd1bd63d3c78cd57e08160ec5ed1e2identity_CelebA.txt)0B7EVK8r0v71pbThiMVRxWXZ4dU0 00566efa6fedff7a56946cd1c10f1c16list_bbox_celeba.txt)0B7EVK8r0v71pd0FJY3Blby1HUTQ cc24ecafdb5b50baae59b03474781f8clist_landmarks_align_celeba.txt)0B7EVK8r0v71pY0NSMzRuSXJEVkk d32c9cbf5e040fd4025c592c306e6668list_eval_partition.txtNrootsplittarget_type	transformtarget_transformdownloadreturnc                    t         |   |||       || _        t        |t              r|| _        n|g| _        | j
                  s| j                  t        d      |r| j                          | j                         st        d      dddd d}|t        t        |t              r|j                         n|dd         }| j                  d	      }	| j                  d
      }
| j                  dd      }| j                  dd      }| j                  dd      }|t        d       n|	j                  |k(  j!                         }|t        d       k(  r|	j"                  | _        nHt'        j                   t'        j(                  |            D cg c]  }|	j"                  |    c}| _        |
j                  |   | _        |j                  |   | _        |j                  |   | _        |j                  |   | _        t'        j2                  | j0                  dz   dd      | _        |j4                  | _        y c c}w )N)r*   r+   z6target_transform is specified but target_type is emptyzHDataset not found or corrupted. You can use download=True to download itr   r	      )trainvalidtestallr(   r&   r   r    )r   r#   r   floor)rounding_mode)super__init__r(   
isinstancelistr)   r+   RuntimeErrorr,   _check_integrityr   strlower	_load_csvslicer   squeezer   filenametorchnonzeroidentitybboxlandmarks_alignattrdivr   
attr_names)selfr'   r(   r)   r*   r+   r,   	split_mapsplit_splitsrD   rE   rF   rG   maski	__class__s                   Y/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/torchvision/datasets/celeba.pyr7   zCelebA.__init__A   s    	EUV
k4(*D +}DD$9$9$EWXXMMO$$&ijj 	
	 !+E3!7U1
  9:>>"78~~4Q~?..)JST.U~~4Q~?$nuT{6;;&3H2Q2Q2S5;"LLDM6;mmEMMRVDW6XYV\\!_YDM d+IIdO	.33D9IIdO	IIdii!mQgF	++ Zs   H;rA   r   c           	          t        t        j                  j                  | j                  | j
                  |            5 }t        t        j                  |dd            }d d d        ||   }||dz   d  }ng }D cg c]  }|d   	 }}|D cg c]  }|dd  	 }}|D cg c]  }t        t        t        |             }	}t        ||t        j                  |	            S # 1 sw Y   xY wc c}w c c}w c c}w )N T)	delimiterskipinitialspacer	   r   )openospathjoinr'   base_folderr9   csvreadermapintr   rB   tensor)
rJ   rA   r   csv_filer   headersrowindicesrO   data_ints
             rQ   r>   zCelebA._load_csv{   s    
 "'',,tyy$*:*:HEF 	T(

8sTRSD	T 6lG
%DG%)*c3q6**#'(CAB((/34!DS!%447GU\\(%;<<	T 	T +(4s   "C%?C1C6# C;%C.c                    | j                   D ]p  \  }}}t        j                  j                  | j                  | j
                  |      }t        j                  j                  |      \  }}|dvsct        ||      rp y t        j                  j                  t        j                  j                  | j                  | j
                  d            S )N)z.zipz.7zFimg_align_celeba)		file_listrW   rX   rY   r'   rZ   splitextr
   isdir)rJ   _md5rA   fpathexts         rQ   r;   zCelebA._check_integrity   s     $ 	AsHGGLLD,<,<hGEWW%%h/FAs /)/%2M	 ww}}RWW\\$))T5E5EGYZ[[    c                 L   | j                         ry | j                  D ]F  \  }}}t        |t        j                  j                  | j                  | j                        ||       H t        t        j                  j                  | j                  | j                  d             y )Nr   )	r;   rg   r   rW   rX   rY   r'   rZ   r   )rJ   file_idrk   rA   s       rQ   r,   zCelebA.download   s|      "&*nn 	o"GS(+GRWW\\$))TM]M]5^`hjmn	o 	TYY0@0@BXYZrn   r   c           	         t         j                  j                  t        j                  j                  | j                  | j                  d| j                  |               }g }| j                  D ]  }|dk(  r#|j                  | j                  |d d f          +|dk(  r!|j                  | j                  |df          Q|dk(  r#|j                  | j                  |d d f          y|dk(  r#|j                  | j                  |d d f          t        d| d       | j                   | j!                  |      }|r?t#        |      d	kD  rt%        |      n|d   }| j&                  | j'                  |      }||fS d }||fS )
Nrf   rG   rD   r   rE   	landmarkszTarget type "z" is not recognized.r	   )PILImagerV   rW   rX   rY   r'   rZ   rA   r)   appendrG   rD   rE   rF   
ValueErrorr*   lentupler+   )rJ   r   Xtargetts        rQ   __getitem__zCelebA.__getitem__   sd   IINN277<<		43C3CEWY]YfYfglYmno!! 	JAF{diiq12jdmmE1H56fdiiq12k!d225!8<= !=3G!HII	J >>%q!A&)&kAoU6]6!9F$$0..v6 &y F&yrn   c                 ,    t        | j                        S N)rw   rG   )rJ   s    rQ   __len__zCelebA.__len__   s    499~rn   c                 `    ddg} dj                  |      j                  di | j                  S )NzTarget type: {target_type}zSplit: {split}
 )rY   format__dict__)rJ   liness     rQ   
extra_reprzCelebA.extra_repr   s0    -/?@&tyy&&777rn   )r0   rG   NNFr~   )r-   N)__name__
__module____qualname____doc__rZ   rg   r   r<   r   r9   r   r   boolr7   r^   r   r>   r;   r,   rx   r   r|   r   r   __classcell__)rP   s   @rQ   r   r      s   > KI  -3(,/38&CI8& 8& 49c>*	8&
 H%8& #8,8& 8& 
8&z !%== = 
	=(
\$ 
\[ sCx < 8C 8rn   r   )r[   rW   collectionsr   pathlibr   typingr   r   r   r   rs   rB   utilsr
   r   r   r   visionr   r   r   r   rn   rQ   <module>r      sA    
 	 "  1 1 
  d d !34w8] w8rn   