Delete loading script
Browse files- imagenet-r.py +0 -131
imagenet-r.py
DELETED
@@ -1,131 +0,0 @@
|
|
1 |
-
"""ImageNet-R Dataset"""
|
2 |
-
|
3 |
-
import os
|
4 |
-
import datasets
|
5 |
-
import json
|
6 |
-
|
7 |
-
|
8 |
-
logger = datasets.logging.get_logger(__name__)
|
9 |
-
|
10 |
-
_CITATION = """\
|
11 |
-
@article{hendrycks2020many,
|
12 |
-
title={The Many Faces of Robustness: A Critical Analysis of Out-of-Distribution Generalization},
|
13 |
-
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},
|
14 |
-
journal={arXiv preprint arXiv:2006.16241},
|
15 |
-
year={2020}
|
16 |
-
}
|
17 |
-
"""
|
18 |
-
|
19 |
-
_DESCRIPTION = """\
|
20 |
-
ImageNet-R(endition) contains art, cartoons, deviantart, graffiti, embroidery, graphics, origami, paintings, patterns, plastic objects, plush objects, sculptures, sketches, tattoos, toys, and video game renditions of ImageNet classes.
|
21 |
-
|
22 |
-
ImageNet-R has renditions of 200 ImageNet classes resulting in 30,000 images.
|
23 |
-
"""
|
24 |
-
|
25 |
-
_HOMEPAGE = "https://github.com/hendrycks/imagenet-r/"
|
26 |
-
|
27 |
-
_URLs = {
|
28 |
-
"imagenet-r": "./imagenet-r.tar",
|
29 |
-
"classes": "./classes.json",
|
30 |
-
"wordnet": "./wordnet.json",
|
31 |
-
}
|
32 |
-
|
33 |
-
|
34 |
-
class ImageNet_R_Config(datasets.BuilderConfig):
|
35 |
-
"""BuilderConfig for ImageNet-R"""
|
36 |
-
|
37 |
-
def __init__(self, **kwargs):
|
38 |
-
"""
|
39 |
-
Args:
|
40 |
-
**kwargs: keyword arguments forwarded to super.
|
41 |
-
"""
|
42 |
-
super(ImageNet_R_Config, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
def list_subfolders_and_files(root_dir):
|
47 |
-
# all images paths
|
48 |
-
all_paths = []
|
49 |
-
|
50 |
-
for root, dirs, files in os.walk(root_dir):
|
51 |
-
for file in files:
|
52 |
-
file_path = os.path.join(root, file)
|
53 |
-
all_paths.append(file_path)
|
54 |
-
# end for
|
55 |
-
return all_paths
|
56 |
-
|
57 |
-
|
58 |
-
class ImageNet_R(datasets.GeneratorBasedBuilder):
|
59 |
-
"""ImageNet-R Dataset"""
|
60 |
-
|
61 |
-
VERSION = datasets.Version("1.0.0")
|
62 |
-
BUILDER_CONFIGS = [
|
63 |
-
ImageNet_R_Config(
|
64 |
-
name="test",
|
65 |
-
description="test-set of ImageNet-R",
|
66 |
-
),
|
67 |
-
]
|
68 |
-
|
69 |
-
def _info(self):
|
70 |
-
if self.config.name == "test":
|
71 |
-
return datasets.DatasetInfo(
|
72 |
-
description=_DESCRIPTION,
|
73 |
-
features=datasets.Features(
|
74 |
-
{
|
75 |
-
"image": datasets.Image(),
|
76 |
-
"wnid": datasets.Value("string"),
|
77 |
-
"class_name": datasets.Value("string"),
|
78 |
-
}
|
79 |
-
),
|
80 |
-
supervised_keys=None,
|
81 |
-
citation=_CITATION,
|
82 |
-
homepage=_HOMEPAGE,
|
83 |
-
)
|
84 |
-
|
85 |
-
def _split_generators(self, dl_manager):
|
86 |
-
"""Returns SplitGenerators."""
|
87 |
-
downloaded_files = dl_manager.download_and_extract(_URLs)
|
88 |
-
|
89 |
-
if self.config.name == "test":
|
90 |
-
return [
|
91 |
-
datasets.SplitGenerator(
|
92 |
-
name = datasets.Split.TEST,
|
93 |
-
gen_kwargs = {
|
94 |
-
"filepath": downloaded_files["imagenet-r"],
|
95 |
-
"classes_path": downloaded_files["classes"],
|
96 |
-
"wordnet_path": downloaded_files["wordnet"],
|
97 |
-
}
|
98 |
-
)
|
99 |
-
]
|
100 |
-
|
101 |
-
|
102 |
-
def _generate_examples(self, filepath, classes_path, wordnet_path):
|
103 |
-
"""Yields examples."""
|
104 |
-
logger.info("generating examples from = %s", filepath)
|
105 |
-
|
106 |
-
with open(classes_path, 'r') as f:
|
107 |
-
classes = json.load(f)
|
108 |
-
with open(wordnet_path, 'r') as f:
|
109 |
-
wordnet = json.load(f)
|
110 |
-
|
111 |
-
imagenet_r_wnids = classes['imagenet_r_wnids']
|
112 |
-
all_wnids = classes['all_wnids']
|
113 |
-
|
114 |
-
image_paths = list_subfolders_and_files(filepath)
|
115 |
-
|
116 |
-
for _id, image_path in enumerate(image_paths):
|
117 |
-
if self.config.name == "test":
|
118 |
-
# img_style = image_path.split('/')[-1].split('_')[0] # [art]_0.jpg
|
119 |
-
wnid = image_path.split('/')[-2]
|
120 |
-
class_name = wordnet[wnid]
|
121 |
-
|
122 |
-
yield _id, {
|
123 |
-
"image": {
|
124 |
-
"path": image_path,
|
125 |
-
"bytes": open(image_path, "rb").read(),
|
126 |
-
},
|
127 |
-
# "label": imagenet_r_wnids.index(wnid),
|
128 |
-
"wnid": wnid,
|
129 |
-
"class_name": class_name,
|
130 |
-
}
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|