
    Phd                     f    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e      Zy)	    N)AnyCallableOptionalTuple)Image   )download_and_extract_archivedownload_urlverify_str_arg)VisionDatasetc                        e Zd ZdZ	 	 	 	 ddededee   dee   deddf fd	Zde	fd
Z
de	deeef   fdZddZdefdZ x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.Nrootsplit	transformtarget_transformdownloadreturnc                 n   	 dd l m} t        |   |||       t        |dd      | _        t        j                  |      dz  | _
        | j                  dz  }| j                  dk(  r|d	z  | _        | j                  d
z  | _        n(| j                  dz  | _        | j                  dz  | _        |r| j                          | j                         st        d      |j                  | j                  d      d   D cg c]%  }t!        | j                  |d   z        |d   dz
  f' c}| _        |j                  t!        |dz        d      d   j%                         | _        t)        | j&                        D 	
ci c]  \  }	}
|
|	
 c}
}	| _        y # t        $ r t        d      w xY wc c}w c c}
}	w )Nr   zQScipy is not found. This dataset needs to have scipy installed: pip install scipy)r   r   r   )trainteststanford_carsdevkitr   zcars_train_annos.mat
cars_trainzcars_test_annos_withlabels.mat	cars_testz;Dataset not found. You can use download=True to download itT)
squeeze_meannotationsfnameclassr   zcars_meta.matclass_names)scipy.ioioImportErrorRuntimeErrorsuper__init__r   _splitpathlibPath_base_folder_annotations_mat_path_images_base_pathr   _check_existsloadmatstr_samplestolistclasses	enumerateclass_to_idx)selfr   r   r   r   r   sior   
annotationicls	__class__s              mC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torchvision/datasets/stanford_cars.pyr&   zStanfordCars.__init__    s   	t" 	EUV$UG5FG#LL.@""X-;;'!)/2H)HD&%)%6%6%ED")-):):=])]D&%)%6%6%DD"MMO!!#\]] "kk$*D*DQUkVWde

 f
 D**Z-@@A7#a' f
 {{3v'?#@T{RS`ahhj2;DLL2IJ2I3S!V2IJ?  	trss	t,
 Ks   F >*F,=F1F)c                 ,    t        | j                        S )N)lenr0   r5   s    r;   __len__zStanfordCars.__len__L   s    4==!!    idxc                     | j                   |   \  }}t        j                  |      j                  d      }| j                  | j	                  |      }| j
                  | j                  |      }||fS )z.Returns pil_image and class_id for given indexRGB)r0   r   openconvertr   r   )r5   rA   
image_pathtarget	pil_images        r;   __getitem__zStanfordCars.__getitem__O   sm    !]]3/
FJJz*2259	>>%y1I  ,**62F&  r@   c                 N   | j                         ry t        dt        | j                        d       | j                  dk(  r"t        dt        | j                        d       y t        dt        | j                        d       t        d	t        | j                        d
       y )Nz4https://ai.stanford.edu/~jkrause/cars/car_devkit.tgz c3b158d763b6e2245038c8ad08e45376)urldownload_rootmd5r   z6https://ai.stanford.edu/~jkrause/car196/cars_train.tgz 065e5b463ae28d29e77c1b4b166cfe61z5https://ai.stanford.edu/~jkrause/car196/cars_test.tgz 4ce7ebf6a94d07f1952d94dd34c4d501zFhttps://ai.stanford.edu/~jkrause/car196/cars_test_annos_withlabels.mat b0a2b23655a3edd16d84508592a98d10)rL   r   rN   )r-   r	   r/   r*   r'   r
   r>   s    r;   r   zStanfordCars.downloadZ   s    $Fd//02	

 ;;'!(L!$"3"346 )K!$"3"346
 \**+6r@   c                     | j                   dz  j                         sy| j                  j                         xr | j                  j                         S )Nr   F)r*   is_dirr+   existsr,   r>   s    r;   r-   zStanfordCars._check_existsu   sE    !!H,446))002Vt7M7M7T7T7VVr@   )r   NNF)r   N)__name__
__module____qualname____doc__r/   r   r   boolr&   intr?   r   r   rI   r   r-   __classcell__)r:   s   @r;   r   r   
   s    !0 (,/3*K*K *K H%	*K
 #8,*K *K 
*KX" "	!s 	!uS#X 	!6Wt Wr@   r   )r(   typingr   r   r   r   PILr   utilsr	   r
   r   visionr   r    r@   r;   <module>ra      s*     1 1  M M !oW= oWr@   