o
    Ni0                     @   s   d Z ddlZddlm  mZ ddlmZ dZ	dZ
ejdZdZed ed ed	 d
Zdddd
ZdZG dd dejjZdd ZG dd dejjZdS )z-The ImageNet-v2 image classification dataset.    Na  
@inproceedings{recht2019imagenet,
  title={Do ImageNet Classifiers Generalize to ImageNet?},
  author={Recht, Benjamin and Roelofs, Rebecca and Schmidt, Ludwig and Shankar, Vaishaal},
  booktitle={International Conference on Machine Learning},
  pages={5389--5400},
  year={2019}
}
a  
ImageNet-v2 is an ImageNet test set (10 per class) collected by closely
following the original labelling protocol. Each image has been labelled by
at least 10 MTurk workers, possibly more, and depending on the strategy used to
select which images to include among the 10 chosen for the given class there are
three different versions of the dataset. Please refer to section four of the
paper for more details on how the different variants were compiled.

The label space is the same as that of ImageNet2012. Each example is
represented as a dictionary with the following keys:

* 'image': The image, a (H, W, 3)-tensor.
* 'label': An integer in the range [0, 1000).
* 'file_name': A unique sting identifying the example within the dataset.
z0.1.0z3https://s3-us-west-2.amazonaws.com/imagenetv2publicz$/imagenetv2-matched-frequency.tar.gzz/imagenetv2-threshold0.7.tar.gzz/imagenetv2-topimages.tar.gz)zmatched-frequencyzthreshold-0.7	topimageszimagenetv2-matched-frequencyzimagenetv2-threshold0.7zimagenetv2-topimagesz,image_classification/imagenet2012_labels.txtc                       s(   e Zd ZdZejj fddZ  ZS )ImagenetV2Configz-"Configuration specifying the variant to use.c                    s<   t t| jdi | |tvrtd|tt|| _dS )aT  The parameters specifying how the dataset will be processed.

    The dataset comes in three different variants. Please refer to the paper
    on more details how they were collected.

    Args:
      variant: One of 'matched-frequency', 'threshold-0.7', or 'topimages'.
      **kwargs: Passed on to the constructor of `BuilderConfig`.
    z*Unknown split number {}, must be one of {}N )superr   __init___IMAGENET_V2_URLS
ValueErrorformatlistvariant)selfr   kwargs	__class__r   h/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/image_classification/imagenet_v2.pyr   E   s   
zImagenetV2Config.__init__)	__name__
__module____qualname____doc__tfdscoredisallow_positional_argsr   __classcell__r   r   r   r   r   B   s    r   c                  c   s"    t D ]} t| | ttdV  qd S )N)r   nameversiondescription)r   r   _VERSION_DESCRIPTION)r   r   r   r   _create_builder_configsW   s   
r   c                   @   s2   e Zd ZdZee Zdd Zdd Zdd Z	dS )	
ImagenetV2zDAn ImageNet test set recollected by following the original protocol.c              	   C   sN   t jt}t jj| tt jt jjddt jj	|dt j
 dddtdS )Njpeg)encoding_format)
names_fileimagelabel	file_name)r$   r%   z*https://github.com/modestyachts/ImageNetV2)builderr   featuressupervised_keyshomepagecitation)r   r   get_tfds_path_IMAGENET_LABELS_FILENAMEDatasetInfor   r(   FeaturesDictImage
ClassLabelText	_CITATION)r   r"   r   r   r   _infod   s   zImagenetV2._infoc                 C   sB   t | jj }tj||t| jj }tj	j
tjjd|idgS )z*Returns a SplitGenerator for the test set.imagenet_v2_root)r   
gen_kwargs)r   builder_configr   ospathjoindownload_and_extract_TAR_TOPDIRr   r   SplitGeneratorSplitTEST)r   
dl_managervariant_urlr5   r   r   r   _split_generatorsw   s   
zImagenetV2._split_generatorsc                 c   sd    t jj|D ]'}tj||}t jj|D ]}tj||}|t||d}||fV  qqdS )zYields the examples.r#   N)tfiogfilelistdirr8   r9   r:   int)r   r5   class_id	class_dirimage_filename
image_pathr(   r   r   r   _generate_examples   s   zImagenetV2._generate_examplesN)
r   r   r   r   r
   r   BUILDER_CONFIGSr4   rB   rL   r   r   r   r   r   _   s    
r   )r   r8   tensorflow.compat.v2compatv2rC   tensorflow_datasets.public_api
public_apir   r3   r   r   Versionr   	_ROOT_URLr   r<   r-   BuilderConfigr   r   GeneratorBasedBuilderr   r   r   r   r   <module>   s(   
