
    %`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dS )    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.
    categoryNFroottarget_type	transformtarget_transformdownloadreturnc           
        	 t                                          t          j                            |d          ||           t          j        | j        d           t          |t                    r|g}d |D             | _	        |r| 
                                 |                                 st          d          t          t          j        t          j                            | j        d                              | _        | j                            d           d	d
ddd	t#          t%          	fd| j                            | _        g | _        g | _        t-          | j                  D ]\  }}t/          t          j        t          j                            | j        d|                              }| j                            t3          d|dz                        | j                            ||gz             d S )N
caltech101r   r   Texist_okc                 0    g | ]}t          |d d          S )r   )r   
annotation)r   ).0ts     p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchvision/datasets/caltech.py
<listcomp>z'Caltech101.__init__.<locals>.<listcomp>5   s&    nnn]^N1m=WXXnnn    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|          n| S N )xname_maps    r    <lambda>z%Caltech101.__init__.<locals>.<lambda>I   s    qH}}Z[ r"   r	   )super__init__ospathjoinmakedirsr   
isinstancestrr   r   _check_integrityRuntimeErrorsortedlistdir
categoriesremovelistmapannotation_categoriesindexy	enumeratelenextendrange)selfr   r   r   r   r   icnr2   	__class__s            @r    r5   zCaltech101.__init__)   s    	dL99Yaqrrr
DI----k3'' 	(&-Knnbmnnn 	MMOOO$$&& 	kijjj BGLLDZ,[,[!\!\]]2333 #)+	
 
 &*#.[.[.[.[]a]l*m*m%n%n" "
do.. 	# 	#DAqBJrw||DI7MqQQRRSSAJeAq1uoo...FMM!qc'""""	# 	#r"   rE   c                    ddl }t          j        t          j                            | j        d| j        | j        |                  d| j	        |         dd                    }g }| j
        D ]}|dk    r!|                    | j        |                    )|dk    r|j                            t          j                            | j        d	| j        | j        |                  d
| j	        |         dd                    }|                    |d                    t          |          dk    rt!          |          n|d         }| 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   openr6   r7   r8   r   r@   rF   rE   r   appendioloadmatrD   rH   tupler   r   )rK   rE   scipyimgtargetr   datas          r    __getitem__zCaltech101.__getitem__R   s~    	jGLL	&u.4E*4444	 
 
 ! 	3 	3AJdfUm,,,,l""x''GLL	%246%=AAdj&7AAAA	   d=1222"%f++//vvay>%..%%C ,**622FF{r"   c                     t           j                            t           j                            | j        d                    S )Nr$   r6   r7   existsr8   r   rK   s    r    r<   zCaltech101._check_integrity}   )    w~~bgll496LMMNNNr"   c                 *    t          | j                  S r/   rH   rE   re   s    r    __len__zCaltech101.__len__       4:r"   c                     |                                  rd S t          d| j        dd           t          j                            | j        d          }t          j        |          D ]J}|                    d          r3t          t          j                            ||          | j                   Kt          j
        |           t          j        t          j                            | j        d                     d S )NzBhttps://data.caltech.edu/records/mzrjq-6wc02/files/caltech-101.zipzcaltech-101.zip 3138e1922a9193bfa496528edbbc45d0)download_rootfilenamemd5zcaltech-101z.gz)r<   r
   r   r6   r7   r8   r?   endswithr   shutilrmtreerA   )rK   gzip_folder	gzip_files      r    r   zCaltech101.download   s      "" 	F$P)&2		
 	
 	
 	
 gll49m<<K00 	Q 	QI!!%(( Q[) D DdiPPPk"""
	"',,ty*;<<=====r"   c                 &     dj         di | j        S )NzTarget type: {target_type}r0   )format__dict__re   s    r    
extra_reprzCaltech101.extra_repr   s    2+2CCT]CCCr"   )r   NNFr   N)__name__
__module____qualname____doc__r   r;   r   rB   r   r   boolr5   intr\   r   ra   r<   ri   r   rx   __classcell__rO   s   @r    r   r      sS        < .8(,/3'# '#CI'# 49c>*'# H%	'#
 #8,'# '# 
'# '# '# '# '# '#R) )sCx ) ) ) )VO$ O O O O    > > > >"DC D D D D D D D 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.
    NFr   r   r   r   r   c                 F   t                                          t          j                            |d          ||           t          j        | j        d           |r|                                  |                                 st          d          t          t          j        t          j                            | j        d                              | _        g | _        g | _        t          | j                  D ]\  }}t!          d t          j        t          j                            | j        d|                    D                       }| j                            t%          d|dz                        | j                            ||gz             d S )	N
caltech256r   Tr   r#   256_ObjectCategoriesc                 <    g | ]}|                     d           |S )rS   )rp   )r   items     r    r!   z'Caltech256.__init__.<locals>.<listcomp>   s:       }}V,,  r"   r	   )r4   r5   r6   r7   r8   r9   r   r   r<   r=   r>   r?   r@   rE   rF   rG   rH   rI   rJ   )	rK   r   r   r   r   rL   rM   rN   rO   s	           r    r5   zCaltech256.__init__   sr    	dL99Yaqrrr
DI---- 	MMOOO$$&& 	kijjj BGLLDZ,[,[!\!\]] "
do.. 		# 		#DAq  "
27<<	CY[\+]+] ^ ^   A JeAq1uoo...FMM!qc'""""		# 		#r"   rE   c                 t   t          j        t          j                            | j        d| j        | j        |                  | j        |         dz   dd| j        |         dd                    }| 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_rR   rS   )r   rX   r6   r7   r8   r   r@   rF   rE   r   r   )rK   rE   r^   r_   s       r    ra   zCaltech256.__getitem__   s     jGLL	&u.6%=1$FFF4:e+<FFFF	 
 
 >%..%%C ,**622FF{r"   c                     t           j                            t           j                            | j        d                    S )Nr   rc   re   s    r    r<   zCaltech256._check_integrity   rf   r"   c                 *    t          | j                  S r/   rh   re   s    r    ri   zCaltech256.__len__   rj   r"   c                 b    |                                  rd S t          d| j        dd           d S )NzKhttps://data.caltech.edu/records/nyy15-4j048/files/256_ObjectCategories.tarz256_ObjectCategories.tar 67b4f42ca05d46448c6bb8ecd2220f6d)rn   ro   )r<   r
   r   re   s    r    r   zCaltech256.download   sJ      "" 	F$YI/2		
 	
 	
 	
 	
 	
r"   )NNFry   )rz   r{   r|   r}   r;   r   r   r~   r5   r   r\   r   ra   r<   ri   r   r   r   s   @r    r   r      s
        " )-/3# ## H%# #8,	#
 # 
# # # # # #< sCx    6O$ O O O O    	
 	
 	
 	
 	
 	
 	
 	
r"   r   )r6   os.pathrq   pathlibr   typingr   r   r   r   PILr   utilsr
   r   r   visionr   r   r   r0   r"   r    <module>r      s   				         1 1 1 1 1 1 1 1 1 1 1 1       P P P P P P P P P P ! ! ! ! ! !ID ID ID ID ID ID ID IDXX
 X
 X
 X
 X
 X
 X
 X
 X
 X
r"   