-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobustness_dataset.py
More file actions
278 lines (239 loc) · 9.86 KB
/
Copy pathrobustness_dataset.py
File metadata and controls
278 lines (239 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
import json, os
import matplotlib.pyplot as plt
import matplotlib.path as path
import matplotlib.patches as patches
DATA_CIFAR10 = "cifar10"
DATA_CIFAR100 = "cifar100"
DATA_IMAGENET16 = "ImageNet16-120"
DATA_ALL = [DATA_CIFAR10, DATA_CIFAR100, DATA_IMAGENET16]
KEYS_CLEAN = ["clean"]
KEYS_ADV = ["aa_apgd-ce@Linf", "aa_square@Linf", "fgsm@Linf",
"pgd@Linf"]
KEYS_CC = ["brightness", "contrast", "defocus_blur", "elastic_transform",
"fog", "frost", "gaussian_noise", "glass_blur", "impulse_noise",
"jpeg_compression", "motion_blur", "pixelate", "shot_noise",
"snow", "zoom_blur"]
KEYS_ALL = KEYS_CLEAN + KEYS_ADV + KEYS_CC
class RobustnessDataset:
"""
Helper class to query evaluation results.
Attributes
----------
keys_clean : list
key for evaluation results on clean data: ["clean"]
keys_adv : list
list that contains keys for all adversarial attack types evaluated
keys_cc : list
list that contains keys for all corruption types evaluated
keys_all : list
list that contains all keys
data_cifar10 : str = "cifar10"
data_cifar100 : str = "cifar100"
data_imagenet16 : str = "ImageNet16-120"
data : list
list that contains all data sources ["cifar10", "cifar100", "ImageNet16-120"]
"""
keys_clean = KEYS_CLEAN
keys_adv = KEYS_ADV
keys_cc = KEYS_CC
keys_all = KEYS_ALL
data_cifar10 = DATA_CIFAR10
data_cifar100 = DATA_CIFAR100
data_imagenet16 = DATA_IMAGENET16
data = DATA_ALL
############################################################################
def __init__(self, path="robustness-data"):
"""
Parameters
----------
path : str
Path to the root folder of the dataset data.
"""
self.path = path
with open(os.path.join(path, "meta.json")) as f:
self.meta = json.load(f)
self.map_str_to_id = {m["nb201-string"]:k for k,m in self.meta["ids"].items()}
self.non_isomorph_ids = [i for i, d in self.meta["ids"].items() if d["isomorph"]==i]
############################################################################
def _ensure_list(self, l):
if type(l) is not list:
l = [l]
return l
############################################################################
def query(
self,
data = DATA_ALL,
key = KEYS_ALL,
measure = ["accuracy", "confidence", "cm"],
missing_ok = False,
tqdm = None
):
"""
Query evaluation results.
Returns a dictionary: dict[<data>][<attack/corruption>][<measure type>][<architecture id>]
Parameters
----------
data : str/list
Data used for evaluation.
key : str/list
Adversarial attack or corruption type.
measure : str/list
Measure type ("accuracy", "confidence", "cm")
"""
data = self._ensure_list(data)
key = self._ensure_list(key)
measure = self._ensure_list(measure)
pbar = tqdm.tqdm(
total = len(data)*len(key)*len(measure)
) if tqdm is not None else None
result = {d:{k:{} for k in key} for d in data}
for d in data:
for k in key:
if d == RobustnessDataset.data_imagenet16:
if k in RobustnessDataset.keys_cc:
if pbar is not None:
pbar.update(1)
continue
for m in measure:
file = os.path.join(self.path, d, f"{k}_{m}.json")
if missing_ok:
if not os.path.isfile(file):
if pbar is not None:
pbar.update(1)
continue
with open(file, "r") as f:
r = json.load(f)
result[d][k][m] = r[d][k][m]
if pbar is not None:
pbar.update(1)
if len(result[d][k]) == 0:
del result[d][k]
if pbar is not None:
pbar.close()
return result
############################################################################
def get_uid(self, i):
"""
Returns the evaluated architecture id (if given id is isomorph to another network)
Parameters
----------
i : str/int
Architecture id.
"""
return self.meta["ids"][str(i)]["isomorph"]
############################################################################
def id_to_string(self, i):
"""
Returns the string representing an architecture in NAS-Bench-201 for the given id.
Parameters
----------
i : str/int
Architecture id.
"""
return self.meta["ids"][str(i)]["nb201-string"]
############################################################################
def string_to_id(self, s):
"""
Returns the id of a given NAS-Bench-201 architecture string.
Parameters
----------
s : str
Architecture string as in NAS-Bench-201.
"""
return self.map_str_to_id[s]
############################################################################
def draw_arch(self, s=None, i=None):
"""
Plot the cell of a given NAS-Bench-201 architecture string or architecture id.
Parameters
----------
i : str/int
Architecture id.
s : str
Architecture string as in NAS-Bench-201.
"""
assert s is not None or i is not None
if s is None:
s = self.id_to_string(i)
if i is None:
i = self.string_to_id(s)
pos = {(0,1):(0.25,0.75),(0,2):(0.5,0),(0,3):(1,-0.75),(1,2):(1,0.5),(1,3):(1.75,0.75),(2,3):(1.5,0)}
m_ops = {"avg_pool_3x3":"avg", "nor_conv_1x1":"1x1", "nor_conv_3x3":"3x3", "skip_connect":"skip", "none":"zero"}
p_0_1 = patches.FancyArrowPatch(
path=path.Path([(0, 0),(0, 1),(1-0.2, 1)],
[path.Path.MOVETO, path.Path.CURVE3, path.Path.CURVE3]),
fc="none", transform=plt.gca().transData, arrowstyle="-|>,head_length=5,head_width=3"
)
p_0_2 = patches.FancyArrowPatch(
path=path.Path([(0,0),(1-0.2,0)],
[path.Path.MOVETO, path.Path.LINETO]),
fc="none", transform=plt.gca().transData, arrowstyle="-|>,head_length=5,head_width=3"
)
p_0_3 = patches.FancyArrowPatch(
path=path.Path([(0, 0),(1,-1.5),(2-0.14,0-0.14)],
[path.Path.MOVETO, path.Path.CURVE3, path.Path.CURVE3]),
fc="none", transform=plt.gca().transData, arrowstyle="-|>,head_length=5,head_width=3"
)
p_1_2 = patches.FancyArrowPatch(
path=path.Path([(1,1),(1,0+0.2)],
[path.Path.MOVETO, path.Path.LINETO]),
fc="none", transform=plt.gca().transData, arrowstyle="-|>,head_length=5,head_width=3"
)
p_1_3 = patches.FancyArrowPatch(
path=path.Path([(1,1),(2,1),(2,0+0.2)],
[path.Path.MOVETO, path.Path.CURVE3, path.Path.CURVE3]),
fc="none", transform=plt.gca().transData, arrowstyle="-|>,head_length=5,head_width=3"
)
p_2_3 = patches.FancyArrowPatch(
path=path.Path([(1,0),(2-0.2,0)],
[path.Path.MOVETO, path.Path.LINETO]),
fc="none", transform=plt.gca().transData, arrowstyle="-|>,head_length=5,head_width=3"
)
plt.gca().add_patch(p_0_1)
plt.gca().add_patch(p_0_2)
plt.gca().add_patch(p_0_3)
plt.gca().add_patch(p_1_2)
plt.gca().add_patch(p_1_3)
plt.gca().add_patch(p_2_3)
circle = plt.Circle((0,0), 0.22, color="black")
plt.gca().add_patch(circle)
circle = plt.Circle((0,0), 0.2, color="white")
plt.gca().add_patch(circle)
circle = plt.Circle((1,1), 0.22, color="black")
plt.gca().add_patch(circle)
circle = plt.Circle((1,1), 0.2, color="white")
plt.gca().add_patch(circle)
circle = plt.Circle((1,0), 0.22, color="black")
plt.gca().add_patch(circle)
circle = plt.Circle((1,0), 0.2, color="white")
plt.gca().add_patch(circle)
circle = plt.Circle((2,0), 0.22, color="black")
plt.gca().add_patch(circle)
circle = plt.Circle((2,0), 0.2, color="white")
plt.gca().add_patch(circle)
plt.text(0, 0, "in", va="center", ha="center")
plt.text(1, 1, "1", va="center", ha="center")
plt.text(1, 0, "2", va="center", ha="center")
plt.text(2, 0, "out", va="center", ha="center")
plt.text(-0.3, 1.3, f"# {i}")
for v, ops in enumerate(s.split("+")):
v += 1
ops = ops[1:-1]
ops = ops.split("|")
for o in ops:
o, v_src = o.split("~")
o = m_ops[o]
x,y = pos[(int(v_src),v)]
plt.text(
x, y, o,
va = "center",
ha = "center",
backgroundcolor = "w"
)
plt.gca().set_aspect("equal")
plt.xlim(-0.5, 2.5)
plt.ylim(-1, 1.5)
plt.xticks([])
plt.yticks([])
plt.show()