-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
260 lines (209 loc) · 10.5 KB
/
main.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
############################################################################
### Written by Gaojie Jin and updated by Xiaowei Huang, 2021
###
### For a 2-nd year undergraduate student competition on
### the robustness of deep neural networks, where a student
### needs to develop
### 1. an attack algorithm, and
### 2. an adversarial training algorithm
###
### The score is based on both algorithms.
############################################################################
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
import torch.optim as optim
import torchvision
from torchvision import transforms
from torch.autograd import Variable
import argparse
import time
import copy
# input id
id_ =""
# setup training parameters
parser = argparse.ArgumentParser(description='PyTorch MNIST Training')
parser.add_argument('--batch-size', type=int, default=128, metavar='N',
help='input batch size for training (default: 128)')
parser.add_argument('--test-batch-size', type=int, default=128, metavar='N',
help='input batch size for testing (default: 128)')
parser.add_argument('--epochs', type=int, default=1, metavar='N',
help='number of epochs to train')
parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
help='learning rate')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
args = parser.parse_args(args=[])
# judge cuda is available or not
use_cuda = not args.no_cuda and torch.cuda.is_available()
# device = torch.device("cuda" if use_cuda else "cpu")
device = torch.device("cuda")
torch.manual_seed(args.seed)
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
############################################################################
################ don't change the below code #####################
############################################################################
train_set = torchvision.datasets.FashionMNIST(root='data', train=True, download=True,
transform=transforms.Compose([transforms.ToTensor()]))
train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True)
test_set = torchvision.datasets.FashionMNIST(root='data', train=False, download=True,
transform=transforms.Compose([transforms.ToTensor()]))
test_loader = DataLoader(test_set, batch_size=args.batch_size, shuffle=True)
# define fully connected network
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(28 * 28, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 32)
self.fc4 = nn.Linear(32, 10)
def forward(self, x):
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = F.relu(x)
x = self.fc3(x)
x = F.relu(x)
x = self.fc4(x)
output = F.log_softmax(x, dim=1)
return output
##############################################################################
############# end of "don't change the below code" ######################
##############################################################################
# generate adversarial data, you can define your adversarial method
def adv_attack(model, X, y, device):
X_adv = Variable(X.data)
################################################################################################
## Note: below is the place you need to edit to implement your own attack algorithm
################################################################################################
# random_noise = torch.FloatTensor(*X_adv.shape).uniform_(-0.1, 0.1).to(device)
# X_adv = Variable(X_adv.data + random_noise)
epsilon = 0.1
num_steps = 100
step_size = 0.003
out = model(X)
err = (out.data.max(1)[1] != y.data).float().sum()
X_pgd = Variable(X.data, requires_grad=True)
random_noise = torch.FloatTensor(*X_pgd.shape).uniform_(-epsilon, epsilon).to(device)
X_pgd = Variable(X_pgd.data + random_noise, requires_grad=True)
for _ in range(num_steps):
opt = optim.SGD([X_pgd], lr=1e-3)
opt.zero_grad()
with torch.enable_grad():
loss = nn.CrossEntropyLoss()(model(X_pgd), y)
loss.backward()
eta = step_size * X_pgd.grad.data.sign()
X_pgd = Variable(X_pgd.data + eta, requires_grad=True)
eta = torch.clamp(X_pgd.data - X.data, -epsilon, epsilon)
X_pgd = Variable(X.data + eta, requires_grad=True)
X_pgd = Variable(torch.clamp(X_pgd, 0, 1.0), requires_grad=True)
err_pgd = (model(X_pgd).data.max(1)[1] != y.data).float().sum()
X_adv = X_pgd
################################################################################################
## end of attack method
################################################################################################
return X_adv
# train function, you can use adversarial training
def train(args, model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
data = data.view(data.size(0), 28 * 28)
# use adverserial data to train the defense model
adv_data = adv_attack(model, data, target, device=device)
# clear gradients
optimizer.zero_grad()
# compute loss
loss = F.nll_loss(model(adv_data), target)
# loss = F.nll_loss(model(data), target)
# get gradients and update
loss.backward()
optimizer.step()
# predict function
def eval_test(model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
data = data.view(data.size(0), 28 * 28)
output = model(data)
test_loss += F.nll_loss(output, target, size_average=False).item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
test_accuracy = correct / len(test_loader.dataset)
return test_loss, test_accuracy
def eval_adv_test(model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
data = data.view(data.size(0), 28 * 28)
adv_data = adv_attack(model, data, target, device=device)
output = model(adv_data)
test_loss += F.nll_loss(output, target, size_average=False).item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
test_accuracy = correct / len(test_loader.dataset)
return test_loss, test_accuracy
# main function, train the dataset and print train loss, test loss for each epoch
def train_model():
model = Net().to(device)
################################################################################################
## Note: below is the place you need to edit to implement your own training algorithm
## You can also edit the functions such as train(...).
################################################################################################
optimizer = optim.SGD(model.parameters(), lr=args.lr)
for epoch in range(1, args.epochs + 1):
start_time = time.time()
# training
# train(args, model, device, train_loader, optimizer, epoch)
model.load_state_dict(torch.load('201676262.pt'))
# get trnloss and testloss
trnloss, trnacc = eval_test(model, device, train_loader)
advloss, advacc = eval_adv_test(model, device, train_loader)
# print trnloss and testloss
print('Epoch ' + str(epoch) + ': ' + str(int(time.time() - start_time)) + 's', end=', ')
print('trn_loss: {:.4f}, trn_acc: {:.2f}%'.format(trnloss, 100. * trnacc), end=', ')
print('adv_loss: {:.4f}, adv_acc: {:.2f}%'.format(advloss, 100. * advacc))
adv_tstloss, adv_tstacc = eval_adv_test(model, device, test_loader)
print('Your estimated attack ability, by applying your attack method on your own trained model, is: {:.4f}'.format(
1 / adv_tstacc))
print('Your estimated defence ability, by evaluating your own defence model over your attack, is: {:.4f}'.format(
adv_tstacc))
################################################################################################
## end of training method
################################################################################################
# save the model
# torch.save(model.state_dict(), str(id_) + '.pt')
return model
# compute perturbation distance
def p_distance(model, train_loader, device):
p = []
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
data = data.view(data.size(0), 28 * 28)
data_ = copy.deepcopy(data.data)
adv_data = adv_attack(model, data, target, device=device)
p.append(torch.norm(data_ - adv_data, float('inf')))
print('epsilon p: ', max(p))
################################################################################################
## Note: below is for testing/debugging purpose, please comment them out in the submission file
################################################################################################
# Comment out the following command when you do not want to re-train the model
# In that case, it will load a pre-trained model you saved in train_model()
model = train_model()
# Call adv_attack() method on a pre-trained model'
# the robustness of the model is evaluated against the infinite-norm distance measure
# important: MAKE SURE the infinite-norm distance (epsilon p) less than 0.11 !!!
p_distance(model, train_loader, device)