3
Á3W^Ø  ã               @   sT   d dl mZ d dlmZ d dlZd dlZddlmZ ddl	m
Z
 G dd„ de
ƒZdS )	é    )Úprint_function)ÚImageNé   )Údownload_url)ÚVisionDatasetc                   sH   e Zd ZdZdddgdddgdœZd‡ fdd„	Zdd„ Zdd„ Z‡  ZS )ÚUSPSa  `USPS <https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass.html#usps>`_ Dataset.
    The data-format is : [label [index:value ]*256 \n] * num_lines, where ``label`` lies in ``[1, 10]``.
    The value for each pixel lies in ``[-1, 1]``. Here we transform the ``label`` into ``[0, 9]``
    and make pixel values in ``[0, 255]``.

    Args:
        root (string): Root directory of dataset to store``USPS`` data files.
        train (bool, optional): If True, creates dataset from ``usps.bz2``,
            otherwise from ``usps.t.bz2``.
        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.

    zKhttps://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/usps.bz2zusps.bz2Z ec16c51db3855ca6c91edd34d0e9b197zMhttps://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/usps.t.bz2z
usps.t.bz2Z 8ea070ee2aca1ac39742fdd1ef5ed118)ÚtrainÚtestTNFc                sò   t t| ƒj|||d� |rdnd}| j| \}}}	tjj| j|ƒ}
|rftjj|
ƒ rft	|| j||	d� dd l
}|j|
ƒ�d}dd„ |jƒ D ƒ}dd„ |D ƒ}tj|tjd	�jdƒ}|d
 d d jtjd	�}dd„ |D ƒ}W d Q R X || _|| _d S )N)Ú	transformÚtarget_transformr   r	   )Úmd5r   c             S   s   g | ]}|j ƒ jƒ ‘qS © )ÚdecodeÚsplit)Ú.0Úlr   r   ú@/tmp/pip-build-7wfi2gtz/torchvision/torchvision/datasets/usps.pyú
<listcomp>5   s    z!USPS.__init__.<locals>.<listcomp>c             S   s"   g | ]}d d„ |dd… D ƒ‘qS )c             S   s   g | ]}|j d ƒd ‘qS )ú:r   éÿÿÿÿ)r   )r   Úxr   r   r   r   6   s    z,USPS.__init__.<locals>.<listcomp>.<listcomp>r   Nr   )r   Údatar   r   r   r   6   s    )Zdtyper   é   é   éÿ   c             S   s   g | ]}t |d  ƒd ‘qS )r   r   )Úint)r   Údr   r   r   r   9   s    r   )r   r   r   )Úsuperr   Ú__init__Ú
split_listÚosÚpathÚjoinÚrootÚexistsr   Úbz2ÚopenÚ	readlinesÚnpZasarrayZfloat32ZreshapeZastypeZuint8r   Útargets)Úselfr#   r   r
   r   Údownloadr   ÚurlÚfilenameZchecksumÚ	full_pathr%   ÚfpZraw_dataZimgsr)   )Ú	__class__r   r   r   (   s     zUSPS.__init__c             C   sX   | j | t| j| ƒ }}tj|d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.
        ÚL)ÚmodeN)r   r   r)   r   Z	fromarrayr
   r   )r*   ÚindexÚimgÚtargetr   r   r   Ú__getitem__>   s    



zUSPS.__getitem__c             C   s
   t | jƒS )N)Úlenr   )r*   r   r   r   Ú__len__T   s    zUSPS.__len__)TNNF)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r6   r8   Ú__classcell__r   r   )r0   r   r   
   s    r   )Ú
__future__r   ZPILr   r    Znumpyr(   Úutilsr   Zvisionr   r   r   r   r   r   Ú<module>   s   