o
    ôjå"  ã                   @   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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ÚListÚOptionalÚTupleÚUnion)ÚImageé   )Údownload_and_extract_archiveÚverify_str_arg)ÚVisionDatasetc                       s®   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d„Zded
eeef fdd„Zd
e
fdd„Zd
efdd„Zddd„Zd
ef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 ƒ| _	|r,|  
¡  |  ¡ s4t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 ]&\}}tt tj | jd|¡¡ƒ}| j td|d ƒ¡ | j ||g ¡ qgd S )NZ
caltech101©r   r   T©Úexist_okc                 S   s   g | ]}t |d dƒ‘qS )r   )r   Ú
annotation)r   )Ú.0Út© r   úm/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/torchvision/datasets/caltech.pyÚ
<listcomp>4   s    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©Úname_mapr   r   Ú<lambda>H   s    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'   (   s0   
üýzCaltech101.__init__r7   c                 C   s  ddl }t tj | jd| j| j|  d| j	| d›d�¡¡}g }| j
D ]6}|dkr3| | j| ¡ q$|dkrZ|j tj | jd	| j| j|  d
| j	| d›d�¡¡}| |d ¡ q$t|ƒdkret|ƒn|d }| jdurs|  |¡}| 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
   )Úscipy.ior	   Úopenr(   r)   r*   r   r2   r8   r7   r   ÚappendÚioÚloadmatr6   r:   Útupler   r   )r=   r7   ÚscipyÚimgÚtargetr   Údatar   r   r   Ú__getitem__Q   s<   üÿ	
üÿ€



zCaltech101.__getitem__c                 C   ó   t j t j | jd¡¡S )Nr    ©r(   r)   Úexistsr*   r   ©r=   r   r   r   r.   |   ó   zCaltech101._check_integrityc                 C   ó
   t | jƒS r!   ©r:   r7   rS   r   r   r   Ú__len__€   ó   
zCaltech101.__len__c                 C   s<   |   ¡ r
tdƒ d S td| jddd� td| jddd� d S )	Nú%Files already downloaded and verifiedzAhttps://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.   Úprintr   r   rS   r   r   r   r   ƒ   s   ü
üzCaltech101.downloadc                 C   s   dj di | j¤ŽS )NzTarget type: {target_type}r   )ÚformatÚ__dict__rS   r   r   r   Ú
extra_repr•   s   zCaltech101.extra_repr)r   NNF©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   r   r   r   r   Úboolr'   Úintr   r   rO   r.   rW   r   ra   Ú__classcell__r   r   rA   r   r      s0    ú
þýüûúù)+
r   c                       s†   e Zd ZdZ			ddedee dee deddf
‡ fd	d
„Zde	de
eef fdd„Zdefdd„Zde	fdd„Z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.
    NFr   r   r   r   r   c              
      sÖ   t ƒ jtj |d¡||d� tj| jdd� |r|  ¡  |  ¡ s$t	dƒ‚t
t tj | jd¡¡ƒ| _g | _g | _t| jƒD ]+\}}tdd„ t tj | jd|¡¡D ƒƒ}| j td	|d	 ƒ¡ | j ||g ¡ q=d S )
NZ
caltech256r   Tr   r   Ú256_ObjectCategoriesc                 S   s   g | ]	}|  d ¡r|‘qS )rD   )Úendswith)r   Úitemr   r   r   r   ½   s    ýÿz'Caltech256.__init__.<locals>.<listcomp>r
   )r&   r'   r(   r)   r*   r+   r   r   r.   r/   r0   r1   r2   r7   r8   r9   r:   r;   r<   )r=   r   r   r   r   r>   r?   r@   rA   r   r   r'   ¨   s$   þÿ÷zCaltech256.__init__r7   c                 C   s€   t  tj | jd| j| j|  | j| d d›d| j| d›d�¡¡}| j| }| j	dur2|  	|¡}| j
dur<|  
|¡}||fS )z–
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        rk   r
   Z03dÚ_rC   rD   N)r	   rF   r(   r)   r*   r   r2   r8   r7   r   r   )r=   r7   rL   rM   r   r   r   rO   Æ   s   "üÿ
	



zCaltech256.__getitem__c                 C   rP   )Nrk   rQ   rS   r   r   r   r.   á   rT   zCaltech256._check_integrityc                 C   rU   r!   rV   rS   r   r   r   rW   å   rX   zCaltech256.__len__c                 C   s*   |   ¡ r
tdƒ d S td| jddd� d S )NrY   zAhttps://drive.google.com/file/d/1r6o0pSROcV1_VwT4oSjA2FBUSCWGuxLKz256_ObjectCategories.tarZ 67b4f42ca05d46448c6bb8ecd2220f6drZ   r]   rS   r   r   r   r   è   s   
üzCaltech256.download)NNFrb   )rc   rd   re   rf   r-   r   r   rg   r'   rh   r   r   rO   r.   rW   r   ri   r   r   rA   r   rj   ™   s(    ûþýüûúrj   )r(   Úos.pathÚpathlibr   Útypingr   r   r   r   r   r   ÚPILr	   Úutilsr   r   Úvisionr   r   rj   r   r   r   r   Ú<module>   s      