U
    ­Ê¹jX  ã                   @   st   d dl Z d dlZd dl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 ddlmZ G dd	„ d	eƒZdS )
é    N)ÚAnyÚCallableÚListÚOptionalÚTuple)Úurlparse)ÚImageé   )Údownload_and_extract_archiveÚverify_str_arg)ÚVisionDatasetc                       s’   e Zd ZdZdZdZdeeee ee e	ddœ‡ fdd	„Z
ed
œdd„Zeeeef dœdd„Ze	d
œdd„Zdd
œdd„Zed
œdd„Z‡  ZS )ÚCLEVRClassificationap  `CLEVR <https://cs.stanford.edu/people/jcjohns/clevr/>`_  classification dataset.

    The number of objects in a scene are used as label.

    Args:
        root (string): Root directory of dataset where directory ``root/clevr`` exists or will be saved to if download is
            set to True.
        split (string, optional): The dataset split, supports ``"train"`` (default), ``"val"``, or ``"test"``.
        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 them 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.
    z3https://dl.fbaipublicfiles.com/clevr/CLEVR_v1.0.zipZ b11922020e72d0cd9154779b2d3d07d2ÚtrainNF)ÚrootÚsplitÚ	transformÚtarget_transformÚdownloadÚreturnc              	      s  t |ddƒ| _tƒ j|||d� t | j¡d | _| jt t| j	ƒj
¡j | _|rZ|  ¡  |  ¡ sjtdƒ‚t| j d| j¡ d¡ƒ| _|  | jdkrðt| jd	 d
| j› d� ƒ�}t |¡}W 5 Q R X dd„ |d	 D ƒ‰ ‡ fdd„| jD ƒ| _nd gt| jƒ | _d S )Nr   )r   ÚvalÚtest)r   r   ZclevrzHDataset not found or corrupted. You can use download=True to download itZimagesÚ*r   ZscenesZCLEVR_z_scenes.jsonc                 S   s   i | ]}|d  t |d ƒ“qS )Zimage_filenameÚobjects)Úlen)Ú.0Zscene© r   úY/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/datasets/clevr.pyÚ
<dictcomp>8   s      z0CLEVRClassification.__init__.<locals>.<dictcomp>c                    s   g | ]}ˆ |j  ‘qS r   )Úname)r   Ú
image_file©Únum_objectsr   r   Ú
<listcomp>9   s     z0CLEVRClassification.__init__.<locals>.<listcomp>)r   Ú_splitÚsuperÚ__init__ÚpathlibÚPathr   Ú_base_folderr   Ú_URLÚpathÚstemÚ_data_folderÚ	_downloadÚ_check_existsÚRuntimeErrorÚsortedÚjoinpathÚglobÚ_image_filesÚopenÚjsonÚloadÚ_labelsr   )Úselfr   r   r   r   r   ÚfileÚcontent©Ú	__class__r    r   r%      s     
zCLEVRClassification.__init__)r   c                 C   s
   t | jƒS ©N)r   r3   ©r8   r   r   r   Ú__len__=   s    zCLEVRClassification.__len__)Úidxr   c                 C   sL   | j | }| j| }t |¡ d¡}| jr4|  |¡}| jrD|  |¡}||fS )NÚRGB)r3   r7   r   r4   Úconvertr   r   )r8   r@   r   ÚlabelÚimager   r   r   Ú__getitem__@   s    



zCLEVRClassification.__getitem__c                 C   s   | j  ¡ o| j  ¡ S r=   )r,   ÚexistsÚis_dirr>   r   r   r   r.   N   s    z!CLEVRClassification._check_existsc                 C   s(   |   ¡ rd S t| jt| jƒ| jd� d S )N)Úmd5)r.   r
   r)   Ústrr(   Ú_MD5r>   r   r   r   r-   Q   s    zCLEVRClassification._downloadc                 C   s   d| j › �S )Nzsplit=)r#   r>   r   r   r   Ú
extra_reprW   s    zCLEVRClassification.extra_repr)r   NNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   rJ   rI   r   r   Úboolr%   Úintr?   r   r   rE   r.   r-   rK   Ú__classcell__r   r   r;   r   r      s(       úùr   )r5   r&   Útypingr   r   r   r   r   Úurllib.parser   ZPILr   Úutilsr
   r   Zvisionr   r   r   r   r   r   Ú<module>   s   