o
    VhB                     @   s|   d dl Zd dlmZ d dlmZmZmZmZm	Z	m
Z
 d dlZd dlmZ ddlmZmZmZ ddlmZ G dd	 d	eZdS )
    N)Path)AnyCallablecastOptionalTupleUnion)Image   )check_integritydownload_and_extract_archiveverify_str_arg)VisionDatasetc                       sJ  e Zd ZdZdZdZdZdZdZdZ	dd	gd
dgddggZ
ddgddggZdZ					d2deeef dedee dee dee deddf fddZdee dee fdd Zd!edeeef fd"d#Zdefd$d%Zd3d&ed'ee deejeej f fd(d)Zdefd*d+Zd4d,d-Zdefd.d/Zdee ddfd0d1Z   Z!S )5STL10a  `STL10 <https://cs.stanford.edu/~acoates/stl10/>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of dataset where directory
            ``stl10_binary`` exists.
        split (string): One of {'train', 'test', 'unlabeled', 'train+unlabeled'}.
            Accordingly, dataset is selected.
        folds (int, optional): One of {0-9} or None.
            For training, loads one of the 10 pre-defined folds of 1k samples for the
            standard evaluation procedure. If no value is passed, loads the 5k samples.
        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.
    stl10_binaryz9http://ai.stanford.edu/~acoates/stl10/stl10_binary.tar.gzzstl10_binary.tar.gz 91f7769df0f17e558f3565bffb0c7dfbzclass_names.txtzfold_indices.txtztrain_X.bin 918c2871b30a85fa023e0c44e0bee87fztrain_y.bin 5a34089d4802c674881badbb80307741zunlabeled_X.bin 5242ba1fed5e4be9e1e742405eb56ca4z
test_X.bin 7f263ba9f9e0b06b93213547f721ac82z
test_y.bin 36f9794fa4beb8a2c72628de14fa638e)traintrain+unlabeled	unlabeledtestr   NFrootsplitfolds	transformtarget_transformdownloadreturnc                    s  t  j|||d t|d| j| _| || _|r|   n|  s&t	d|  | jdkrO| 
| jd d | jd d \| _| _ttj| j| _| | n| jdkr| 
| jd d | jd d \| _| _ttj| j| _| | | 
| jd d \}}t| j|f| _t| jtd	g|jd  f| _n4| jd
kr| 
| jd d \| _}td	g| jjd  | _n| 
| jd d | jd d \| _| _tj| j| j| j}	tj|	rt|	}
|
  | _ W d    d S 1 sw   Y  d S d S )N)r   r   r   zHDataset not found or corrupted. You can use download=True to download itr   r   r
   r      r   )!super__init__r   splitsr   _verify_foldsr   r    _check_integrityRuntimeError_STL10__loadfile
train_listdatalabelsr   npndarray_STL10__load_foldsconcatenateasarrayshape	test_listospathjoinr   base_folderclass_names_fileisfileopenread
splitlinesclasses)selfr   r   r   r   r   r    unlabeled_data_
class_filef	__class__ N/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/datasets/stl10.pyr%   /   s:   	

(
(
&
(
"zSTL10.__init__c                 C   sN   |d u r|S t |tr|tdv r|S d}t||d}t|t|)N
   zDValue for argument folds should be in the range [0, 10), but got {}.z>Expected type None or int for argument folds, but got type {}.)
isinstanceintrange
ValueErrorformattype)r?   r   msgrF   rF   rG   r'   [   s   
zSTL10._verify_foldsindexc                 C   sx   | j dur| j| t| j | }}n| j| d}}tt|d}| jdur.| |}| jdur8| |}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        N)r
   r"   r   )	r-   r,   rJ   r	   	fromarrayr.   	transposer   r   )r?   rP   imgtargetrF   rF   rG   __getitem__g   s   
	



zSTL10.__getitem__c                 C   s   | j jd S )Nr   )r,   r3   r?   rF   rF   rG   __len__   s   zSTL10.__len__	data_filelabels_filec           	      C   s   d }|r-t j| j| j|}t|d}tj|tjdd }W d    n1 s(w   Y  t j| j| j|}t|d}tj|tjd}t	|d}t
|d}W d    ||fS 1 s^w   Y  ||fS )Nrb)dtyper
   )r#      `   r]   )r   r
   r\   r"   )r5   r6   r7   r   r8   r;   r.   fromfileuint8reshaperR   )	r?   rX   rY   r-   path_to_labelsrC   path_to_data
everythingimagesrF   rF   rG   
__loadfile   s   
zSTL10.__loadfilec                 C   s>   | j | j D ]\}}tj| j| j|}t||s dS qdS )NFT)r+   r4   r5   r6   r7   r   r8   r   )r?   filenamemd5fpathrF   rF   rG   r(      s   
zSTL10._check_integrityc                 C   s0   |   rd S t| j| j| j| jd |    d S )N)rf   rg   )r(   r   urlr   rf   tgz_md5rV   rF   rF   rG   r       s   zSTL10.downloadc                 C   s   dj di | jS )NzSplit: {split}rF   )rM   __dict__rV   rF   rF   rG   
extra_repr   s   zSTL10.extra_reprc                 C   s   |d u rd S t j| j| j| j}t|=}|  | }t	j
|t	jdd}| j|d d d d d d f | _| jd urJ| j| | _W d    d S W d    d S 1 sUw   Y  d S )N )r[   sep)r5   r6   r7   r   r8   folds_list_filer;   r<   r=   r.   
fromstringint64r,   r-   )r?   r   path_to_foldsrC   str_idxlist_idxrF   rF   rG   __load_folds   s   
 
"zSTL10.__load_folds)r   NNNF)N)r!   N)"__name__
__module____qualname____doc__r8   ri   rf   rj   r9   ro   r+   r4   r&   r   strr   r   rJ   r   boolr%   r'   r   r   rU   rW   r.   r/   r*   r(   r    rl   r0   __classcell__rF   rF   rD   rG   r      sT    
,,
r   )os.pathr5   pathlibr   typingr   r   r   r   r   r   numpyr.   PILr	   utilsr   r   r   visionr   r   rF   rF   rF   rG   <module>   s     