U
    (d                     @   s&  d dl Z d dlZ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d	d
ZdZG dd deZdee
e eeeef ee f dddZeeeddddZdee
e ddddZd ee
e eddddZd!ee
e e
ee  eddddZdS )"    N)contextmanager)AnyDictListIteratorOptionalTuple   )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                       s\   e Zd ZdZdeeedd fddZdddd	Zeedd
dZ	edddZ
  ZS )ImageNeta  `ImageNet <http://image-net.org/>`_ 2012 Classification Dataset.

    Args:
        root (string): 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 an 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   N)rootsplitkwargsreturnc                    s   t j| }| _t|dd| _|   t| jd  t j	| j
f| || _| j| _| j| _ fdd| jD | _dd t| jD | _d S )Nr   )r   r   r   c                    s   g | ]} | qS  r   ).0wnidwnid_to_classesr   A/tmp/pip-unpacked-wheel-vx7f76es/torchvision/datasets/imagenet.py
<listcomp>6   s     z%ImageNet.__init__.<locals>.<listcomp>c                 S   s    i | ]\}}|D ]
}||qqS r   r   )r   idxclssclsr   r   r   
<dictcomp>7   s
        z%ImageNet.__init__.<locals>.<dictcomp>)ospath
expanduserr   r   r   parse_archivesload_meta_filesuper__init__split_folderclasseswnidsZclass_to_idxZwnid_to_idx	enumerate)selfr   r   r   	__class__r   r   r'   *   s    zImageNet.__init__r   c                 C   sZ   t tj| jtst| j tj| jsV| j	dkrBt
| j n| j	dkrVt| j d S )Nr   r   )r   r!   r"   joinr   	META_FILEparse_devkit_archiveisdirr(   r   parse_train_archiveparse_val_archiver,   r   r   r   r$   9   s    


zImageNet.parse_archivesc                 C   s   t j| j| jS N)r!   r"   r0   r   r   r6   r   r   r   r(   C   s    zImageNet.split_folderc                 C   s   dj f | jS )NzSplit: {split})format__dict__r6   r   r   r   
extra_reprG   s    zImageNet.extra_repr)r   )__name__
__module____qualname____doc__strr   r'   r$   propertyr(   r:   __classcell__r   r   r-   r   r      s   
r   )r   filer   c                 C   sD   |d krt }tj| |}t|r,t|S d}t||| d S )NzThe meta file {} is not present in the root directory or is corrupted. This file is automatically created by the ImageNet dataset.)	r1   r!   r"   r0   r   torchloadRuntimeErrorr8   )r   rB   msgr   r   r   r%   K   s    
r%   )r   rB   md5r   c                 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"   r0   rE   r8   )r   rB   rG   rF   r   r   r   _verify_archiveZ   s    rH   c              	      s  ddl m tttttf ttttdf f f dfdd}ttt ddd}ttt d	d
d}t	d }|dkr|d }|d }t
| || | h}ttj| || tj|d}||\ }	||}
 fdd|
D }t|	|ftj| t W 5 Q R X dS )a5  Parse the devkit archive of the ImageNet2012 classification dataset and save
    the meta information in a binary file.

    Args:
        root (str): 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)Z
squeeze_meZsynsets   c                    s    g | ]\}}|d kr | qS )r   r   )r   r   Znum_childrenmetar   r   r   r   s      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   t   s     c                 S   s   i | ]\}}||qS r   r   )r   r   r   r   r   r   r    u   s      z@parse_devkit_archive.<locals>.parse_meta_mat.<locals>.<dictcomp>c                 S   s   i | ]\}}||qS r   r   )r   r   r   r   r   r   r    v   s      )r!   r"   r0   Zloadmatlistzipr+   )rI   ZmetafileZnums_childrenZidcsr*   r)   idx_to_wnidr   )siorL   r   parse_meta_matn   s    z,parse_devkit_archive.<locals>.parse_meta_matc              	   S   s:   t j| dd}t|}| }W 5 Q R X dd |D S )NrJ   z&ILSVRC2012_validation_ground_truth.txtc                 S   s   g | ]}t |qS r   )int)r   Zval_idxr   r   r   r   }   s     zKparse_devkit_archive.<locals>.parse_val_groundtruth_txt.<locals>.<listcomp>)r!   r"   r0   open	readlines)rI   rB   Ztxtfhval_idcsr   r   r   parse_val_groundtruth_txty   s    
z7parse_devkit_archive.<locals>.parse_val_groundtruth_txtr/   c               	   s   s$   t  } z
| V  W 5 t|  X d S r7   )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                    s   g | ]} | qS r   r   )r   r   )rR   r   r   r      s     z(parse_devkit_archive.<locals>.<listcomp>)Zscipy.ioior?   r   r   rU   r   r   r   ARCHIVE_METArH   r   r!   r"   r0   rC   saver1   )r   rB   rT   rY   r_   archive_metarG   r^   rI   r   rX   Z	val_wnidsr   )rR   rS   r   r2   c   s"    	4r2   r   )r   rB   folderr   c                    s   t d }|dkr|d }|d }t| || tj| | ttj| |   fddt D }|D ]}t|tj|d dd qjdS )	a  Parse the train images archive of the ImageNet2012 classification dataset and
    prepare it for usage with the ImageNet dataset.

    Args:
        root (str): 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"   r0   )r   archiveZ
train_rootr   r   r      s     z'parse_train_archive.<locals>.<listcomp>T)Zremove_finished)ra   rH   r!   r"   r0   r   listdirsplitext)r   rB   rd   rc   rG   Zarchivesrf   r   rg   r   r4      s    r4   r   )r   rB   r*   rd   r   c           	         s   t d }|dkr|d }|d }|dkr4t| d }t| || tj| | ttj| |  t fddt D }t	|D ]}t
tj | qt||D ](\}}t|tj |tj| qdS )af  Parse the validation images archive of the ImageNet2012 classification dataset
    and prepare it for usage with the ImageNet dataset.

    Args:
        root (str): 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 r7   re   )r   imageZval_rootr   r   	<genexpr>   s     z$parse_val_archive.<locals>.<genexpr>)ra   r%   rH   r!   r"   r0   r   sortedrh   setmkdirrQ   r\   movebasename)	r   rB   r*   rd   rc   rG   Zimagesr   Zimg_filer   rk   r   r5      s    r5   )N)N)Nr   )NNr   )r!   r\   rZ   
contextlibr   typingr   r   r   r   r   r   rC   rd   r
   utilsr   r   r   ra   r1   r   r?   r%   rH   r2   r4   r5   r   r   r   r   <module>   s6    6,	6       
 