a
    PSicX                     @   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OptionalTupleList)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 d    n1 s0    Y  dd |d	 D   fdd| jD | _nd gt| j | _d S )Nr   )r   valtest)r   r   clevrzHDataset not found or corrupted. You can use download=True to download itimages*r   ZscenesZCLEVR_z_scenes.jsonc                 S   s   i | ]}|d  t |d qS )Zimage_filenameobjects)len).0scene r   V/var/www/html/django/DPS/env/lib/python3.9/site-packages/torchvision/datasets/clevr.py
<dictcomp>8       z0CLEVRClassification.__init__.<locals>.<dictcomp>c                    s   g | ]} |j  qS r   )name)r   
image_filenum_objectsr   r   
<listcomp>9   r!   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   r7   r<   r   r   r   __len__=   s    zCLEVRClassification.__len__)idxr   c                 C   sL   | j | }| j| }t|d}| jr4| |}| jrD| |}||fS )NRGB)r7   r;   r   r8   convertr   r   )r<   rD   r#   labelimager   r   r   __getitem__@   s    



zCLEVRClassification.__getitem__c                 C   s   | j  o| j  S rA   )r0   existsis_dirrB   r   r   r   r2   N   s    z!CLEVRClassification._check_existsc                 C   s(   |   rd S t| jt| j| jd d S )N)md5)r2   r
   r-   strr,   _MD5rB   r   r   r   r1   Q   s    zCLEVRClassification._downloadc                 C   s   d| j  S )Nzsplit=)r'   rB   r   r   r   
extra_reprW   s    zCLEVRClassification.extra_repr)r   NNF)__name__
__module____qualname____doc__r-   rN   rM   r   r   boolr)   intrC   r   r   rI   r2   r1   rO   __classcell__r   r   r?   r   r      s(       r   )r9   r*   typingr   r   r   r   r   urllib.parser   PILr   utilsr
   r   visionr   r   r   r   r   r   <module>   s   