-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAttackAdvRobustTest.py
More file actions
150 lines (133 loc) · 5.83 KB
/
AttackAdvRobustTest.py
File metadata and controls
150 lines (133 loc) · 5.83 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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project : AdvRobust
@File : AttackAdvRobustTest.py
@Author : igeng
@Date : 2022/3/18 16:54
@Descrip :
'''
from __future__ import print_function
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import os
import argparse
import tqdm
import random
import numpy as np
from models import *
from attacks import *
import torchattacks
from robustbench.utils import load_model
from robustbench.model_zoo.cifar10 import linf
def attack_test(adversary, testloader, args, net):
net.eval()
adv_correct = 0
total = 0
batch_total = len(testloader)
for batch_step, (imgs, labels) in enumerate(testloader):
imgs, labels = imgs.to(args.device), labels.to(args.device)
total += labels.size(0)
adv_examples = adversary.perturb(imgs, labels)
adv_outputs = net(adv_examples)
_, predicted = adv_outputs.max(1)
# _, predicted = torch.max(adv_outputs, 1)
adv_correct += predicted.eq(labels).sum().item()
acc = 100 * adv_correct / total
print('Progress : {:.2f}% / Accuracy : {:.2f}%'.format(
(batch_step + 1) / batch_total * 100, acc), end='\r'
)
print('Model: {} is attacked by {}. The predict accuracy is {}.'.format(args.model, args.attack, acc))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Adversarial Attack Test')
parser.add_argument('--seed', default=199592, type=int)
parser.add_argument('--attack', default='PGD', type=str)
parser.add_argument('--model', default='Carmon2019Unlabeled', type=str)
parser.add_argument('--batch_size', default=100, type=int)
parser.add_argument('--iteration', default=100, type=int)
parser.add_argument('--momentum_decay', default=0.9, type=float)
parser.add_argument('--random_start', default=True, type=bool)
parser.add_argument('--norm_ord', default='Linf', type=str)
parser.add_argument('--eps_division', default=1e-10, type=float)
parser.add_argument('--attack_targeted', default=False, type=bool)
parser.add_argument('--decay', default=0.01, type=float)
# FGSM attack setting.
parser.add_argument('--fgsm_epsilon', default=2.0 / 255, type=float)
# PGD attack setting.
parser.add_argument('--pgd_epsilon', default=8.0 / 255, type=float)
parser.add_argument('--pgd_eps_step', default=2.0 / 255, type=float)
parser.add_argument('--pgd_n_steps', default=40, type=int)
# BIM(I-FGSM) attack setting.
parser.add_argument('--bim_epsilon', default=4.0/255, type=float)
parser.add_argument('--bim_eps_iter', default=1.0 / 255, type=float)
parser.add_argument('--bim_n_iters', default=10, type=int)
# MI-FGSM attack setting.
parser.add_argument('--mim_epsilon', default=8.0 / 255, type=float)
parser.add_argument('--mim_eps_iter', default=2.0 / 255, type=float)
parser.add_argument('--mim_n_iters', default=5, type=int)
# CW attack setting.
parser.add_argument('--cw_c', default=1e+100, type=float)
parser.add_argument('--cw_k', default=-10000.0, type=float)
parser.add_argument('--cw_n_iters', default=1000, type=int)
parser.add_argument('--cw_lr', default=0.0001, type=float)
parser.add_argument('--cw_binary_search_steps', default=9, type=int)
args = parser.parse_args()
args.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('Arguments for attack:')
print(args)
# Set random seed.
seed = args.seed
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
print('Preparing attack data!')
transforms_test = transforms.Compose([transforms.ToTensor()])
test_set = torchvision.datasets.CIFAR10(root='./datasets', train=False, download=True, transform=transforms_test)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=args.batch_size, shuffle=False, num_workers=4)
# model_list contains all cifar10-Linf models.
model_list = []
for model, _ in linf.items():
print('Loading {} from pre_models in RobustBench!'.format(model))
net = load_model(model_name = model, model_dir='./per_comparison/models',dataset='cifar10', threat_model='Linf').to("cuda")
net = torch.nn.DataParallel(net)
args.model = model
for i in range(1, 2):
adversary = Attack(net, args)
if i == 0:
print("####### AdvRobust FGSM attack #######")
adversary = FGSM(net, args)
elif i == 1:
print("####### AdvRobust PGD attack #######")
adversary = PGD(net, args)
elif i == 2:
print("####### AdvRobust PGDL2 attack #######")
adversary = PGDL2(net, args)
elif i == 3:
print("####### AdvRobust BIM attack #######")
adversary = BIM(net, args)
elif i == 4:
print("####### AdvRobust MIM attack #######")
adversary = MIM(net, args)
elif i == 5:
print("####### AdvRobust CW attack #######")
adversary = CW(net, args)
elif i == 6:
print("####### AdvRobust CWL2 attack #######")
adversary = CWL2(net, args)
elif i == 7:
print("####### AdvRobust WRNM_PGD_Vanila attack #######")
adversary = WRNM_PGD_Vanila(net, args)
elif i == 8:
print("####### AdvRobust WRNM_PGD_LTG attack #######")
adversary = WRNM_PGD_LTG(net, args)
else:
print("No attack is running, bye!")
break
attack_test(adversary, test_loader, args, net)