
    Phl(                         d dl Z d dlZ d dlmZmZ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Zdd	d
ddddZdddddddZ G d de      Zy)    N)AnyCallableDictListOptionalTupleUnion)Image   )download_and_extract_archiveverify_str_arg)VisionDataset)kingdomphylumclassorderfamilygenuszRhttps://ml-inat-competition-datasets.s3.amazonaws.com/2017/train_val_images.tar.gzzOhttps://ml-inat-competition-datasets.s3.amazonaws.com/2018/train_val2018.tar.gzzOhttps://ml-inat-competition-datasets.s3.amazonaws.com/2019/train_val2019.tar.gzzGhttps://ml-inat-competition-datasets.s3.amazonaws.com/2021/train.tar.gzzLhttps://ml-inat-competition-datasets.s3.amazonaws.com/2021/train_mini.tar.gzzEhttps://ml-inat-competition-datasets.s3.amazonaws.com/2021/val.tar.gz)201720182019
2021_train2021_train_mini
2021_valid 7c784ea5e424efaec655bd392f87301f b1c6952ce38f31868cc50ea72d066cc3 c60a6e2962c9b8ccbd458d12c8582644 e0526d53c7f7b2e3167b2b43bb2690ed db6ed8330e634445efc8fec83ae81442 f6f6e0e242e3d4c9569ba56400938afcc                        e Zd ZdZ	 	 	 	 	 ddededeee   ef   dee   dee   de	d	df fd
Z
ddZddZded	eeef   fdZd	efdZdeded	efdZd	e	fdZddZ xZS )INaturalistaF  `iNaturalist <https://github.com/visipedia/inat_comp>`_ Dataset.

    Args:
        root (string): Root directory of dataset where the image files are stored.
            This class does not require/use annotation files.
        version (string, optional): Which version of the dataset to download/use. One of
            '2017', '2018', '2019', '2021_train', '2021_train_mini', '2021_valid'.
            Default: `2021_train`.
        target_type (string or list, optional): Type of target to use, for 2021 versions, one of:

            - ``full``: the full category (species)
            - ``kingdom``: e.g. "Animalia"
            - ``phylum``: e.g. "Arthropoda"
            - ``class``: e.g. "Insecta"
            - ``order``: e.g. "Coleoptera"
            - ``family``: e.g. "Cleridae"
            - ``genus``: e.g. "Trichodes"

            for 2017-2019 versions, one of:

            - ``full``: the full (numeric) category
            - ``super``: the super category, e.g. "Amphibians"

            Can also be a list to output a tuple with all specified target types.
            Defaults to ``full``.
        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.
    Nrootversiontarget_type	transformtarget_transformdownloadreturnc           	         t        |dt        j                               | _        t        |   t        j                  j                  ||      ||       t        j                  |d       |r| j                          | j                         st        d      g | _        i | _        g | _        t!        |t"              s|g}| j                  d d dk(  r9|D cg c]  }t        |dd	gt$               c}| _        | j)                          n0|D cg c]  }t        |dd
       c}| _        | j+                          g | _        t/        | j                        D ]f  \  }}	t        j0                  t        j                  j                  | j2                  |	            }
|
D ]  }| j,                  j5                  ||f       ! h y c c}w c c}w )Nr$   )r&   r'   T)exist_okzHDataset not found or corrupted. You can use download=True to download it   2021r%   full)r.   super)r   DATASET_URLSkeysr$   r/   __init__ospathjoinmakedirsr(   _check_integrityRuntimeErrorall_categoriescategories_indexcategories_map
isinstancelistCATEGORIES_2021r%   
_init_2021_init_pre2021index	enumeratelistdirr#   append)selfr#   r$   r%   r&   r'   r(   t	dir_indexdir_namefilesfname	__class__s               kC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torchvision/datasets/inaturalist.pyr2   zINaturalist.__init__B   s    &gy,:K:K:MNdG4	\lm
D4(MMO$$&ijj)+ <> 57+t,&-K<<v%fqrfqabq-&A[?A[ \fqrDOO]hi]hXYq-AR S]hiD  -/
#,T-@-@#AIxJJrww||DIIx@AE

!!9e"45  $B  s  js   GGc                    t        t        j                  | j                              | _        t
        D ci c]  }|i  c}| _        t        | j                        D ]  \  }}|j                  d      }t        |      dk7  rt        d| d      |d   |dk7  rt        d|d    d|d      i }t        t
        |d	d
       D ]X  \  }}|| j                  |   v r| j                  |   |   }n*t        | j                  |         }|| j                  |   |<   |||<   Z | j                  j                  |        yc c}w )zInitialize based on 2021 layout_   zUnexpected category name z, wrong number of piecesr   05dzUnexpected category id z, expecting r      N)sortedr3   rC   r#   r9   r>   r:   rB   splitlenr8   zipr;   rD   )	rE   krG   rH   piecescat_mapcatnamecat_ids	            rL   r?   zINaturalist._init_2021o   sP    %RZZ		%:; 1@ @1B @#,T-@-@#AIx^^C(F6{a"%>xjH`#abbayyo."%<VAYK|T]^aSb#cddG &1+>	T40055!2237=F !6!6s!;<F7=D))#.t4% ? &&w/ $B !As   
