
    %`i>(                         d dl Z d dlZ d dlm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Zd	d
dddddZdddddddZ G d de          ZdS )    N)Path)AnyCallableOptional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eef         dedeee         ef         d	ee	         d
ee	         de
dee	eeef         gef                  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 )INaturalistaX  `iNaturalist <https://github.com/visipedia/inat_comp>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): 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 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.
        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.
    r   fullNFrootversiontarget_type	transformtarget_transformdownloadloaderreturnc                    t          |dt                                                    | _        t	                                          t          j                            ||          ||           t          j	        |d           |r| 
                                 |                                 st          d          g | _        i | _        g | _        t!          |t"                    s|g}| j        d d         dk    r&d |D             | _        |                                  n%d	 |D             | _        |                                  g | _        t-          | j                  D ]]\  }}	t          j        t          j                            | j        |	                    }
|
D ]}| j                            ||f           ^|| _        d S )
Nr#   )r%   r&   T)exist_okzHDataset not found or corrupted. You can use download=True to download it   2021c                 B    g | ]}t          |d dgt          R           S )r$   r!   )r   CATEGORIES_2021.0ts     t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchvision/datasets/inaturalist.py
<listcomp>z(INaturalist.__init__.<locals>.<listcomp>f   s0    rrrabq-&A[?A[A[ \ \rrr    c                 0    g | ]}t          |d d          S )r$   )r!   super)r   r0   s     r3   r4   z(INaturalist.__init__.<locals>.<listcomp>i   s&    iiiXYq-AR S Siiir5   )r   DATASET_URLSkeysr#   r7   __init__ospathjoinmakedirsr'   _check_existsRuntimeErrorall_categoriescategories_indexcategories_map
isinstancelistr$   
_init_2021_init_pre2021index	enumeratelistdirr"   appendr(   )selfr"   r#   r$   r%   r&   r'   r(   	dir_indexdir_namefilesfname	__class__s               r3   r:   zINaturalist.__init__F   s    &gy,:K:K:M:MNNdG44	\lmmm
D4(((( 	MMOOO!!## 	kijjj)+ <> 57+t,, 	(&-K<v%%rrfqrrrDOOii]hiiiD    -/
#,T-@#A#A 	6 	6IxJrw||DIx@@AAE 6 6
!!9e"455556 r5   c                    t          t          j        | j                            | _        d t
          D             | _        t          | j                  D ]\  }}|                    d          }t          |          dk    rt          d| d          |d         |dk    rt          d|d          d	|d          i }t          t
          |d
d                   D ]W\  }}|| j        |         v r| j        |         |         }n*t          | j        |                   }|| j        |         |<   |||<   X| j                            |           dS )zInitialize based on 2021 layoutc                     i | ]}|i S  rT   )r1   ks     r3   
<dictcomp>z*INaturalist._init_2021.<locals>.<dictcomp>|   s     @ @ @1B @ @ @r5   _   zUnexpected category name z, wrong number of piecesr   05dzUnexpected category id z, expecting r	      N)sortedr;   rJ   r"   rA   r/   rB   rI   splitlenr@   ziprC   rK   )rL   rM   rN   piecescat_mapcatnamecat_ids           r3   rF   zINaturalist._init_2021v   sw    %RZ	%:%:;; !A @ @ @ @#,T-@#A#A 	0 	0Ix^^C((F6{{a"#ax#a#a#abbbayy...."#cVAY#c#cT]#c#c#cdddG &1+>> & &	T40555!237=FF !6s!;<<F7=D)#.t4%&&w////	0 	0r5   c           	         di i| _         d}t          t          j        | j                            }t          |          D ]l\  }}|| j         d         |<   t          t          j        t          j                            | j        |                              }|D ]}| j        dk    r|}|dz  }n1	 t          |          }n # t          $ r t          d|           w xY w|t          | j                  k    r\t          | j                  }| j                            i g||z
  dz   z             | j                            dg||z
  dz   z             | j        |         rt          d|           d|i| j        |<   t          j                            ||          | j        |<   nt          | j                  D ]\  }	}
