o
    Vh                     @   sx   d dl Z d dlZd dl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 ddlmZ G dd	 d	eZdS )
    N)AnyCallableListOptionalTupleUnion)urlparse   )default_loader)download_and_extract_archiveverify_str_arg)VisionDatasetc                       s   e Zd ZdZdZdZddddefdeee	j
f ded	ee d
ee dedeeee	j
f gef ddf fddZdefddZdedeeef fddZdefddZdddZdefddZ  ZS )CLEVRClassificationa  `CLEVR <https://cs.stanford.edu/people/jcjohns/clevr/>`_  classification dataset.

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

    Args:
        root (str or ``pathlib.Path``): 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 a PIL image or torch.Tensor, depends on the given loader,
            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.
        loader (callable, optional): A function to load an image given its path.
            By default, it uses PIL as its image loader, but users could also pass in
            ``torchvision.io.decode_image`` for decoding image data into tensors directly.
    z3https://dl.fbaipublicfiles.com/clevr/CLEVR_v1.0.zip b11922020e72d0cd9154779b2d3d07d2trainNFrootsplit	transformtarget_transformdownloadloaderreturnc           	         s"  t |dd| _t j|||d || _t| jd | _| jtt	| j
jj | _|r0|   |  s8tdt| jd| jd| _|  | jdkrt| jd	 d
| j d }t|}W d    n1 skw   Y  dd |d	 D   fdd| jD | _d S 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   scenesCLEVR_z_scenes.jsonc                 S   s   i | ]}|d  t |d qS )image_filenameobjects)len).0scene r$   N/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/datasets/clevr.py
<dictcomp>=   s    z0CLEVRClassification.__init__.<locals>.<dictcomp>c                    s   g | ]} |j  qS r$   )name)r"   
image_filenum_objectsr$   r%   
<listcomp>>   s    z0CLEVRClassification.__init__.<locals>.<listcomp>)r   _splitsuper__init__r   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   r   filecontent	__class__r)   r%   r.   "   s$   	
zCLEVRClassification.__init__c                 C   s
   t | jS N)r!   r<   rA   r$   r$   r%   __len__B   s   
zCLEVRClassification.__len__idxc                 C   sF   | j | }| j| }| |}| jr| |}| jr| |}||fS rF   )r<   r@   r   r   r   )rA   rI   r(   labelimager$   r$   r%   __getitem__E   s   




zCLEVRClassification.__getitem__c                 C   s   | j  o	| j  S rF   )r5   existsis_dirrG   r$   r$   r%   r7   S   s   z!CLEVRClassification._check_existsc                 C   s(   |   rd S t| jt| j| jd d S )N)md5)r7   r   r2   strr1   _MD5rG   r$   r$   r%   r6   V   s   zCLEVRClassification._downloadc                 C   s   d| j  S )Nzsplit=)r,   rG   r$   r$   r%   
extra_repr\   s   zCLEVRClassification.extra_repr)r   N)__name__
__module____qualname____doc__r2   rQ   r
   r   rP   r/   r0   r   r   boolr   r.   intrH   r   rL   r7   r6   rR   __classcell__r$   r$   rD   r%   r      s:     
r   )r>   r/   typingr   r   r   r   r   r   urllib.parser   folderr
   utilsr   r   visionr   r   r$   r$   r$   r%   <module>   s     