o
    ôj}  ã                   @   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.
    Zstl10_binaryz9http://ai.stanford.edu/~acoates/stl10/stl10_binary.tar.gzzstl10_binary.tar.gzZ 91f7769df0f17e558f3565bffb0c7dfbzclass_names.txtzfold_indices.txtztrain_X.binZ 918c2871b30a85fa023e0c44e0bee87fztrain_y.binZ 5a34089d4802c674881badbb80307741zunlabeled_X.binZ 5242ba1fed5e4be9e1e742405eb56ca4z
test_X.binZ 7f263ba9f9e0b06b93213547f721ac82z
test_y.binZ 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 súw   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   Zunlabeled_dataÚ_Z
class_fileÚf©Ú	__class__© úk/var/www/html/academy/tools/voices/clone/sv-venv/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)r8   r   Úmsgr=   r=   r>   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%   rA   r	   Ú	fromarrayr'   Ú	transposer   r   )r8   rG   ÚimgÚtargetr=   r=   r>   Ú__getitem__g   s   
	



zSTL10.__getitem__c                 C   s   | j jd S )Nr   )r%   r,   ©r8   r=   r=   r>   Ú__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   é   é`   rT   )r   r
   rS   r   )r.   r/   r0   r   r1   r4   r'   ÚfromfileÚuint8ÚreshaperI   )	r8   rO   rP   r&   Zpath_to_labelsr:   Zpath_to_dataÚ
everythingÚimagesr=   r=   r>   Z
__loadfile„   s   ÿ
üúzSTL10.__loadfilec                 C   s>   | j | j D ]\}}tj | j| j|¡}t||ƒs dS qdS )NFT)r$   r-   r.   r/   r0   r   r1   r   )r8   ÚfilenameÚmd5Úfpathr=   r=   r>   r!   ”   s   
ÿzSTL10._check_integrityc                 C   s8   |   ¡ r
tdƒ d S t| j| j| j| jd� |   ¡  d S )Nz%Files already downloaded and verified)rZ   r[   )r!   Úprintr   Úurlr   rZ   Útgz_md5rM   r=   r=   r>   r   ›   s
   zSTL10.downloadc                 C   s   dj di | j¤ŽS )NzSplit: {split}r=   )rD   Ú__dict__rM   r=   r=   r>   Ú
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ú )rR   Úsep)r.   r/   r0   r   r1   Úfolds_list_filer4   r5   r6   r'   Ú
fromstringÚint64r%   r&   )r8   r   Zpath_to_foldsr:   Zstr_idxÚlist_idxr=   r=   r>   Z__load_folds¥   s   
 
û"üzSTL10.__load_folds)r   NNNF)N)r   N)"Ú__name__Ú
__module__Ú__qualname__Ú__doc__r1   r^   rZ   r_   r2   rd   r$   r-   r   r   Ústrr   r   rA   r   Úboolr   r    r   r   rL   rN   r'   r(   r#   r!   r   ra   r)   Ú__classcell__r=   r=   r;   r>   r      sT    ýù
þýüûúùø,,
r   )Úos.pathr.   Úpathlibr   Útypingr   r   r   r   r   r   Únumpyr'   ÚPILr	   Úutilsr   r   r   Úvisionr   r   r=   r=   r=   r>   Ú<module>   s     