U
    <jg[                     @   s   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m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)Path)AnyCallableDictListOptionalTupleUnion)urljoin   )default_loader)check_integritydownload_and_extract_archiveverify_str_arg)VisionDatasetc                
       sx  e Zd ZdZdZdZdddZdZdd	d
dZdddddddZ	ddddde
feeef eeeee ee eegef dd fddZeeeef dddZedddZeedddZeedd d!Zd4eeee eeef f d#d$d%Zd5eeeeeef  ee f d#d&d'Zddd(d)Zddd*d+Zedd,d-Z eed.d/d0Z!eeeed1d2d3Z"  Z#S )6	Places365a  `Places365 <http://places2.csail.mit.edu/index.html>`_ classification dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of the Places365 dataset.
        split (string, optional): The dataset split. Can be one of ``train-standard`` (default), ``train-challenge``,
            ``val``.
        small (bool, optional): If ``True``, uses the small images, i.e. resized to 256 x 256 pixels, instead of the
            high resolution ones.
        download (bool, optional): If ``True``, downloads the dataset components and places them in ``root``. Already
            downloaded archives are not downloaded again.
        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 names.
        class_to_idx (dict): Dict with items (class_name, class_index).
        imgs (list): List of (image path, class_index) tuples
        targets (list): The class_index value for each image in the dataset

    Raises:
        RuntimeError: If ``download is False`` and the meta files, i.e. the devkit, are not present or corrupted.
        RuntimeError: If ``download is True`` and the image archive is already extracted.
    )train-standardtrain-challengevalz+http://data.csail.mit.edu/places/places365/)zfilelist_places365-standard.tarZ 35a0585fee1fa656440f3ab298f8479c)z filelist_places365-challenge.tarZ 70a8307e459c3de41690a7c76c931734)standard	challenge)zcategories_places365.txtZ 06c963b85866bd0649f97cb43dd16673)zplaces365_train_standard.txtZ 30f37515461640559006b8329efbed1a)zplaces365_train_challenge.txtZ b2931dc997b8c33c27e7329c073a6b57)zplaces365_val.txtZ e9f2fd57bfd9d07630173f4e8708e4b1)z!train_large_places365standard.tarZ 67e186b496a84c929568076ed01a8aa1)z"train_large_places365challenge.tarZ 605f18e68e510c82b958664ea134545f)zval_large.tarZ 9b71c4993ad89d2d8bcbdc4aef38042f)ztrain_256_places365standard.tarZ 53ca1c756c3d1e7809517cc47c5561c5)z train_256_places365challenge.tarZ 741915038a5e3471ec7332404dfb64ef)zval_256.tarZ e27b17d8d44f4af9a78502beb927f808))r   F)r   F)r   F)r   T)r   T)r   Tr   FN)rootsplitsmalldownload	transformtarget_transformloaderreturnc                    s^   t  j|||d | || _|| _|| _| |\| _| _| 	|\| _
| _|rZ|   d S )N)r   r   )super__init___verify_splitr   r   r   load_categoriesclassesclass_to_idxload_file_listimgstargetsdownload_images)selfr   r   r   r   r   r   r   	__class__ B/tmp/pip-unpacked-wheel-xh8d94dr/torchvision/datasets/places365.pyr!   @   s    
zPlaces365.__init__)indexr   c                 C   s:   | j | \}}| |}| jd k	r2| ||\}}||fS N)r'   r   Z
transforms)r*   r/   filetargetimager-   r-   r.   __getitem__V   s
    

zPlaces365.__getitem__)r   c                 C   s
   t | jS r0   )lenr'   r*   r-   r-   r.   __len___   s    zPlaces365.__len__c                 C   s   d| j krdS dS )Nr   r   )r   r6   r-   r-   r.   variantb   s    zPlaces365.variantc                 C   sL   | j r
dnd}| jdr.d| d| j }n| j d| }t| j|S )NZ256ZlargetrainZdata__)r   r   
startswithr8   r   joinr   )r*   sizedirr-   r-   r.   
images_dirf   s
    zPlaces365.images_dirT)r   r   c              	      s   t tt tf ddd | j\}}t| j|}| |||sF|   t	|}t
 fdd|D }W 5 Q R X t| |fS )Nliner   c                 S   s   |   \}}|t|fS r0   )r   int)rA   clsidxr-   r-   r.   processp   s    z*Places365.load_categories.<locals>.processc                 3   s   | ]} |V  qd S r0   r-   .0rA   rE   r-   r.   	<genexpr>z   s     z,Places365.load_categories.<locals>.<genexpr>)strr	   rB   _CATEGORIES_METAr   r<   r   _check_integritydownload_devkitopendictsortedkeys)r*   r   r1   md5fhr%   r-   rH   r.   r#   o   s    

 zPlaces365.load_categoriesc              	      s   dt tt tf dfdd jj \}}tj|}|||sR	  t
|} fdd|D }W 5 Q R X t| \}}|t|fS )N/r@   c                    s2   |   \}}t j|||tjt|fS r0   )	r   r   r<   r?   lstripreplaceosseprB   )rA   rX   r3   rD   r6   r-   r.   rE      s    z)Places365.load_file_list.<locals>.processc                    s   g | ]} |qS r-   r-   rF   rH   r-   r.   
<listcomp>   s     z,Places365.load_file_list.<locals>.<listcomp>)rT   )rJ   r	   rB   _FILE_LIST_METAr   r   r<   r   rL   rM   rN   ziplist)r*   r   r1   rR   rS   Zimagesr:   r(   r-   )rE   r*   r.   r&   ~   s    
zPlaces365.load_file_listc                 C   s,   | j | j \}}tt| j|| j|d d S )NrR   )_DEVKIT_METAr8   r   r   	_BASE_URLr   r*   r1   rR   r-   r-   r.   rM      s    zPlaces365.download_devkitc                 C   sx   t | jrtd| j d| j| j| jf \}}tt| j	|| j
|d | jdrtt| jddd | j d S )NzThe directory z[ already exists. If you want to re-download or re-extract the images, delete the directory.r]   r9   r:   r   r   )r   existsr?   RuntimeError_IMAGES_METAr   r   r   r   r_   r   r;   rW   renamersplitr`   r-   r-   r.   r)      s    zPlaces365.download_imagesc                 C   s   d djf | jS )N
)zSplit: {split}zSmall: {small})r<   format__dict__r6   r-   r-   r.   
extra_repr   s    zPlaces365.extra_repr)r   r   c                 C   s   t |d| jS )Nr   )r   _SPLITS)r*   r   r-   r-   r.   r"      s    zPlaces365._verify_split)r1   rR   r   r   c                 C   s(   t ||d}|s$|s$td| d|S )Nr]   z	The file zJ does not exist or is corrupted. You can set download=True to download it.)r   rb   )r*   r1   rR   r   Z	integrityr-   r-   r.   rL      s    
zPlaces365._check_integrity)T)T)$__name__
__module____qualname____doc__rj   r_   r^   rK   rZ   rc   r   r
   rJ   r   boolr   r   r   r!   rB   r	   r4   r7   propertyr8   r?   r   r   r#   r&   rM   r)   ri   r"   rL   __classcell__r-   r-   r+   r.   r      s^   
	&*r   )rW   r   pathlibr   typingr   r   r   r   r   r	   r
   urllib.parser   folderr   utilsr   r   r   Zvisionr   r   r-   r-   r-   r.   <module>   s   $