o
    0i@)                     @   s   d dl Z d dlmZ d dlmZmZmZmZmZm	Z	m
Z
 d dlmZ ddlmZmZmZmZ ddlmZ G dd	 d	eZG d
d deZG dd deZdS )    N)Path)AnyCallableDictListOptionalTupleUnion)Image   )check_integritydownload_and_extract_archivedownload_urlverify_str_arg)VisionDatasetc                       s   e Zd ZdZdZddddZddd	d
ddddZddddZdZ			d/de	e
ef de
de
de
dee dee deddf fddZd e
dejfd!d"Zdefd#d$Zd0d%d&Zd'e
d(e	ee
f de
fd)d*Zde
fd+d,Zdefd-d.Z  ZS )1_LFWzlfw-pyz http://vis-www.cs.umass.edu/lfw/)lfwzlfw.tgz a17d05bd522c52d84eca14327a23d494)lfw_funneledzlfw-funneled.tgz 1b42dfed7d15c9b2dd63d5e5840c86ad)zlfw-deepfunneledzlfw-deepfunneled.tgz 68331da3eb755a505a502b5aacb3c201)originalfunneleddeepfunneled 9f1ba174e4e1c508ff7cdf10ac338a7d 5132f7440eb68cf58910c8a45a2ac10b 4f27cbf15b2da4a85c1907eb4181ad21 450f0863dd89e85e73936a6d71a3474b e4bf5be0a43b5dcd9dc5ccfcb8fb19c5 54eaac34beb6d042ed3a7d883e247a21 a6d0a479bd074669f656265a6e693f6d)z	pairs.txtzpairsDevTest.txtzpairsDevTrain.txtz
people.txtzpeopleDevTest.txtzpeopleDevTrain.txtlfw-names.txt DevTrainDevTest10foldtraintestr!   NFrootsplit	image_setview	transformtarget_transformdownloadreturnc           	         s   t  jtj|| j||d t| d| j	 | _
| j| j
 \}| _| _t| dddg| _t| dg d| _| j | j| j  d| _g | _|rR|   |  sZtd	tj| j|| _d S )
N)r-   r.   r+   r,   peoplepairsr*   r%   z.txtzHDataset not found or corrupted. You can use download=True to download it)super__init__ospathjoinbase_folderr   lower	file_dictkeysr+   filenamemd5r,   r*   
annot_filelabels_filedatar/   _check_integrityRuntimeErrorr)   
images_dir)	selfr)   r*   r+   r,   r-   r.   r/   rC   	__class__ V/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torchvision/datasets/lfw.pyr4   !   s   
z_LFW.__init__r6   c                 C   sB   t |d}t |}|dW  d    S 1 sw   Y  d S )NrbRGB)openr
   convert)rD   r6   fimgrG   rG   rH   _loader=   s   
$z_LFW._loaderc                 C   st   t tj| j| j| j}t tj| j| j| j| j }|r!|s#dS | j	dkr8t tj| j| j
| j| j
 S dS )NFr1   T)r   r5   r6   r7   r)   r<   r=   r?   	checksumsr,   names)rD   st1st2rG   rG   rH   rA   B   s    
 z_LFW._check_integrityc                 C   s|   |   r
td d S | j | j }t|| j| j| jd t| j | j | j | j	dkr<t| j | j
 | j d S d S )Nz%Files already downloaded and verified)r<   r=   r1   )rA   printdownload_url_prefixr<   r   r)   r=   r   r?   r,   rQ   )rD   urlrG   rG   rH   r/   K   s   
z_LFW.downloadidentitynoc                 C   s$   t j| j|| dt|ddS )N_04dz.jpg)r5   r6   r7   rC   int)rD   rW   rX   rG   rG   rH   	_get_pathU   s   $z_LFW._get_pathc                 C   s   d| j  d| j S )NzAlignment: z
Split: )r+   r*   rD   rG   rG   rH   
extra_reprX   s   z_LFW.extra_reprc                 C   s
   t | jS )N)lenr@   r]   rG   rG   rH   __len__[   s   
z_LFW.__len__)NNF)r0   N)__name__
__module____qualname__r8   rU   r:   rP   r>   rQ   r	   strr   r   r   boolr4   r
   rO   rA   r/   r[   r\   r^   r`   __classcell__rG   rG   rE   rH   r      sV    	
	
	
r   c                       s   e Zd ZdZ					ddededed	ee d
ee deddf fddZde	e
e e
e f fddZdeeef fddZdede	eef fddZdef fddZ  ZS )	LFWPeoplea  `LFW <http://vis-www.cs.umass.edu/lfw/>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of dataset where directory
            ``lfw-py`` exists or will be saved to if download is set to True.
        split (string, optional): The image split to use. Can be one of ``train``, ``test``,
            ``10fold`` (default).
        image_set (str, optional): Type of image funneling to use, ``original``, ``funneled`` or
            ``deepfunneled``. Defaults to ``funneled``.
        transform (callable, optional): A function/transform that  takes in a PIL image
            and returns a transformed version. E.g, ``transforms.RandomRotation``
        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.

    r&   r   NFr)   r*   r+   r-   r.   r/   r0   c              	      s6   t  |||d||| |  | _|  \| _| _d S )Nr1   )r3   r4   _get_classesclass_to_idx_get_peopler@   targetsrD   r)   r*   r+   r-   r.   r/   rE   rG   rH   r4   s   s   	
