
    ,}h                     ~    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y)
    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 b11922020e72d0cd9154779b2d3d07d2N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| j                          | j                         st        d      t!        | j                  j#                  d| j                        j%                  d            | _        |  | j                  dk7  rt)        | j                  d	z  d
| j                   dz        5 }t+        j,                  |      }d d d        d	   D ci c]  }|d   t/        |d          }	}| j&                  D 
cg c]  }
|	|
j0                      c}
| _        y d gt/        | j&                        z  | _        y # 1 sw Y   wxY wc c}w c c}
w )Nr   )trainvaltest)r   r   clevrzHDataset not found or corrupted. You can use download=True to download itimages*r   scenesCLEVR_z_scenes.jsonimage_filenameobjects)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lenname_labels)selfr   r   r   r   r   filecontentscenenum_objects
image_file	__class__s              Y/var/www/html/test/engine/venv/lib/python3.12/site-packages/torchvision/datasets/clevr.pyr%   zCLEVRClassification.__init__   s    %UG5MNEUV#LL3g= --Xdii=P=U=U0V0[0[[NN!!#ijj"4#4#4#=#=h#T#Y#YZ]#^_;;& d''(2vdkk],5WWX *\`))D/*W^_gWhie5!12Ci8H4IIiKiKOK\K\]ZK
8]DL 6C(9(9$::DL* *i]s   G*GG"Gc                 ,    t        | j                        S N)r7   r3   r:   s    rA   __len__zCLEVRClassification.__len__=   s    4$$%%    idxc                    | j                   |   }| j                  |   }t        j                  |      j	                  d      }| j
                  r| j                  |      }| j                  r| j                  |      }||fS )NRGB)r3   r9   r
   r4   convertr   r   )r:   rG   r?   labelimages        rA   __getitem__zCLEVRClassification.__getitem__@   sq    &&s+
S!

:&..u5>>NN5)E  ))%0Ee|rF   c                 n    | j                   j                         xr | j                   j                         S rC   )r,   existsis_dirrD   s    rA   r.   z!CLEVRClassification._check_existsN   s+      '')Hd.?.?.F.F.HHrF   c                     | j                         ry t        | j                  t        | j                        | j
                         y )N)md5)r.   r   r)   strr(   _MD5rD   s    rA   r-   zCLEVRClassification._downloadQ   s0    $TYYD4E4E0FDIIVrF   c                      d| j                    S )Nzsplit=)r#   rD   s    rA   
extra_reprzCLEVRClassification.extra_reprW   s    }%%rF   )r   NNF)r   N)__name__
__module____qualname____doc__r)   rT   r   rS   r&   r'   r   r   boolr%   intrE   r   r   rM   r.   r-   rV   __classcell__)r@   s   @rA   r   r      s     AD-D
 (,/3;C%&; ; H%	;
 #8,; ; 
;<& &s uS#X It IW&C &rF   r   )r5   r&   typingr   r   r   r   r   r   urllib.parser	   PILr
   utilsr   r   visionr   r    rF   rA   <module>rd      s+      > > !  ? !L&- L&rF   