
    kh                     v    d dl Z d dlmZ d dlmZ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)Path)AnyCallableOptionalTupleUnion)Image   )download_url)VisionDatasetc                        e Zd ZdZg dg ddZ	 	 	 	 ddeeef   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 (str or ``pathlib.Path``): 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 a 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_datar3   xtmp_listimgsdr4   	__class__s                        U/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/datasets/usps.pyr!   zUSPS.__init__-   sv    	EUV f"&//%"8XxGGLLH5	BGGNN95diix@XXi  	8B:<,,.I$++-IHIIQR48<ab)<RHR::hbjj9AA,ODAXNS(00rxx0@D.67s1Q4y1}7G7	8 	 J<R 8	8 	8sO   &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)r3   r2   r4   r	   	fromarrayr   r   )r5   rC   imgtargets       rB   __getitem__zUSPS.__getitem__I   st     ii&DLL,?(@V ooc,>>%..%C  ,**62FF{    c                 ,    t        | j                        S )N)lenr3   )r5   s    rB   __len__zUSPS.__len___   s    499~rK   )TNNF)__name__
__module____qualname____doc__r"   r   strr   boolr   r   r!   r2   r   r   rJ   rN   __classcell__)rA   s   @rB   r   r      s    (


J  (,/3CI  H%	
 #8,  
8 sCx , rK   r   )r#   pathlibr   typingr   r   r   r   r   numpyr,   PILr	   utilsr   visionr   r    rK   rB   <module>r]      s+    	  8 8    !T= TrK   