
    Yh                     ~    d dl Z d dlZd dl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	e          ZdS )
    N)AnyCallableOptionalUnion)urlparse   )default_loader)download_and_extract_archiveverify_str_arg)VisionDatasetc                        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ZdefdZdedeeef         fdZdefdZddZdefdZ x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                   	 t          |dd          | _        t                                          |||           || _        t          j        | j                  dz  | _        | j        t          j        t          | j
                  j                  j        z  | _        |r|                                  |                                 st!          d          t#          | j                            d| j                                      d                    | _        |  | j        dk    r{t+          | j        d	z  d
| j         dz            5 }t-          j        |          }d d d            n# 1 swxY w Y   d |d	         D             		fd| j        D             | _        d S d gt3          | j                  z  | _        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                 F    i | ]}|d          t          |d                   S )image_filenameobjects)len).0scenes     l/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torchvision/datasets/clevr.py
<dictcomp>z0CLEVRClassification.__init__.<locals>.<dictcomp>=   s-    iiie5!12Ci8H4I4Iiii    c                 *    g | ]}|j                  S  )name)r$   
image_filenum_objectss     r&   
<listcomp>z0CLEVRClassification.__init__.<locals>.<listcomp>>   s     ]]]ZK
8]]]r(   )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contentr-   	__class__s            @r&   r1   zCLEVRClassification.__init__"   s    %UG5MNNEUVVV#L33g= -Xdi=P=P=U0V0V0[[ 	NN!!## 	kijjj"4#4#=#=h#T#T#Y#YZ]#^#^__);&  d'(25Wdk5W5W5WWXX *\`)D//* * * * * * * * * * * * * * *iiW^_gWhiiiK]]]]4K\]]]DLLL 6C(9$:$::DLLLs   E11E58E5c                 *    t          | j                  S N)r#   r?   rD   s    r&   __len__zCLEVRClassification.__len__B   s    4$%%%r(   idxc                     | j         |         }| j        |         }|                     |          }| j        r|                     |          }| j        r|                     |          }||fS rI   )r?   rC   r   r   r   )rD   rL   r,   labelimages        r&   __getitem__zCLEVRClassification.__getitem__E   so    &s+
S!J''> 	*NN5))E  	1))%00Ee|r(   c                 f    | j                                         o| j                                         S rI   )r8   existsis_dirrJ   s    r&   r:   z!CLEVRClassification._check_existsS   s+     ''))Hd.?.F.F.H.HHr(   c                     |                                  rd S t          | j        t          | j                  | j                   d S )N)md5)r:   r
   r5   strr4   _MD5rJ   s    r&   r9   zCLEVRClassification._downloadV   sE     	F$TYD4E0F0FDIVVVVVVr(   c                     d| j          S )Nzsplit=)r/   rJ   s    r&   
extra_reprzCLEVRClassification.extra_repr\   s    %%%%r(   )r   N)__name__
__module____qualname____doc__r5   rW   r	   r   rV   r2   r3   r   r   boolr   r1   intrK   tuplerP   r:   r9   rY   __classcell__)rG   s   @r&   r   r      sv        $ AD-D
 (,/3<J; ;C%&; ; H%	;
 #8,; ; %W\ 123S89; 
; ; ; ; ; ;@& & & & &s uS#X    It I I I IW W W W&C & & & & & & & &r(   r   )rA   r2   typingr   r   r   r   urllib.parser   folderr	   utilsr
   r   visionr   r   r*   r(   r&   <module>rg      s      1 1 1 1 1 1 1 1 1 1 1 1 ! ! ! ! ! ! " " " " " " ? ? ? ? ? ? ? ? ! ! ! ! ! !Q& Q& Q& Q& Q&- Q& Q& Q& Q& Q&r(   