o
    Vh                     @  sx   d dl mZ d dlZ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 G d	d
 d
eZdS )    )annotationsN)Path)AnyCallableOptionalTupleUnion   )default_loader)download_and_extract_archiveverify_str_arg)VisionDatasetc                      s\   e Zd ZdZdZdddddefd" fddZd#ddZd$ddZd%ddZ	d&d d!Z
  ZS )'FGVCAircrafta2  `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 (str or ``pathlib.Path``): 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 a PIL image or torch.Tensor, depends on the given loader,
            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.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    zWhttps://www.robots.ox.ac.uk/~vgg/data/fgvc-aircraft/archives/fgvc-aircraft-2013b.tar.gztrainvalvariantNFrootUnion[str, Path]splitstrannotation_level	transformOptional[Callable]target_transformdownloadboolloaderCallable[[str], Any]returnNonec              	     s  t  j|||d t|dd| _t|dd| _tj| jd| _	|r&| 
  |  s.tdtj| j	dd	d
dd| j }t|d}	dd |	D | _W d    n1 sVw   Y  tt| jtt| j| _tj| j	dd}
tj| j	dd| j d| j d}g | _g | _t|d.}	|	D ]#}| dd\}}| jtj|
| d | j| j|  qW d    n1 sw   Y  || _d S )N)r   r   r   )trainvalr   testr   )r   family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&   V/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/datasets/fgvc_aircraft.py
<listcomp>O   s    z)FGVCAircraft.__init__.<locals>.<listcomp>imagesimages__z.txt r	   z.jpg)super__init__r   _split_annotation_levelospathjoinr   
_data_path	_download_check_existsRuntimeErroropenclassesdictziprangelenclass_to_idx_image_files_labelsr'   r   appendr   )selfr   r   r   r   r   r   r   annotation_filefimage_data_folderlabels_filer)   
image_name
label_name	__class__r&   r*   r1   .   sH   
	$
zFGVCAircraft.__init__intc                 C  s
   t | jS N)r@   rB   rE   r&   r&   r*   __len__`   s   
zFGVCAircraft.__len__idxTuple[Any, Any]c                 C  sH   | j | | j| }}| |}| jr| |}| jr | |}||fS rO   )rB   rC   r   r   r   )rE   rR   
image_filelabelimager&   r&   r*   __getitem__c   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)r9   r   _URLr   rP   r&   r&   r*   r8   o   s   zFGVCAircraft._downloadc                 C  s   t j| jot j| jS rO   )r4   r5   existsr7   isdirrP   r&   r&   r*   r9   w   s   zFGVCAircraft._check_exists)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   rN   )rR   rN   r   rS   )r   r   )r   r   )__name__
__module____qualname____doc__rX   r
   r1   rQ   rW   r8   r9   __classcell__r&   r&   rL   r*   r      s    
2

r   )
__future__r   r4   pathlibr   typingr   r   r   r   r   folderr
   utilsr   r   visionr   r   r&   r&   r&   r*   <module>   s    