U
    ­Ê¹jÖ  ã                   @  sd   d dl mZ d dlZd dlmZmZmZmZ d dlZ	ddl
mZmZ ddlmZ G dd„ deƒZdS )	é    )ÚannotationsN)ÚAnyÚCallableÚOptionalÚTupleé   )Údownload_and_extract_archiveÚverify_str_arg)ÚVisionDatasetc                	      sr   e Zd ZdZdZddddddd	d
dœ‡ fdd„Zddœdd„Zdddœdd„Zd
dœdd„Zd	dœdd„Z	‡  Z
S )ÚFGVCAircraftaó  `FGVC Aircraft <https://www.robots.ox.ac.uk/~vgg/data/fgvc-aircraft/>`_ Dataset.

    The dataset contains 10,000 images of aircraft, with 100 images for each of 100
    different aircraft model variants, most of which are airplanes.
    Aircraft models are organized in a three-levels hierarchy. The three levels, from
    finer to coarser, are:

    - ``variant``, e.g. Boeing 737-700. A variant collapses all the models that are visually
        indistinguishable into one class. The dataset comprises 100 different variants.
    - ``family``, e.g. Boeing 737. The dataset comprises 70 different families.
    - ``manufacturer``, e.g. Boeing. The dataset comprises 30 different manufacturers.

    Args:
        root (string): Root directory of the FGVC Aircraft dataset.
        split (string, optional): The dataset split, supports ``train``, ``val``,
            ``trainval`` and ``test``.
        annotation_level (str, optional): The annotation level, supports ``variant``,
            ``family`` and ``manufacturer``.
        transform (callable, optional): A function/transform that  takes in an 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.
    zWhttps://www.robots.ox.ac.uk/~vgg/data/fgvc-aircraft/archives/fgvc-aircraft-2013b.tar.gzÚtrainvalÚvariantNFÚstrzOptional[Callable]ÚboolÚNone)ÚrootÚsplitÚannotation_levelÚ	transformÚtarget_transformÚdownloadÚreturnc              	     sh  t ƒ j|||d� t|ddƒ| _t|ddƒ| _tj | jd¡| _	|rL|  
¡  |  ¡ s\tdƒ‚tj | j	dd	d
ddœ| j ¡}t|dƒ�}dd„ |D ƒ| _W 5 Q R X tt| jtt| jƒƒƒƒ| _tj | j	dd¡}	tj | j	dd| j› d| j› d�¡}
g | _g | _t|
dƒ�T}|D ]H}| ¡  dd¡\}}| j tj |	|› d�¡¡ | j | j| ¡ �qW 5 Q R X d S )N)r   r   r   )ÚtrainÚvalr   Útestr   )r   ÚfamilyZmanufacturerzfgvc-aircraft-2013bz;Dataset not found. You can use download=True to download itÚdatazvariants.txtzfamilies.txtzmanufacturers.txtÚrc                 S  s   g | ]}|  ¡ ‘qS © )Ústrip)Ú.0Úliner   r   úa/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/datasets/fgvc_aircraft.pyÚ
<listcomp>J   s     z)FGVCAircraft.__init__.<locals>.<listcomp>ZimagesZimages_Ú_z.txtú r   z.jpg)ÚsuperÚ__init__r	   Ú_splitZ_annotation_levelÚosÚpathÚjoinr   Ú
_data_pathÚ	_downloadÚ_check_existsÚRuntimeErrorÚopenÚclassesÚdictÚzipÚrangeÚlenZclass_to_idxÚ_image_filesÚ_labelsr   r   Úappend)Úselfr   r   r   r   r   r   Zannotation_fileÚfZimage_data_folderZlabels_filer!   Z
image_nameZ
label_name©Ú	__class__r   r"   r'   *   sD    	  ÿýüý	$zFGVCAircraft.__init__Úint)r   c                 C  s
   t | jƒS ©N)r5   r6   ©r9   r   r   r"   Ú__len__Z   s    zFGVCAircraft.__len__zTuple[Any, Any])Úidxr   c                 C  sP   | j | | j|  }}tj |¡ d¡}| jr8|  |¡}| jrH|  |¡}||fS )NÚRGB)r6   r7   ÚPILZImager0   Úconvertr   r   )r9   rA   Z
image_fileÚlabelÚimager   r   r"   Ú__getitem__]   s    

zFGVCAircraft.__getitem__c                 C  s   |   ¡ rdS t| j| jƒ dS )zW
        Download the FGVC Aircraft dataset archive and extract it under root.
        N)r.   r   Ú_URLr   r?   r   r   r"   r-   i   s    zFGVCAircraft._downloadc                 C  s   t j | j¡ot j | j¡S r>   )r)   r*   Úexistsr,   Úisdirr?   r   r   r"   r.   q   s    zFGVCAircraft._check_exists)r   r   NNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rH   r'   r@   rG   r-   r.   Ú__classcell__r   r   r;   r"   r      s        ù 0r   )Ú
__future__r   r)   Útypingr   r   r   r   Z	PIL.ImagerC   Úutilsr   r	   Zvisionr
   r   r   r   r   r"   Ú<module>   s   