
    ,}h@)                         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	e      Z G d
 de      Z G d de      Zy)    N)Path)AnyCallableDictListOptionalTupleUnion)Image   )check_integritydownload_and_extract_archivedownload_urlverify_str_arg)VisionDatasetc                       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Zde
dej                   fdZdefd Zd(d!Zd"e
d#e	ee
f   de
fd$Zde
fd%Zdefd&Z xZS ))_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#   Nrootsplit	image_setview	transformtarget_transformdownloadreturnc                    t         	|   t        j                  j	                  || j
                        ||       t        |j                         d| j                  j                               | _
        | j                  | j                     \  }| _        | _        t        |j                         dddg      | _        t        |j                         dg d      | _        | j                   | j                  | j                      d| _        g | _        |r| j%                          | j'                         st)        d	      t        j                  j	                  | j*                  |      | _        y )
N)r/   r0   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datar1   _check_integrityRuntimeErrorr+   
images_dir)
selfr+   r,   r-   r.   r/   r0   r1   rF   	__class__s
            W/var/www/html/test/engine/venv/lib/python3.12/site-packages/torchvision/datasets/lfw.pyr7   z_LFW.__init__!   s    	dD,<,<=euv'	(9;H[H[H]^.2nnT^^.L+
DM48"4::<(G9LM	#EKKM7<WX
"ii[)D(ETJ!	MMO$$&ijj'',,tyy*=    r9   c                     t        |d      5 }t        j                   |      }|j                  d      cd d d        S # 1 sw Y   y xY w)NrbRGB)openr   convert)rG   r9   fimgs       rI   _loaderz_LFW._loader=   s<    $ 	&**Q-C;;u%	& 	& 	&s	   &=Ac                    t        t        j                  j                  | j                  | j
                        | j                        }t        t        j                  j                  | j                  | j                        | j                  | j                           }|r|sy| j                  dk(  rUt        t        j                  j                  | j                  | j                        | j                  | j                           S y)NFr4   T)r   r8   r9   r:   r+   r?   r@   rB   	checksumsr.   names)rG   st1st2s      rI   rD   z_LFW._check_integrityB   s    bggll499dmmDdhhObggll499d6F6FGX\XhXhIij#99 "277<<		4::#FW[WaWaHbccrJ   c                    | j                         rt        d       y | j                   | j                   }t	        || j
                  | j                  | j                         t        | j                   | j                   | j
                         | j                  dk(  r/t        | j                   | j                   | j
                         y y )Nz%Files already downloaded and verified)r?   r@   r4   )rD   printdownload_url_prefixr?   r   r+   r@   r   rB   r.   rU   )rG   urls     rI   r1   z_LFW.downloadK   s      "9:))*4==/:$S$))dmmQUQYQYZ001$2B2B1CDdiiP99 D445djj\BDIIN !rJ   identitynoc           	      x    t         j                  j                  | j                  || dt	        |      dd      S )N_04dz.jpg)r8   r9   r:   rF   int)rG   r\   r]   s      rI   	_get_pathz_LFW._get_pathU   s1    ww||DOOX(1SWSMQU7VWWrJ   c                 :    d| j                    d| j                   S )NzAlignment: z
Split: )r-   r,   rG   s    rI   
extra_reprz_LFW.extra_reprX   s    T^^,Idjj\BBrJ   c                 ,    t        | j                        S )N)lenrC   rd   s    rI   __len__z_LFW.__len__[   s    499~rJ   )NNF)r2   N)__name__
__module____qualname__r;   rZ   r=   rT   rA   rU   r
   strr   r   r   boolr7   r   rR   rD   r1   ra   rb   re   rh   __classcell__rH   s   @rI   r   r      s,   K< K\hI 8>?8?@;I YGJE )-/3>CI> > 	>
 > H%> #8,> > 
