-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconformal_knowledge.py
282 lines (254 loc) · 12.6 KB
/
conformal_knowledge.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
import argparse
from used_knowledge.used_knowledge import get_knowledge
import numpy as np
import pandas as pd
import os.path
from os import path
from dataset.dataset_conformal import GetDataset
import random
import torch
import sys
from arc.classification import ProbabilityAccumulator as ProbAccum
from dataset.dataset import DataMain
from model.model import NEURAL
from model.model_single import NEURAL_single
from tqdm import tqdm
from scipy.stats.mstats import mquantiles
import arc
from conformal_attack import conformal_attack
from conformal_attack import conformal_attack_knowledge
def conformal_knowledge(args):
print(f'Loading dataset')
if args.dataset == 'GTSRB':
data = DataMain(batch_size=args.batch_size)
data.data_set_up(istrain=False)
data.greeting()
num_label, num_attribute, num_channel = 12, 13, 3
mappings_label, mappings_attribute = get_knowledge(args.dataset)
print(f'Loading models')
model_list_label = []
model_list_attribute = []
for i in range(num_label):
model = NEURAL(n_class=1, n_channel=num_channel)
if os.path.exists("pretrained_models/hier/model_%d_%.2f_5.pt" % (i, args.sigma)):
model.load_state_dict(torch.load("pretrained_models/hier/model_%d_%.2f_5.pt" % (i, args.sigma)))
else:
model.load_state_dict(torch.load("pretrained_models/hier/model_%d_%.2f.pt" % (i, args.sigma)))
model = model.cuda()
model.eval()
model_list_label.append(model)
for i in range(num_attribute):
model = NEURAL(n_class=1, n_channel=num_channel)
if os.path.exists("pretrained_models/attr/model_%d_%.2f_5.pt" % (i, args.sigma)):
model.load_state_dict(torch.load("pretrained_models/attr/model_%d_%.2f_5.pt" % (i, args.sigma)))
else:
model.load_state_dict(torch.load("pretrained_models/attr/model_%d_%.2f.pt" % (i, args.sigma)))
model = model.cuda()
model.eval()
model_list_attribute.append(model)
else:
print(f'Dataset {args.dataset} is not implemented!')
sys.exit(1)
# Unioun bound
args.alpha = args.alpha / (num_label + num_attribute)
if args.method_conformal == 'split_conformal':
if args.calibrate and not args.inference:
alpha_calibrated_label = []
alpha_calibrated_attribute = []
# calibrate label sensors
for i in tqdm(range(num_label)):
model = model_list_label[i]
label_mapping = mappings_label[i]
cur = 0
y_hat = torch.zeros((len(data.y_val), 2)).cuda()
label = torch.zeros((len(data.y_val))).cuda()
X, GT = data.sequential_val_batch()
while X is not None:
X = X.cuda() # to(device)
# X = X + torch.randn_like(X).cuda() * sigma
GT = torch.from_numpy(label_mapping[GT.numpy()]).cuda()
Y = model(X)
this_batch_size = len(Y)
Y = Y.sigmoid()
for ii in range(Y.shape[0]):
sum_ = Y[ii].sum()
Y[ii] /= sum_
y_hat[cur:cur + this_batch_size, :] = Y
label[cur:cur + this_batch_size] = GT
cur = cur + this_batch_size
X, GT = data.sequential_val_batch()
y_hat = y_hat.detach().cpu()
label = label.cpu()
n2 = y_hat.shape[0]
grey_box = ProbAccum(y_hat)
rng = np.random.default_rng(args.seed)
epsilon = rng.uniform(low=0.0, high=1.0, size=n2)
label = label.int()
alpha_max = grey_box.calibrate_scores(label, epsilon=epsilon)
scores = args.alpha - alpha_max
level_adjusted = (1.0 - args.alpha) * (1.0 + 1.0 / float(n2))
alpha_correction = mquantiles(scores, prob=level_adjusted)
alpha_calibrated_ = args.alpha - alpha_correction
alpha_calibrated_label.append(alpha_calibrated_[0])
# calibrate attribute sensors
for i in tqdm(range(num_attribute)):
model = model_list_attribute[i]
label_mapping = mappings_attribute[i]
cur = 0
y_hat = torch.zeros((len(data.y_val), 2)).cuda()
label = torch.zeros((len(data.y_val))).cuda()
X, GT = data.sequential_val_batch()
while X is not None:
X = X.cuda() # to(device)
# X = X + torch.randn_like(X).cuda() * sigma
GT = torch.from_numpy(label_mapping[GT.numpy()]).cuda()
Y = model(X)
this_batch_size = len(Y)
Y = Y.sigmoid()
for ii in range(Y.shape[0]):
sum_ = Y[ii].sum()
Y[ii] /= sum_
y_hat[cur:cur + this_batch_size, :] = Y
label[cur:cur + this_batch_size] = GT
cur = cur + this_batch_size
X, GT = data.sequential_val_batch()
y_hat = y_hat.detach().cpu()
label = label.cpu()
n2 = y_hat.shape[0]
grey_box = ProbAccum(y_hat)
rng = np.random.default_rng(args.seed)
epsilon = rng.uniform(low=0.0, high=1.0, size=n2)
label = label.int()
alpha_max = grey_box.calibrate_scores(label, epsilon=epsilon)
scores = args.alpha - alpha_max
level_adjusted = (1.0 - args.alpha) * (1.0 + 1.0 / float(n2))
alpha_correction = mquantiles(scores, prob=level_adjusted)
alpha_calibrated_ = args.alpha - alpha_correction
alpha_calibrated_attribute.append(alpha_calibrated_[0])
print(f'alpha_calibrated_label: {alpha_calibrated_label}')
print(f'alpha_calibrated_attribute: {alpha_calibrated_attribute}')
torch.save(alpha_calibrated_label, 'log/alpha_calibrated_label_knowledge')
print(f'saving alpha_calibrated_label at log/alpha_calibrated_label_knowledge')
torch.save(alpha_calibrated_attribute, 'log/alpha_calibrated_attribute_knowledge')
print(f'saving alpha_calibrated_attribute at log/alpha_calibrated_attribute_knowledge')
elif not args.calibrate and args.inference:
if args.attack_type=='pgd':
print('Performing pgd attack')
X, GT = data.sequential_test_batch()
cnt = 0
while X is not None:
cnt += 1
print(f'pgd attack batch {cnt}')
X = X.cuda()
GT_attr = torch.from_numpy(mappings_attribute[:, GT.numpy()]).transpose(0, 1)
GT = GT.cuda()
GT_attr = GT_attr.cuda()
X = conformal_attack_knowledge(X, GT, model_list_label, GT_attr, model_list_attribute, max_norm=args.max_norm)
torch.save(X, f'log/adv_sample/adv_knowledge_{cnt}')
X, GT = data.sequential_test_batch()
alpha_calibrated_label = torch.load('log/alpha_calibrated_label_knowledge')
alpha_calibrated_attribute = torch.load('log/alpha_calibrated_attribute_knowledge')
size_all = torch.zeros((len(data.y_test)))
size_all = size_all + num_label
label_test = data.y_test
y_hat = {}
for i in range(len(label_test)):
y_hat[i] = list(range(0, num_label))
# conformal inference on label sensors
for i in tqdm(range(num_label)):
model = model_list_label[i]
label_mapping = mappings_label[i]
cur = 0
P_test = torch.zeros((len(data.y_test), 2)).cuda()
X, GT = data.sequential_test_batch()
cnt = 0
while X is not None:
cnt += 1
X = X.cuda() # to(device)
# X = X + torch.randn_like(X).cuda() * sigma
if args.attack_type=='pgd':
X = torch.load(f'log/adv_sample/adv_knowledge_{cnt}')
GT = torch.from_numpy(label_mapping[GT.numpy()]).cuda()
Y = model(X)
this_batch_size = len(Y)
Y = Y.sigmoid()
for ii in range(Y.shape[0]):
sum_ = Y[ii].sum()
Y[ii] /= sum_
P_test[cur:cur + this_batch_size, :] = Y
cur = cur + this_batch_size
X, GT = data.sequential_test_batch()
P_test = P_test.detach().cpu()
rng = np.random.default_rng(args.seed)
epsilon = rng.uniform(low=0.0, high=1.0, size=len(P_test))
grey_box_test = ProbAccum(P_test)
S_hat = grey_box_test.predict_sets(alpha_calibrated_label[i], epsilon=epsilon, allow_empty=False)
for k, l in enumerate(S_hat):
# if size_all[k] == 1:
# continue
if len(l) == 1 and l[0] == 0:
size_all[k] -= 1
y_hat[k].remove(i)
# elif len(l) == 1 and l[0] == 1:
# size_all[k] = 1
# y_hat[k] = [i]
# conformal inference on attribute sensors
for i in tqdm(range(num_attribute)):
model = model_list_attribute[i]
label_mapping = mappings_attribute[i]
cur = 0
P_test = torch.zeros((len(data.y_test), 2)).cuda()
X, GT = data.sequential_test_batch()
cnt = 0
while X is not None:
cnt += 1
X = X.cuda() # to(device)
# X = X + torch.randn_like(X).cuda() * sigma
if args.attack_type=='pgd':
X = torch.load(f'log/adv_sample/adv_knowledge_{cnt}')
GT = torch.from_numpy(label_mapping[GT.numpy()]).cuda()
Y = model(X)
this_batch_size = len(Y)
Y = Y.sigmoid()
for ii in range(Y.shape[0]):
sum_ = Y[ii].sum()
Y[ii] /= sum_
P_test[cur:cur + this_batch_size, :] = Y
cur = cur + this_batch_size
X, GT = data.sequential_test_batch()
P_test = P_test.detach().cpu()
rng = np.random.default_rng(args.seed)
epsilon = rng.uniform(low=0.0, high=1.0, size=len(P_test))
grey_box_test = ProbAccum(P_test)
S_hat = grey_box_test.predict_sets(alpha_calibrated_attribute[i], epsilon=epsilon, allow_empty=False)
if args.knowledge_set_correction:
for k, l in enumerate(S_hat):
# if size_all[k] == 1:
# continue
if len(l) == 1 and l[0] == 0:
for jj in range(len(mappings_attribute[i])):
if mappings_attribute[i][jj] == 1:
if jj in y_hat[k]:
size_all[k] -= 1
y_hat[k].remove(jj)
elif len(l) == 1 and l[0] == 1:
for jj in range(len(mappings_attribute[i])):
if mappings_attribute[i][jj] == 0:
if jj in y_hat[k]:
size_all[k] -= 1
y_hat[k].remove(jj)
# evaluation
avg_size = size_all.sum() / len(size_all)
marginal_coverage = 0
for i in range(len(label_test)):
if label_test[i] in y_hat[i]:
marginal_coverage += 1
marginal_coverage = marginal_coverage / len(label_test)
print(f'marginal coverage under attack {args.attack_type}: {marginal_coverage}')
print(f'avg_size under attack {args.attack_type}: {avg_size}')
else:
print(f'args.calibrate={args.calibrate}, args.inference={args.inference} are not satisfactory!')
sys.exit(1)
else:
print(f'Conformal method {args.method_conformal} is not implemented!')
sys.exit(1)