o
    ôjó!  ã                   @   s|  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ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	d
dœZdZG dd„ deƒZd!deeef dee dee	eef ee f fdd„Zdeeef dededdfdd„Zd!deeef dee ddfdd„Zd"deeef dee deddfdd„Z	d#deeef dee deee  deddf
dd „ZdS )$é    N)Úcontextmanager)ÚPath)ÚAnyÚDictÚIteratorÚListÚOptionalÚTupleÚUnioné   )ÚImageFolder)Úcheck_integrityÚextract_archiveÚverify_str_arg)zILSVRC2012_img_train.tarZ 1d675b47d978889d74fa0da5fadfb00e)zILSVRC2012_img_val.tarZ 29b22e2961454d5413ddabcf34fc5622)zILSVRC2012_devkit_t12.tar.gzZ fa75699e90414af021442c21a62c3abf)ÚtrainÚvalÚdevkitzmeta.binc                	       sf   e Zd ZdZddeeef dededdf‡ fdd	„Zdd
d„Z	e
defdd„ƒZdefdd„Z‡  ZS )ÚImageNeta]  `ImageNet <http://image-net.org/>`_ 2012 Classification Dataset.

    .. note::
        Before using this class, it is required to download ImageNet 2012 dataset from
        `here <https://image-net.org/challenges/LSVRC/2012/2012-downloads.php>`_ and
        place the files ``ILSVRC2012_devkit_t12.tar.gz`` and ``ILSVRC2012_img_train.tar``
        or ``ILSVRC2012_img_val.tar`` based on ``split`` in the root directory.

    Args:
        root (str or ``pathlib.Path``): Root directory of the ImageNet Dataset.
        split (string, optional): The dataset split, supports ``train``, or ``val``.
        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.
        loader (callable, optional): A function to load an image given its path.

     Attributes:
        classes (list): List of the class name tuples.
        class_to_idx (dict): Dict with items (class_name, class_index).
        wnids (list): List of the WordNet IDs.
        wnid_to_idx (dict): Dict with items (wordnet_id, class_index).
        imgs (list): List of (image path, class_index) tuples
        targets (list): The class_index value for each image in the dataset
    r   ÚrootÚsplitÚkwargsÚreturnNc                    s’   t j |¡ }| _t|ddƒ| _|  ¡  t| jƒd ‰ tƒ j	| j
fi |¤Ž || _| j| _| j| _‡ fdd„| jD ƒ| _dd„ t| jƒD ƒ| _d S )Nr   )r   r   r   c                    ó   g | ]}ˆ | ‘qS © r   )Ú.0Úwnid©Úwnid_to_classesr   ún/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/torchvision/datasets/imagenet.pyÚ
<listcomp>=   ó    z%ImageNet.__init__.<locals>.<listcomp>c                 S   s    i | ]\}}|D ]}||“qqS r   r   )r   ÚidxÚclssÚclsr   r   r   Ú
<dictcomp>>   ó     z%ImageNet.__init__.<locals>.<dictcomp>)ÚosÚpathÚ
expanduserr   r   r   Úparse_archivesÚload_meta_fileÚsuperÚ__init__Úsplit_folderÚclassesÚwnidsÚclass_to_idxZwnid_to_idxÚ	enumerate)Úselfr   r   r   ©Ú	__class__r   r   r,   1   s   zImageNet.__init__c                 C   sd   t tj | jt¡ƒst| jƒ tj | j¡s.| j	dkr"t
| jƒ d S | j	dkr0t| jƒ d S d S d S )Nr   r   )r   r&   r'   Újoinr   Ú	META_FILEÚparse_devkit_archiveÚisdirr-   r   Úparse_train_archiveÚparse_val_archive©r2   r   r   r   r)   @   s   


üzImageNet.parse_archivesc                 C   s   t j | j| j¡S ©N)r&   r'   r5   r   r   r;   r   r   r   r-   J   s   zImageNet.split_folderc                 C   s   dj di | j¤ŽS )NzSplit: {split}r   )ÚformatÚ__dict__r;   r   r   r   Ú
extra_reprN   s   zImageNet.extra_repr)r   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   Ústrr   r   r,   r)   Úpropertyr-   r?   Ú__classcell__r   r   r3   r   r      s    (

r   r   Úfiler   c                 C   sD   |d u rt }tj | |¡}t|ƒrtj|dd�S d}t| || ¡ƒ‚)NT)Úweights_onlyz‚The meta file {} is not present in the root directory or is corrupted. This file is automatically created by the ImageNet dataset.)	r6   r&   r'   r5   r   ÚtorchÚloadÚRuntimeErrorr=   )r   rG   Úmsgr   r   r   r*   R   s   ÿr*   Úmd5c                 C   s,   t tj | |¡|ƒsd}t| || ¡ƒ‚d S )Nz{The archive {} is not present in the root directory or is corrupted. You need to download it externally and place it in {}.)r   r&   r'   r5   rK   r=   )r   rG   rM   rL   r   r   r   Ú_verify_archivea   s
   ÿûrN   c              
      s(  ddl m‰ dtdttttf ttttdf f f f‡fdd„}dtdtt fdd	„}tdtt fd
d„ƒ}t	d }|du rC|d }|d }t
| ||ƒ |ƒ �:}ttj | |¡|ƒ tj |d¡}||ƒ\‰ }	||ƒ}
‡ fdd„|
D ƒ}t |	|ftj | t¡¡ W d  ƒ dS 1 s�w   Y  dS )aI  Parse the devkit archive of the ImageNet2012 classification dataset and save
    the meta information in a binary file.

    Args:
        root (str or ``pathlib.Path``): Root directory containing the devkit archive
        file (str, optional): Name of devkit archive. Defaults to
            'ILSVRC2012_devkit_t12.tar.gz'
    r   NÚdevkit_rootr   .c                    s    t j | dd¡}ˆj|dd�d ‰ ttˆ Ž ƒd }‡ fdd„t|ƒD ƒ‰ ttˆ Ž ƒd d	… \}}}d