|
st          d|	           d	S )
z$Initialize based on 2017-2019 layoutr7   r   r   r	   z!Unexpected non-numeric dir name:  zDuplicate category zMissing category N)rB   r[   r;   rJ   r"   rI   r<   r=   r#   int
ValueErrorr@   r]   rC   extendrA   )rL   	cat_indexsuper_categoriessindexscatsubcategoriessubcatsubcat_iold_lencindexcs              r3   rG   zINaturalist._init_pre2021   s)    ")"	!"*TY"7"788%&677 	K 	KLFD39D!'*40"2:bgll49d.K.K#L#LMMM' K K<6))(HNIIY#&v;;% Y Y Y*+Wv+W+WXXXYs4#67777!$"566G'..tx'7IA7M/NOOO'..tx'7IA7M/NOOO&x0 G&'EV'E'EFFF18&0A#H-02T60J0J#H--#K( #4#677 	A 	AIFA A"#?v#?#?@@@A	A 	As   4CC!rH   c                 L   | j         |         \  }}t          j                            | j        | j        |         |          }| j        |                     |          nt          j        |          }g }| j	        D ]D}|dk    r|
                    |           |
                    | j        |         |                    Et          |          dk    rt          |          n|d         }| j        |                     |          }| j        |                     |          }||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where the type of target specified by target_type.
        Nr!   r	   r   )rH   r;   r<   r=   r"   rA   r(   r   openr$   rK   rC   r]   tupler%   r&   )rL   rH   rc   rP   
image_pathimgtargetr2   s           r3   __getitem__zINaturalist.__getitem__   s    
5)W\\$)T-@-H%PP
)-)@dkk*%%%ejQ[F\F\! 	> 	>AF{{f%%%%d1&9!<===="%f++//vvay>%..%%C ,**622FF{r5   c                 *    t          | j                  S )N)r]   rH   rL   s    r3   __len__zINaturalist.__len__   s    4:r5   category_typecategory_idc                     |dk    r| j         |         S || j        vrt          d| d          | j        |                                         D ]\  }}||k    r|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 )rA   rB   rg   items)rL   r}   r~   rb   ids        r3   category_namezINaturalist.category_name   s     F""&{33D$999 !K=!K!K!KLLL $ 5m D J J L L $ $HD"[((# ) !Y!Y!Y-!Y!YZZZr5   c                     t           j                            | j                  o)t	          t          j        | j                            dk    S )Nr   )r;   r<   existsr"   r]   rJ   r{   s    r3   r?   zINaturalist._check_exists   s6    w~~di((KSDI1F1F-G-G!-KKr5   c                 F   |                                  rd S t          j                            | j                  }t          t          | j                 || j         dt          | j                            t          j        	                    |t          j        
                    t          | j                                               d                    }t          j                            |          st          d|           t          j        || j                   d S )Nz.tgz)filenamemd5z.tar.gzz#Unable to find downloaded files at )r?   r;   r<   dirnamer"   r
   r8   r#   DATASET_MD5r=   basenamerstripr   r@   rename)rL   	base_rootorig_dir_names      r3   r'   zINaturalist.download   s     	FGOODI..	$&	t|<Q<Q<QWbcgcoWp	
 	
 	
 	
 Y0@0@dlA[0\0\0c0cdm0n0noow~~m,, 	VT]TTUUU
	-+++++r5   )r   r!   NNFN)r)   N)__name__
__module____qualname____doc__r   strr   rE   r   r   boolr   r:   rF   rG   rf   ru   ry   r|   r   r?   r'   __classcell__)rQ   s   @r3   r    r        s       # #P $-3(,/3>B. .CI. . 49c>*	.
 H%. #8,. . 5d#3"4c"9:;. 
. . . . . .`0 0 0 00!A !A !A !AF sCx    :    [3 [S [S [ [ [ [(Lt L L L L, , , , , , , ,r5   r    )r;   os.pathpathlibr   typingr   r   r   r   PILr   utilsr
   r   visionr   r/   r8   r   r    rT   r5   r3   <module>r      s   				        1 1 1 1 1 1 1 1 1 1 1 1       ? ? ? ? ? ? ? ? ! ! ! ! ! !LLL a]][eY  /..494 U, U, U, U, U,- U, U, U, U, U,r5   