-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluate.py
331 lines (298 loc) · 9.36 KB
/
evaluate.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from dataclasses import dataclass
from foolbox.models import pytorch
from foolbox.utils import accuracy
from foolbox.attacks import projected_gradient_descent
from foolbox.attacks import LinfPGD, carlini_wagner
from prototorch.core.distances import (
squared_euclidean_distance,
lpnorm_distance,
)
from distance import lpips_distance
import torch
@dataclass
class LowerBoundRTM:
LRTE: float
LRTA: float
CTE: float
CTA: float
@dataclass
class UpperBoundRTM:
URTE: float
URTA: float
def get_lowerbound_certification(
model,
data: torch.Tensor,
labels: torch.LongTensor,
epsilon: float,
p_norm: int | str,
q_norm: int | str,
device: str = "cpu",
) -> LowerBoundRTM:
# load model if any or live certification
match isinstance(model, str):
case True:
model = torch.load(model)
model.eval()
case _:
pass
condition1 = q_norm in ["lpips-linf", "lpips-l2", "lpips-l1"]
match condition1:
case True:
device ="cuda" if torch.cuda.is_available() else "cpu"
case False:
device = "cpu"
fmodel = pytorch.PyTorchModel(
model=model,
bounds=(0, 1),
device=device,
)
data = data.to(device)
labels = labels.to(device)
clean_acc = accuracy(fmodel, data, labels)
# setup attacks
match p_norm:
case "linf":
attack = LinfPGD()
raw_advs, clipped_advs, success = attack(
fmodel, data, labels, epsilons=epsilon
)
case "l2":
attack = carlini_wagner.L2CarliniWagnerAttack()
raw_advs, clipped_advs, success = attack(
fmodel, data, labels, epsilons=epsilon
)
case "l1":
attack = projected_gradient_descent.L1ProjectedGradientDescentAttack()
raw_advs, clipped_advs, success = attack(
fmodel, data, labels, epsilons=epsilon
)
case _:
raise NotImplementedError(
"evaluate with either l1, l2 or linf norm",
)
robust_test_error = success.float().mean(axis=-1).item()
robust_test_accuracy = 1 - robust_test_error
return LowerBoundRTM(
LRTE=round(robust_test_error, 4),
LRTA=round(robust_test_accuracy, 4),
CTA=round(clean_acc, 4),
CTE=round(1 - clean_acc, 4),
)
def get_upperbound_certification(
x_test: torch.Tensor,
prototypes: torch.Tensor,
labels: torch.LongTensor,
ppc: int,
epsilon: float,
p_norm: str | int | None = None,
q_norm: str | int | None = None,
device: str = "cpu",
) -> float:
condition1 = q_norm in ["lpips-linf", "lpips-l2", "lpips-l1"]
match condition1:
case True:
device ="cuda" if torch.cuda.is_available() else "cpu"
case False:
device = "cpu"
x_test = x_test.to(device)
prototypes = prototypes.to(device)
match (condition1, p_norm):
case (False, "l2"):
distance_space = lpnorm_distance(
x=x_test,
y=prototypes,
p=2,
)
urte = get_urte(
distance_space=distance_space,
labels=labels,
ppc=ppc,
epsilon=epsilon,
p_norm=p_norm,
q_norm=q_norm,
x_test=x_test,
)
score = len(urte) / len(x_test)
return round(score, 4)
case (False, "linf"):
distance_space = lpnorm_distance(
x=x_test,
y=prototypes,
p=float("inf"),
)
urte = get_urte(
distance_space=distance_space,
labels=labels,
ppc=ppc,
epsilon=epsilon,
p_norm=p_norm,
q_norm=q_norm,
x_test=x_test,
)
score = len(urte) / len(x_test)
return round(score, 4)
case (False, "l1"):
distance_space = lpnorm_distance(
x=x_test,
y=prototypes,
p=1,
)
urte = get_urte(
distance_space=distance_space,
labels=labels,
ppc=ppc,
epsilon=epsilon,
p_norm=p_norm,
q_norm=q_norm,
x_test=x_test,
)
score = len(urte) / len(x_test)
return round(score, 4)
case (False, None):
distance_space = squared_euclidean_distance(
x=x_test,
y=prototypes,
)
urte = get_urte(
distance_space=distance_space,
labels=labels,
ppc=ppc,
epsilon=epsilon,
p_norm="l2",
q_norm=q_norm,
x_test=x_test,
)
score = len(urte) / len(x_test)
return round(score, 4)
case (True, "l2"): # use stable version only
distance_space = lpips_distance(
x=x_test,
y=prototypes,
p="l2",
)
urte = get_urte(
distance_space=distance_space,
labels=labels,
ppc=ppc,
epsilon=epsilon,
p_norm=p_norm,
q_norm=q_norm,
x_test=x_test,
)
score = len(urte) / len(x_test)
return round(score, 4)
case (True, "l1"): # only to investiage numerical stability
distance_space = lpips_distance(
x=x_test,
y=prototypes,
p="l1",
)
urte = get_urte(
distance_space=distance_space,
labels=labels,
ppc=ppc,
epsilon=epsilon,
p_norm=p_norm,
q_norm=q_norm,
x_test=x_test,
)
score = len(urte) / len(x_test)
return round(score, 4)
case (True, "linf"): # only to investigate numerical stability
distance_space = lpips_distance(
x=x_test,
y=prototypes,
p="linf",
)
urte = get_urte(
distance_space=distance_space,
labels=labels,
ppc=ppc,
epsilon=epsilon,
p_norm=p_norm,
q_norm=q_norm,
x_test=x_test,
)
score = len(urte) / len(x_test)
return round(score, 4)
case _:
raise NotImplementedError(
"compute the closest distance: use l2, linf or l1 norm"
)
def get_urte(
distance_space: torch.Tensor,
labels: torch.LongTensor,
ppc: int,
epsilon: float,
p_norm: str,
q_norm: str,
x_test: torch.Tensor,
) -> list[float]:
margin = []
p = get_certification_norms(p_norm)
q = get_certification_norms(q_norm)
cond, epsilon = p <= q, 2 * epsilon
input_shape = torch.Tensor(list(x_test.shape[1:]))
n = int(torch.prod(input_shape).item())
for i, v in enumerate(distance_space):
dist = distance_space[i].reshape(len(torch.unique(labels)), ppc)
min_dist = torch.Tensor([torch.min(protos_dis) for protos_dis in dist])
wp_wm_indices = torch.argsort(min_dist)[:2]
if labels[i] == wp_wm_indices[0]:
wp = wp_wm_indices[0]
wm = wp_wm_indices[1]
else:
wp = wp_wm_indices[1]
wm = wp_wm_indices[0]
dp = min_dist[wp].item()
dm = min_dist[wm].item()
hypothesis_margin = dm - dp
hypothesis_margin = round(hypothesis_margin, 4)
match cond:
case True:
if hypothesis_margin <= epsilon:
margin.append(hypothesis_margin)
case False:
if (n ** (1 / p - 1 / q) * hypothesis_margin) <= epsilon:
margin.append(hypothesis_margin)
return margin
def get_certification_norms(input_norm: str):
match input_norm:
case "l1":
return 1
case "l2":
return 2
case "linf":
return float("inf")
case "lpips-l2":
return 2
case "lpips-l1":
return 1
case "lpips-linf":
return float("inf")
case _:
raise NotImplementedError(
"use either l1,l2 and linf for arbitrary semi-norms"
"or lpips-l2 and lpips-l1 for semi-metric"
)
@dataclass(slots=True)
class LPN:
lp_norm: str
def get_lpnorms(self, x, y):
match self.lp_norm:
case "l2":
return lpnorm_distance(x, y, 2)
case "linf":
return lpnorm_distance(x, y, float("inf"))
case "l1":
return lpnorm_distance(x, y, 1)
case "lpips-l2":
return lpips_distance(x, y, "l2") # semi_metric stable version
case "lpips-l1":
return lpips_distance(x, y, "l1") # semi-metric
case "lpips-linf":
return lpips_distance(x, y, "linf") # semi-metric
case _:
raise NotImplementedError(
"get_lpnorms:none of the cases did match",
)