d„ |D ƒ}dd„ t||ƒD ƒ}dd„ t||ƒD ƒ}||fS )NÚdatazmeta.matT)Ú
squeeze_meZsynsetsé   c                    s    g | ]\}}|d krˆ | ‘qS )r   r   )r   r!   Únum_children©Úmetar   r   r   y   r%   z@parse_devkit_archive.<locals>.parse_meta_mat.<locals>.<listcomp>é   c                 S   s   g | ]	}t | d ¡ƒ‘qS )z, )Útupler   )r   r"   r   r   r   r   {   ó    c                 S   ó   i | ]\}}||“qS r   r   )r   r!   r   r   r   r   r$   |   ó    z@parse_devkit_archive.<locals>.parse_meta_mat.<locals>.<dictcomp>c                 S   rY   r   r   )r   r   r"   r   r   r   r$   }   rZ   )r&   r'   r5   ÚloadmatÚlistÚzipr1   )rO   ZmetafileZnums_childrenZidcsr/   r.   Úidx_to_wnidr   )ÚsiorT   r   Úparse_meta_matu   s   z,parse_devkit_archive.<locals>.parse_meta_matc                 S   sN   t j | dd¡}t|ƒ�}| ¡ }W d   ƒ n1 sw   Y  dd„ |D ƒS )NrP   z&ILSVRC2012_validation_ground_truth.txtc                 S   s   g | ]}t |ƒ‘qS r   )Úint)r   Zval_idxr   r   r   r   „   r    zKparse_devkit_archive.<locals>.parse_val_groundtruth_txt.<locals>.<listcomp>)r&   r'   r5   ÚopenÚ	readlines)rO   rG   ZtxtfhÚval_idcsr   r   r   Úparse_val_groundtruth_txt€   s
   

ÿz7parse_devkit_archive.<locals>.parse_val_groundtruth_txtc               	   s   s.   � t  ¡ } z| V  W t | ¡ d S t | ¡ w r<   )ÚtempfileÚmkdtempÚshutilÚrmtree)Útmp_dirr   r   r   Úget_tmp_dir†   s
   €z)parse_devkit_archive.<locals>.get_tmp_dirr   r   ZILSVRC2012_devkit_t12c                    r   r   r   )r   r!   )r^   r   r   r   ›   r    z(parse_devkit_archive.<locals>.<listcomp>)Úscipy.ioÚiorD   r	   r   ra   r   r   r   ÚARCHIVE_METArN   r   r&   r'   r5   rI   Úsaver6   )r   rG   r`   re   rk   Úarchive_metarM   rj   rO   r   rd   Z	val_wnidsr   )r^   r_   r   r7   j   s$   	6"ør7   r   Úfolderc                    sŽ   t d }|du r|d }|d }t| ||ƒ tj | |¡‰ ttj | |¡ˆ ƒ ‡ fdd„t ˆ ¡D ƒ}|D ]}t|tj |¡d dd� q5dS )	aÂ  Parse the train images archive of the ImageNet2012 classification dataset and
    prepare it for usage with the ImageNet dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory containing the train images archive
        file (str, optional): Name of train images archive. Defaults to
            'ILSVRC2012_img_train.tar'
        folder (str, optional): Optional name for train images folder. Defaults to
            'train'
    r   Nr   r   c                    s   g | ]	}t j ˆ |¡‘qS r   ©r&   r'   r5   )r   Úarchive©Z
train_rootr   r   r   µ   rX   z'parse_train_archive.<locals>.<listcomp>T)Úremove_finished)rn   rN   r&   r'   r5   r   ÚlistdirÚsplitext)r   rG   rq   rp   rM   Zarchivesrs   r   rt   r   r9       s   ÿr9   r   r/   c           	         sØ   t d }|du r|d }|d }|du rt| ƒd }t| ||ƒ tj | |¡‰ ttj | |¡ˆ ƒ t‡ fdd„t ˆ ¡D ƒƒ}t	|ƒD ]}t 
tj ˆ |¡¡ qCt||ƒD ]\}}t |tj ˆ |tj |¡¡¡ qUdS )az  Parse the validation images archive of the ImageNet2012 classification dataset
    and prepare it for usage with the ImageNet dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory containing the validation images archive
        file (str, optional): Name of validation images archive. Defaults to
            'ILSVRC2012_img_val.tar'
        wnids (list, optional): List of WordNet IDs of the validation images. If None
            is given, the IDs are loaded from the meta file in the root directory
        folder (str, optional): Optional name for validation images folder. Defaults to
            'val'
    r   Nr   r   c                 3   s   � | ]
}t j ˆ |¡V  qd S r<   rr   )r   Úimage©Zval_rootr   r   Ú	<genexpr>Õ   s   € z$parse_val_archive.<locals>.<genexpr>)rn   r*   rN   r&   r'   r5   r   Úsortedrv   ÚsetÚmkdirr]   rh   ÚmoveÚbasename)	r   rG   r/   rq   rp   rM   Úimagesr   Zimg_filer   ry   r   r:   º   s   "ÿr:   r<   )Nr   )NNr   )r&   rh   rf   Ú
contextlibr   Úpathlibr   Útypingr   r   r   r   r   r	   r
   rI   rq   r   Úutilsr   r   r   rn   r6   r   rD   r*   rN   r7   r9   r:   r   r   r   r   Ú<module>   s>    $ý8<"$	(6ÿ
ÿÿ
ÿÿþ