3
Á3W^¿  ã               @   s˜   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Zej	d  dkrPd dl
Znd dlZddlmZ ddlmZmZ G dd	„ d	eƒZG d
d„ deƒZdS )é    )Úprint_function)ÚImageNé   é   )ÚVisionDataset)Úcheck_integrityÚdownload_and_extract_archivec                   sš   e Zd ZdZdZdZdZdZddgdd	gd
dgddgddggZddggZ	ddddœZ
d'‡ fdd„	Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Z‡  ZS )(ÚCIFAR10aR  `CIFAR10 <https://www.cs.toronto.edu/~kriz/cifar.html>`_ Dataset.

    Args:
        root (string): Root directory of dataset where directory
            ``cifar-10-batches-py`` exists or will be saved to if download is set to True.
        train (bool, optional): If True, creates dataset from training set, otherwise
            creates from test set.
        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cifar-10-batches-pyz7https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gzzcifar-10-python.tar.gzZ c58f30108f718f92721af3b95e74349aZdata_batch_1Z c99cafc152244af753f735de768cd75fZdata_batch_2Z d4bba439e000b95fd0a9bffe97cbabecZdata_batch_3Z 54ebc095f3ab1f0389bbae665268c751Zdata_batch_4Z 634d18415352ddfa80567beed471001aZdata_batch_5Z 482c414d41f54cd18b22e5b47cb7c3cbZ
test_batchZ 40351d587109b95175f43aff81a1287ezbatches.metaZlabel_namesZ 5ff9c542aee3614f3951f8cda6e48888)ÚfilenameÚkeyÚmd5TNFc                s*  t t| ƒj|||d� || _|r(| jƒ  | jƒ s8tdƒ‚| jrF| j}n| j}g | _	g | _
xœ|D ]”\}}tjj| j| j|ƒ}	t|	dƒ�h}
tjd dkr tj|
ƒ}ntj|
dd�}| j	j|d	 ƒ d
|krØ| j
j|d
 ƒ n| j
j|d ƒ W d Q R X q^W tj| j	ƒjddddƒ| _	| j	jdƒ| _	| jƒ  d S )N)Ú	transformÚtarget_transformzDataset not found or corrupted.z) You can use download=True to download itÚrbr   r   Úlatin1)ÚencodingÚdataÚlabelsZfine_labelsr   é   é    zHDataset not found or corrupted. You can use download=True to download itéÿÿÿÿ)r   r   r   r   )Úsuperr	   Ú__init__ÚtrainÚdownloadÚ_check_integrityÚRuntimeErrorÚ
train_listÚ	test_listr   ÚtargetsÚosÚpathÚjoinÚrootÚbase_folderÚopenÚsysÚversion_infoÚpickleÚloadÚappendÚextendÚnpZvstackZreshapeZ	transposeÚ
_load_meta)Úselfr#   r   r   r   r   Zdownloaded_listÚ	file_nameZchecksumÚ	file_pathÚfÚentry)Ú	__class__© úA/tmp/pip-build-7wfi2gtz/torchvision/torchvision/datasets/cifar.pyr   7   s4    zCIFAR10.__init__c             C   sš   t jj| j| j| jd ƒ}t|| jd ƒs2tdƒ‚t|dƒ�>}t	j
d dkrXtj|ƒ}ntj|dd	�}|| jd
  | _W d Q R X dd„ t| jƒD ƒ| _d S )Nr
   r   z-Dataset metadata file not found or corrupted.z) You can use download=True to download itr   r   r   r   )r   r   c             S   s   i | ]\}}||“qS r4   r4   )Ú.0ÚiÚ_classr4   r4   r5   ú
<dictcomp>l   s    z&CIFAR10._load_meta.<locals>.<dictcomp>zVDataset metadata file not found or corrupted. You can use download=True to download it)r    r!   r"   r#   r$   Úmetar   r   r%   r&   r'   r(   r)   ÚclassesÚ	enumerateZclass_to_idx)r.   r!   Úinfiler   r4   r4   r5   r-   a   s    zCIFAR10._load_metac             C   sP   | j | | j|  }}tj|ƒ}| jdk	r4| j|ƒ}| jdk	rH| j|ƒ}||fS )z–
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        N)r   r   r   Z	fromarrayr   r   )r.   ÚindexÚimgÚtargetr4   r4   r5   Ú__getitem__n   s    




zCIFAR10.__getitem__c             C   s
   t | jƒS )N)Úlenr   )r.   r4   r4   r5   Ú__len__„   s    zCIFAR10.__len__c             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fentryr
   r   Zfpathr4   r4   r5   r   ‡   s    
zCIFAR10._check_integrityc             C   s0   | j ƒ rtdƒ d S t| j| j| j| jd� d S )Nz%Files already downloaded and verified)r
   r   )r   Úprintr   Úurlr#   r
   Útgz_md5)r.   r4   r4   r5   r   �   s    zCIFAR10.downloadc             C   s   dj | jdkrdndƒS )Nz	Split: {}TZTrainZTest)Úformatr   )r.   r4   r4   r5   Ú
extra_repr–   s    zCIFAR10.extra_repr)TNNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   rE   r
   rF   r   r   r:   r   r-   rA   rC   r   r   rH   Ú__classcell__r4   r4   )r3   r5   r	      s,   

 )	r	   c               @   s@   e Zd ZdZdZdZdZdZddggZdd	ggZ	d
dddœZ
dS )ÚCIFAR100zy`CIFAR100 <https://www.cs.toronto.edu/~kriz/cifar.html>`_ Dataset.

    This is a subclass of the `CIFAR10` Dataset.
    zcifar-100-pythonz8https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gzzcifar-100-python.tar.gzZ eb9058c3a382ffc7106e4002c42a8d85r   Z 16019d7e3df5f24257cddd939b257f8dÚtestZ f0ef6b0ae62326f3e7ffdfab6717acfcr:   Zfine_label_namesZ 7973b15100ade9c7d40fb424638fde48)r
   r   r   N)rI   rJ   rK   rL   r$   rE   r
   rF   r   r   r:   r4   r4   r4   r5   rN   š   s   

rN   )Ú
__future__r   ZPILr   r    Úos.pathZnumpyr,   r&   r'   ÚcPickler(   Zvisionr   Úutilsr   r   r	   rN   r4   r4   r4   r5   Ú<module>   s   
 
