
    ڧg                         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	e          ZdS )
    N)AnyCallableListOptionalTupleUnion)urlparse)Image   )download_and_extract_archiveverify_str_arg)VisionDatasetc                        e Zd ZdZdZdZ	 	 	 	 ddeeej	        f         ded	e
e         d
e
e         de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 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.zip b11922020e72d0cd9154779b2d3d07d2trainNFrootsplit	transformtarget_transformdownload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t1          | 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     V/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/datasets/clevr.py
<dictcomp>z0CLEVRClassification.__init__.<locals>.<dictcomp>8   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>9   s     ]]]ZK
8]]]r)   )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contentr.   	__class__s
           @r'   r2   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   	E**E.1E.c                 *    t          | j                  S N)r$   r@   rE   s    r'   __len__zCLEVRClassification.__len__=   s    4$%%%r)   idxc                     | j         |         }| j        |         }t          j        |                              d          }| j        r|                     |          }| j        r|                     |          }||fS )NRGB)r@   rD   r
   rA   convertr   r   )rE   rM   r-   labelimages        r'   __getitem__zCLEVRClassification.__getitem__@   s{    &s+
S!
:&&..u55> 	*NN5))E  	1))%00Ee|r)   c                 f    | j                                         o| j                                         S rJ   )r9   existsis_dirrK   s    r'   r;   z!CLEVRClassification._check_existsN   s+     ''))Hd.?.F.F.H.HHr)   c                     |                                  rd S t          | j        t          | j                  | j                   d S )N)md5)r;   r   r6   strr5   _MD5rK   s    r'   r:   zCLEVRClassification._downloadQ   sE     	F$TYD4E0F0FDIVVVVVVr)   c                     d| j          S )Nzsplit=)r0   rK   s    r'   
extra_reprzCLEVRClassification.extra_reprW   s    %%%%r)   )r   NNF)r   N)__name__
__module____qualname____doc__r6   rZ   r   rY   r3   r4   r   r   boolr2   intrL   r   r   rS   r;   r:   r\   __classcell__)rH   s   @r'   r   r      sO         AD-D
 (,/3; ;C%&; ; H%	;
 #8,; ; 
; ; ; ; ; ;<& & & & &s uS#X    It I I I IW W W W&C & & & & & & & &r)   r   )rB   r3   typingr   r   r   r   r   r   urllib.parser	   PILr
   utilsr   r   visionr   r   r+   r)   r'   <module>ri      s      > > > > > > > > > > > > > > > > ! ! ! ! ! !       ? ? ? ? ? ? ? ? ! ! ! ! ! !L& L& L& L& L&- L& L& L& L& L&r)   