zLFWPeople.__init__c                 C   s  g g }}t tj| j| jn}| }| jdkr"t|d dfnd\}}t	|D ]I}t|| }dd ||d || d  D }	||d 7 }t
|	D ]%\}
\}}t	dt|d D ]}| ||}|| || j|  q\qMq*W d    ||fS 1 sw   Y  ||fS )Nr&   r   r   )r   r   c                 S      g | ]	}|  d qS 	stripr*   .0linerG   rG   rH   
<listcomp>       z)LFWPeople._get_people.<locals>.<listcomp>)rK   r5   r6   r7   r)   r?   	readlinesr*   r[   range	enumerater\   appendri   )rD   r@   rk   rM   linesn_foldssfoldn_linesr1   irW   num_imgsnumrN   rG   rG   rH   rj      s(   
""

zLFWPeople._get_peoplec                 C   sb   t tj| j| j}| }dd |D }W d    n1 s!w   Y  dd t|D }|S )Nc                 S   s   g | ]
}|   d  qS )r   rp   rr   rG   rG   rH   ru      s    z*LFWPeople._get_classes.<locals>.<listcomp>c                 S   s   i | ]\}}||qS rG   rG   )rs   r   namerG   rG   rH   
<dictcomp>   s    z*LFWPeople._get_classes.<locals>.<dictcomp>)rK   r5   r6   r7   r)   rQ   rw   ry   )rD   rM   r{   rQ   ri   rG   rG   rH   rh      s   zLFWPeople._get_classesindexc                 C   sJ   |  | j| }| j| }| jdur| |}| jdur!| |}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: Tuple (image, target) where target is the identity of the person.
        N)rO   r@   rk   r-   r.   )rD   r   rN   targetrG   rG   rH   __getitem__   s   




zLFWPeople.__getitem__c                    s   t   dt| j  S )Nz
Classes (identities): )r3   r^   r_   ri   r]   rE   rG   rH   r^      s   zLFWPeople.extra_reprr&   r   NNF)ra   rb   rc   __doc__rd   r   r   re   r4   r   r   r[   rj   r   rh   r   r   r^   rf   rG   rG   rE   rH   rg   _   s4    rg   c                       s   e Zd ZdZ					ddededed	ee d
ee deddf fddZdede	e
e	eef  e
e	eef  e
e f fddZdede	eeef fddZ  ZS )LFWPairsa  `LFW <http://vis-www.cs.umass.edu/lfw/>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of dataset where directory
            ``lfw-py`` exists or will be saved to if download is set to True.
        split (string, optional): The image split to use. Can be one of ``train``, ``test``,
            ``10fold``. Defaults to ``10fold``.
        image_set (str, optional): Type of image funneling to use, ``original``, ``funneled`` or
            ``deepfunneled``. Defaults to ``funneled``.
        transform (callable, optional): A function/transform that takes in a PIL image
            and returns a transformed version. E.g, ``transforms.RandomRotation``
        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.

    r&   r   NFr)   r*   r+   r-   r.   r/   r0   c              	      s4   t  |||d||| | | j\| _| _| _d S )Nr2   )r3   r4   
_get_pairsrC   
pair_namesr@   rk   rl   rE   rG   rH   r4      s   	zLFWPairs.__init__rC   c              	   C   s  g g g }}}t tj| j| j}| }| jdkr0|d d\}}t|t|}}n	dt|d }}d}	t	|D ]}
dd ||	|	|  D }dd ||	| |	d|   D }|	d| 7 }	|D ]1}| 
|d |d | 
|d |d d}}}||d |d f |||f || qg|D ]1}| 
|d |d | 
|d |d	 d}}}||d |d f |||f || qq?W d    n1 sw   Y  |||fS )
Nr&   r   ro   r   c                 S   rm   rn   rp   rr   rG   rG   rH   ru      rv   z'LFWPairs._get_pairs.<locals>.<listcomp>c                 S   rm   rn   rp   rr   rG   rG   rH   ru      rv         )rK   r5   r6   r7   r)   r?   rw   r*   r[   rx   r\   rz   )rD   rC   r   r@   rk   rM   r{   r|   n_pairsr}   r~   matched_pairsunmatched_pairspairimg1img2samerG   rG   rH   r      s4   
"00
zLFWPairs._get_pairsr   c                 C   sl   | j | \}}| || |}}| j| }| jdur'| || |}}| jdur1| |}|||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image1, image2, target) where target is `0` for different indentities and `1` for same identities.
        N)r@   rO   rk   r-   r.   )rD   r   r   r   r   rG   rG   rH   r      s   




zLFWPairs.__getitem__r   )ra   rb   rc   r   rd   r   r   re   r4   r   r   r[   r   r   r   rf   rG   rG   rE   rH   r      s0    8$r   )r5   pathlibr   typingr   r   r   r   r   r   r	   PILr
   utilsr   r   r   r   visionr   r   rg   r   rG   rG   rG   rH   <module>   s    $TR