U
    h                     @   sh   d dl Z d dlmZ d dlmZmZmZmZmZ d dl	Z
ddlmZmZ ddlmZ G dd deZdS )	    N)Path)AnyCallableOptionalTupleUnion   )download_and_extract_archiveverify_str_arg)VisionDatasetc                       s   e Zd ZdZdZdZdeeef ee	e
 e	e
 e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 )Food101a  `The Food-101 Data Set <https://data.vision.ee.ethz.ch/cvl/datasets_extra/food-101/>`_.

    The Food-101 is a challenging data set of 101 food categories with 101,000 images.
    For each class, 250 manually reviewed test images are provided as well as 750 training images.
    On purpose, the training images were not cleaned, and thus still contain some amount of noise.
    This comes mostly in the form of intense colors and sometimes wrong labels. All images were
    rescaled to have a maximum side length of 512 pixels.


    Args:
        root (str or ``pathlib.Path``): Root directory of the dataset.
        split (string, optional): The dataset split, supports ``"train"`` (default) and ``"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 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.
    z1http://data.vision.ee.ethz.ch/cvl/food-101.tar.gzZ 85eeb15f3717b99a5da872d97d918f87trainNF)rootsplit	transformtarget_transformdownloadreturnc           
   	      s  t  j|||d t|dd _t jd  _ jd  _ jd  _|rT 	   
 sdtdg  _g  _t j| d }t| }W 5 Q R X t|  _tt jtt j _| D ]B\}}	  j j| gt|	 7  _  j fd	d
|	D 7  _qd S )N)r   r   r   )r   testzfood-101metaZimagesz;Dataset not found. You can use download=True to download itz.jsonc                    s$   g | ]} j j| d d qS )z.jpg/)_images_folderjoinpathr   ).0Zim_rel_pathself N/var/www/html/venv/lib/python3.8/site-packages/torchvision/datasets/food101.py
<listcomp>A   s    z$Food101.__init__.<locals>.<listcomp>)super__init__r
   _splitr   r   Z_base_folder_meta_folderr   	_download_check_existsRuntimeError_labels_image_filesopenjsonloadsreadsortedkeysclassesdictziprangelenZclass_to_idxitems)
r   r   r   r   r   r   fmetadataZclass_labelZim_rel_paths	__class__r   r   r    #   s(    zFood101.__init__)r   c                 C   s
   t | jS N)r2   r'   r   r   r   r   __len__E   s    zFood101.__len__)idxr   c                 C   sP   | j | | j|  }}tj|d}| jr8| |}| jrH| |}||fS )NRGB)r'   r&   PILZImager(   convertr   r   )r   r:   Z
image_filelabelimager   r   r   __getitem__H   s    

zFood101.__getitem__c                 C   s   d| j  S )Nzsplit=)r!   r   r   r   r   
extra_reprT   s    zFood101.extra_reprc                 C   s   t dd | j| jfD S )Nc                 s   s   | ]}|  o| V  qd S r8   )existsis_dir)r   folderr   r   r   	<genexpr>X   s     z(Food101._check_exists.<locals>.<genexpr>)allr"   r   r   r   r   r   r$   W   s    zFood101._check_existsc                 C   s$   |   rd S t| j| j| jd d S )N)Zdownload_rootmd5)r$   r	   _URLr   _MD5r   r   r   r   r#   Z   s    zFood101._download)r   NNF)__name__
__module____qualname____doc__rH   rI   r   strr   r   r   boolr    intr9   r   r   r@   rA   r$   r#   __classcell__r   r   r6   r   r      s(       
"r   )r)   pathlibr   typingr   r   r   r   r   Z	PIL.Imager<   utilsr	   r
   Zvisionr   r   r   r   r   r   <module>   s   