|
"""ImageNet-R Dataset""" |
|
|
|
import os |
|
import datasets |
|
import json |
|
|
|
|
|
logger = datasets.logging.get_logger(__name__) |
|
|
|
_CITATION = """\ |
|
@article{hendrycks2020many, |
|
title={The Many Faces of Robustness: A Critical Analysis of Out-of-Distribution Generalization}, |
|
author={Dan Hendrycks and Steven Basart and Norman Mu and Saurav Kadavath and Frank Wang and Evan Dorundo and Rahul Desai and Tyler Zhu and Samyak Parajuli and Mike Guo and Dawn Song and Jacob Steinhardt and Justin Gilmer}, |
|
journal={arXiv preprint arXiv:2006.16241}, |
|
year={2020} |
|
} |
|
""" |
|
|
|
_DESCRIPTION = """\ |
|
之后放 |
|
""" |
|
|
|
_HOMEPAGE = "之后搞" |
|
|
|
_URLs = { |
|
"imagenet-r": "./imagenet-r.tar", |
|
"classes": "./classes.json", |
|
"wordnet": "./wordnet.json", |
|
} |
|
|
|
|
|
class ImageNet_R_Config(datasets.BuilderConfig): |
|
"""BuilderConfig for ImageNet-R""" |
|
|
|
def __init__(self, **kwargs): |
|
""" |
|
Args: |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
super(ImageNet_R_Config, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs) |
|
|
|
|
|
|
|
def list_subfolders_and_files(root_dir): |
|
all_paths = [] |
|
|
|
for root, dirs, files in os.walk(root_dir): |
|
|
|
|
|
|
|
|
|
for file in files: |
|
file_path = os.path.join(root, file) |
|
all_paths.append(file_path) |
|
|
|
|
|
return all_paths |
|
|
|
|
|
class ImageNet_R(datasets.GeneratorBasedBuilder): |
|
"""ImageNet-R Dataset""" |
|
|
|
VERSION = datasets.Version("1.0.0") |
|
BUILDER_CONFIGS = [ |
|
ImageNet_R_Config( |
|
name="test", |
|
description="test set.", |
|
), |
|
] |
|
|
|
def _info(self): |
|
if self.config.name == "test": |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"image": datasets.Value("string"), |
|
"wnid": datasets.Value("string"), |
|
"class_name": datasets.Value("string"), |
|
} |
|
), |
|
supervised_keys=None, |
|
citation=_CITATION, |
|
homepage=_HOMEPAGE, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
downloaded_files = dl_manager.download_and_extract(_URLs) |
|
|
|
if self.config.name == "test": |
|
return [ |
|
datasets.SplitGenerator( |
|
name = datasets.Split.TEST, |
|
gen_kwargs = { |
|
"filepath": downloaded_files["imagenet-r"], |
|
"classes_path": downloaded_files["classes"], |
|
"wordnet_path": downloaded_files["wordnet"], |
|
} |
|
) |
|
] |
|
|
|
|
|
def _generate_examples(self, filepath, classes_path, wordnet_path): |
|
"""Yields examples.""" |
|
logger.info("generating examples from = %s", filepath) |
|
|
|
with open(classes_path, 'r') as f: |
|
classes = json.load(f) |
|
with open(wordnet_path, 'r') as f: |
|
wordnet = json.load(f) |
|
|
|
imagenet_r_wnids = classes['imagenet_r_wnids'] |
|
all_wnids = classes['all_wnids'] |
|
|
|
image_paths = list_subfolders_and_files(filepath) |
|
|
|
|
|
for _id, image_path in enumerate(image_paths): |
|
if self.config.name == "test": |
|
img_style = image_path.split('/')[-1].split('_')[0] |
|
wnid = image_path.split('/')[-2] |
|
class_name = wordnet[wnid] |
|
|
|
yield _id, { |
|
"image": image_path, |
|
|
|
"wnid": wnid, |
|
"class_name": class_name, |
|
} |
|
|