U
    Ð‹Ñhh  ã                   @   st   d dl mZ d dl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mZ ddlmZ G dd	„ d	eƒZd
S )é    )ÚPath)ÚAnyÚCallableÚOptionalÚTupleÚUnion)ÚImageé   )Úfind_classesÚmake_dataset)Údownload_and_extract_archiveÚverify_str_arg)ÚVisionDatasetc                       s¤   e Zd ZdZddddœZdddd	d
ddddddœ
Zd"eeef eee	e
 e	e
 ddœ‡ fdd„Zedœdd„Zdd„ Zeeeef dœdd„Zedœd d!„Z‡  ZS )#Ú
Imagenettea`  `Imagenette <https://github.com/fastai/imagenette#imagenette-1>`_ image classification dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of the Imagenette dataset.
        split (string, optional): The dataset split. Supports ``"train"`` (default), and ``"val"``.
        size (string, optional): The image size. Supports ``"full"`` (default), ``"320px"``, and ``"160px"``.
        download (bool, optional): If ``True``, downloads the dataset components and places them in ``root``. Already
            downloaded archives are not downloaded again.
        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.

     Attributes:
        classes (list): List of the class name tuples.
        class_to_idx (dict): Dict with items (class name, class index).
        wnids (list): List of the WordNet IDs.
        wnid_to_idx (dict): Dict with items (WordNet ID, class index).
    )z:https://s3.amazonaws.com/fast-ai-imageclas/imagenette2.tgzZ fe2fc210e6bb7c5664d602c3cd71e612)z>https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-320.tgzZ 3df6f0d01a2c9592104656642f5e78a3)z>https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-160.tgzZ e793b78cc4c9e9a4ccc0c1155377a412)ÚfullÚ320pxÚ160px)ZtenchzTinca tinca)zEnglish springerzEnglish springer spaniel)zcassette player)z	chain sawZchainsaw)Zchurchzchurch building)zFrench hornZhorn)zgarbage truckZdustcart)zgas pumpzgasoline pumpzpetrol pumpzisland dispenser)z	golf ball)Z	parachuteZchute)
Z	n01440764Z	n02102040Z	n02979186Z	n03000684Z	n03028079Z	n03394916Z	n03417042Z	n03425413Z	n03445777Z	n03888257Útrainr   FN)ÚrootÚsplitÚsizeÚ	transformÚtarget_transformÚreturnc                    sî   t ƒ j|||d� t|dddgƒˆ _t|ddddgƒˆ _ˆ jˆ j \ˆ _ˆ _tˆ j	ƒtˆ jƒj
 ˆ _tˆ jˆ j ƒˆ _|r„ˆ  ¡  nˆ  ¡ s”td	ƒ‚tˆ jƒ\ˆ _ˆ _‡ fd
d„ˆ jD ƒˆ _‡ fdd„ˆ j ¡ D ƒˆ _tˆ jˆ jdd�ˆ _d S )N)r   r   r   r   Úvalr   r   r   r   z<Dataset not found. You can use download=True to download it.c                    s   g | ]}ˆ j | ‘qS © ©Ú_WNID_TO_CLASS)Ú.0Úwnid©Úselfr   úM/tmp/user/2202/pip-unpacked-wheel-mjxxo1pc/torchvision/datasets/imagenette.pyÚ
<listcomp>I   s     z'Imagenette.__init__.<locals>.<listcomp>c                    s&   i | ]\}}ˆ j | D ]
}||“qqS r   r   )r   r   ÚidxÚ
class_namer    r   r"   Ú
<dictcomp>J   s
       z'Imagenette.__init__.<locals>.<dictcomp>z.jpeg)Ú
extensions)ÚsuperÚ__init__r   Ú_splitÚ_sizeÚ	_ARCHIVESÚ_urlÚ_md5r   r   ÚstemÚ
_size_rootÚstrZ_image_rootÚ	_downloadÚ_check_existsÚRuntimeErrorr
   ZwnidsZwnid_to_idxÚclassesÚitemsZclass_to_idxr   Ú_samples)r!   r   r   r   Údownloadr   r   ©Ú	__class__r    r"   r)   1   s     	

ÿzImagenette.__init__)r   c                 C   s
   | j  ¡ S ©N)r0   Úexistsr    r   r   r"   r3   O   s    zImagenette._check_existsc                 C   s2   |   ¡ rtd| j› d�ƒ‚t| j| j| jd� d S )NzThe directory z[ already exists. If you want to re-download or re-extract the images, delete the directory.)Úmd5)r3   r4   r0   r   r-   r   r.   r    r   r   r"   r2   R   s
    ÿzImagenette._download)r$   r   c                 C   sN   | j | \}}t |¡ d¡}| jd k	r2|  |¡}| jd k	rF|  |¡}||fS )NZRGB)r7   r   ÚopenÚconvertr   r   )r!   r$   ÚpathÚlabelÚimager   r   r"   Ú__getitem__[   s    



zImagenette.__getitem__c                 C   s
   t | jƒS r;   )Úlenr7   r    r   r   r"   Ú__len__g   s    zImagenette.__len__)r   r   FNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   r   r   r1   r   r   r   r)   Úboolr3   r2   Úintr   r   rC   rE   Ú__classcell__r   r   r9   r"   r      sB   ýö     ù
ø	r   N)Úpathlibr   Útypingr   r   r   r   r   ZPILr   Úfolderr
   r   Úutilsr   r   Zvisionr   r   r   r   r   r"   Ú<module>   s   