o
    ôj„  ã                   @   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)ÚImageé   )Údownload_and_extract_archiveÚverify_str_arg)ÚVisionDatasetc                       s¬   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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 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 | _|r-|  ¡  |  ¡ s5tdƒ‚t| j d| j¡ d¡ƒ| _|  | jdkrƒt| jd	 d
| j› d� ƒ�}t |¡}W d   ƒ n1 shw   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   ZscenesZCLEVR_z_scenes.jsonc                 S   s   i | ]}|d  t |d ƒ“qS )Zimage_filenameÚobjects)Úlen)Ú.0Úscene© r   úk/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/torchvision/datasets/clevr.pyÚ
<dictcomp>8   s    z0CLEVRClassification.__init__.<locals>.<dictcomp>c                    s   g | ]}ˆ |j  ‘qS r   )Úname)r   Ú
image_file©Únum_objectsr   r    Ú
<listcomp>9   s    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__c                 C   s
   t | jƒS ©N)r   r7   ©r<   r   r   r    Ú__len__=   s   
zCLEVRClassification.__len__Úidxc                 C   sL   | j | }| j| }t |¡ d¡}| jr|  |¡}| jr"|  |¡}||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)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   rN   r   rM   r*   r+   r   r   Úboolr)   ÚintrC   r   r   rI   r2   r1   rO   Ú__classcell__r   r   r?   r    r      s4    úþýüûúù
r   )r9   r*   Útypingr   r   r   r   r   r   Úurllib.parser   ÚPILr	   Úutilsr   r   Úvisionr   r   r   r   r   r    Ú<module>   s     