Ë
    Dêñiá"  ã                   ó˜   — d dl Z d dlZ 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 ddlmZ  G d„ d	e«      Z G d
„ de«      Zy)é    N)ÚPath)ÚAnyÚCallableÚOptionalÚUnion)ÚImageé   )Údownload_and_extract_archiveÚextract_archiveÚverify_str_arg)ÚVisionDatasetc                   ó²   ‡ — e Zd ZdZ	 	 	 	 ddeeef   deee   ef   dee	   dee	   de
ddfˆ fd	„Zd
edeeef   fd„Zde
fd„Zdefd„Zdd„Zdefd„Zˆ xZS )Ú
Caltech101a?  `Caltech 101 <https://data.caltech.edu/records/20086>`_ Dataset.

    .. warning::

        This class needs `scipy <https://docs.scipy.org/doc/>`_ to load target files from `.mat` format.

    Args:
        root (str or ``pathlib.Path``): Root directory of dataset where directory
            ``caltech101`` exists or will be saved to if download is set to True.
        target_type (string or list, optional): Type of target to use, ``category`` or
            ``annotation``. Can also be a list to output a tuple with all specified
            target types.  ``category`` represents the target class, and
            ``annotation`` is a list of points from a hand-generated outline.
            Defaults to ``category``.
        transform (callable, optional): A function/transform that takes in a PIL image
            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.

            .. warning::

                To download the dataset `gdown <https://github.com/wkentaro/gdown>`_ is required.
    NÚrootÚtarget_typeÚ	transformÚtarget_transformÚdownloadÚreturnc           
      ó  •‡
— t         ‰| �  t        j                  j	                  |d«      ||¬«       t        j
                  | j                  d¬«       t        |t        «      r|g}|D �cg c]  }t        |dd«      ‘Œ c}| _
        |r| j                  «        | j                  «       st        d«      ‚t        t        j                  t        j                  j	                  | j                  d«      «      «      | _        | j                   j#                  d	«       d
ddddœŠ
t%        t'        ˆ
fd„| j                   «      «      | _        g | _        g | _        t/        | j                   «      D ]“  \  }}t1        t        j                  t        j                  j	                  | j                  d|«      «      «      }	| j*                  j3                  t5        d|	dz   «      «       | j,                  j3                  |	|gz  «       Œ• y c c}w )NÚ
caltech101©r   r   T©Úexist_okr   )ÚcategoryÚ
annotationúHDataset not found or corrupted. You can use download=True to download itÚ101_ObjectCategoriesÚBACKGROUND_GoogleÚFaces_2ÚFaces_3ÚMotorbikes_16ÚAirplanes_Side_2)ÚFacesÚ
Faces_easyÚ
MotorbikesÚ	airplanesc                 ó   •— | ‰v r‰|    S | S ©N© )ÚxÚname_maps    €ú^/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/torchvision/datasets/caltech.pyú<lambda>z%Caltech101.__init__.<locals>.<lambda>I   s   ø€ ÀqÈHÁ}¸À¹€ ÐZ[€ ó    r	   )ÚsuperÚ__init__ÚosÚpathÚjoinÚmakedirsr   Ú
isinstanceÚstrr   r   r   Ú_check_integrityÚRuntimeErrorÚsortedÚlistdirÚ
categoriesÚremoveÚlistÚmapÚannotation_categoriesÚindexÚyÚ	enumerateÚlenÚextendÚrange)Úselfr   r   r   r   r   ÚtÚiÚcÚnr,   Ú	__class__s             @€r-   r1   zCaltech101.__init__)   sˆ  ù€ ô 	‰ÑœŸ™Ÿ™ d¨LÓ9ÀYÐaqÐÔrÜ
�‰�D—I‘I¨Õ-Ü�k¤3Ô'Ø&˜-ˆKØbmÖnÐ]^œN¨1¨mÐ=WÕXÒnˆÔáØ�M‰MŒOà×$Ñ$Ô&ÜÐiÓjÐjä ¤§¡¬B¯G©G¯L©L¸¿¹ÐDZÓ,[Ó!\Ó]ˆŒØ�‰×ÑÐ2Ô3ð Ø#Ø)Ø+ñ	
ˆô &*¬#Ó.[Ð]a×]lÑ]lÓ*mÓ%nˆÔ"à "ˆŒ
ØˆŒÜ˜dŸo™oÓ.ò 	#‰DˆAˆqÜ”B—J‘JœrŸw™wŸ|™|¨D¯I©IÐ7MÈqÓQÓRÓSˆAØ�J‰J×Ñœe A q¨1¡u›oÔ.Ø�F‰F�M‰M˜!˜q˜c™'Õ"ñ	#ùò1 os   Á+HrA   c                 ó  — ddl }t        j                  t        j                  j                  | j                  d| j                  | j                  |      d| j                  |   d›d�«      «      }g }| j                  D ]±  }|dk(  r|j                  | j                  |   «       Œ'|dk(  sŒ-|j                  j                  t        j                  j                  | j                  d	| j                  | j                  |      d
| j                  |   d›d�«      «      }|j                  |d   «       Œ³ t        |«      dkD  rt!        |«      n|d   }| j"                  �| j#                  |«      }| j$                  �| j%                  |«      }||fS )zž
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where the type of target specified by target_type.
        r   Nr   Úimage_Ú04dú.jpgr   r   ÚAnnotationsÚannotation_z.matÚobj_contourr	   )Úscipy.ior   Úopenr2   r3   r4   r   r<   rB   rA   r   ÚappendÚioÚloadmatr@   rD   Útupler   r   )rG   rA   ÚscipyÚimgÚtargetrH   Údatas          r-   Ú__getitem__zCaltech101.__getitem__R   sb  € ó 	ä�j‰jÜ�G‰G�L‰LØ—	‘	Ø&Ø—‘ §¡ u¡Ñ.Ø˜Ÿ™ EÑ*¨3Ð/¨tÐ4ó	ó