>8&C &EKK &
$ OX# X5c? Xs XCC C rJ   r   c                        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
Zd	e	e
e   e
e   f   fdZd	eeef   fdZded	e	eef   fdZd	ef fdZ x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.

    Nr+   r,   r-   r/   r0   r1   r2   c           	          t         |   |||d|||       | j                         | _        | j	                         \  | _        | _        y )Nr4   )r6   r7   _get_classesclass_to_idx_get_peoplerC   targetsrG   r+   r,   r-   r/   r0   r1   rH   s          rI   r7   zLFWPeople.__init__s   sH     	ui9FVX`a --/"&"2"2"4	4<rJ   c           	         g g }}t        t        j                  j                  | j                  | j
                              5 }|j                         }| j                  dk(  rt        |d         dfnd\  }}t        |      D ]  }t        ||         }||dz   ||z   dz    D 	cg c]!  }	|	j                         j                  d      # }
}	||dz   z  }t        |
      D ]f  \  }\  }}t        dt        |      dz         D ]C  }| j                  ||      }|j                  |       |j                  | j                  |          E h  	 d d d        ||fS c c}	w # 1 sw Y   ||fS xY w)Nr(   r   r   )r   r   	)rN   r8   r9   r:   r+   rB   	readlinesr,   ra   rangestrip	enumeraterb   appendrt   )rG   rC   rv   rP   linesn_foldssfoldn_linesliner4   ir\   num_imgsnumrQ   s                   rI   ru   zLFWPeople._get_people   sj   Bg"'',,tyy$*:*:;< 	DKKME/3zzX/E#eAh-+6JGQg DeAh-?DQUQQX[[\_?]^t$**,,,T2^^Wq[ /8/@ D+A+($QH(9: D"nnXs;C(t'8'8'BCDD	D		D W} _	D W}s    A!E#&E	B EEE'c                 x   t        t        j                  j                  | j                  | j
                              5 }|j                         }|D cg c]#  }|j                         j                         d   % }}d d d        t              D ci c]  \  }}||
 }}}|S c c}w # 1 sw Y   .xY wc c}}w )Nr   )
rN   r8   r9   r:   r+   rU   rz   r|   r,   r}   )rG   rP   r   r   rU   r   namert   s           rI   rs   zLFWPeople._get_classes   s    "'',,tyy$**56 	@!KKME9>?TZZ\'')!,?E?	@ 09/?@GAta@@ @	@ 	@ As#   B*(B%;B*B6%B**B3indexc                     | j                  | j                  |         }| j                  |   }| j                  | j                  |      }| j                  | j	                  |      }||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: Tuple (image, target) where target is the identity of the person.
        )rR   rC   rv   r/   r0   )rG   r   rQ   targets       rI   __getitem__zLFWPeople.__getitem__   sg     ll499U+,e$>>%..%C  ,**62FF{rJ   c                 R    t         |          dt        | j                         z   S )Nz
Classes (identities): )r6   re   rg   rt   )rG   rH   s    rI   re   zLFWPeople.extra_repr   s*    w!#(@TEVEVAW@X&YYYrJ   r(   r   NNF)ri   rj   rk   __doc__rl   r   r   rm   r7   r	   r   ra   ru   r   rs   r   r   re   rn   ro   s   @rI   rq   rq   _   s    , #(,/355 5 	5
 H%5 #8,5 5 
5U49d3i#78 $d38n  sCx &ZC Z ZrJ   rq   c                        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
Zded	e	e
e	eef      e
e	eef      e
e   f   fdZded	e	eeef   fdZ x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.

    Nr+   r,   r-   r/   r0   r1   r2   c           	          t         |   |||d|||       | j                  | j                        \  | _        | _        | _        y )Nr5   )r6   r7   
_get_pairsrF   
pair_namesrC   rv   rw   s          rI   r7   zLFWPairs.__init__   s?     	ui)EUW_`37??4??3S0DLrJ   rF   c           	      p   g g g }}}t        t        j                  j                  | j                  | j
                              5 }|j                         }| j                  dk(  r.|d   j                  d      \  }}t        |      t        |      }}ndt        |d         }}d}	t        |      D ]a  }
||	|	|z    D cg c]!  }|j                         j                  d      # }}||	|z   |	d|z  z    D cg c]!  }|j                         j                  d      # }}|	d|z  z  }	|D ]q  }| j                  |d   |d         | j                  |d   |d         d}}}|j                  |d   |d   f       |j                  ||f       |j                  |       s |D ]q  }| j                  |d   |d         | j                  |d   |d         d}}}|j                  |d   |d   f       |j                  ||f       |j                  |       s d 	 d d d        |||fS c c}w c c}w # 1 sw Y   xY w)Nr(   r   ry   r         )rN   r8   r9   r:   r+   rB   rz   r,   ra   r{   r|   rb   r~   )rG   rF   r   rC   rv   rP   r   r   n_pairsr   r   r   matched_pairsunmatched_pairspairimg1img2sames                     rI   r   zLFWPairs._get_pairs   sF   $&B'D
"'',,tyy$*:*:;< 	)KKMEzzX%#(8>>$#7 #&w<W#$c%(mAg )FKAPQT[P[F\ ]d!3!3D!9 ] ]HMaRYk\]abelal\mHn"o4::<#5#5d#;"o"oQ[ ) )D'+~~d1gtAw'GX\]^X_aefgahIikl$D%%tAwQ&89KKt-NN4(	)
 , )D'+~~d1gtAw'GX\]^X_aefgahIikl$D%%tAwQ&89KKt-NN4(	))	)0 4(( !^"o	) 	)s,   A9H,=&H"#H,5&H'C9H,"
H,,H5r   c                 (   | j                   |   \  }}| j                  |      | j                  |      }}| j                  |   }| j                  "| j                  |      | j                  |      }}| j                  | j	                  |      }|||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image1, image2, target) where target is `0` for different indentities and `1` for same identities.
        )rC   rR   rv   r/   r0   )rG   r   r   r   r   s        rI   r   zLFWPairs.__getitem__   s     YYu%
d\\$'d);de$>>%-t~~d/C$D  ,**62FT6!!rJ   r   )ri   rj   rk   r   rl   r   r   rm   r7   r	   r   ra   r   r   r   rn   ro   s   @rI   r   r      s    , #(,/3TT T 	T
 H%T #8,T T 
T)S )U4c3h3H$uUXZ]U]J_aefiaj3j-k )8" "sC})= "rJ   r   )r8   pathlibr   typingr   r   r   r   r   r	   r
   PILr   utilsr   r   r   r   visionr   r   rq   r    rJ   rI   <module>r      sN    	  D D D  ^ ^ !Q= QhOZ OZdO"t O"rJ   