
    h,                         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	e      Z G d
 de      Z G d de      Zy)    N)Path)AnyCallableOptional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t%        d	      | j)                         st+        d
      t        j                  j	                  | j,                  |	      | _        || _        y )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/   rE   	__class__s             V/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/torchvision/datasets/lfw.pyr5   z_LFW.__init__    s    	dD,<,<=euv'	(9;H[H[H]^.2nnT^^.L+
DM48"4::<(G9LM	#EKKM7<WX
"ii[)D(ETJ!	_  $$&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)NFr2   T)r
   r6   r7   r8   r(   r=   r>   r@   	checksumsr+   names)rG   st1st2s      rI   rC   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y | j                   | j                   }t        || j                  | j                  | j
                         t        | j                   | j                   | j                         | j                  dk(  r/t        | j                   | j                   | j                         y y )N)r=   r>   r2   )
rC   download_url_prefixr=   r   r(   r>   r   r@   r+   rM   )rG   urls     rI   r.   z_LFW.downloadK   s      "))*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)r6   r7   r8   rE   int)rG   rS   rT   s      rI   	_get_pathz_LFW._get_pathT   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_reprW   s    T^^,Idjj\BBrJ   c                 ,    t        | j                        S )N)lenrA   r[   s    rI   __len__z_LFW.__len__Z   s    499~rJ   )r0   N)__name__
__module____qualname__r9   rQ   r;   rL   r?   rM   r	   r   strr   r   r   boolr   r5   rC   r.   rX   rY   r\   r_   __classcell__rH   s   @rI   r   r   
   s.   K< K\hI 8>?8?@;I YGJE )-/3'5 CI    	 
   H%  #8,    #$  
 D$ OX# X5c? Xs XCC C rJ   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.

    .. 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/   r0   c           
          t         |   |||d||||       | j                         | _        | j	                         \  | _        | _        y )Nr2   r/   )r4   r5   _get_classesclass_to_idx_get_peoplerA   targets	rG   r(   r)   r*   r,   r-   r.   r/   rH   s	           rI   r5   zLFWPeople.__init__w   sN     	ui9FVX`iop --/"&"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   	)openr6   r7   r8   r(   r@   	readlinesr)   rX   rangestrip	enumeraterY   appendrl   )rG   rA   rn   flinesn_foldssfoldn_linesliner2   irS   num_imgsnumimgs                   rI   rm   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   )
rr   r6   r7   r8   r(   rM   rs   ru   r)   rv   )rG   rx   ry   r~   rM   r   namerl   s           rI   rk   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.
        )rF   rA   rn   r,   r-   )rG   r   r   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): )r4   r\   r^   rl   )rG   rH   s    rI   r\   zLFWPeople.extra_repr   s*    w!#(@TEVEVAW@X&YYYrJ   )r`   ra   rb   __doc__r	   rc   r   r   rd   r   r5   tuplelistrX   rm   dictrk   r   r\   re   rf   s   @rI   rh   rh   ^   s    6 #(,/3'555 5 	5
 H%5 #8,5 5 #$5 
5U49d3i#78 $d38n  sCx &ZC Z ZrJ   rh   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.

    .. 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/   r0   c           
          t         |   |||d||||       | j                  | j                        \  | _        | _        | _        y )Nr3   rj   )r4   r5   
_get_pairsrE   
pair_namesrA   rn   ro   s	           rI   r5   zLFWPairs.__init__   sE     	ui)EUW_hno37??4??3S0DLrJ   rE   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   rq   r         )rr   r6   r7   r8   r(   r@   rs   r)   rX   rt   ru   rY   rw   )rG   rE   r   rA   rn   rx   ry   rz   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.
        )rA   rF   rn   r,   r-   )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`   ra   rb   r   r	   rc   r   r   rd   r   r5   r   r   rX   r   r   re   rf   s   @rI   r   r      s    8 #(,/3'5TT T 	T
 H%T #8,T T #$T 
T)S )U4c3h3H$uUXZ]U]J_aefiaj3j-k )8" "sC})= "rJ   r   )r6   pathlibr   typingr   r   r   r   folderr	   utilsr
   r   r   r   visionr   r   rh   r    rJ   rI   <module>r      sK    	  1 1 " ^ ^ !Q= QhUZ UZpV"t V"rJ   