ˆð ˆØ×!Ñ!ò 	3ˆAØ�JŠØ—‘˜dŸf™f U™mÕ,Ø�lÓ"Ø—x‘x×'Ñ'Ü—G‘G—L‘LØŸ	™	Ø%Ø×2Ñ2°4·6±6¸%±=ÑAØ% d§j¡j°Ñ&7¸Ð%<¸DÐAó	ó�ð —‘˜d =Ñ1Õ2ð	3ô #& f£+°¢/”�v”°v¸a±yˆà�>‰>Ð%Ø—.‘. Ó%ˆCà× Ñ Ð,Ø×*Ñ*¨6Ó2ˆFà�Fˆ{Ðr/   c                 ó�   — t         j                  j                  t         j                  j                  | j                  d«      «      S )Nr   ©r2   r3   Úexistsr4   r   ©rG   s    r-   r8   zCaltech101._check_integrity}   ó)   € ä�w‰w�~‰~œbŸg™gŸl™l¨4¯9©9Ð6LÓMÓNÐNr/   c                 ó,   — t        | j                  «      S r)   ©rD   rA   rb   s    r-   Ú__len__zCaltech101.__len__�   ó   € Ü�4—:‘:‹Ðr/   c                 ó  — | j                  «       ry t        d| j                  dd¬«       t        j                  j                  | j                  d«      }t        j                  |«      D ]H  }|j                  d«      sŒt        t        j                  j                  ||«      | j                  «       ŒJ t        j                  |«       t        j                  t        j                  j                  | j                  d«      «       y )NzBhttps://data.caltech.edu/records/mzrjq-6wc02/files/caltech-101.zipzcaltech-101.zipÚ 3138e1922a9193bfa496528edbbc45d0)Údownload_rootÚfilenameÚmd5zcaltech-101z.gz)r8   r
   r   r2   r3   r4   r;   Úendswithr   ÚshutilÚrmtreer=   )rG   Úgzip_folderÚ	gzip_files      r-   r   zCaltech101.download„   s¶   € Ø× Ñ Ô"Øä$ØPØŸ)™)Ø&Ø2õ		
