-
Notifications
You must be signed in to change notification settings - Fork 1
/
use_attacks.py
151 lines (107 loc) · 5.31 KB
/
use_attacks.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
from attacks import *
from tqdm import tqdm
def FGSM_test(net,testloader,optim,criterion,eps,device):
print("\nFGSM Attack")
net.eval()
test_loss,total,total_correct = 0,0,0
iterator = tqdm(testloader)
for inputs, targets in iterator:
inputs, targets = inputs.to(device), targets.to(device)
adv_inputs = FGSM(net,inputs,targets,device,eps,criterion)
outputs,_ = net(adv_inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
total_correct += (predicted == targets).sum().item()
# Save checkpoint when best model
acc = 100. * total_correct / total
print("FGSM Attack Acc Score \tLoss: %.4f Acc@1: %.2f%%" %(test_loss, acc))
def MIFGSM_test(net,testloader,optim,criterion,eps,max_iter,momentum,device):
print("\nMIFGSM Attack")
net.eval()
test_loss,total,total_correct = 0,0,0
iterator = tqdm(testloader)
for inputs, targets in iterator:
inputs, targets = inputs.to(device), targets.to(device)
adv_inputs = MIFGSM(net,inputs,targets,device,eps,max_iter,momentum,criterion)
outputs,_ = net(adv_inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
total_correct += (predicted == targets).sum().item()
# Save checkpoint when best model
acc = 100. * total_correct / total
print("MIFGSM Attack Acc Score \tLoss: %.4f Acc@1: %.2f%%" %(test_loss, acc))
def PGD_test(net,testloader,optim,criterion,eps,attack_steps,step_size,device):
print("\nPGD Attack")
net.eval()
test_loss,total,total_correct = 0,0,0
iterator = tqdm(testloader)
for inputs, targets in iterator:
inputs, targets = inputs.to(device), targets.to(device)
#adv_inputs = PGD(net,inputs,targets,device,eps,attack_steps,step_size,criterion)
adv_inputs = PGD(net,inputs,targets,device,eps,attack_steps,step_size,criterion)
outputs,_ = net(adv_inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
total_correct += (predicted == targets).sum().item()
# Save checkpoint when best model
acc = 100. * total_correct / total
print("PGD Attack Acc Score \tLoss: %.4f Acc@1: %.2f%%" %(test_loss, acc))
def FGSM_train(net,trainloader,optim,criterion,epoch,eps,device):
net.train()
train_loss, total, total_correct = 0,0,0
iterator = tqdm(trainloader)
for inputs,targets in iterator:
net,inputs,targets = net.to(device),inputs.to(device), targets.to(device)
adv_inputs = FGSM(net,inputs,targets,device,eps,criterion)
optim.zero_grad()
outputs,_ = net(adv_inputs)
loss = criterion(outputs,targets)
loss.backward()
optim.step()
train_loss += loss.item()
_,predicted = torch.max(outputs.data,1)
total_correct += (predicted == targets).sum().item()
total += targets.size(0)
print("Epoch: [{}] loss: [{:.2f}] Accuracy [{:.2f}] ".format(epoch+1,train_loss/len(trainloader),
total_correct*100/total))
def PGD_train(net,trainloader,optim,criterion,epoch,eps,attack_steps,step_size,device):
net.train()
train_loss, total, total_correct = 0,0,0
iterator = tqdm(trainloader)
for inputs,targets in iterator:
net,inputs,targets = net.to(device),inputs.to(device), targets.to(device)
adv_inputs = PGD(net,inputs,targets,device,eps,attack_steps,step_size,criterion)
optim.zero_grad()
outputs,_ = net(adv_inputs)
loss = criterion(outputs,targets)
loss.backward()
optim.step()
train_loss += loss.item()
_,predicted = torch.max(outputs.data,1)
total_correct += (predicted == targets).sum().item()
total += targets.size(0)
print("Epoch: [{}] loss: [{:.2f}] Accuracy [{:.2f}] ".format(epoch+1,train_loss/len(trainloader),
total_correct*100/total))
def FeaScatter_train(net,trainloader,optim,criterion,epoch,eps,attack_steps,step_size,device):
net.train()
train_loss, total, total_correct = 0,0,0
iterator = tqdm(trainloader)
for inputs,targets in iterator:
net,inputs,targets = net.to(device),inputs.to(device), targets.to(device)
optim.zero_grad()
outputs,loss_fs = FeatureScatter(net,inputs,targets,device,eps,attack_steps,step_size,criterion)
loss = loss_fs.mean()
loss.backward()
optim.step()
train_loss += loss.item()
_,predicted = torch.max(outputs.data,1)
total_correct += (predicted == targets).sum().item()
total += targets.size(0)
print("Epoch: [{}] loss: [{:.2f}] Accuracy [{:.2f}] ".format(epoch+1,train_loss/len(trainloader),
total_correct*100/total))