
    Ph                     f    d dl Z d dlmZmZmZmZ d dlZd dlm	Z	 ddl
mZ ddlmZ  G d de      Zy)	    N)AnyCallableOptionalTuple)Image   )download_url)VisionDatasetc                        e Zd ZdZg dg ddZ	 	 	 	 ddededee   d	ee   d
eddf fdZ	de
deeef   fdZde
fdZ x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.bz2 ec16c51db3855ca6c91edd34d0e9b197)zMhttps://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/usps.t.bz2z
usps.t.bz2 8ea070ee2aca1ac39742fdd1ef5ed118)traintestNrootr   	transformtarget_transformdownloadreturnc                    t         |   |||       |rdnd}| j                  |   \  }}}	t        j                  j                  | j                  |      }
|r8t        j                  j                  |
      st        || j                  ||	       dd l	}|j                  |
      5 }|j                         D cg c]   }|j                         j                         " }}|D cg c]'  }|dd  D cg c]  }|j                  d      d    c}) }}}t        j                  |t        j                   	      j#                  d
      }|dz   dz  dz  j%                  t        j&                  	      }|D cg c]  }t)        |d         dz
   }}d d d        | _        | _        y c c}w c c}w c c}}w c c}w # 1 sw Y   -xY w)N)r   r   r   r   )md5r   r   :)dtype)r      r         )super__init__
split_listospathjoinr   existsr	   bz2open	readlinesdecodesplitnpasarrayfloat32reshapeastypeuint8intdatatargets)selfr   r   r   r   r   r)   urlfilenamechecksum	full_pathr%   fplineraw_datar1   xtmp_listimgsdr2   	__class__s                        dC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torchvision/datasets/usps.pyr   zUSPS.__init__,   sv    	EUV f"&//%"8XxGGLLH5	BGGNN95diix@XXi B:<,,.I.$++-.HIIQR48<8ab)8<HR::hbjj9AA,ODAXNS(00rxx0@D.67hs1Q4y1}hG7 ! 	 J<R 8 ! sO   &F;9%F&F;%F01F+F0A$F;5F6F;&F;+F00F;;Gindexc                     | j                   |   t        | j                  |         }}t        j                  |d      }| j
                  | j                  |      }| j                  | j                  |      }||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        L)mode)r1   r0   r2   r   	fromarrayr   r   )r3   rA   imgtargets       r@   __getitem__zUSPS.__getitem__H   st     ii&DLL,?(@V ooc,>>%..%C  ,**62FF{    c                 ,    t        | j                        S )N)lenr1   )r3   s    r@   __len__zUSPS.__len__^   s    499~rI   )TNNF)__name__
__module____qualname____doc__r    strboolr   r   r   r0   r   r   rH   rL   __classcell__)r?   s   @r@   r   r      s    (


J  (,/3  H%	
 #8,  
8 sCx , rI   r   )r!   typingr   r   r   r   numpyr*   PILr   utilsr	   visionr
   r    rI   r@   <module>rZ      s(    	 1 1    !T= TrI   