
    h>(                         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y)    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.
    Nrootversiontarget_type	transformtarget_transformdownloadloader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_existsRuntimeErrorall_categoriescategories_indexcategories_map
isinstancelistCATEGORIES_2021r#   
_init_2021_init_pre2021index	enumeratelistdirr!   appendr'   )selfr!   r"   r#   r$   r%   r&   r'   t	dir_indexdir_namefilesfname	__class__s                ^/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/torchvision/datasets/inaturalist.pyr1   zINaturalist.__init__F   s    &gy,:K:K:MNdG4	\lm
D4(MMO!!#ijj)+ <> 57+t,&-K<<v%fqrabq-&A[?A[ \rDOO]hiXYq-AR SiD  -/
#,T-@-@#A 	6IxJJrww||DIIx@AE 6

!!9e"456	6
   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)sortedr2   rB   r!   r8   r=   r9   rA   splitlenr7   zipr:   rC   )	rD   krF   rG   piecescat_mapcatnamecat_ids	            rK   r>   zINaturalist._init_2021v   sU    %RZZ		%:; 1@ @1B @#,T-@-@#A 	0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/	0 !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)r9   rQ   r2   rB   r!   rA   r3   r4   r"   int
ValueErrorr7   rS   r:   extendr8   )rD   	cat_indexsuper_categoriessindexscatsubcategoriessubcatsubcat_iold_lencindexcs              rK   r?   zINaturalist._init_pre2021   s    ")"	!"**TYY"78%&67 	KLFD39D!!'*40"2::bggll499d.K#LMM' K<<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-#K	K. #4#6#67 	AIFA"%6vh#?@@	A & Y*-Nvh+WXXYs   ;GG#r@   c                 H   | j                   |   \  }}t        j                  j                  | j                  | j
                  |   |      }| j                  | j                  |      nt        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   )r@   r2   r3   r4   r!   r8   r'   r   openr#   rC   r:   rS   tupler$   r%   )rD   r@   rZ   rI   
image_pathimgtargetrE   s           rK   __getitem__zINaturalist.__getitem__   s     

5)WW\\$))T-@-@-H%P
)-)@dkk*%ejjQ[F\!! 	>AF{f%d11&9!<=		>
 #&f+/vvay>>%..%C  ,**62FF{    c                 ,    t        | j                        S )N)rS   r@   rD   s    rK   __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 )r8   r9   r^   items)rD   ru   rv   rY   ids        rK   category_namezINaturalist.category_name   s     F"&&{33D$9$99 #:=/!KLL $ 5 5m D J J L $HD"[(#$ !#7}E-!YZZrq   c                     t         j                  j                  | j                        xr+ t	        t        j
                  | j                              dkD  S )Nr   )r2   r3   existsr!   rS   rB   rs   s    rK   r6   zINaturalist._check_exists   s5    ww~~dii(KSDII1F-G!-KKrq   c                 H   | j                         ry 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                         y )Nz.tgz)filenamemd5z.tar.gzz#Unable to find downloaded files at )r6   r2   r3   dirnamer!   r
   r/   r"   DATASET_MD5r4   basenamerstripr}   r7   rename)rD   	base_rootorig_dir_names      rK   r&   zINaturalist.download   s    GGOODII.	$&	t||nD<QWbcgcocoWp	
 Y0@0@dllA[0\0c0cdm0noww~~m,!D]OTUU
		-+rq   )r   r-   NNFN)r(   N)__name__
__module____qualname____doc__r   strr   r<   r   r   boolr   r1   r>   r?   r]   rl   rp   rt   r{   r6   r&   __classcell__)rJ   s   @rK   r    r        s   #P $-3(,/3>B.CI. . 49c>*	.
 H%. #8,. . 5d#3"4c"9:;. 
.`00!AF sCx : [3 [S [S [(Lt L,rq   r    )r2   os.pathpathlibr   typingr   r   r   r   PILr   utilsr
   r   visionr   r=   r/   r   r     rq   rK   <module>r      sg    	   1 1  ? !L a]][eY /..494U,- U,rq   