This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtrain.py
221 lines (171 loc) · 7.9 KB
/
train.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
import torch
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
from metrics import *
# determine device to run network on (runs on gpu if available)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def train(net, data_loader, test_loader, optimizer, criterion, n_epochs, classes=None, verbose=False):
losses = []
for epoch in range(n_epochs):
net.train()
for i, batch in enumerate(data_loader):
imgs, labels = batch
imgs, labels = imgs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = net(imgs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
losses.append(loss.item())
if verbose:
print("[%d/%d][%d/%d] loss = %f" % (epoch, n_epochs, i, len(data_loader), loss.item()))
# evaluate performance on testset at the end of each epoch
print("[%d/%d]" %(epoch, n_epochs))
print("Training:")
train_accuracy = eval_target_net(net, data_loader, classes=classes)
print("Test:")
test_accuracy = eval_target_net(net, test_loader, classes=classes)
#plt.plot(losses)
#plt.show()
return train_accuracy, test_accuracy
def train_attacker(attack_net, shadow, shadow_train, shadow_out, optimizer, criterion, n_epochs, k):
"""
Trains attack model (classifies a sample as in or out of training set) using
shadow model outputs (probabilities for sample class predictions).
The type of shadow model used can vary.
"""
in_predicts=[]
out_predicts=[]
losses = []
if type(shadow) is not Pipeline:
shadow_net=shadow
shadow_net.eval()
for epoch in range(n_epochs):
total = 0
correct = 0
#train_top = np.array([])
#train_top = []
train_top = np.empty((0,2))
out_top = np.empty((0,2))
for i, ((train_imgs, _), (out_imgs, _)) in enumerate(zip(shadow_train, shadow_out)):
if train_imgs.shape[0] != out_imgs.shape[0]:
break
#######out_imgs = torch.randn(out_imgs.shape)
mini_batch_size = train_imgs.shape[0]
if type(shadow) is not Pipeline:
train_imgs, out_imgs = train_imgs.to(device), out_imgs.to(device)
train_posteriors = F.softmax(shadow_net(train_imgs.detach()), dim=1)
out_posteriors = F.softmax(shadow_net(out_imgs.detach()), dim=1)
else:
traininputs= train_imgs.view(train_imgs.shape[0],-1)
outinputs=out_imgs.view(out_imgs.shape[0], -1)
in_preds=shadow.predict_proba(traininputs)
train_posteriors=torch.from_numpy(in_preds).float()
#for p in in_preds:
# in_predicts.append(p.max())
out_preds=shadow.predict_proba(outinputs)
out_posteriors=torch.from_numpy(out_preds).float()
#for p in out_preds:
# out_predicts.append(p.max())
train_sort, _ = torch.sort(train_posteriors, descending=True)
train_top_k = train_sort[:,:k].clone().to(device)
for p in train_top_k:
in_predicts.append((p.max()).item())
out_sort, _ = torch.sort(out_posteriors, descending=True)
out_top_k = out_sort[:,:k].clone().to(device)
for p in out_top_k:
out_predicts.append((p.max()).item())
train_top = np.vstack((train_top,train_top_k[:,:2].cpu().detach().numpy()))
out_top = np.vstack((out_top, out_top_k[:,:2].cpu().detach().numpy()))
train_lbl = torch.ones(mini_batch_size).to(device)
out_lbl = torch.zeros(mini_batch_size).to(device)
optimizer.zero_grad()
train_predictions = torch.squeeze(attack_net(train_top_k))
out_predictions = torch.squeeze(attack_net(out_top_k))
loss_train = criterion(train_predictions, train_lbl)
loss_out = criterion(out_predictions, out_lbl)
loss = (loss_train + loss_out) / 2
if type(shadow) is not Pipeline:
loss.backward()
optimizer.step()
correct += (F.sigmoid(train_predictions)>=0.5).sum().item()
correct += (F.sigmoid(out_predictions)<0.5).sum().item()
total += train_predictions.size(0) + out_predictions.size(0)
print("[%d/%d][%d/%d] loss = %.2f, accuracy = %.2f" % (epoch, n_epochs, i, len(shadow_train), loss.item(), 100 * correct / total))
#Plot distributions for target predictions in training set and out of training set
"""
fig, ax = plt.subplots(2,1)
plt.subplot(2,1,1)
plt.hist(in_predicts, bins='auto')
plt.title('In')
plt.subplot(2,1,2)
plt.hist(out_predicts, bins='auto')
plt.title('Out')
"""
'''
plt.scatter(out_top.T[0,:], out_top.T[1,:], c='b')
plt.scatter(train_top.T[0,:], train_top.T[1,:], c='r')
plt.show()
'''
class softCrossEntropy(torch.nn.Module):
def __init__(self, alpha=0.95):
"""
:param alpha: Strength (0-1) of influence from soft labels in training
"""
super(softCrossEntropy, self).__init__()
self.alpha = alpha
return
def forward(self, inputs, target, true_labels):
"""
:param inputs: predictions
:param target: target (soft) labels
:param true_labels: true (hard) labels
:return: loss
"""
KD_loss = self.alpha*torch.nn.KLDivLoss(size_average=False)(F.log_softmax(inputs, dim=1),
F.softmax(target, dim=1))
+ (1-self.alpha)*F.cross_entropy(inputs, true_labels)
return KD_loss
def distill_training(teacher, learner, data_loader, test_loader, optimizer,
criterion, n_epochs, verbose=False):
"""
:param teacher: network to provide soft labels in training
:param learner: network to distill knowledge into
:param data_loader: data loader for training data set
:param test_loaderL data loader for validation data
:param optimizer: optimizer for training
:param criterion: objective function, should allow for soft labels.
We suggest softCrossEntropy
:param n_epochs: epochs for training
:param verbose: verbose == True will print loss at each batch
:return: None, teacher model is trained in place
"""
losses = []
for epoch in range(n_epochs):
teacher.eval()
learner.train()
for i, batch in enumerate(data_loader):
with torch.set_grad_enabled(False):
imgs, labels = batch
imgs, labels = imgs.to(device), labels.to(device)
soft_lables = teacher(imgs)
with torch.set_grad_enabled(True):
optimizer.zero_grad()
outputs = learner(imgs)
loss = criterion(outputs, soft_lables, labels)
loss.backward()
optimizer.step()
losses.append(loss.item())
if verbose:
print("[%d/%d][%d/%d] loss = %f" % (epoch, n_epochs, i,
len(data_loader),
loss.item()))
# evaluate performance on testset at the end of each epoch
print("[%d/%d]" %(epoch, n_epochs))
print("Training:")
eval_target_net(learner, data_loader, classes=None)
print("Test:")
eval_target_net(learner, test_loader, classes=None)
# plt.plot(losses)
# plt.show()