
    ڧg                     ~    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dS )
    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testTNFrootr   	transformtarget_transformdownloadreturnc                    t                                          |||           |rdnd}| j        |         \  }}}	t          j                            | j        |          }
|r7t          j                            |
          st          || j        ||	           dd l	}|
                    |
          5 }d |                                D             }d |D             }t          j        |t          j                                      d	          }|d
z   dz  dz                      t          j                  }d |D             }d d d            n# 1 swxY w Y   || _        || _        d S )N)r   r   r   r   )md5r   c                 Z    g | ](}|                                                                 )S  )decodesplit).0lines     U/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/datasets/usps.py
<listcomp>z!USPS.__init__.<locals>.<listcomp>@   s,    III$++--III    c                 6    g | ]}d  |dd         D             S )c                 D    g | ]}|                     d           d         S ):)r   )r   xs     r    r!   z,USPS.__init__.<locals>.<listcomp>.<listcomp>A   s'    <<<ab)<<<r"   r
   Nr   )r   datas     r    r!   z!USPS.__init__.<locals>.<listcomp>A   s/    RRR<<48<<<RRRr"   )dtype)r&      r*   r
         c                 >    g | ]}t          |d                    dz
  S )r   r
   )int)r   ds     r    r!   z!USPS.__init__.<locals>.<listcomp>D   s&    777s1Q4yy1}777r"   )super__init__
split_listospathjoinr   existsr   bz2open	readlinesnpasarrayfloat32reshapeastypeuint8r(   targets)selfr   r   r   r   r   r   urlfilenamechecksum	full_pathr7   fpraw_datatmp_listimgsr@   	__class__s                    r    r1   zUSPS.__init__-   s    	EUVVV ,f"&/%"8XxGLLH55	 	ABGNN955 	Adix@@@@


XXi   	8BII",,..IIIHRRRRRH:hbj999AA,OODAXNS(00rx0@@D77h777G	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	s   4BEEEindexc                     | j         |         t          | j        |                   }}t          j        |d          }| j        |                     |          }| j        |                     |          }||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        L)mode)r(   r.   r@   r	   	fromarrayr   r   )rA   rK   imgtargets       r    __getitem__zUSPS.__getitem__I   sx     i&DL,?(@(@V oc,,,>%..%%C ,**622FF{r"   c                 *    t          | j                  S )N)lenr(   )rA   s    r    __len__zUSPS.__len___   s    49~~r"   )TNNF)__name__
__module____qualname____doc__r2   r   strr   boolr   r   r1   r.   r   r   rR   rU   __classcell__)rJ   s   @r    r   r      s        (
 
 

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