a
    PSic                     @   sd   d dl Z d dlmZ d dl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TupleCallableOptional   )verify_str_argdownload_and_extract_archive)VisionDatasetc                       s   e Zd ZdZdZdZdeeee ee e	dd fdd	Z
ed
ddZ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 (string): 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 an 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 d    n1 s0    Y  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meta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 X/var/www/html/django/DPS/env/lib/python3.9/site-packages/torchvision/datasets/food101.py
<listcomp>A   s   z$Food101.__init__.<locals>.<listcomp>)super__init__r   _splitr   r   _base_folder_meta_folderr   	_download_check_existsRuntimeError_labels_image_filesopenjsonloadsreadsortedkeysclassesdictziprangelen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)r3   r(   r   r   r   r   __len__E   s    zFood101.__len__c                 C   sP   | j | | j|  }}tj|d}| jr8| |}| jrH| |}||fS )NRGB)r(   r'   PILImager)   convertr   r   )r   idx
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 r:   )existsis_dir)r   folderr   r   r   	<genexpr>X       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)download_rootmd5)r%   r	   _URLr   _MD5r   r   r   r   r$   Z   s    zFood101._download)r   NNF)__name__
__module____qualname____doc__rN   rO   strr   r   boolr    intr;   r   r   rD   rE   r%   r$   __classcell__r   r   r8   r   r      s(       "r   )r*   pathlibr   typingr   r   r   r   	PIL.Imager=   utilsr   r	   visionr
   r   r   r   r   r   <module>   s   