-
Notifications
You must be signed in to change notification settings - Fork 0
/
adv_train_online.py
252 lines (217 loc) · 8.92 KB
/
adv_train_online.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
import argparse
from copy import deepcopy
import torchattacks
import os
import time
import torchvision
import torch.nn as nn
import torch
from model import net_module
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def setup_logging():
save_path = os.path.join(args.experiment, time.strftime("%Y_%m_%d_%H_%M_%S"))
os.makedirs(save_path)
ckp_path = os.path.join(save_path, 'ckp')
os.mkdir(ckp_path)
args.ckp_path = ckp_path
args.save_path = save_path
with open(os.path.join(save_path, 'records_batch.csv'), 'w') as f:
f.write('Epoch,Batch,Time,Time_sum,Loss,Loss_avg\n')
with open(os.path.join(save_path, 'args.txt'), 'w') as f:
f.write(str(args))
with open(os.path.join(save_path, 'records_val.csv'), 'w') as f:
f.write('Epoch,Loss\n')
def save_checkpoint(state, filename='checkpoint.pth.tar'):
torch.save(state, filename)
def cuda(model):
if torch.cuda.is_available():
model = model.cuda()
device_num = torch.cuda.device_count()
print('you have %d available GPU' % (device_num))
if device_num > 1:
device_ids = [x for x in range(device_num)]
model = torch.nn.DataParallel(model, device_ids=device_ids)
args.batch_size *= device_num
return model
def attack_method(method,model):
if method == 'fgsm':
attack = torchattacks.FGSM(model,eps=args.eps)
return attack
def load(model):
if args.load:
model.load_state_dict(torch.load(args.load)['state_dict'])
print('Model loaded from {}'.format(args.load))
def train(model, train_data_loader, optimizer, criterion, epoch):
model.train()
batch_time = AverageMeter()
losses = AverageMeter()
end = time.time()
for i, data in enumerate(train_data_loader):
image, label = data
image = image.cuda()
label = label.cuda()
optimizer.zero_grad()
output = model(image)
loss = criterion(output, label)
losses.update(loss.item(), image.size(0))
loss.backward()
optimizer.step()
batch_time.update(time.time() - end)
end = time.time()
print('%d,%d/%d,%f' % (epoch, i, len(train_data_loader), loss.item()))
with open(os.path.join(args.save_path, 'records_batch.csv'), 'a') as f:
f.write('%d,%d/%d,%f,%f,%f,%f\n' % (
epoch, i, len(train_data_loader), batch_time.val, batch_time.sum, losses.val, losses.avg))
def train_adv_exmp(model, train_data_loader, optimizer, criterion, dist_criterion, epoch):
model.train()
batch_time = AverageMeter()
losses = AverageMeter()
end = time.time()
for i, data in enumerate(train_data_loader):
image, label = data
image = image.cuda()
label = label.cuda()
attack = attack_method(args.attack_method, model)
adv_image = attack(image, label)
nag_model = deepcopy(model)
optimizer.zero_grad()
output_adv, fea = model(adv_image)
output, nag_fea = nag_model(image)
entropy_loss = criterion(output_adv, label)
dist_loss = dist_criterion(output, output_adv, torch.tensor(-1).float().cuda())
loss = entropy_loss + dist_loss
losses.update(loss.item(), image.size(0))
loss.backward()
optimizer.step()
batch_time.update(time.time() - end)
end = time.time()
print('%d,%d/%d,%f' % (epoch, i, len(train_data_loader), loss.item()))
with open(os.path.join(args.save_path, 'records_adv_batch.csv'), 'a') as f:
f.write('%d,%d/%d,%f,%f,%f,%f\n' % (
epoch, i, len(train_data_loader), batch_time.val, batch_time.sum, losses.val, losses.avg))
def test_adv_exmp(model, test_data_loader, epoch):
model.eval()
correct = 0
total = 0
for i, data in enumerate(test_data_loader):
image, label = data
image = image.cuda()
label = label.cuda()
attack = attack_method(args.attack_method, model)
adv_images = attack(image, label)
outputs, fea = model(adv_images)
# the class with the highest energy is what we choose as prediction
_, predicted = torch.max(outputs.data, 1)
total += label.size(0)
correct += (predicted == label).sum().item()
print('%d,%d/%d,%s' % (epoch, i, len(test_data_loader), 'test process'))
acc = correct / total
print('Accuracy of the network on the 10000 test adversarial images: %d %%' % (
100 * correct / total))
with open(os.path.join(args.save_path, './records_adv_val.csv'), 'a') as f:
f.write('%d,%f\n' % (epoch, acc))
return acc
def test(model, test_data_loader, epoch):
model.eval()
correct = 0
total = 0
for i, data in enumerate(test_data_loader):
image, label = data
image = image.cuda()
label = label.cuda()
outputs, fea = model(image)
# the class with the highest energy is what we choose as prediction
_, predicted = torch.max(outputs.data, 1)
total += label.size(0)
correct += (predicted == label).sum().item()
print('%d,%d/%d,%s' % (epoch, i, len(test_data_loader), 'test process'))
acc = correct / total
print('Accuracy of the network on the 10000 test images: %d %%' % (
100 * correct / total))
with open(os.path.join(args.save_path, './records_val.csv'), 'a') as f:
f.write('%d,%f\n' % (epoch, acc))
return acc
def adjust_learning_rate(optimizer, epoch):
"""decrease the learning rate"""
lr = args.lr
if epoch >= 10:
lr = args.lr * 0.1
if epoch >= 15:
lr = args.lr * 0.01
if epoch >= 20:
lr = args.lr * 0.001
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def define_model(net_arch, dataset='ImageNet'):
if net_arch == 'resnet18':
from model import ResNet
if dataset == 'ImageNet':
model = ResNet.resnet18_ImageNet
if dataset == 'CIFAR':
model = ResNet.resnet18_CIFAR
elif net_arch == 'MNIST_Net':
from model import MNIST_Net
model = MNIST_Net.MNIST_net
elif net_arch == 'CIFAR_Net':
from model import CIFAR_Net
model = CIFAR_Net.CIFAR_Net
elif net_arch == 'wideresnet':
from model import wideresnet
model = wideresnet.WideResNet()
return model
def choose_data(dataset):
if 'MNIST' in dataset:
from data_scripts import MNIST
train_data_loader, test_data_loader = MNIST.main(args)
elif 'CIFAR' in dataset:
from data_scripts import CIFAR
train_data_loader, test_data_loader = CIFAR.encapsulate_loader(args)
return train_data_loader, test_data_loader
def main(args):
setup_logging()
model = define_model(args.net_arch,args.dataset)
model = cuda(model)
train_data_loader, test_data_loader = choose_data(args.dataset)
load(model)
optimizer = torch.optim.Adam(model.parameters(), args.lr)
criterion = nn.CrossEntropyLoss()
dist_criterion = nn.CosineEmbeddingLoss(margin=0)
for i in range(args.epoch):
adjust_learning_rate(optimizer, i)
#test(model, test_data_loader, i)
#test_adv_exmp(model, test_data_loader, i)
#train(model, train_data_loader, optimizer, criterion, i)
train_adv_exmp(model, train_data_loader, optimizer, criterion, dist_criterion, i)
if i == args.epoch -1:
test(model, test_data_loader, i)
test_adv_exmp(model, test_data_loader, i)
save_checkpoint({'state_dict': model.state_dict()},
filename=os.path.join(args.ckp_path, '%02dcheckpoint.pth.tar' % i))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='train script')
parser.add_argument('--attack_method', type=str, default='fgsm', choices=['fgsm','deepfool'])
parser.add_argument('--data_root', type=str, default='/home/panmeng/data/')
parser.add_argument('--dataset', type=str, default='CIFAR',choices=['ImageNet','CIFAR','MNIST'])
parser.add_argument('--net_arch', type=str, default='wideresnet', choices=['resnet18', 'mnist_net', 'CIFAR_Net','wideresnet'])
parser.add_argument('--batch_size', type=int, default=64)
parser.add_argument('--num_worker', type=int, default=4)
parser.add_argument('--lr', type=float, default=0.1)
parser.add_argument('--epoch', type=int, default=25)
parser.add_argument('--eps',type=float, default=0.03137255)
parser.add_argument('--load', type=str, default='/home/panmeng/adv_frame/adv_frame/experiments/baseline/ckp/23checkpoint.pth.tar')
parser.add_argument('--experiment', default='./experiments', type=str, help='path of experiments')
args = parser.parse_args()
#os.environ['CUDA_VISIBLE_DEVICES'] ='0,1,2'
main(args)