a
    hD                     @   sd   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
mZ ddlmZ G dd deZdS )	    N)AnyCallableOptional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eee	j
f eeee ee eeeee	j
f gef dd fd	d
ZedddZeeeef dddZedddZedddZd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
  _|r     stdg  _g }t j j  j d L}	|	D ]6}
|
 d\}} j j|| || qW d    n1 s,0    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   Z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   F/var/www/auris/lib/python3.9/site-packages/torchvision/datasets/dtd.py
<listcomp>N       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)   &   s8    
 ,zDTD.__init__)r   c                 C   s
   t | jS N)r?   r4   r   r   r   r    __len__Q   s    zDTD.__len__)idxr   c                 C   sH   | j | | j|  }}| |}| jr0| |}| jr@| |}||fS rF   )r4   r@   r   r   r   )r   rH   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 rF   )ospathexistsr0   isdirr   r   r   r    r2   c   s    zDTD._check_existsc                 C   s(   |   rd S t| jt| j| jd d S )N)Zdownload_rootmd5)r2   r   _URLstrr/   _MD5r   r   r   r    r1   f   s    zDTD._download)r-   
__module____qualname____doc__rR   rT   r   r   rS   r*   r+   r%   r   r   boolr   r)   rG   tuplerK   rL   r2   r1   __classcell__r   r   rD   r    r      s0   +r   )rM   r*   typingr   r   r   r   folderr   utilsr   r	   Zvisionr
   r   r   r   r   r    <module>   s   