U
    (d~                     @   sl   d dl Zd dlmZmZmZmZmZ d dl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)AnyCallableOptionalTuplecast)Image   )check_integritydownload_and_extract_archiveverify_str_arg)VisionDatasetc                	       s&  e Zd ZdZdZdZdZdZdZdZ	dd	gd
dgddggZ
ddgddggZdZd-eeee ee ee edd fddZee ee dddZeeeef dddZedd d!Zd.eee eejeej f d"d#d$Zedd%d&Zddd'd(Zedd)d*Zee ddd+d,Z  ZS )/STL10a  `STL10 <https://cs.stanford.edu/~acoates/stl10/>`_ Dataset.

    Args:
        root (string): Root directory of dataset where directory
            ``stl10_binary`` exists.
        split (string): One of {'train', 'test', 'unlabeled', 'train+unlabeled'}.
            Accordingly dataset is selected.
        folds (int, optional): One of {0-9} or None.
            For training, loads one of the 10 pre-defined folds of 1k samples for the
            standard evaluation procedure. If no value is passed, loads the 5k samples.
        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.
    Zstl10_binaryz9http://ai.stanford.edu/~acoates/stl10/stl10_binary.tar.gzzstl10_binary.tar.gzZ 91f7769df0f17e558f3565bffb0c7dfbzclass_names.txtzfold_indices.txtztrain_X.binZ 918c2871b30a85fa023e0c44e0bee87fztrain_y.binZ 5a34089d4802c674881badbb80307741zunlabeled_X.binZ 5242ba1fed5e4be9e1e742405eb56ca4z
test_X.binZ 7f263ba9f9e0b06b93213547f721ac82z
test_y.binZ 36f9794fa4beb8a2c72628de14fa638e)traintrain+unlabeled	unlabeledtestr   NF)rootsplitfolds	transformtarget_transformdownloadreturnc              	      s  t  j|||d t|d| j| _| || _|r<|   n|  sLt	d|  | jdkr| 
| jd d | jd d \| _| _ttj| j| _| | n| jdkr>| 
| jd d | jd d \| _| _ttj| j| _| | | 
| jd d \}}t| j|f| _t| jtd	g|jd  f| _nj| jd
kr| 
| jd d \| _}td	g| jjd  | _n(| 
| jd d | jd d \| _| _tj| j| j| j}	tj|	rt|	}
|
  | _ W 5 Q R X d S )N)r   r   r   zHDataset not found or corrupted. You can use download=True to download itr   r   r   r      r   )!super__init__r   splitsr   _verify_foldsr   r   _check_integrityRuntimeError_STL10__loadfile
train_listdatalabelsr   npndarray_STL10__load_foldsZconcatenateZasarrayshape	test_listospathjoinr   base_folderclass_names_fileisfileopenread
splitlinesclasses)selfr   r   r   r   r   r   Zunlabeled_data_Z
class_filef	__class__ >/tmp/pip-unpacked-wheel-vx7f76es/torchvision/datasets/stl10.pyr   .   s6    	

((
&(
zSTL10.__init__)r   r   c                 C   sT   |d kr|S t |tr:|tdkr&|S d}t||nd}t|t|d S )N
   zDValue for argument folds should be in the range [0, 10), but got {}.z>Expected type None or int for argument folds, but got type {}.)
isinstanceintrange
ValueErrorformattype)r4   r   msgr9   r9   r:   r   Z   s    
zSTL10._verify_folds)indexr   c                 C   sx   | j dk	r&| j| t| j |  }}n| j| d }}tt|d}| jdk	r\| |}| jdk	rp| |}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        N)r   r   r   )	r$   r#   r=   r   Z	fromarrayr%   	transposer   r   )r4   rC   imgtargetr9   r9   r:   __getitem__f   s    	




zSTL10.__getitem__)r   c                 C   s   | j jd S )Nr   )r#   r(   r4   r9   r9   r:   __len__   s    zSTL10.__len__)	data_filelabels_filer   c           	   	   C   s   d }|rFt j| j| j|}t|d}tj|tjdd }W 5 Q R X t j| j| j|}t|d.}tj|tjd}t	|d}t
|d}W 5 Q R X ||fS )Nrb)dtyper   )r      `   rO   )r   r   rN   r   )r*   r+   r,   r   r-   r0   r%   ZfromfileZuint8ZreshaperD   )	r4   rJ   rK   r$   Zpath_to_labelsr6   Zpath_to_dataZ
everythingZimagesr9   r9   r:   Z
__loadfile   s    zSTL10.__loadfilec                 C   sP   | j }| j| j D ]8}|d |d  }}tj|| j|}t||s dS qdS )Nr   r   FT)r   r"   r)   r*   r+   r,   r-   r	   )r4   r   Zfentryfilenamemd5Zfpathr9   r9   r:   r      s    
zSTL10._check_integrityc                 C   s8   |   rtd d S t| j| j| j| jd |    d S )Nz%Files already downloaded and verified)rP   rQ   )r   printr
   urlr   rP   tgz_md5rH   r9   r9   r:   r      s
    zSTL10.downloadc                 C   s   dj f | jS )NzSplit: {split})r@   __dict__rH   r9   r9   r:   
extra_repr   s    zSTL10.extra_reprc              	   C   s   |d krd S t j| j| j| j}t|^}|  | }t	j
|t	jdd}| j|d d d d d d f | _| jd k	r| j| | _W 5 Q R X d S )N )rM   sep)r*   r+   r,   r   r-   folds_list_filer0   r1   r2   r%   
fromstringZint64r#   r$   )r4   r   Zpath_to_foldsr6   Zstr_idxZlist_idxr9   r9   r:   Z__load_folds   s    
 
zSTL10.__load_folds)r   NNNF)N) __name__
__module____qualname____doc__r-   rS   rP   rT   r.   rY   r"   r)   r   strr   r=   r   boolr   r   r   r   rG   rI   r%   r&   r!   r   r   rV   r'   __classcell__r9   r9   r7   r:   r      sF        ,(	r   )os.pathr*   typingr   r   r   r   r   Znumpyr%   ZPILr   utilsr	   r
   r   Zvisionr   r   r9   r9   r9   r:   <module>   s   