U
    (d                     @   s`   d dl Z d dlmZmZmZmZ d dlmZ ddlm	Z	m
Z
mZ ddlmZ G dd deZdS )	    N)CallableOptionalAnyTuple)Image   )download_and_extract_archivedownload_urlverify_str_arg)VisionDatasetc                       s|   e Zd ZdZdeeee ee edd fddZe	dd	d
Z
e	eeef dddZddddZedddZ  ZS )StanfordCarsa"  `Stanford Cars <https://ai.stanford.edu/~jkrause/cars/car_dataset.html>`_ Dataset

    The Cars dataset contains 16,185 images of 196 classes of cars. The data is
    split into 8,144 training images and 8,041 testing images, where each class
    has been split roughly in a 50-50 split

    .. note::

        This class needs `scipy <https://docs.scipy.org/doc/>`_ to load target files from `.mat` format.

    Args:
        root (string): Root directory of dataset
        split (string, optional): The dataset split, supports ``"train"`` (default) or ``"test"``.
        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.trainNF)rootsplit	transformtarget_transformdownloadreturnc                    s  zdd l m} W n tk
r,   tdY nX t j|||d t|dd _t	|d  _
 j
d } jdkr|d	  _ j
d
  _n j
d  _ j
d  _|r     std fdd|j jddd D  _|jt|d ddd   _dd t jD  _d S )Nr   zQScipy is not found. This dataset needs to have scipy installed: pip install scipy)r   r   r   )r   testZstanford_carsdevkitr   zcars_train_annos.matZ
cars_trainzcars_test_annos_withlabels.matZ	cars_testz;Dataset not found. You can use download=True to download itc                    s*   g | ]"}t  j|d   |d d fqS )fnameclassr   )str_images_base_path).0
annotationself F/tmp/pip-unpacked-wheel-vx7f76es/torchvision/datasets/stanford_cars.py
<listcomp>A   s   
z)StanfordCars.__init__.<locals>.<listcomp>T)Z
squeeze_meannotationszcars_meta.matZclass_namesc                 S   s   i | ]\}}||qS r   r   )r   iclsr   r   r   
<dictcomp>J   s      z)StanfordCars.__init__.<locals>.<dictcomp>)Zscipy.ioioImportErrorRuntimeErrorsuper__init__r
   _splitpathlibPath_base_folder_annotations_mat_pathr   r   _check_existsZloadmat_samplesr   tolistclasses	enumerateZclass_to_idx)r   r   r   r   r   r   sior   	__class__r   r   r)       s,    	



 zStanfordCars.__init__)r   c                 C   s
   t | jS )N)lenr0   r   r   r   r   __len__L   s    zStanfordCars.__len__)idxr   c                 C   sN   | j | \}}t|d}| jdk	r2| |}| jdk	rF| |}||fS )z.Returns pil_image and class_id for given indexZRGBN)r0   r   openconvertr   r   )r   r9   Z
image_pathtargetZ	pil_imager   r   r   __getitem__O   s    



zStanfordCars.__getitem__c                 C   sl   |   rd S tdt| jdd | jdkr@tdt| jdd n(tdt| jdd td	t| jd
d d S )Nz4https://ai.stanford.edu/~jkrause/cars/car_devkit.tgzZ c3b158d763b6e2245038c8ad08e45376)urlZdownload_rootmd5r   z6https://ai.stanford.edu/~jkrause/car196/cars_train.tgzZ 065e5b463ae28d29e77c1b4b166cfe61z5https://ai.stanford.edu/~jkrause/car196/cars_test.tgzZ 4ce7ebf6a94d07f1952d94dd34c4d501zFhttps://ai.stanford.edu/~jkrause/car196/cars_test_annos_withlabels.matZ b0a2b23655a3edd16d84508592a98d10)r>   r   r?   )r/   r   r   r-   r*   r	   r   r   r   r   r   Z   s.    
zStanfordCars.downloadc                 C   s&   | j d  sdS | j o$| j S )Nr   F)r-   is_dirr.   existsr   r   r   r   r   r/   u   s    zStanfordCars._check_exists)r   NNF)__name__
__module____qualname____doc__r   r   r   boolr)   intr8   r   r   r=   r   r/   __classcell__r   r   r5   r   r   
   s"       ,r   )r+   typingr   r   r   r   ZPILr   utilsr   r	   r
   Zvisionr   r   r   r   r   r   <module>   s
   