-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtrain_BSS_distillation.py
208 lines (165 loc) · 7.33 KB
/
train_BSS_distillation.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
'''Train CIFAR10 with PyTorch.'''
from __future__ import print_function
import os
import time
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import torch.nn.functional as F
import attacks
from models import *
# Parameters
dataset_name = 'CIFAR-10'
res_folder = 'results/BSS_distillation_80epoch_res8_C10'
temperature = 3
gpu_num = 0
attack_size = 64
max_epoch = 80
if not os.path.isdir(res_folder):
os.mkdir(res_folder)
use_cuda = torch.cuda.is_available()
# Dataset
if dataset_name is 'CIFAR-10':
# CIFAR-10
print('==> Preparing data..')
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=256, shuffle=True, num_workers=4)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=4)
else:
raise Exception('Undefined Dataset')
# Teacher network
teacher = BN_version_fix(torch.load('./results/Res26_C10/320_epoch.t7', map_location=lambda storage, loc: storage.cuda(0))['net'])
t_net = ResNet26()
t_net.load_state_dict(teacher.state_dict())
# Student network
s_net = ResNet8()
if use_cuda:
torch.cuda.set_device(gpu_num)
t_net.cuda()
s_net.cuda()
cudnn.benchmark = True
# Proposed adversarial attack algorithm (BSS)
attack = attacks.AttackBSS(targeted=True, num_steps=10, max_epsilon=16, step_alpha=0.3, cuda=True, norm=2)
criterion_MSE = nn.MSELoss(size_average=False)
criterion_CE = nn.CrossEntropyLoss()
# Training
def train_attack_KD(t_net, s_net, ratio, ratio_attack, epoch):
epoch_start_time = time.time()
print('\nStage 1 Epoch: %d' % epoch)
s_net.train()
t_net.eval()
train_loss = 0
correct = 0
total = 0
global optimizer
for batch_idx, (inputs, targets) in enumerate(trainloader):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
batch_size1 = inputs.shape[0]
optimizer.zero_grad()
inputs, targets = Variable(inputs), Variable(targets)
out_s = s_net(inputs)
# Cross-entropy loss
loss = criterion_CE(out_s[0:batch_size1, :], targets)
out_t = t_net(inputs)
# KD loss
loss += - ratio * (F.softmax(out_t/temperature, 1).detach() * F.log_softmax(out_s/temperature, 1)).sum() / batch_size1
if ratio_attack > 0:
condition1 = targets.data == out_t.sort(dim=1, descending=True)[1][:, 0].data
condition2 = targets.data == out_s.sort(dim=1, descending=True)[1][:, 0].data
attack_flag = condition1 & condition2
if attack_flag.sum():
# Base sample selection
attack_idx = attack_flag.nonzero().squeeze()
if attack_idx.shape[0] > attack_size:
diff = (F.softmax(out_t[attack_idx,:], 1).data - F.softmax(out_s[attack_idx,:], 1).data) ** 2
distill_score = diff.sum(dim=1) - diff.gather(1, targets[attack_idx].data.unsqueeze(1)).squeeze()
attack_idx = attack_idx[distill_score.sort(descending=True)[1][:attack_size]]
# Target class sampling
attack_class = out_t.sort(dim=1, descending=True)[1][:, 1][attack_idx].data
class_score, class_idx = F.softmax(out_t, 1)[attack_idx, :].data.sort(dim=1, descending=True)
class_score = class_score[:, 1:]
class_idx = class_idx[:, 1:]
rand_seed = 1 * (class_score.sum(dim=1) * torch.rand([attack_idx.shape[0]]).cuda()).unsqueeze(1)
prob = class_score.cumsum(dim=1)
for k in range(attack_idx.shape[0]):
for c in range(prob.shape[1]):
if (prob[k, c] >= rand_seed[k]).cpu().numpy():
attack_class[k] = class_idx[k, c]
break
# Forward and backward for adversarial samples
attacked_inputs = Variable(attack.run(t_net, inputs[attack_idx, :, :, :].data, attack_class))
batch_size2 = attacked_inputs.shape[0]
attack_out_t = t_net(attacked_inputs)
attack_out_s = s_net(attacked_inputs)
# KD loss for Boundary Supporting Samples (BSS)
loss += - ratio_attack * (F.softmax(attack_out_t / temperature, 1).detach() * F.log_softmax(attack_out_s / temperature, 1)).sum() / batch_size2
loss.backward()
optimizer.step()
train_loss += loss.data.item()
_, predicted = torch.max(out_s[0:batch_size1, :].data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().float().sum()
b_idx = batch_idx
print('Train \t Time Taken: %.2f sec' % (time.time() - epoch_start_time))
print('Loss: %.3f | Acc: %.3f%% (%d/%d)' % (train_loss / (b_idx + 1), 100. * correct / total, correct, total))
def test(net, epoch, save=False):
epoch_start_time = time.time()
net.eval()
test_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(testloader):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = Variable(inputs), Variable(targets)
outputs = net(inputs)
loss = criterion_CE(outputs, targets)
test_loss += loss.data.item()
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().float().sum()
b_idx= batch_idx
print('Test \t Time Taken: %.2f sec' % (time.time() - epoch_start_time))
print('Loss: %.3f | Acc: %.3f%% (%d/%d)' % (test_loss/(b_idx+1), 100.*correct/total, correct, total))
if save:
# Save checkpoint.
acc = 100.*correct/total
if epoch is not 0 and epoch % 80 is 0:
print('Saving..')
state = {
'net': net if use_cuda else net,
'acc': acc,
'epoch': epoch,
}
torch.save(state, './' + res_folder + '/%d_epoch.t7' % epoch)
for epoch in range(1, max_epoch+1):
if epoch == 1:
optimizer = optim.SGD(s_net.parameters(), lr=0.1, momentum=0.9, weight_decay=1e-4)
elif epoch == max_epoch/2:
optimizer = optim.SGD(s_net.parameters(), lr=0.01, momentum=0.9, weight_decay=1e-4)
elif epoch == max_epoch/4*3:
optimizer = optim.SGD(s_net.parameters(), lr=0.001, momentum=0.9, weight_decay=1e-4)
ratio = max(3 * (1 - epoch / max_epoch), 0) + 1
attack_ratio = max(2 * (1 - 4 / 3 * epoch / max_epoch), 0) + 0
train_attack_KD(t_net, s_net, ratio, attack_ratio, epoch)
test(s_net, epoch, save=True)
state = {
'net': s_net,
'epoch': max_epoch,
}
torch.save(state, './' + res_folder + '/%depoch_final.t7' % (max_epoch))