o
    0ih                     @   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&deeef dedede	e
 de	e
 ddf fddZdefddZdd  Zd!edeeef fd"d#Zdef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.tgz fe2fc210e6bb7c5664d602c3cd71e612)z>https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-320.tgz 3df6f0d01a2c9592104656642f5e78a3)z>https://s3.amazonaws.com/fast-ai-imageclas/imagenette2-160.tgz e793b78cc4c9e9a4ccc0c1155377a412full320px160px)tenchzTinca tinca)zEnglish springerzEnglish springer spaniel)zcassette player)z	chain sawchainsaw)churchzchurch building)zFrench hornhorn)zgarbage truckdustcart)zgas pumpzgasoline pumpzpetrol pumpzisland dispenser)z	golf ball)	parachutechute)
	n01440764	n02102040	n02979186	n03000684	n03028079	n03394916	n03417042	n03425413	n03445777	n03888257trainr   FNrootsplitsize	transformtarget_transformreturnc                    s   t  j|||d t|dddg _t|dg d _ j j \ _ _t j	t jj
  _t j j  _|rA   n  sIt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   z<Dataset not found. You can use download=True to download it.c                    s   g | ]} j | qS  _WNID_TO_CLASS).0wnidselfr0   ]/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torchvision/datasets/imagenette.py
<listcomp>I   s    z'Imagenette.__init__.<locals>.<listcomp>c                    s&   i | ]\}} j | D ]}||qqS r0   r1   )r3   r4   idx
class_namer5   r0   r7   
<dictcomp>J   s
    z'Imagenette.__init__.<locals>.<dictcomp>z.jpeg)
extensions)super__init__r   _split_size	_ARCHIVES_url_md5r   r)   stem
_size_rootstr_image_root	_download_check_existsRuntimeErrorr
   wnidswnid_to_idxclassesitemsclass_to_idxr   _samples)r6   r)   r*   r+   downloadr,   r-   	__class__r5   r7   r>   1   s    	

zImagenette.__init__c                 C   s
   | j  S N)rE   existsr5   r0   r0   r7   rI   O      
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)rI   rJ   rE   r   rB   r)   rC   r5   r0   r0   r7   rH   R   s
   zImagenette._downloadr9   c                 C   sN   | j | \}}t|d}| jd ur| |}| jd ur#| |}||fS )NRGB)rP   r   openconvertr,   r-   )r6   r9   pathlabelimager0   r0   r7   __getitem__[   s   



zImagenette.__getitem__c                 C   s
   t | jS rT   )lenrP   r5   r0   r0   r7   __len__g   rV   zImagenette.__len__)r(   r   FNN)__name__
__module____qualname____doc__rA   r2   r   rF   r   r   r   r>   boolrI   rH   intr   r   r^   r`   __classcell__r0   r0   rR   r7   r      sN    
	r   N)pathlibr   typingr   r   r   r   r   PILr   folderr
   r   utilsr   r   visionr   r   r0   r0   r0   r7   <module>   s    