
    ڧg"                         d dl Z d dlZ d dlmZ d dlmZmZmZmZm	Z	m
Z
 d dlmZ ddl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ListOptionalTupleUnion)Image   )download_and_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     X/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/datasets/caltech.py
<listcomp>z'Caltech101.__init__.<locals>.<listcomp>4   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>H   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nr3   	__class__s            @r!   r6   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" "
00 	# 	#FQBJrw||DI7MqQQRRSSAJeAq1uoo...FMM!qc'""""	# 	#r#   rF   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
   openr7   r8   r9   r   rA   rG   rF   r   appendioloadmatrE   rI   tupler   r   )rL   rF   scipyimgtargetr    datas          r!   __getitem__zCaltech101.__getitem__Q   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%   r7   r8   existsr9   r   rL   s    r!   r=   zCaltech101._check_integrity|   )    w~~bgll496LMMNNNr#   c                 *    t          | j                  S r0   rI   rF   rf   s    r!   __len__zCaltech101.__len__       4:r#   c                     |                                  rt          d           d S t          d| j        dd           t          d| j        dd           d S )	N%Files already downloaded and verifiedzAhttps://drive.google.com/file/d/137RyRjvTBkBiIfeYBNZBtViDHQ6_Ewspz101_ObjectCategories.tar.gz b224c7392d521a49829488ab0f1120d9filenamemd5zAhttps://drive.google.com/file/d/175kQy3UsZ0wUEHZjqkUDdNVssr7bgh_mzAnnotations.tar 6f83eeb1f24d99cab4eb377263132c91r=   printr   r   rf   s    r!   r   zCaltech101.download   s      "" 	9:::F$OI22		
 	
 	
 	
 	%OI&2		
 	
 	
 	
 	
 	
r#   c                 &     dj         di | j        S )NzTarget type: {target_type}r1   )format__dict__rf   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   r   r   r   boolr6   intr   r   rb   r=   rj   r   rx   __classcell__rP   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 )rT   )endswith)r   items     r!   r"   z'Caltech256.__init__.<locals>.<listcomp>   s:       }}V,,  r#   r   )r5   r6   r7   r8   r9   r:   r   r   r=   r>   r?   r@   rA   rF   rG   rH   rI   rJ   rK   )	rL   r   r   r   r   rM   rN   rO   rP   s	           r!   r6   zCaltech256.__init__   sr    	dL99Yaqrrr
DI---- 	MMOOO$$&& 	kijjj BGLLDZ,[,[!\!\]] "
00 		# 		#FQ  "
27<<	CY[\+]+] ^ ^   A JeAq1uoo...FMM!qc'""""		# 		#r#   rF   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_rS   rT   )r
   rY   r7   r8   r9   r   rA   rG   rF   r   r   )rL   rF   r_   r`   s       r!   rb   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   rd   rf   s    r!   r=   zCaltech256._check_integrity   rg   r#   c                 *    t          | j                  S r0   ri   rf   s    r!   rj   zCaltech256.__len__   rk   r#   c                     |                                  rt          d           d S t          d| j        dd           d S )Nrm   zAhttps://drive.google.com/file/d/1r6o0pSROcV1_VwT4oSjA2FBUSCWGuxLKz256_ObjectCategories.tar 67b4f42ca05d46448c6bb8ecd2220f6dro   rs   rf   s    r!   r   zCaltech256.download   sY      "" 	9:::F$OI/2		
 	
 	
 	
 	
 	
r#   )NNFry   )rz   r{   r|   r}   r<   r   r   r~   r6   r   r   r   rb   r=   rj   r   r   r   s   @r!   r   r      s
        " )-/3# ## H%# #8,	#
 # 
# # # # # #< sCx    6O$ O O O O    

 

 

 

 

 

 

 

r#   r   )r7   os.pathpathlibr   typingr   r   r   r   r   r	   PILr
   utilsr   r   visionr   r   r   r1   r#   r!   <module>r      s   				        > > > > > > > > > > > > > > > >       ? ? ? ? ? ? ? ? ! ! ! ! ! !JD JD JD JD JD JD JD JDZY
 Y
 Y
 Y
 Y
 Y
 Y
 Y
 Y
 Y
r#   