a
    áñhÀ  ã                   @   sp   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mZ d dlmZmZmZ G dd„ deƒZdS )	é   )ÚVisionDataseté    )ÚImageN)ÚAnyÚCallableÚOptionalÚTuple)Údownload_urlÚcheck_integrityÚverify_str_argc                       s¢   e Zd ZdZg d¢g d¢g d¢dœZdeeee ee edd	œ‡ fd
d„Z	e
eeef dœdd„Ze
dœdd„Zedœdd„Zddœdd„Zedœ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]`

    .. warning::

        This class needs `scipy <https://docs.scipy.org/doc/>`_ to load data from `.mat` format.

    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 e26dedcc434d2e4c54c9b2d4a06d8373)z5http://ufldl.stanford.edu/housenumbers/test_32x32.matztest_32x32.matZ eb5a983be6a315427106f1b164d9cef3)z6http://ufldl.stanford.edu/housenumbers/extra_32x32.matzextra_32x32.matZ a93ce644f1a588dc4d68dda5feec44a7)ÚtrainÚtestÚextrar   NF)ÚrootÚsplitÚ	transformÚtarget_transformÚdownloadÚreturnc                    sè   t t| ƒj|||d� t|dt| j ¡ ƒƒ| _| j| d | _| j| d | _	| j| d | _
|rj|  ¡  |  ¡ sztdƒ‚dd lm} | tj | j| j	¡¡}|d | _|d  tj¡ ¡ | _t | j| jd	kd¡ t | jd
¡| _d S )N)r   r   r   r   r   é   zHDataset not found or corrupted. You can use download=True to download itÚXÚyé
   )é   r   r   r   )Úsuperr   Ú__init__r   ÚtupleÚ
split_listÚkeysr   ÚurlÚfilenameZfile_md5r   Ú_check_integrityÚRuntimeErrorZscipy.ioÚioZloadmatÚosÚpathÚjoinr   ÚdataZastypeÚnpÚint64ZsqueezeÚlabelsZplaceÚ	transpose)Úselfr   r   r   r   r   ÚsioZ
loaded_mat©Ú	__class__© úY/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torchvision/datasets/svhn.pyr   +   s"    ÿ
zSVHN.__init__)Úindexr   c                 C   s\   | j | t| j| ƒ }}t t |d¡¡}| jdur@|  |¡}| jdurT|  |¡}||fS )z–
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        )r   r   r   N)	r(   Úintr+   r   Z	fromarrayr)   r,   r   r   )r-   r3   ÚimgÚtargetr1   r1   r2   Ú__getitem__U   s    



zSVHN.__getitem__)r   c                 C   s
   t | jƒS )N)Úlenr(   ©r-   r1   r1   r2   Ú__len__k   s    zSVHN.__len__c                 C   s0   | j }| j| j d }tj || j¡}t||ƒS ©Nr   )r   r   r   r%   r&   r'   r!   r
   )r-   r   Úmd5Zfpathr1   r1   r2   r"   n   s    zSVHN._check_integrityc                 C   s(   | j | j d }t| j| j| j|ƒ d S r;   )r   r   r	   r    r   r!   )r-   r<   r1   r1   r2   r   t   s    zSVHN.downloadc                 C   s   dj f i | j¤ŽS )NzSplit: {split})ÚformatÚ__dict__r9   r1   r1   r2   Ú
extra_reprx   s    zSVHN.extra_repr)r   NNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr   r   Úboolr   r4   r   r   r7   r:   r"   r   r?   Ú__classcell__r1   r1   r/   r2   r   
   s,   û    úù*r   )Zvisionr   ZPILr   r%   Úos.pathÚnumpyr)   Útypingr   r   r   r   Úutilsr	   r
   r   r   r1   r1   r1   r2   Ú<module>   s   