a
    h^"                     @   s   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	 d	eZG d
d deZdS )    N)Path)AnyCallableOptionalUnion)Image   )download_and_extract_archiveverify_str_arg)VisionDatasetc                       s   e Zd ZdZdeeef eee ef ee	 ee	 e
dd fddZeeeef dd	d
Ze
dddZedddZddddZedddZ  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           	   	      s   t  jtj|d||d tj| jdd t|tr<|g}dd |D | _	|rX| 
  |  shtdtttj| jd| _| jd	 d
dddd tt fdd| j| _g | _g | _t| jD ]L\}}tttj| jd|}| jtd|d  | j||g  qd S )NZ
caltech101r   r   Texist_okc                 S   s   g | ]}t |d dqS )r   )r   
annotation)r
   ).0t r   J/var/www/auris/lib/python3.9/site-packages/torchvision/datasets/caltech.py
<listcomp>4       z'Caltech101.__init__.<locals>.<listcomp>HDataset not found or corrupted. You can use download=True to download it101_ObjectCategoriesZBACKGROUND_GoogleZFaces_2ZFaces_3ZMotorbikes_16ZAirplanes_Side_2)ZFacesZ
Faces_easyZ
MotorbikesZ	airplanesc                    s   |  v r |  S | S Nr   )xZname_mapr   r   <lambda>H   r   z%Caltech101.__init__.<locals>.<lambda>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n	__class__r"   r   r%   (   s.    
zCaltech101.__init__r5   r   c                 C   s  ddl }ttj| jd| j| j|  d| j	| dd}g }| j
D ]l}|dkrf|| j|  qH|dkrH|jtj| jd	| j| j|  d
| j	| dd}||d  qHt|dkrt|n|d }| jdur| |}| jdur| |}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where the type of target specified by target_type.
        r   Nr   Zimage_04d.jpgr   r   ZAnnotationsZannotation_z.matZobj_contourr   )Zscipy.ior   openr&   r'   r(   r   r0   r6   r5   r   appendioZloadmatr4   r8   tupler   r   )r;   r5   Zscipyimgtargetr   datar   r   r   __getitem__Q   s:    	




zCaltech101.__getitem__r   c                 C   s   t jt j| jdS )Nr   r&   r'   existsr(   r   r;   r   r   r   r,   |   s    zCaltech101._check_integrityc                 C   s
   t | jS r    r8   r5   rO   r   r   r   __len__   s    zCaltech101.__len__c                 C   s4   |   rd S td| jddd td| jddd d S )NzAhttps://drive.google.com/file/d/137RyRjvTBkBiIfeYBNZBtViDHQ6_Ewspz101_ObjectCategories.tar.gzZ b224c7392d521a49829488ab0f1120d9filenamemd5zAhttps://drive.google.com/file/d/175kQy3UsZ0wUEHZjqkUDdNVssr7bgh_mzAnnotations.tarZ 6f83eeb1f24d99cab4eb377263132c91r,   r	   r   rO   r   r   r   r      s    zCaltech101.downloadc                 C   s   dj f i | jS )NzTarget type: {target_type})format__dict__rO   r   r   r   
extra_repr   s    zCaltech101.extra_repr)r   NNF)__name__
__module____qualname____doc__r   r+   r   r2   r   r   boolr%   intrG   r   rK   r,   rQ   r   rX   __classcell__r   r   r?   r   r      s$       
)+r   c                       sz   e Zd ZdZdeee ee edd fddZe	e
eef ddd	Zed
ddZe	d
ddZdd
ddZ  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.
    NF)r   r   r   r   r   c              
      s   t  jtj|d||d tj| jdd |r8|   |  sHt	dt
ttj| jd| _g | _g | _t| jD ]V\}}tdd ttj| jd|D }| jtd	|d	  | j||g  qzd S )
NZ
caltech256r   Tr   r   256_ObjectCategoriesc                 S   s   g | ]}| d r|qS )rC   )endswith)r   itemr   r   r   r      s   
z'Caltech256.__init__.<locals>.<listcomp>r   )r$   r%   r&   r'   r(   r)   r   r   r,   r-   r.   r/   r0   r5   r6   r7   r8   r9   r:   )r;   r   r   r   r   r<   r=   r>   r?   r   r   r%      s"    zCaltech256.__init__rA   c                 C   s   t tj| jd| j| j|  | j| d dd| j| dd}| j| }| j	durd| 	|}| j
durx| 
|}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        ra   r   Z03d_rB   rC   N)r   rD   r&   r'   r(   r   r0   r6   r5   r   r   )r;   r5   rH   rI   r   r   r   rK      s    "	




zCaltech256.__getitem__rL   c                 C   s   t jt j| jdS )Nra   rM   rO   r   r   r   r,      s    zCaltech256._check_integrityc                 C   s
   t | jS r    rP   rO   r   r   r   rQ      s    zCaltech256.__len__c                 C   s"   |   rd S td| jddd d S )NzAhttps://drive.google.com/file/d/1r6o0pSROcV1_VwT4oSjA2FBUSCWGuxLKz256_ObjectCategories.tarZ 67b4f42ca05d46448c6bb8ecd2220f6drR   rU   rO   r   r   r   r      s    zCaltech256.download)NNF)rY   rZ   r[   r\   r+   r   r   r]   r%   r^   rG   r   rK   r,   rQ   r   r_   r   r   r?   r   r`      s      r`   )r&   os.pathpathlibr   typingr   r   r   r   ZPILr   utilsr	   r
   Zvisionr   r   r`   r   r   r   r   <module>   s    