-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathensemble_defense.py
457 lines (410 loc) · 19.9 KB
/
ensemble_defense.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import torch
import time
import shutil
import torch.nn as nn
import resnet
from datasets import get_train_test_dataset
import attacks
import lib.adversary as adversary
import numpy as np
true_label = []
model1_prob = []
model2_prob = []
model3_prob = []
model4_prob = []
total_prob = []
def ensemble_analysis(y, prob1, prob2, prob3, prob4, tot):
# print("Outside :", len(y), len(prob1), len(prob2), len(prob3),len(prob4),len(tot))
for i in range(len(y)):
# print("Inside: ", len(y[i]), len(prob1[i]), len(prob2[i]), len(prob3[i]), len(prob4[i]), len(tot[i]))
# if y[i] != np.argmax(prob1[i]) or y[i] != np.argmax(prob2[i]) or y[i] != np.argmax(prob3[i]) or \
# y[i] != np.argmax(prob4[i]):
true_label.append(y[i])
model1_prob.append(prob1[i])
model2_prob.append(prob2[i])
model3_prob.append(prob3[i])
model4_prob.append(prob4[i])
total_prob.append(tot[i])
def get_ensemble_pred_batches(x, y, model1, model2, model3, model4): #, model3, model4, model5, model6
correct = 0
correct_idx = []
with torch.no_grad():
images, labels = x.cuda(), y.cuda()
# o_base = base_model(images)
pred_prob1 = model1(images)
pred_prob2 = model2(images)
pred_prob3 = model3(images)
pred_prob4 = model4(images)
# pred_prob5 = model5(images)
# pred_prob6 = model6(images)
# true_label.append(labels.cpu().numpy())
# model1_prob.append(pred_prob1.cpu().numpy())
# model2_prob.append(pred_prob2.cpu().numpy())
# model3_prob.append(pred_prob3.cpu().numpy())
# model4_prob.append(pred_prob4.cpu().numpy())
final_pred = pred_prob1 + pred_prob2 + pred_prob3 + pred_prob4
# total_prob.append(final_pred.cpu().numpy())
# + pred_prob3 + pred_prob4 + pred_prob5 + pred_prob6
# ensemble_analysis(labels.cpu().numpy(), pred_prob1.cpu().numpy(), pred_prob2.cpu().numpy()
# , pred_prob3.cpu().numpy(), pred_prob4.cpu().numpy(), final_pred.cpu().numpy())
predicted = torch.max(final_pred.data, 1)[1]
correct += (predicted == labels).sum().item()
for i in range(len(labels)):
if predicted[i] == labels[i]:
correct_idx.append(i)
return correct_idx
def get_correct_pred_batchs(x, y, model):
correct = 0
correct_idx = []
with torch.no_grad():
images, labels = x.cuda(), y.cuda()
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
correct += (predicted == labels).sum().item()
for i in range(len(labels)):
if predicted[i] == labels[i]:
correct_idx.append(i)
return correct_idx
def run_ens_fgsm_attack(test_loader, model, model1, model2, model3, model4): # , model3, model4, model5, model6
start = time.time()
total = 0
correct = 0
adv_examples = []
print_freq = 10
df = attacks.FGSM()
for i, (images, labels) in enumerate(test_loader):
images = images.cuda()
labels = labels.cuda()
correctly_predicted_ids = get_correct_pred_batchs(images, labels, model)
if len(correctly_predicted_ids)>0:
total += len(correctly_predicted_ids)
images = [images[i] for i in correctly_predicted_ids]
images = torch.stack(images)# .squeeze()
labels = [labels[i] for i in correctly_predicted_ids]
labels = torch.stack(labels)# .squeeze()
# print(images.size())
adv_data = df(model, images.data.clone(), labels.cpu(), 0.02)
correct_ids = get_ensemble_pred_batches(adv_data, labels, model1, model2, model3, model4)
correct += len(correct_ids)
# adv_outputs = model(adv_data)
# _, adv_pred = torch.max(adv_outputs.data, 1)
# # print(adv_pred)
# for k in range(len(adv_pred)):
# if adv_pred[k].item() != labels[k].item():
# clean_ex = images[k].squeeze().detach().cpu().numpy()
# adv_examples.append((labels[k].item(), labels[k].item(), clean_ex))
# adv_ex = adv_data[k].squeeze().detach().cpu().numpy()
# adv_examples.append((labels[k].item(), adv_pred[k].item(), adv_ex))
if i % print_freq == 0:
print("Correct vs Total = {:.2f}/{:.2f} = {:.2f}".format(correct, total, correct / total))
print('Accuracy of the network on the {} test images where {} can withstand attack: {:.2f} %'
.format(total, correct, 100 * correct / total))
end = time.time()
print("Execution time: ", end - start)
return adv_examples
def run_ens_bim_attack(test_loader, model, model1, model2, model3, model4 # , model3, model4, model5, model6
, num_classes):
start = time.time()
total = 0
correct = 0
adv_examples = []
print_freq = 10
df = attacks.BIM()
for i, (images, labels) in enumerate(test_loader):
images = images.cuda()
labels = labels.cuda()
correctly_predicted_ids = get_correct_pred_batchs(images, labels, model)
if len(correctly_predicted_ids)>0:
total += len(correctly_predicted_ids)
images = [images[i] for i in correctly_predicted_ids]
images = torch.stack(images)# .squeeze()
labels = [labels[i] for i in correctly_predicted_ids]
labels = torch.stack(labels)# .squeeze()
# print(images.size())
adv_data = df(model, images.data.clone(), labels.cpu(), num_classes)
correct_ids = get_ensemble_pred_batches(adv_data, labels, model1, model2, model3, model4)
correct += len(correct_ids)
adv_outputs = model(adv_data)
_, adv_pred = torch.max(adv_outputs.data, 1)
# print(adv_pred)
for k in range(len(adv_pred)):
if adv_pred[k].item() != labels[k].item():
clean_ex = images[k].squeeze().detach().cpu().numpy()
adv_examples.append((labels[k].item(), labels[k].item(), clean_ex))
adv_ex = adv_data[k].squeeze().detach().cpu().numpy()
adv_examples.append((labels[k].item(), adv_pred[k].item(), adv_ex))
if i % print_freq == 0:
print("Correct vs Total = {:.2f}/{:.2f} = {:.2f}".format(correct, total, correct / total))
print('Accuracy of the network on the {} test images where {} can withstand attack: {:.2f} %'
.format(total, correct, 100 * correct / total))
end = time.time()
print("Execution time: ", end - start)
return adv_examples
def run_ens_illc_attack(test_loader, model, model1, model2, model3, model4 # , model3, model4, model5, model6
, num_classes):
start = time.time()
total = 0
correct = 0
adv_examples = []
print_freq = 10
df = attacks.ILLC()
for i, (images, labels) in enumerate(test_loader):
images = images.cuda()
labels = labels.cuda()
correctly_predicted_ids = get_correct_pred_batchs(images, labels, model)
if len(correctly_predicted_ids)>0:
total += len(correctly_predicted_ids)
images = [images[i] for i in correctly_predicted_ids]
images = torch.stack(images)# .squeeze()
labels = [labels[i] for i in correctly_predicted_ids]
labels = torch.stack(labels)# .squeeze()
# print(images.size())
adv_data = df(model, images.data.clone(), labels.cpu(), num_classes)
correct_ids = get_ensemble_pred_batches(adv_data, labels, model1, model2, model3, model4)
correct += len(correct_ids)
adv_outputs = model(adv_data)
_, adv_pred = torch.max(adv_outputs.data, 1)
# print(adv_pred)
for k in range(len(adv_pred)):
if adv_pred[k].item() != labels[k].item():
clean_ex = images[k].squeeze().detach().cpu().numpy()
adv_examples.append((labels[k].item(), labels[k].item(), clean_ex))
adv_ex = adv_data[k].squeeze().detach().cpu().numpy()
adv_examples.append((labels[k].item(), adv_pred[k].item(), adv_ex))
if i % print_freq == 0:
print("Correct vs Total = {:.2f}/{:.2f} = {:.2f}".format(correct, total, correct / total))
print('Accuracy of the network on the {} test images where {} can withstand attack: {:.2f} %'
.format(total, correct, 100 * correct / total))
end = time.time()
print("Execution time: ", end - start)
return adv_examples
def run_ens_deepfool_attack(test_loader, model, model1, model2, model3, model4 # , model3, model4, model5, model6
, num_classes):
start = time.time()
model.eval()
total = 0
correct = 0
adv_examples = []
# perts = 0
print_freq = 5
# df = attacks.DeepFool()
for i, (images, labels) in enumerate(test_loader):
images = images.cuda()
labels = labels.cuda()
correctly_predicted_ids = get_correct_pred_batchs(images, labels, model)
if len(correctly_predicted_ids)>0:
total += len(correctly_predicted_ids)
# adv_data = df(model, images.data.clone(), labels.cpu(), correctly_predicted_ids,
# num_classes=num_classes, max_iter=10)
images = [images[i] for i in correctly_predicted_ids]
images = torch.stack(images) # .squeeze()
labels = [labels[i] for i in correctly_predicted_ids]
labels = torch.stack(labels) # .squeeze()
adv_data = adversary.deepfool(model,images.data.clone(), labels.data.cpu(), num_classes, train_mode=False)
# labels = [labels[i] for i in correctly_predicted_ids]
# labels = torch.stack(labels)# .squeeze()
correct_ids = get_ensemble_pred_batches(adv_data, labels, model1, model2, model3, model4)
correct += len(correct_ids)
# adv_data = adv_data.cuda()
# adv_outputs = model(adv_data)
# _, adv_pred = torch.max(adv_outputs.data, 1)
# # print(adv_pred)
# for k in range(len(adv_pred)):
# if adv_pred[k].item() != labels[k].item():
# clean_ex = images[k].squeeze().detach().cpu().numpy()
# adv_examples.append((labels[k].item(), labels[k].item(), clean_ex))
# adv_ex = adv_data[k].squeeze().detach().cpu().numpy()
# adv_examples.append((labels[k].item(), adv_pred[k].item(), adv_ex))
if i % print_freq == 0:
print("Correct vs Total = {:.2f}/{:.2f} = {:.2f}".format(correct, total, correct / total))
print('Accuracy of the network on the {} test images where {} can withstand attack: {:.2f} %'
.format(total, correct, 100 * correct / total))
end = time.time()
print("Execution time: ", end - start)
return adv_examples
def run_ens_cw_attack1(test_loader, model, model1, model2, model3, model4 # , model3 model4, model5, model6
, num_classes, loss_str='l2'):
start = time.time()
total = 0
correct = 0
adv_examples = []
print_freq = 10
j = 0
for i, (images, labels) in enumerate(test_loader):
images = images.cuda()
labels = labels.cuda()
# correctly_predicted_ids = get_correct_pred_batchs(images, labels, model)
# if len(correctly_predicted_ids)>0:
# total += len(correctly_predicted_ids)
# images = [images[i] for i in correctly_predicted_ids]
# images = torch.stack(images)# .squeeze()
# labels = [labels[i] for i in correctly_predicted_ids]
# labels = torch.stack(labels)# .squeeze()
# print(images.size())
total += labels.size()[0]
adv_data = adversary.cw(model, images.data.clone(), labels.cpu(), 1.0, loss_str, num_classes) # linf
correct_ids = get_ensemble_pred_batches(adv_data, labels, model1, model2, model3, model4)
correct += len(correct_ids)
if i % print_freq == 0:
print("Correct vs Total = {:.2f}/{:.2f} = {:.2f}".format(correct, total, correct / total))
print('Accuracy of the network on the {} test images where {} can withstand attack: {:.2f} %'
.format(total, correct, 100 * correct / total))
end = time.time()
print("Execution time: ", end - start)
return adv_examples
def run_ens_cw_attack(test_loader, model, model1, model2, model3, model4 # , model3 model4, model5, model6
, num_classes, loss_str='l2'):
start = time.time()
total = 0
correct = 0
adv_examples = []
print_freq = 10
j = 0
for i, (images, labels) in enumerate(test_loader):
images = images.cuda()
labels = labels.cuda()
correctly_predicted_ids = get_correct_pred_batchs(images, labels, model)
if len(correctly_predicted_ids)>0:
total += len(correctly_predicted_ids)
images = [images[i] for i in correctly_predicted_ids]
images = torch.stack(images)# .squeeze()
labels = [labels[i] for i in correctly_predicted_ids]
labels = torch.stack(labels)# .squeeze()
# print(images.size())
adv_data = adversary.cw(model, images.data.clone(), labels.cpu(), 1.0, loss_str, num_classes) # linf
correct_ids = get_ensemble_pred_batches(adv_data, labels, model1, model2, model3, model4)
correct += len(correct_ids)
# adv_outputs = model(adv_data)
# _, adv_pred = torch.max(adv_outputs.data, 1)
# # print(adv_pred)
# for k in range(len(adv_pred)):
# if adv_pred[k].item() != labels[k].item():
# clean_ex = images[k].squeeze().detach().cpu().numpy()
# adv_examples.append((labels[k].item(), labels[k].item(), clean_ex))
# adv_ex = adv_data[k].squeeze().detach().cpu().numpy()
# adv_examples.append((labels[k].item(), adv_pred[k].item(), adv_ex))
if i % print_freq == 0:
print("Correct vs Total = {:.2f}/{:.2f} = {:.2f}".format(correct, total, correct / total))
print('Accuracy of the network on the {} test images where {} can withstand attack: {:.2f} %'
.format(total, correct, 100 * correct / total))
end = time.time()
print("Execution time: ", end - start)
return adv_examples
def run_ensemble_test(base_model, model1, model2, model3, model4, test_loader): # , model3, model4, model5, model6
start = time.time()
correct = 0
total = 0
base_model.eval()
model1.eval()
model2.eval()
# model3.eval()
# model4.eval()
# model5.eval()
# model6.eval()
with torch.no_grad():
for data in test_loader:
images, labels = data
images, labels = images.cuda(), labels.cuda()
# o_base = base_model(images)
# # _, p_base = torch.max(o_base.data, 1)
pred_prob1 = model1(images)
pred_prob2 = model2(images)
pred_prob3 = model3(images)
pred_prob4 = model4(images)
# pred_prob5 = model5(images)
# pred_prob6 = model6(images)
final_pred = pred_prob1 + pred_prob2 + pred_prob3 + pred_prob4 # + pred_prob3 + pred_prob4 + pred_prob5 + pred_prob6
predicted = torch.max(final_pred.data, 1)[1]
total += labels.size(0)
correct += (predicted == labels).sum().item()
print("Correct vs Total = {:.2f}/{:.2f} = {:.2f}".format(correct, total, correct / total))
acc = 100 * correct / total
print('Accuracy of the network on the {} test images: {:.2f} %'.format(total,
100 * correct / total))
end = time.time()
print("Execution time: ", end - start)
return acc
def main():
dataloaders, classes = get_train_test_dataset('cifar10', 64, False)
print("Data Loaded successfully")
# Loading Base model
base_res = resnet.ResNet34(10)
base_res.load_state_dict(torch.load('pre_trained/resnet_cifar10.pth'))
base_res.cuda()
# Loading FGSM trained model
# fgsm_res = resnet.ResNet34(10)
# chkpoint = torch.load('saved_models/resnet34_checkpoint_cifar10_fgsm_317.pth.tar')
# fgsm_res.load_state_dict(chkpoint['state_dict'])
# fgsm_res.cuda()
# Loading ILLC trained model
# illc_res = resnet.ResNet34(10)
# chkpoint = torch.load('saved_models/resnet34_checkpoint_cifar10_illc_406.pth.tar')
# illc_res.load_state_dict(chkpoint['state_dict'])
# illc_res.cuda()
# Loading BIM trained model
bim_res = resnet.ResNet34(10)
chkpoint = torch.load('saved_models/resnet34_checkpoint_cifar10_trial.pth.tar')
bim_res.load_state_dict(chkpoint['state_dict'])
bim_res.cuda()
# Loading DeepFool trained model
df_res = resnet.ResNet34(10)
chkpoint = torch.load('saved_models/resnet34_checkpoint_cifar10_df.pth.tar')
df_res.load_state_dict(chkpoint['state_dict'])
df_res.cuda()
# Loading CWL2 trained model
cw_res = resnet.ResNet34(10)
chkpoint = torch.load('saved_models/resnet34_checkpoint_cifar10_cw_317.pth.tar')
cw_res.load_state_dict(chkpoint['state_dict'])
cw_res.cuda()
# Loading CWLinf trained model
cwinf_res = resnet.ResNet34(10)
chkpoint = torch.load('saved_models/resnet34_checkpoint_cifar10_cwinf_406.pth.tar')
cwinf_res.load_state_dict(chkpoint['state_dict'])
cwinf_res.cuda()
a = run_ensemble_test(base_res, cwinf_res, bim_res, df_res, cw_res, dataloaders)
# print()
# print("Evaluating FGSM Attack")
# a = run_ens_fgsm_attack(dataloaders, base_res, cwinf_res, bim_res, df_res, cw_res)
# # illc_res, df_res, cw_res, cwinf_res
# print()
# print("Evaluating BIM Attack")
# a = run_ens_bim_attack(dataloaders, base_res, cwinf_res, bim_res, df_res, cw_res, 10)
# print()
# print("Evaluating ILLC Attack")
# a = run_ens_illc_attack(dataloaders, base_res, cwinf_res, bim_res, df_res, cw_res, 10)
# print()
# print("Evaluating CWL2 Attack")
# a = run_ens_cw_attack1(dataloaders, base_res, cwinf_res, bim_res, df_res, cw_res, 10)
# print()
# print("Evaluating CWLinf Attack")
# a = run_ens_cw_attack(dataloaders, base_res, cwinf_res, bim_res, df_res, cw_res, 10, 'linf')
# print()
# print("Evaluating DeepFool Attack")
# a = run_ens_deepfool_attack(dataloaders, base_res, cwinf_res, bim_res, df_res, cw_res, 10)
# print()
# print(true_label[10][0])
# print()
# print(model1_prob[10][0])
# print()
# print(model2_prob[10][0])
# print()
# print(model3_prob[10][0])
# print()
# print(model4_prob[10][0])
# print()
# print(total_prob[10][0])
true_label_np = np.array(true_label)
model1_prob_np = np.array(model1_prob)
model2_prob_np = np.array(model2_prob)
model3_prob_np = np.array(model3_prob)
model4_prob_np = np.array(model4_prob)
total_prob_np = np.array(total_prob)
# print(true_label_np.shape, model1_prob_np.shape, model2_prob_np.shape, model3_prob_np.shape, model4_prob_np.shape)
np.savetxt('data/true_label.csv',true_label_np, delimiter=',')
np.savetxt('data/model1_prob.csv', model1_prob_np, delimiter=',')
np.savetxt('data/model2_prob.csv', model2_prob_np, delimiter=',')
np.savetxt('data/model3_prob.csv', model3_prob_np, delimiter=',')
np.savetxt('data/model4_prob_np.csv', model4_prob_np, delimiter=',')
np.savetxt('data/total_prob_np.csv', total_prob_np, delimiter=',')
if __name__ == "__main__":
main()