
    Yh^"                         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 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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     n/var/www/tools.fuzzalab.pt/emblema-extractor/venv/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nr1   	__class__s            @r   r4   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!   rD   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   openr5   r6   r7   r   r?   rE   rD   r   appendioloadmatrC   rG   tupler   r   )rJ   rD   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#   r5   r6   existsr7   r   rJ   s    r   r;   zCaltech101._check_integrity|   )    w~~bgll496LMMNNNr!   c                 *    t          | j                  S r.   rG   rD   rd   s    r   __len__zCaltech101.__len__       4:r!   c                     |                                  rd S t          d| j        dd           t          d| j        dd           d S )NzAhttps://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;   r
   r   rd   s    r   r   zCaltech101.download   sq      "" 	F$OI22		
 	
 	
 	
 	%OI&2		
 	
 	
 	
 	
 	
r!   c                 &     dj         di | j        S )NzTarget type: {target_type}r/   )format__dict__rd   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   rA   r   r   boolr4   intr[   r   r`   r;   rh   r   rt   __classcell__rN   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 )rR   )endswith)r   items     r   r    z'Caltech256.__init__.<locals>.<listcomp>   s:       }}V,,  r!   r	   )r3   r4   r5   r6   r7   r8   r   r   r;   r<   r=   r>   r?   rD   rE   rF   rG   rH   rI   )	rJ   r   r   r   r   rK   rL   rM   rN   s	           r   r4   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!   rD   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_rQ   rR   )r   rW   r5   r6   r7   r   r?   rE   rD   r   r   )rJ   rD   r]   r^   s       r   r`   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   rb   rd   s    r   r;   zCaltech256._check_integrity   re   r!   c                 *    t          | j                  S r.   rg   rd   s    r   rh   zCaltech256.__len__   ri   r!   c                 b    |                                  rd S t          d| j        dd           d S )NzAhttps://drive.google.com/file/d/1r6o0pSROcV1_VwT4oSjA2FBUSCWGuxLKz256_ObjectCategories.tar 67b4f42ca05d46448c6bb8ecd2220f6drl   rp   rd   s    r   r   zCaltech256.download   sJ      "" 	F$OI/2		
 	
 	
 	
 	
 	
r!   )NNFru   )rv   rw   rx   ry   r:   r   r   rz   r4   r{   r[   r   r`   r;   rh   r   r|   r}   s   @r   r   r      s
        " )-/3# ## H%# #8,	#
 # 
# # # # # #< sCx    6O$ O O O O    	
 	
 	
 	
 	
 	
 	
 	
r!   r   )r5   os.pathpathlibr   typingr   r   r   r   PILr   utilsr
   r   visionr   r   r   r/   r!   r   <module>r      s   				        1 1 1 1 1 1 1 1 1 1 1 1       ? ? ? ? ? ? ? ? ! ! ! ! ! !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!   