D>c           	         di i| _         d}t        t        j                  | j                              }t        |      D ]Z  \  }}|| j                   d   |<   t        t        j                  t        j                  j                  | j                  |                  }|D ]  }| j                  dk(  r|}|dz  }n	 t        |      }|t        | j                        k\  r_t        | j                        }| j                  j                  i g||z
  dz   z         | j                  j                  dg||z
  dz   z         | j                  |   rt        d|       d|i| j                  |<   t        j                  j                  ||      | j                  |<    ] t        | j                        D ]  \  }	}
|
r	t        d|	        y	# t        $ r t        d|       w xY w)
z$Initialize based on 2017-2019 layoutr/   r   r   r   z!Unexpected non-numeric dir name:  zDuplicate category zMissing category N)r:   rR   r3   rC   r#   rB   r4   r5   r$   int
ValueErrorr8   rT   r;   extendr9   )rE   	cat_indexsuper_categoriessindexscatsubcategoriessubcatsubcat_iold_lencindexcs              rL   r@   zINaturalist._init_pre2021   s    ")"	!"**TYY"78%&67LFD39D!!'*40"2::bggll499d.K#LMM'<<6)(HNIY#&v; s4#6#677!$"5"56G''..tx'7IA7M/NO''..tx'7IA7M/NO&&x0&)<VH'EFF18&0A##H-02T60J##H-# ( 8. #4#6#67IFA"%6vh#?@@ 8 & Y*-Nvh+WXXYs   ;GG#rA   c                 
   | j                   |   \  }}t        j                  t        j                  j                  | j                  | j                  |   |            }g }| j                  D ]:  }|dk(  r|j                  |       |j                  | j                  |   |          < t        |      dkD  rt        |      n|d   }| j                  | j                  |      }| j                  | j                  |      }||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where the type of target specified by target_type.
        r.   r   r   )rA   r
   openr3   r4   r5   r#   r9   r%   rD   r;   rT   tupler&   r'   )rE   rA   r[   rJ   imgtargetrF   s          rL   __getitem__zINaturalist.__getitem__   s     

5)jjdii1D1DV1LeTU!!AF{f%d11&9!<=	 "
 #&f+/vvay>>%..%C  ,**62FF{    c                 ,    t        | j                        S )N)rT   rA   rE   s    rL   __len__zINaturalist.__len__   s    4::rq   category_typecategory_idc                     |dk(  r| j                   |   S || j                  vrt        d| d      | j                  |   j                         D ]  \  }}||k(  s|c S  t        d| d|       )a  
        Args:
            category_type(str): one of "full", "kingdom", "phylum", "class", "order", "family", "genus" or "super"
            category_id(int): an index (class id) from this category

        Returns:
            the name of the category
        r.   zInvalid category type ''zInvalid category id z for )r9   r:   r_   items)rE   ru   rv   rZ   ids        rL   category_namezINaturalist.category_name   s     F"&&{33D$9$99 #:=/!KLL $ 5 5m D J J LHD"[(# !M !#7}E-!YZZrq   c                     t         j                  j                  | j                        xr+ t	        t        j
                  | j                              dkD  S )Nr   )r3   r4   existsr#   rT   rC   rs   s    rL   r7   zINaturalist._check_integrity   s5    ww~~dii(KSDII1F-G!-KKrq   c                    | j                         rt        d| j                   d      t        j                  j                  | j                        }t        t        | j                     || j                   dt        | j                            t        j                  j                  |t        j                  j                  t        | j                           j                  d            }t        j                  j                  |      st        d|       t        j                  || j                         t        d| j                   d       y )	NzThe directory z[ already exists. If you want to re-download or re-extract the images, delete the directory.z.tgz)filenamemd5z.tar.gzz#Unable to find downloaded files at zDataset version 'z*' has been downloaded and prepared for use)r7   r8   r#   r3   r4   dirnamer   r0   r$   DATASET_MD5r5   basenamerstripr}   renameprint)rE   	base_rootorig_dir_names      rL   r(   zINaturalist.download   s     "  ,] ^ 
 GGOODII.	$&	t||nD<QWbcgcocoWp	
 Y0@0@dllA[0\0c0cdm0noww~~m,!D]OTUU
		-+!$,,/YZ[rq   )r   r.   NNF)r)   N)__name__
__module____qualname____doc__strr	   r   r   r   boolr2   r?   r@   r^   r   r   rp   rt   r{   r7   r(   __classcell__)rK   s   @rL   r"   r"      s     J $-3(,/3+6+6 +6 49c>*	+6
 H%+6 #8,+6 +6 
+6Z00!AF sCx 8 [3 [S [S [(L$ L\rq   r"   )r3   os.pathtypingr   r   r   r   r   r   r	   PILr
   utilsr   r   visionr   r>   r0   r   r"    rq   rL   <module>r      si    	  D D D  ? !L a]][eY /..494R\- R\rq   