o
    Ni                     @   sR   d Z ddlZddlm  mZ ddlmZ dZ	dZ
dZdZG dd dejjZdS )	z,The ImageNet-A image classification dataset.    Nz
@article{hendrycks2019nae,
  title={Natural Adversarial Examples},
  author={Dan Hendrycks and Kevin Zhao and Steven Basart and Jacob Steinhardt and Dawn Song},
  journal={arXiv preprint arXiv:1907.07174},
  year={2019}
}
a  
ImageNet-A is a set of images labelled with ImageNet labels that were obtained
by collecting new data and keeping only those images that ResNet-50 models fail
to correctly classify. For more details please refer to the paper.

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,image_classification/imagenet2012_labels.txtz:https://people.eecs.berkeley.edu/~hendrycks/imagenet-a.tarc                   @   s4   e Zd ZdZejdZdd Zdd Z	dd Z
d	S )
	ImagenetAz2Natural adversarial examples with ImageNet labels.z0.1.0c              	   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   z1https://github.com/hendrycks/natural-adv-examples)builderdescriptionfeaturessupervised_keyshomepagecitation)tfdscoreget_tfds_path_IMAGENET_LABELS_FILENAMEDatasetInfo_DESCRIPTIONr   FeaturesDictImage
ClassLabelText	_CITATION)selfr    r   g/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/image_classification/imagenet_a.py_info7   s   zImagenetA._infoc                 C   s.   t j|td}tjjtjj	d|idgS )z*Returns a SplitGenerator for the test set.z
imagenet-aimagenet_a_root)name
gen_kwargs)
ospathjoindownload_and_extract_IMAGENET_A_URLr   r   SplitGeneratorSplitTEST)r   
dl_managerr   r   r   r   _split_generatorsJ   s   
zImagenetA._split_generatorsc                 c   sp    t jj|D ]-}tj||}t jj|sqt jj|D ]}tj||}|||d}||fV  q qdS )zYields the examples.r   N)tfiogfilelistdirr"   r#   r$   isdir)r   r   class_synset	class_dirimage_filename
image_pathr   r   r   r   _generate_examplesW   s   zImagenetA._generate_examplesN)__name__
__module____qualname____doc__r   r   VersionVERSIONr   r+   r5   r   r   r   r   r   2   s    r   )r9   r"   tensorflow.compat.v2compatv2r,   tensorflow_datasets.public_api
public_apir   r   r   r   r&   r   GeneratorBasedBuilderr   r   r   r   r   <module>   s   	