-
Notifications
You must be signed in to change notification settings - Fork 30
/
training_functions.py
371 lines (304 loc) · 14.8 KB
/
training_functions.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import utils
import time
import torch
import numpy as np
import copy
from sklearn.cluster import KMeans
# Training function (from my torch_DCEC implementation, kept for completeness)
def train_model(model, dataloader, criteria, optimizers, schedulers, num_epochs, params):
# Note the time
since = time.time()
# Unpack parameters
writer = params['writer']
if writer is not None: board = True
txt_file = params['txt_file']
pretrained = params['model_files'][1]
pretrain = params['pretrain']
print_freq = params['print_freq']
dataset_size = params['dataset_size']
device = params['device']
batch = params['batch']
pretrain_epochs = params['pretrain_epochs']
gamma = params['gamma']
update_interval = params['update_interval']
tol = params['tol']
dl = dataloader
# Pretrain or load weights
if pretrain:
while True:
pretrained_model = pretraining(model, copy.deepcopy(dl), criteria[0], optimizers[1], schedulers[1], pretrain_epochs, params)
if pretrained_model:
break
else:
for layer in model.children():
if hasattr(layer, 'reset_parameters'):
layer.reset_parameters()
model = pretrained_model
else:
try:
model.load_state_dict(torch.load(pretrained))
utils.print_both(txt_file, 'Pretrained weights loaded from file: ' + str(pretrained))
except:
print("Couldn't load pretrained weights")
# Initialise clusters
utils.print_both(txt_file, '\nInitializing cluster centers based on K-means')
kmeans(model, copy.deepcopy(dl), params)
utils.print_both(txt_file, '\nBegin clusters training')
# Prep variables for weights and accuracy of the best model
best_model_wts = copy.deepcopy(model.state_dict())
best_loss = 10000.0
# Initial target distribution
utils.print_both(txt_file, '\nUpdating target distribution')
output_distribution, labels, preds_prev = calculate_predictions(model, copy.deepcopy(dl), params)
target_distribution = target(output_distribution)
nmi = utils.metrics.nmi(labels, preds_prev)
ari = utils.metrics.ari(labels, preds_prev)
acc = utils.metrics.acc(labels, preds_prev)
utils.print_both(txt_file,
'NMI: {0:.5f}\tARI: {1:.5f}\tAcc {2:.5f}\n'.format(nmi, ari, acc))
if board:
niter = 0
writer.add_scalar('/NMI', nmi, niter)
writer.add_scalar('/ARI', ari, niter)
writer.add_scalar('/Acc', acc, niter)
update_iter = 1
finished = False
# Go through all epochs
for epoch in range(num_epochs):
utils.print_both(txt_file, 'Epoch {}/{}'.format(epoch + 1, num_epochs))
utils.print_both(txt_file, '-' * 10)
schedulers[0].step()
model.train(True) # Set model to training mode
running_loss = 0.0
running_loss_rec = 0.0
running_loss_clust = 0.0
# Keep the batch number for inter-phase statistics
batch_num = 1
img_counter = 0
# Iterate over data.
for data in dataloader:
# Get the inputs and labels
inputs, _ = data
inputs = inputs.to(device)
# Uptade target distribution, chack and print performance
if (batch_num - 1) % update_interval == 0 and not (batch_num == 1 and epoch == 0):
utils.print_both(txt_file, '\nUpdating target distribution:')
output_distribution, labels, preds = calculate_predictions(model, dataloader, params)
target_distribution = target(output_distribution)
nmi = utils.metrics.nmi(labels, preds)
ari = utils.metrics.ari(labels, preds)
acc = utils.metrics.acc(labels, preds)
utils.print_both(txt_file,
'NMI: {0:.5f}\tARI: {1:.5f}\tAcc {2:.5f}\t'.format(nmi, ari, acc))
if board:
niter = update_iter
writer.add_scalar('/NMI', nmi, niter)
writer.add_scalar('/ARI', ari, niter)
writer.add_scalar('/Acc', acc, niter)
update_iter += 1
# check stop criterion
delta_label = np.sum(preds != preds_prev).astype(np.float32) / preds.shape[0]
preds_prev = np.copy(preds)
if delta_label < tol:
utils.print_both(txt_file, 'Label divergence ' + str(delta_label) + '< tol ' + str(tol))
utils.print_both(txt_file, 'Reached tolerance threshold. Stopping training.')
finished = True
break
tar_dist = target_distribution[((batch_num - 1) * batch):(batch_num*batch), :]
tar_dist = torch.from_numpy(tar_dist).to(device)
# print(tar_dist)
# zero the parameter gradients
optimizers[0].zero_grad()
# Calculate losses and backpropagate
with torch.set_grad_enabled(True):
outputs, clusters, _ = model(inputs)
loss_rec = criteria[0](outputs, inputs)
loss_clust = gamma *criteria[1](torch.log(clusters), tar_dist) / batch
loss = loss_rec + loss_clust
loss.backward()
optimizers[0].step()
# For keeping statistics
running_loss += loss.item() * inputs.size(0)
running_loss_rec += loss_rec.item() * inputs.size(0)
running_loss_clust += loss_clust.item() * inputs.size(0)
# Some current stats
loss_batch = loss.item()
loss_batch_rec = loss_rec.item()
loss_batch_clust = loss_clust.item()
loss_accum = running_loss / ((batch_num - 1) * batch + inputs.size(0))
loss_accum_rec = running_loss_rec / ((batch_num - 1) * batch + inputs.size(0))
loss_accum_clust = running_loss_clust / ((batch_num - 1) * batch + inputs.size(0))
if batch_num % print_freq == 0:
utils.print_both(txt_file, 'Epoch: [{0}][{1}/{2}]\t'
'Loss {3:.4f} ({4:.4f})\t'
'Loss_recovery {5:.4f} ({6:.4f})\t'
'Loss clustering {7:.4f} ({8:.4f})\t'.format(epoch + 1, batch_num,
len(dataloader),
loss_batch,
loss_accum, loss_batch_rec,
loss_accum_rec,
loss_batch_clust,
loss_accum_clust))
if board:
niter = epoch * len(dataloader) + batch_num
writer.add_scalar('/Loss', loss_accum, niter)
writer.add_scalar('/Loss_recovery', loss_accum_rec, niter)
writer.add_scalar('/Loss_clustering', loss_accum_clust, niter)
batch_num = batch_num + 1
# Print image to tensorboard
if batch_num == len(dataloader) and (epoch+1) % 5:
inp = utils.tensor2img(inputs)
out = utils.tensor2img(outputs)
if board:
img = np.concatenate((inp, out), axis=1)
writer.add_image('Clustering/Epoch_' + str(epoch + 1).zfill(3) + '/Sample_' + str(img_counter).zfill(2), img)
img_counter += 1
if finished: break
epoch_loss = running_loss / dataset_size
epoch_loss_rec = running_loss_rec / dataset_size
epoch_loss_clust = running_loss_clust / dataset_size
if board:
writer.add_scalar('/Loss' + '/Epoch', epoch_loss, epoch + 1)
writer.add_scalar('/Loss_rec' + '/Epoch', epoch_loss_rec, epoch + 1)
writer.add_scalar('/Loss_clust' + '/Epoch', epoch_loss_clust, epoch + 1)
utils.print_both(txt_file, 'Loss: {0:.4f}\tLoss_recovery: {1:.4f}\tLoss_clustering: {2:.4f}'.format(epoch_loss,
epoch_loss_rec,
epoch_loss_clust))
# If wanted to do some criterium in the future (for now useless)
if epoch_loss < best_loss or epoch_loss >= best_loss:
best_loss = epoch_loss
best_model_wts = copy.deepcopy(model.state_dict())
utils.print_both(txt_file, '')
time_elapsed = time.time() - since
utils.print_both(txt_file, 'Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
# load best model weights
model.load_state_dict(best_model_wts)
return model
# Pretraining function for recovery loss only
def pretraining(model, dataloader, criterion, optimizer, scheduler, num_epochs, params):
# Note the time
since = time.time()
# Unpack parameters
writer = params['writer']
if writer is not None: board = True
txt_file = params['txt_file']
pretrained = params['model_files'][1]
print_freq = params['print_freq']
dataset_size = params['dataset_size']
device = params['device']
batch = params['batch']
# Prep variables for weights and accuracy of the best model
best_model_wts = copy.deepcopy(model.state_dict())
best_loss = 10000.0
# Go through all epochs
for epoch in range(num_epochs):
utils.print_both(txt_file, 'Pretraining:\tEpoch {}/{}'.format(epoch + 1, num_epochs))
utils.print_both(txt_file, '-' * 10)
scheduler.step()
model.train(True) # Set model to training mode
running_loss = 0.0
# Keep the batch number for inter-phase statistics
batch_num = 1
# Images to show
img_counter = 0
# Iterate over data.
for data in dataloader:
# Get the inputs and labels
inputs, _ = data
inputs = inputs.to(device)
# zero the parameter gradients
optimizer.zero_grad()
with torch.set_grad_enabled(True):
outputs, _, _ = model(inputs)
loss = criterion(outputs, inputs)
loss.backward()
optimizer.step()
# For keeping statistics
running_loss += loss.item() * inputs.size(0)
# Some current stats
loss_batch = loss.item()
loss_accum = running_loss / ((batch_num - 1) * batch + inputs.size(0))
if batch_num % print_freq == 0:
utils.print_both(txt_file, 'Pretraining:\tEpoch: [{0}][{1}/{2}]\t'
'Loss {3:.4f} ({4:.4f})\t'.format(epoch + 1, batch_num, len(dataloader),
loss_batch,
loss_accum))
if board:
niter = epoch * len(dataloader) + batch_num
writer.add_scalar('Pretraining/Loss', loss_accum, niter)
batch_num = batch_num + 1
if batch_num in [len(dataloader), len(dataloader)//2, len(dataloader)//4, 3*len(dataloader)//4]:
inp = utils.tensor2img(inputs)
out = utils.tensor2img(outputs)
if board:
img = np.concatenate((inp, out), axis=1)
writer.add_image('Pretraining/Epoch_' + str(epoch + 1).zfill(3) + '/Sample_' + str(img_counter).zfill(2), img)
img_counter += 1
epoch_loss = running_loss / dataset_size
if epoch == 0: first_loss = epoch_loss
if epoch == 4 and epoch_loss / first_loss > 1:
utils.print_both(txt_file, "\nLoss not converging, starting pretraining again\n")
return False
if board:
writer.add_scalar('Pretraining/Loss' + '/Epoch', epoch_loss, epoch + 1)
utils.print_both(txt_file, 'Pretraining:\t Loss: {:.4f}'.format(epoch_loss))
# If wanted to add some criterium in the future
if epoch_loss < best_loss or epoch_loss >= best_loss:
best_loss = epoch_loss
best_model_wts = copy.deepcopy(model.state_dict())
utils.print_both(txt_file, '')
time_elapsed = time.time() - since
utils.print_both(txt_file, 'Pretraining complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
# load best model weights
model.load_state_dict(best_model_wts)
model.pretrained = True
torch.save(model.state_dict(), pretrained)
return model
# K-means clusters initialisation
def kmeans(model, dataloader, params):
km = KMeans(n_clusters=model.num_clusters, n_init=20)
output_array = None
model.eval()
# Itarate throught the data and concatenate the latent space representations of images
for data in dataloader:
inputs, _ = data
inputs = inputs.to(params['device'])
_, _, outputs = model(inputs)
if output_array is not None:
output_array = np.concatenate((output_array, outputs.cpu().detach().numpy()), 0)
else:
output_array = outputs.cpu().detach().numpy()
# print(output_array.shape)
if output_array.shape[0] > 50000: break
# Perform K-means
km.fit_predict(output_array)
# Update clustering layer weights
weights = torch.from_numpy(km.cluster_centers_)
model.clustering.set_weight(weights.to(params['device']))
# torch.cuda.empty_cache()
# Function forwarding data through network, collecting clustering weight output and returning prediciotns and labels
def calculate_predictions(model, dataloader, params):
output_array = None
label_array = None
model.eval()
for data in dataloader:
inputs, labels = data
inputs = inputs.to(params['device'])
labels = labels.to(params['device'])
_, outputs, _ = model(inputs)
if output_array is not None:
output_array = np.concatenate((output_array, outputs.cpu().detach().numpy()), 0)
label_array = np.concatenate((label_array, labels.cpu().detach().numpy()), 0)
else:
output_array = outputs.cpu().detach().numpy()
label_array = labels.cpu().detach().numpy()
preds = np.argmax(output_array.data, axis=1)
# print(output_array.shape)
return output_array, label_array, preds
# Calculate target distribution
def target(out_distr):
tar_dist = out_distr ** 2 / np.sum(out_distr, axis=0)
tar_dist = np.transpose(np.transpose(tar_dist) / np.sum(tar_dist, axis=1))
return tar_dist