forked from mathebell/model-watermarking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
323 lines (241 loc) · 11.8 KB
/
trainer.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'''Provides train and test function'''
import os
import logging
import torch
import numpy as np
from helpers.utils import progress_bar, make_loss_plot, save_obj
from helpers.pytorchtools import EarlyStopping
def train(epoch, net, criterion, optimizer, train_loader, device, avg_train_losses, avg_valid_losses,
valid_loader=False, wmloader=False, tune_all=True):
print('\nEpoch: %d' % epoch)
net.train()
# clear lists to track next epoch
train_losses = []
valid_losses = []
train_acc = 0
valid_acc = 0
train_loss = 0
correct = 0
total = 0
iteration = -1
# update only the last layer
if not tune_all:
if type(net) is torch.nn.DataParallel:
net.module.freeze_hidden_layers()
else:
net.freeze_hidden_layers()
# get the watermark images
wminputs, wmtargets = [], []
if wmloader:
for wm_idx, (wminput, wmtarget) in enumerate(wmloader):
wminput, wmtarget = wminput.to(device), wmtarget.to(device)
wminputs.append(wminput)
wmtargets.append(wmtarget)
# the wm_idx to start from
wm_idx = np.random.randint(len(wminputs))
for batch_idx, (inputs, targets) in enumerate(train_loader):
print('\nBatch: %d' % batch_idx)
iteration += 1
inputs, targets = inputs.to(device), targets.to(device)
# add wmimages and targets
if wmloader:
inputs = torch.cat([inputs, wminputs[(wm_idx + batch_idx) % len(wminputs)]], dim=0)
targets = torch.cat([targets, wmtargets[(wm_idx + batch_idx) % len(wminputs)]], dim=0)
# clear the gradients of all optimized variables
optimizer.zero_grad()
# forward pass: compute predicted outputs by passing inputs to the model
outputs = net(inputs)
# calculate the loss
loss = criterion(outputs, targets)
# backward pass: compute gradient of the loss with respect to model parameters
loss.backward(retain_graph=True)
# perform a single optimization step (parameter update)
optimizer.step()
# record training loss
train_losses.append(loss.item())
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
train_acc = 100. * correct / total
progress_bar(batch_idx, len(train_loader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' %
(np.average(train_losses), train_acc, correct, total))
######################
# validate the model #
######################
if valid_loader:
correct = 0
total = 0
net.eval() # prep model for evaluation
with torch.no_grad():
for inputs, targets in valid_loader:
inputs, targets = inputs.to(device), targets.to(device)
# forward pass: compute predicted outputs by passing inputs to the model
outputs = net(inputs)
# calculate the loss
loss = criterion(outputs, targets)
# record validation loss
valid_losses.append(loss.item())
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
valid_acc = 100. * correct / total
# print training / validation statistics
# calculate average loss over an epoch
train_loss = np.average(train_losses)
valid_loss = np.average(valid_losses)
avg_train_losses.append(train_loss)
avg_valid_losses.append(valid_loss)
logging.info(('Epoch %d: Train loss: %.3f | Valid loss: %.3f | Acc: %.3f%% (%d/%d)'
% (epoch, train_loss, valid_loss, train_acc, correct, total)))
return avg_train_losses, avg_valid_losses, train_acc, valid_acc
def test(net, criterion, loader, device):
net.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(loader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
logging.info('Test results: Loss: %.3f | Acc: %.3f%% (%d/%d)'
% (test_loss / (batch_idx + 1), 100. * correct / total, correct, total))
return 100. * correct / total
def train_wo_wms(epochs, net, criterion, optimizer, scheduler, patience, train_loader, test_loader, valid_loader,
device,
save_dir, save_model, history=dict()):
logging.info("Training model without watermarks.")
avg_train_losses = []
avg_valid_losses = []
test_acc_list = []
best_test_acc, best_epoch = 0, 0
# initialize the early_stopping object
early_stopping = EarlyStopping(patience=int(patience), verbose=True,
path=os.path.join(save_dir, save_model + '.t7'),
trace_func=logging.info)
for epoch in range(epochs):
avg_train_losses, avg_valid_losses, train_acc, valid_acc = train(epoch, net, criterion, optimizer, train_loader,
device,
avg_train_losses, avg_valid_losses,
valid_loader)
logging.info("Testing dataset.")
test_acc = test(net, criterion, test_loader, device)
logging.info("Test acc: %.3f%%" % test_acc)
test_acc_list.append(test_acc)
if avg_valid_losses[-1] < early_stopping.val_loss_min: # bc this model will be saved
best_test_acc = test_acc
best_epoch = epoch
# early_stopping needs the validation loss to check if it has decreased,
# and if it has, it will make a checkpoint of the current model
early_stopping(avg_valid_losses[-1], net)
if early_stopping.early_stop:
logging.info("Early stopping")
break
scheduler.step()
# make_loss_plot(avg_train_losses, avg_valid_losses, save_model)
history['train_losses'] = avg_train_losses
history['valid_losses'] = avg_valid_losses
history['test_acc'] = test_acc_list
history['train_acc'] = train_acc
history['valid_acc'] = valid_acc
# torch.save(torch.tensor([avg_train_losses, avg_valid_losses, test_acc_list]),
# os.path.join('results', save_model + '.t7'))
return best_test_acc, early_stopping.val_loss_min, best_epoch, history
def train_on_wms(epochs, device, net, optimizer, criterion, scheduler, wm_loader, test_loader, save_dir, save_model,
history):
logging.info("Training model only on trigger set.")
avg_train_losses = []
avg_valid_losses = []
test_acc_list = []
wm_acc_list = []
for epoch in range(epochs):
# no valid_loader for wm_loader
avg_train_losses, avg_valid_losses, train_acc, valid_acc = train(epoch, net, criterion, optimizer, wm_loader,
device,
avg_train_losses, avg_valid_losses)
logging.info("Testing dataset.")
test_acc = test(net, criterion, test_loader, device)
logging.info("Test acc: %.3f%%" % test_acc)
test_acc_list.append(test_acc)
logging.info("Testing triggerset (no train, test split).")
wm_acc = test(net, criterion, wm_loader, device)
logging.info("WM acc: %.3f%%" % wm_acc)
wm_acc_list.append(wm_acc)
# save model every 5 epochs # could be variable
if (epoch + 1) % 5 == 0:
durtr_dir = os.path.join(save_dir, save_model + '_duringtraining')
os.makedirs(durtr_dir, exist_ok=True)
torch.save(net.state_dict(), os.path.join(durtr_dir, save_model + 'epoch_' + str(epoch + 1) + '.t7'))
scheduler.step()
logging.info("Saving model.")
torch.save(net.state_dict(), os.path.join(save_dir, save_model + '.t7'))
# history = {'train_losses': avg_train_losses,
# 'valid_losses': avg_valid_losses,
# 'test_acc': test_acc_list,
# 'wm_acc': wm_acc_list}
history['train_losses'] = avg_train_losses
history['valid_losses'] = avg_valid_losses
history['test_acc'] = test_acc_list
history['wm_acc'] = wm_acc_list
history['train_acc'] = train_acc
history['valid_acc'] = valid_acc
# save_obj(history, save_model)
# torch.save(torch.tensor([avg_train_losses, avg_valid_losses, test_acc_list, wm_acc_list]),
# os.path.join('results', save_model + '.t7'))
return test_acc, wm_acc, None, epoch, history
def train_on_augmented(epochs, device, net, optimizer, criterion, scheduler, patience, train_loader, test_loader,
valid_loader,
wm_loader, save_dir, save_model, history):
logging.info("Training on dataset augmented with trigger set.")
avg_train_losses = []
avg_valid_losses = []
test_acc_list = []
wm_acc_list = []
best_test_acc, best_wm_acc, best_epoch = 0, 0, 0
early_stopping = EarlyStopping(patience=int(patience), verbose=True,
path=os.path.join(save_dir, save_model + '.t7'),
trace_func=logging.info)
for epoch in range(epochs):
avg_train_losses, avg_valid_losses, train_acc, valid_acc = train(epoch, net, criterion, optimizer,
train_loader, device,
avg_train_losses, avg_valid_losses,
valid_loader, wm_loader)
logging.info("Testing dataset.")
test_acc = test(net, criterion, test_loader, device)
logging.info("Test acc: %.3f%%" % test_acc)
test_acc_list.append(test_acc)
logging.info("Testing triggerset (no train, test split).")
wm_acc = test(net, criterion, wm_loader, device)
logging.info("WM acc: %.3f%%" % wm_acc)
wm_acc_list.append(wm_acc)
if avg_valid_losses[-1] < early_stopping.val_loss_min: # bc this model will be saved
best_test_acc = test_acc
best_wm_acc = wm_acc
best_epoch = epoch
# save model every 5 epochs # could be variable
if (epoch + 1) % 5 == 0:
durtr_dir = os.path.join(save_dir, save_model + '_duringtraining')
os.makedirs(durtr_dir, exist_ok=True)
torch.save(net.state_dict(), os.path.join(durtr_dir, save_model + 'epoch_' + str(epoch + 1) + '.t7'))
# early_stopping needs the validation loss to check if it has decreased,
# and if it has, it will make a checkpoint of the current model
early_stopping(avg_valid_losses[-1], net)
if early_stopping.early_stop:
logging.info("Early stopping")
break
scheduler.step()
# make_loss_plot(avg_train_losses, avg_valid_losses, save_model)
history['train_losses'] = avg_train_losses
history['valid_losses'] = avg_valid_losses
history['test_acc'] = test_acc_list
history['wm_acc'] = wm_acc_list
history['train_acc'] = train_acc
history['valid_acc'] = valid_acc
# save_obj(history, save_model)
# torch.save(torch.tensor([avg_train_losses, avg_valid_losses, test_acc_list, wm_acc_list]),
# os.path.join('results', save_model + '.t7'))
return best_test_acc, best_wm_acc, early_stopping.val_loss_min, best_epoch, history