o
    ;)iK                     @   sh   d dl Z d dlZd dlmZmZmZmZ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OptionalTupleUnion   )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defdeee	j
f d	e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efddZdddZ  ZS )DTDa}  `Describable Textures Dataset (DTD) <https://www.robots.ox.ac.uk/~vgg/data/dtd/>`_.

    Args:
        root (str or ``pathlib.Path``): Root directory of the dataset.
        split (string, optional): The dataset split, supports ``"train"`` (default), ``"val"``, or ``"test"``.
        partition (int, optional): The dataset partition. Should be ``1 <= partition <= 10``. Defaults to ``1``.

            .. note::

                The partition only changes which split each image belongs to. Thus, regardless of the selected
                partition, combining all splits will result in all images.

        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 the 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. Default is False.
        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.
    zDhttps://www.robots.ox.ac.uk/~vgg/data/dtd/download/dtd-r1.0.1.tar.gzZ fff73e5086ae6bdbea199a49dfb8a4c1trainr   NFrootsplit	partition	transformtarget_transformdownloadloaderreturnc                    s|  t |dd _t|tsd|  krdksn td| d| _t j|||d t	 j
t j   _ jd  _ jd	  _ jd
  _|rQ     sYtdg  _g }t j j  j d &}	|	D ]}
|
 d\}} j j|| || qoW d    n1 sw   Y  tt| _tt jt t! j _" fdd|D  _#| _$d S )Nr   )r   valtestr   
   zPParameter 'partition' should be an integer with `1 <= partition <= 10`, but got z instead)r   r   dtdlabelsZimagesz;Dataset not found. You can use download=True to download itz.txt/c                    s   g | ]} j | qS  )class_to_idx).0clsselfr   S/home/app/PyTorch/.pytorch/lib/python3.10/site-packages/torchvision/datasets/dtd.py
<listcomp>N   s    z DTD.__init__.<locals>.<listcomp>)%r
   _split
isinstanceint
ValueError
_partitionsuper__init__pathlibPathr   type__name__lower_base_folder_data_folderZ_meta_folderZ_images_folder	_download_check_existsRuntimeError_image_filesopenstripr   appendjoinpathsortedsetclassesdictziprangelenr   _labelsr   )r!   r   r   r   r   r   r   r   r<   fileliner   name	__class__r    r"   r*   &   s<   
 
zDTD.__init__c                 C   s
   t | jS N)r@   r5   r    r   r   r"   __len__Q   s   
zDTD.__len__idxc                 C   sH   | j | | j| }}| |}| jr| |}| jr | |}||fS rG   )r5   rA   r   r   r   )r!   rI   Z
image_filelabelimager   r   r"   __getitem__T   s   


zDTD.__getitem__c                 C   s   d| j  d| j S )Nzsplit=z, partition=)r$   r(   r    r   r   r"   
extra_repr`   s   zDTD.extra_reprc                 C   s   t j| jot j| jS rG   )ospathexistsr1   isdirr    r   r   r"   r3   c   s   zDTD._check_existsc                 C   s(   |   rd S t| jt| j| jd d S )N)Zdownload_rootmd5)r3   r	   _URLstrr0   _MD5r    r   r   r"   r2   f   s   zDTD._download)r   N)r.   
__module____qualname____doc__rS   rU   r   r   rT   r+   r,   r&   r   r   boolr   r*   rH   r   rL   rM   r3   r2   __classcell__r   r   rE   r"   r      s@    	+r   )rN   r+   typingr   r   r   r   r   folderr   utilsr	   r
   Zvisionr   r   r   r   r   r"   <module>   s    