ô —g‘g—l‘l 4§9¡9¨mÓ<ˆÜŸ™ KÓ0ò 	QˆIØ×!Ñ! %Õ(Ü¤§¡§¡¨[¸)Ó DÀdÇiÁiÕPð	Qô 	�‰�kÔ"Ü
�	‰	”"—'‘'—,‘,˜tŸy™yÐ*;Ó<Õ=r/   c                 ó:   —  dj                   di | j                  ¤ŽS )NzTarget type: {target_type}r*   )ÚformatÚ__dict__rb   s    r-   Ú
extra_reprzCaltech101.extra_repr•   s   € Ø2Ð+×2Ñ2ÑC°T·]±]ÑCÐCr/   )r   NNF©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r7   r   r>   r   r   Úboolr1   ÚintrY   r   r^   r8   rf   r   ru   Ú__classcell__©rL   s   @r-   r   r      s¿   ø„ ñð< .8Ø(,Ø/3Øñ'#à�C˜�IÑð'#ð ˜4 ™9 c˜>Ñ*ð'#ð ˜HÑ%ð	'#ð
 # 8Ñ,ð'#ð ð'#ð 
õ'#ðR) ð )¨¨s°C¨x©ó )ðVO $ó Oð˜ó ó>ð"D˜C÷ Dr/   r   c                   ó†   ‡ — e Zd ZdZ	 	 	 ddedee   dee   deddf
ˆ fd„Zd	e	de
eef   fd
„Zdefd„Zde	fd„Zdd„Zˆ xZS )Ú
Caltech256aÞ  `Caltech 256 <https://data.caltech.edu/records/20087>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of dataset where directory
            ``caltech256`` exists or will be saved to if download is set to True.
        transform (callable, optional): A function/transform that takes in a PIL image
            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.
    Nr   r   r   r   r   c           
      óR  •— t         ‰	| �  t        j                  j	                  |d«      ||¬«       t        j
                  | j                  d¬«       |r| j                  «        | j                  «       st        d«      ‚t        t        j                  t        j                  j	                  | j                  d«      «      «      | _        g | _        g | _        t        | j                  «      D ]²  \  }}t!        t        j                  t        j                  j	                  | j                  d|«      «      D �cg c]  }|j#                  d«      r|‘Œ c}«      }| j                  j%                  t'        d|dz   «      «       | j                  j%                  ||gz  «       Œ´ y c c}w )	NÚ
caltech256r   Tr   r   Ú256_ObjectCategoriesrP   r	   )r0   r1   r2   r3   r4   r5   r   r   r8   r9   r:   r;   r<   rA   rB   rC   rD   rm   rE   rF   )
rG   r   r   r   r   rI   rJ   ÚitemrK   rL   s
            €r-   r1   zCaltech256.__init__¨   s9  ø€ ô 	‰ÑœŸ™Ÿ™ d¨LÓ9ÀYÐaqÐÔrÜ
�‰�D—I‘I¨Õ-áØ�M‰MŒOà×$Ñ$Ô&ÜÐiÓjÐjä ¤§¡¬B¯G©G¯L©L¸¿¹ÐDZÓ,[Ó!\Ó]ˆŒØ "ˆŒ
ØˆŒÜ˜dŸo™oÓ.ò 		#‰DˆAˆqÜô !#§
¡
¬2¯7©7¯<©<¸¿	¹	ÐCYÐ[\Ó+]Ó ^öàØ—}‘} VÔ,ò òóˆAð �J‰J×Ñœe A q¨1¡u›oÔ.Ø�F‰F�M‰M˜!˜q˜c™'Õ"ñ		#ùòs   Ä9F$
rA   c                 ó˜  — t        j                  t        j                  j	                  | j
                  d| j                  | j                  |      | j                  |   dz   d›d| j                  |   d›d�«      «      }| j                  |   }| j                  �| j                  |«      }| j                  �| j                  |«      }||fS )z–
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        rƒ   r	   Ú03dÚ_rO   rP   )r   rU   r2   r3   r4   r   r<   rB   rA   r   r   )rG   rA   r[   r\   s       r-   r^   zCaltech256.__getitem__Æ   s¹   € ô �j‰jÜ�G‰G�L‰LØ—	‘	Ø&Ø—‘ §¡ u¡Ñ.Ø—6‘6˜%‘= 1Ñ$ SÐ)¨¨4¯:©:°eÑ+<¸SÐ*AÀÐFó	ó
ˆð —‘˜‘ˆà�>‰>Ð%Ø—.‘. Ó%ˆCà× Ñ Ð,Ø×*Ñ*¨6Ó2ˆFà�Fˆ{Ðr/   c                 ó�   — t         j                  j                  t         j                  j                  | j                  d«      «      S )Nrƒ   r`   rb   s    r-   r8   zCaltech256._check_integrityá   rc   r/   c                 ó,   — t        | j                  «      S r)   re   rb   s    r-   rf   zCaltech256.__len__å   rg   r/   c                 óX   — | j                  «       ry t        d| j                  dd¬«       y )NzKhttps://data.caltech.edu/records/nyy15-4j048/files/256_ObjectCategories.tarz256_ObjectCategories.tarÚ 67b4f42ca05d46448c6bb8ecd2220f6d)rk   rl   )r8   r
   r   rb   s    r-   r   zCaltech256.downloadè   s)   € Ø× Ñ Ô"Øä$ØYØ�I‰IØ/Ø2ö		
r/   )NNFrv   )rw   rx   ry   rz   r7   r   r   r{   r1   r|   rY   r   r^   r8   rf   r   r}   r~   s   @r-   r€   r€   ™   sŒ   ø„ ñð" )-Ø/3Øñ#àð#ð ˜HÑ%ð#ð # 8Ñ,ð	#ð
 ð#ð 
õ#ð< ð ¨¨s°C¨x©ó ð6O $ó Oð˜ó ÷	
r/   r€   )r2   Úos.pathrn   Úpathlibr   Útypingr   r   r   r   ÚPILr   Úutilsr
   r   r   Úvisionr   r   r€   r*   r/   r-   ú<module>r’      sB   ðÛ 	Û Û Ý ß 1Ó 1å ç PÑ PÝ !ôID�ô IDôXX
�õ X
r/   