
    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
annotationHDataset 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    V/var/www/html/engine/venv/lib/python3.12/site-packages/torchvision/datasets/caltech.py<lambda>z%Caltech101.__init__.<locals>.<lambda>I   s    qH} 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    	dL9Yaqr
DII-k3'&-Kbmn]^N1m=WXnMMO$$&ijj BGGLLDZ,[!\]23 #)+	
 &*#.[]a]l]l*m%n" "
doo. 	#DAqBJJrww||DII7MqQRSAJJeAq1uo.FFMM!qc'"	#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    	jjGGLL		&u.E*3/t4	
 !! 	3AJdffUm,l"xx''GGLL		%22466%=A%djj&7%<DA	 d=12	3 #&f+/vvay>>%..%C  ,**62F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}   )    ww~~bggll4996L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		
 ggll499m<K0 	QI!!%([) DdiiP	Q 	k"
		"'',,tyy*;<=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CT]]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9c>*'# H%	'#
 #8,'# '# 
'#R) )sCx )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    	dL9Yaqr
DII-MMO$$&ijj BGGLLDZ,[!\] "
doo. 		#DAq !#

277<<		CY[\+] ^}}V, A JJeAq1uo.FFMM!qc'"		#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     jjGGLL		&u.66%=1$S)4::e+<S*AF	
 >>%..%C  ,**62F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II/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,	#
 # 
#< sCx 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/   