o
    h,                     @   s   d dl Z d dlmZ d dl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OptionalUnion   )default_loader)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de	fde
eef dedededee dee dedeegef ddf fdd Zdefd!d"Zd-d#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 )._LFWzlfw-pyz http://vis-www.cs.umass.edu/lfw/)Zlfwzlfw.tgzZ a17d05bd522c52d84eca14327a23d494)Zlfw_funneledzlfw-funneled.tgzZ 1b42dfed7d15c9b2dd63d5e5840c86ad)zlfw-deepfunneledzlfw-deepfunneled.tgzZ 68331da3eb755a505a502b5aacb3c201)originalfunneledZdeepfunneledZ 9f1ba174e4e1c508ff7cdf10ac338a7dZ 5132f7440eb68cf58910c8a45a2ac10bZ 4f27cbf15b2da4a85c1907eb4181ad21Z 450f0863dd89e85e73936a6d71a3474bZ e4bf5be0a43b5dcd9dc5ccfcb8fb19c5Z 54eaac34beb6d042ed3a7d883e247a21Z a6d0a479bd074669f656265a6e693f6d)z	pairs.txtzpairsDevTest.txtzpairsDevTrain.txtz
people.txtzpeopleDevTest.txtzpeopleDevTrain.txtlfw-names.txt ZDevTrainZDevTest10foldtraintestr   NFrootsplit	image_setview	transformtarget_transformdownloadloader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 | _|rRtd	|  sZtd
tj| j|	| _|| _d S )N)r   r   r   r   peoplepairsr   r   z.txtz|LFW dataset is no longer available for download.Please download the dataset manually and place it in the specified directoryzHDataset 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data
ValueErrorr   _check_integrityRuntimeErrorr   
images_dir_loader)
selfr   r   r   r   r   r   r   r   r3   	__class__ ^/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torchvision/datasets/lfw.pyr#       s   
z_LFW.__init__c                 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 )NFr    T)r	   r$   r%   r&   r   r+   r,   r.   	checksumsr   names)r5   Zst1Zst2r8   r8   r9   r1   B   s    
 z_LFW._check_integrityc                 C   st   |   rd S | j | j }t|| j| j| jd t| j | j | j | jdkr8t| j | j	 | j d S d S )N)r+   r,   r    )
r1   download_url_prefixr+   r
   r   r,   r   r.   r   r;   )r5   urlr8   r8   r9   r   K   s   
z_LFW.downloadidentitynoc                 C   s$   t j| j|| dt|ddS )N_Z04dz.jpg)r$   r%   r&   r3   int)r5   r>   r?   r8   r8   r9   	_get_pathT   s   $z_LFW._get_pathc                 C   s   d| j  d| j S )NzAlignment: z
Split: )r   r   r5   r8   r8   r9   
extra_reprW   s   z_LFW.extra_reprc                 C   s
   t | jS )N)lenr/   rC   r8   r8   r9   __len__Z   s   
z_LFW.__len__)r   N)__name__
__module____qualname__r'   r<   r)   r:   r-   r;   r   r   strr   r   r   boolr   r#   r1   r   rA   rB   rD   rF   __classcell__r8   r8   r6   r9   r   
   sZ    	
	
"
		r   c                       s   e Zd ZdZdddddefdededed	ee d
ee dedeege	f 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.

    .. warning:

        The LFW dataset is no longer available for automatic download. Please
        download it manually and place it in the specified directory.

    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 or torch.Tensor, depends on the given loader,
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        download (bool, optional): NOT SUPPORTED ANYMORE, leave to False.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    r   r   NFr   r   r   r   r   r   r   r   c              
      s:   t  j|||d||||d |  | _|  \| _| _d S )Nr    r   )r"   r#   _get_classesclass_to_idx_get_peopler/   targetsr5   r   r   r   r   r   r   r   r6   r8   r9   r#   w   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liner8   r8   r9   
<listcomp>       z)LFWPeople._get_people.<locals>.<listcomp>)openr$   r%   r&   r   r.   	readlinesr   rA   range	enumeraterB   appendrP   )r5   r/   rR   flinesn_foldssfoldZn_linesr    ir>   Znum_imgsnumimgr8   r8   r9   rQ      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   rW   rY   r8   r8   r9   r\      s    z*LFWPeople._get_classes.<locals>.<listcomp>c                 S   s   i | ]\}}||qS r8   r8   )rZ   rh   namer8   r8   r9   
<dictcomp>   s    z*LFWPeople._get_classes.<locals>.<dictcomp>)r^   r$   r%   r&   r   r;   r_   ra   )r5   rc   rd   r;   rP   r8   r8   r9   rO      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)r4   r/   rR   r   r   )r5   rm   rj   targetr8   r8   r9   __getitem__   s   




zLFWPeople.__getitem__c                    s   t   dt| j  S )Nz
Classes (identities): )r"   rD   rE   rP   rC   r6   r8   r9   rD      s   zLFWPeople.extra_repr)rG   rH   rI   __doc__r   rJ   r   r   rK   r   r#   tuplelistrA   rQ   dictrO   ro   rD   rL   r8   r8   r6   r9   rM   ^   s:    	rM   c                       s   e Zd ZdZdddddefdededed	ee d
ee dedeege	f 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.

    .. warning:

        The LFW dataset is no longer available for automatic download. Please
        download it manually and place it in the specified directory.

    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): NOT SUPPORTED ANYMORE, leave to False.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.

    r   r   NFr   r   r   r   r   r   r   r   c              
      s8   t  j|||d||||d | | j\| _| _| _d S )Nr!   rN   )r"   r#   
_get_pairsr3   
pair_namesr/   rR   rS   r6   r8   r9   r#      s   
zLFWPairs.__init__r3   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   rV   r   c                 S   rT   rU   rW   rY   r8   r8   r9   r\      r]   z'LFWPairs._get_pairs.<locals>.<listcomp>c                 S   rT   rU   rW   rY   r8   r8   r9   r\      r]         )r^   r$   r%   r&   r   r.   r_   r   rA   r`   rB   rb   )r5   r3   rv   r/   rR   rc   rd   re   Zn_pairsrf   rg   Zmatched_pairsZunmatched_pairspairimg1img2Zsamer8   r8   r9   ru      s4   
"00
zLFWPairs._get_pairsrm   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/   r4   rR   r   r   )r5   rm   rz   r{   rn   r8   r8   r9   ro      s   




zLFWPairs.__getitem__)rG   rH   rI   rp   r   rJ   r   r   rK   r   r#   rq   rr   rA   ru   ro   rL   r8   r8   r6   r9   rt      s6    	8$rt   )r$   pathlibr   typingr   r   r   r   folderr   utilsr	   r
   r   r   Zvisionr   r   rM   rt   r8   r8   r8   r9   <module>   s    TX