3
Á3W^µ  ã               @   sd   d dl mZ 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mZmZ G dd„ deƒZdS )	é    )Úprint_functioné   )ÚVisionDataset)ÚImageN)Údownload_urlÚcheck_integrityÚverify_str_argc                   st   e Zd ZdZdZdZdZdddgdddgd	d
dgdœZd‡ fdd„	Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Z‡  ZS )ÚSVHNa  `SVHN <http://ufldl.stanford.edu/housenumbers/>`_ Dataset.
    Note: The SVHN dataset assigns the label `10` to the digit `0`. However, in this Dataset,
    we assign the label `0` to the digit `0` to be compatible with PyTorch loss functions which
    expect the class labels to be in the range `[0, C-1]`

    Args:
        root (string): Root directory of dataset where directory
            ``SVHN`` exists.
        split (string): One of {'train', 'test', 'extra'}.
            Accordingly dataset is selected. 'extra' is Extra training 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.

    Ú z6http://ufldl.stanford.edu/housenumbers/train_32x32.matztrain_32x32.matZ e26dedcc434d2e4c54c9b2d4a06d8373z5http://ufldl.stanford.edu/housenumbers/test_32x32.matztest_32x32.matZ eb5a983be6a315427106f1b164d9cef3z6http://ufldl.stanford.edu/housenumbers/extra_32x32.matzextra_32x32.matZ a93ce644f1a588dc4d68dda5feec44a7)ÚtrainÚtestÚextrar   NFc                sæ   t t| ƒj|||d� t|dt| jjƒ ƒƒ| _| j| d | _| j| d | _	| j| d | _
|rj| jƒ  | jƒ sztdƒ‚dd lj}|jtjj| j| j	ƒƒ}|d | _|d	 jtjƒjƒ | _tj| j| jd
kdƒ tj| jdƒ| _d S )N)Ú	transformÚtarget_transformÚsplitr   r   é   zDataset not found or corrupted.z) You can use download=True to download itÚXÚyé
   é   zHDataset not found or corrupted. You can use download=True to download it)r   r   r   r   )Úsuperr	   Ú__init__r   ÚtupleÚ
split_listÚkeysr   ÚurlÚfilenameÚfile_md5ÚdownloadÚ_check_integrityÚRuntimeErrorZscipy.ioÚioZloadmatÚosÚpathÚjoinÚrootÚdataZastypeÚnpZint64ZsqueezeÚlabelsZplaceÚ	transpose)Úselfr%   r   r   r   r   ÚsioZ
loaded_mat)Ú	__class__© ú@/tmp/pip-build-7wfi2gtz/torchvision/torchvision/datasets/svhn.pyr   *   s"    

zSVHN.__init__c             C   s\   | j | t| j| ƒ }}tjtj|dƒƒ}| jdk	r@| j|ƒ}| jdk	rT| j|ƒ}||fS )z–
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        r   r   r   N)r   r   r   )	r&   Úintr(   r   Z	fromarrayr'   r)   r   r   )r*   ÚindexÚimgÚtargetr-   r-   r.   Ú__getitem__N   s    



zSVHN.__getitem__c             C   s
   t | jƒS )N)Úlenr&   )r*   r-   r-   r.   Ú__len__d   s    zSVHN.__len__c             C   s0   | j }| j| j d }tjj|| jƒ}t||ƒS )Nr   )r%   r   r   r"   r#   r$   r   r   )r*   r%   Úmd5Zfpathr-   r-   r.   r   g   s    zSVHN._check_integrityc             C   s(   | j | j d }t| j| j| j|ƒ d S )Nr   )r   r   r   r   r%   r   )r*   r6   r-   r-   r.   r   m   s    zSVHN.downloadc             C   s   dj f | jŽS )NzSplit: {split})ÚformatÚ__dict__)r*   r-   r-   r.   Ú
extra_reprq   s    zSVHN.extra_repr)r   NNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r3   r5   r   r   r9   Ú__classcell__r-   r-   )r,   r.   r	   
   s"    #r	   )Ú
__future__r   Zvisionr   ZPILr   r"   Úos.pathZnumpyr'   Úutilsr   r   r   r	   r-   r-   r-   r.   Ú<module>   s   