
    kh+                         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   )default_loader)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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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#   NFrootsplit	image_setview	transformtarget_transformdownloadloader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_loader)selfr+   r,   r-   r.   r/   r0   r1   r2   rG   	__class__s             T/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/datasets/lfw.pyr8   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*=    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)NFr5   T)r   r9   r:   r;   r+   r@   rA   rC   	checksumsr.   names)rI   st1st2s      rK   rE   z_LFW._check_integrity>   s    bggll499dmmDdhhObggll499d6F6FGX\XhXhIij#99 "277<<		4::#FW[WaWaHbccrL   c                    | j                         ry | j                   | j                   }t        || j                  | j                  | j
                         t        | j                   | j                   | j                         | j                  dk(  r/t        | j                   | j                   | j                         y y )N)r@   rA   r5   )
rE   download_url_prefixr@   r   r+   rA   r   rC   r.   rO   )rI   urls     rK   r1   z_LFW.downloadG   s      "))*4==/:$S$))dmmQUQYQYZ001$2B2B1CDdiiP99 D445djj\BDIIN !rL   identitynoc           	      x    t         j                  j                  | j                  || dt	        |      dd      S )N_04dz.jpg)r9   r:   r;   rG   int)rI   rU   rV   s      rK   	_get_pathz_LFW._get_pathP   s1    ww||DOOX(1SWSMQU7VWWrL   c                 :    d| j                    d| j                   S )NzAlignment: z
Split: )r-   r,   rI   s    rK   
extra_reprz_LFW.extra_reprS   s    T^^,Idjj\BBrL   c                 ,    t        | j                        S )N)lenrD   r]   s    rK   __len__z_LFW.__len__V   s    499~rL   )r3   N)__name__
__module____qualname__r<   rS   r>   rN   rB   rO   r   r
   strr   r   r   boolr   r8   rE   r1   rZ   r[   r^   ra   __classcell__rJ   s   @rK   r   r   
   s-   K< K\hI 8>?8?@;I YGJE )-/3'5CI  	
  H% #8,  #$ 
<$ OX# X5c? Xs XCC C rL   r   c                        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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 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): If true, downloads the dataset from the internet and
            puts it in root directory. If dataset is already downloaded, it is not
            downloaded again.
        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/   r0   r1   r2   r3   c           
          t         |   |||d||||       | j                         | _        | j	                         \  | _        | _        y )Nr5   r2   )r7   r8   _get_classesclass_to_idx_get_peoplerD   targets	rI   r+   r,   r-   r/   r0   r1   r2   rJ   s	           rK   r8   zLFWPeople.__init__p   sN     	ui9FVX`iop --/"&"2"2"4	4<rL   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   	)openr9   r:   r;   r+   rC   	readlinesr,   rZ   rangestrip	enumerater[   appendrn   )rI   rD   rp   flinesn_foldssfoldn_linesliner5   irU   num_imgsnumimgs                   rK   ro   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   )
rt   r9   r:   r;   r+   rO   ru   rw   r,   rx   )rI   rz   r{   r   rO   r   namern   s           rK   rm   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.
        )rH   rD   rp   r/   r0   )rI   r   r   targets       rK   __getitem__zLFWPeople.__getitem__   sg     ll499U+,e$>>%..%C  ,**62FF{rL   c                 R    t         |          dt        | j                         z   S )Nz
Classes (identities): )r7   r^   r`   rn   )rI   rJ   s    rK   r^   zLFWPeople.extra_repr   s*    w!#(@TEVEVAW@X&YYYrL   )rb   rc   rd   __doc__r   re   r   r   rf   r   r8   r	   r   rZ   ro   r   rm   r   r^   rg   rh   s   @rK   rj   rj   Z   s    0 #(,/3'555 5 	5
 H%5 #8,5 5 #$5 
5U49d3i#78 $d38n  sCx &ZC Z ZrL   rj   c                        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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.
        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/   r0   r1   r2   r3   c           
          t         |   |||d||||       | j                  | j                        \  | _        | _        | _        y )Nr6   rl   )r7   r8   
_get_pairsrG   
pair_namesrD   rp   rq   s	           rK   r8   zLFWPairs.__init__   sE     	ui)EUW_hno37??4??3S0DLrL   rG   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   rs   r         )rt   r9   r:   r;   r+   rC   ru   r,   rZ   rv   rw   r[   ry   )rI   rG   r   rD   rp   rz   r{   r|   n_pairsr}   r~   r   matched_pairsunmatched_pairspairimg1img2sames                     rK   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.
        )rD   rH   rp   r/   r0   )rI   r   r   r   r   s        rK   r   zLFWPairs.__getitem__   s     YYu%
d\\$'d);de$>>%-t~~d/C$D  ,**62FT6!!rL   )rb   rc   rd   r   r   re   r   r   rf   r   r8   r	   r   rZ   r   r   rg   rh   s   @rK   r   r      s    2 #(,/3'5TT T 	T
 H%T #8,T T #$T 
T)S )U4c3h3H$uUXZ]U]J_aefiaj3j-k )8" "sC})= "rL   r   )r9   pathlibr   typingr   r   r   r   r   r	   r
   folderr   utilsr   r   r   r   visionr   r   rj   r    rL   rK   <module>r      sN    	  D D D " ^ ^ !M= M`RZ RZjS"t S"rL   