
    ڧg'                         d dl Z d dlZ d dlm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dS )    N)Path)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eef         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 )INaturalistaV  `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.
    r   fullNFrootversiontarget_type	transformtarget_transformdownload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     \/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/datasets/inaturalist.py
<listcomp>z(INaturalist.__init__.<locals>.<listcomp>b   s0    rrrabq-&A[?A[A[ \ \rrr    c                 0    g | ]}t          |d d          S )r'   )r$   super)r   r2   s     r5   r6   z(INaturalist.__init__.<locals>.<listcomp>e   s&    iiiXYq-AR S Siiir7   )r   DATASET_URLSkeysr&   r9   __init__ospathjoinmakedirsr*   _check_integrityRuntimeErrorall_categoriescategories_indexcategories_map
isinstancelistr'   
_init_2021_init_pre2021index	enumeratelistdirr%   append)selfr%   r&   r'   r(   r)   r*   	dir_indexdir_namefilesfname	__class__s              r5   r<   zINaturalist.__init__C   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	6 	6r7   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  rV   )r3   ks     r5   
<dictcomp>z*INaturalist._init_2021.<locals>.<dictcomp>v   s     @ @ @1B @ @ @r7   _   zUnexpected category name z, wrong number of piecesr   05dzUnexpected category id z, expecting r      N)sortedr=   rL   r%   rC   r1   rD   rK   splitlenrB   ziprE   rM   )rN   rO   rP   piecescat_mapcatnamecat_ids           r5   rH   zINaturalist._init_2021p   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 	0r7   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 layoutr9   r   r   r   z!Unexpected non-numeric dir name:  zDuplicate category zMissing category N)rD   r]   r=   rL   r%   rK   r>   r?   r&   int
ValueErrorrB   r_   rE   extendrC   )rN   	cat_indexsuper_categoriessindexscatsubcategoriessubcatsubcat_iold_lencindexcs              r5   rI   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!rJ   c                    | j         |         \  }}t          j        t          j                            | j        | 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.
        r$   r   r   )rJ   r   openr=   r>   r?   r%   rC   r'   rM   rE   r_   tupler(   r)   )rN   rJ   re   rR   imgtargetr4   s          r5   __getitem__zINaturalist.__getitem__   s     
5)jdi1DV1LeTTUU! 	> 	>AF{{f%%%%d1&9!<===="%f++//vvay>%..%%C ,**622FF{r7   c                 *    t          | j                  S )N)r_   rJ   rN   s    r5   __len__zINaturalist.__len__   s    4:r7   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 )rC   rD   ri   items)rN   r~   r   rd   ids        r5   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Zr7   c                     t           j                            | j                  o)t	          t          j        | j                            dk    S )Nr   )r=   r>   existsr%   r_   rL   r|   s    r5   rA   zINaturalist._check_integrity   s6    w~~di((KSDI1F1F-G-G!-KKr7   c                    |                                  rt          d| j         d          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                   t          d| j         d           d S )	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)rA   rB   r%   r=   r>   dirnamer   r:   r&   DATASET_MD5r?   basenamerstripr   renameprint)rN   	base_rootorig_dir_names      r5   r*   zINaturalist.download   s8     "" 	^ ^ ^ ^  
 GOODI..	$&	t|<Q<Q<QWbcgcoWp	
 	
 	
 	
 Y0@0@dlA[0\0\0c0cdm0n0noow~~m,, 	VT]TTUUU
	-+++Z$,ZZZ[[[[[r7   )r   r$   NNF)r+   N)__name__
__module____qualname____doc__r
   strr   r   r   r   boolr<   rH   rI   rh   r	   r   rz   r}   r   rA   r*   __classcell__)rS   s   @r5   r#   r#       s          J $-3(,/3+6 +6CI+6 +6 49c>*	+6
 H%+6 #8,+6 +6 
+6 +6 +6 +6 +6 +6Z0 0 0 00!A !A !A !AF sCx    8    [3 [S [S [ [ [ [(L$ L L L L\ \ \ \ \ \ \ \r7   r#   )r=   os.pathpathlibr   typingr   r   r   r   r   r	   r
   PILr   utilsr   r   visionr   r1   r:   r   r#   rV   r7   r5   <module>r      s/   				        D D D D D D D D D D D D D D D D D D       ? ? ? ? ? ? ? ? ! ! ! ! ! !LLL a]][eY  /..494 R\ R\ R\ R\ R\- R\ R\ R\ R\ R\r7   