3
Á3W^&  ã               @   sd   d dl mZ d dlmZ d dlZd dlZd dlZddlm	Z	 ddl
mZmZmZ G dd„ de	ƒZdS )	é    )Úprint_function)ÚImageNé   )ÚVisionDataset)Úcheck_integrityÚdownload_and_extract_archiveÚverify_str_argc                   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+‡ fdd„	Zdd„ Zdd„ Zdd„ Zd,d d!„Zd"d#„ Zd$d%„ Zd&d'„ Z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ÚtestNFc          	      sÎ  t t| ƒj|||d� t|d| jƒ| _| j|ƒ| _|r>| jƒ  | j	ƒ sNt
dƒ‚| jdkrŒ| j| jd d | jd d ƒ\| _| _| j|ƒ nø| jdk�r| j| jd d | jd d ƒ\| _| _| j|ƒ | j| jd d ƒ\}}tj| j|fƒ| _tj| jtjd
g|jd  ƒfƒ| _nj| jd	k�r\| j| jd d ƒ\| _}tjdg| jjd  ƒ| _n(| j| jd d | jd d ƒ\| _| _tjj| j| j| jƒ}	tjj|	ƒ�rÊt|	ƒ�}
|
jƒ jƒ | _W d Q R X d S )N)Ú	transformÚtarget_transformÚsplitzHDataset not found or corrupted. You can use download=True to download itr
   r   r   ztrain+unlabeledé   r   éÿÿÿÿr   ) Úsuperr	   Ú__init__r   Úsplitsr   Ú_verify_foldsÚfoldsÚdownloadÚ_check_integrityÚRuntimeErrorÚ_STL10__loadfileÚ
train_listÚdataÚlabelsÚ_STL10__load_foldsÚnpZconcatenateZasarrayÚshapeÚ	test_listÚosÚpathÚjoinÚrootÚbase_folderÚclass_names_fileÚisfileÚopenÚreadÚ
splitlinesÚclasses)Úselfr&   r   r   r   r   r   Zunlabeled_dataÚ_Z
class_fileÚf)Ú	__class__© úA/tmp/pip-build-7wfi2gtz/torchvision/torchvision/datasets/stl10.pyr   1   s>    
$$
"$
zSTL10.__init__c             C   sT   |d kr|S t |tƒr:|tdƒkr&|S d}t|j|ƒƒ‚nd}t|j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)r.   r   Úmsgr2   r2   r3   r   \   s    
zSTL10._verify_foldsc             C   sx   | j dk	r&| j| t| j | ƒ }}n| j| d }}tjtj|dƒƒ}| jdk	r\| j|ƒ}| jdk	rp| j|ƒ}||fS )z–
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        Nr   r   r   )r   r   r   )	r   r   r6   r   Z	fromarrayr    Ú	transposer   r   )r.   ÚindexÚimgÚtargetr2   r2   r3   Ú__getitem__i   s    




zSTL10.__getitem__c             C   s   | j jd S )Nr   )r   r!   )r.   r2   r2   r3   Ú__len__‚   s    zSTL10.__len__c       	      C   s    d }|rFt jj| j| j|ƒ}t|dƒ�}tj|tjd�d }W d Q R X t jj| j| j|ƒ}t|dƒ�.}tj|tjd�}tj	|d	ƒ}tj
|d
ƒ}W d Q R X ||fS )NÚrb)Údtyper   é   é`   r   r   r   )r   rD   rE   rE   )r   r   rD   r   )r#   r$   r%   r&   r'   r*   r    ZfromfileÚuint8Zreshaper<   )	r.   Z	data_fileZlabels_filer   Zpath_to_labelsr0   Zpath_to_dataZ
everythingZimagesr2   r2   r3   Z
__loadfile…   s    zSTL10.__loadfilec             C   sR   | j }xF| j| j D ]6}|d |d  }}tjj|| j|ƒ}t||ƒsdS qW dS )Nr   r   FT)r&   r   r"   r#   r$   r%   r'   r   )r.   r&   ZfentryÚfilenameÚmd5Zfpathr2   r2   r3   r   –   s    
zSTL10._check_integrityc             C   s0   | j ƒ rtdƒ d S t| j| j| j| jd� d S )Nz%Files already downloaded and verified)rG   rH   )r   Úprintr   Úurlr&   rG   Útgz_md5)r.   r2   r2   r3   r   Ÿ   s    zSTL10.downloadc             C   s   dj f | jŽS )NzSplit: {split})r9   Ú__dict__)r.   r2   r2   r3   Ú
extra_repr¥   s    zSTL10.extra_reprc             C   sŒ   |d krd S t jj| j| j| jƒ}t|dƒ�V}|jƒ jƒ | }t	j
|t	jdd�}| j|d d …d d …d d …f | j|  | _| _W d Q R X d S )NÚrú )rC   Úsep)r#   r$   r%   r&   r'   Úfolds_list_filer*   r+   r,   r    Ú
fromstringrF   r   r   )r.   r   Zpath_to_foldsr0   Zstr_idxZlist_idxr2   r2   r3   Z__load_folds¨   s    zSTL10.__load_folds)r
   r   r   r   )r
   NNNF)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   rJ   rG   rK   r(   rQ   r   r"   r   r   r   r@   rA   r   r   r   rM   r   Ú__classcell__r2   r2   )r1   r3   r	      s.   

 *
	r	   )Ú
__future__r   ZPILr   r#   Úos.pathZnumpyr    Zvisionr   Úutilsr   r   r   r	   r2   r2   r2   r3   Ú<module>   s   