-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadamatch.py
437 lines (331 loc) · 16.9 KB
/
adamatch.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import torch
from torch import nn
import torch.nn.functional as F
import numpy as np
import torch.optim as optim
import matplotlib.pyplot as plt
import sklearn.metrics
import seaborn as sns
class Adamatch():
"""
Paper: AdaMatch: A Unified Approach to Semi-Supervised Learning and Domain Adaptation
Authors: David Berthelot, Rebecca Roelofs, Kihyuk Sohn, Nicholas Carlini, Alex Kurakin
"""
def __init__(self, encoder, classifier):
"""
NOTE: the actual AdaMatch paper doesn't separate between encoder and classifier,
but I find it more practical for the purposes of setting up the networks.
Arguments:
----------
encoder: PyTorch neural network
Neural network that receives images and encodes them into an array of size X.
classifier: PyTorch neural network
Neural network that receives an array of size X and classifies it into N classes.
"""
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.encoder = encoder.to(self.device)
self.classifier = classifier.to(self.device)
def train(self, source_dataloader_weak, source_dataloader_strong,
target_dataloader_weak, target_dataloader_strong, target_dataloader_test,
epochs, hyperparams, save_path):
"""
Trains the model (encoder + classifier).
Arguments:
----------
source_dataloader_weak: PyTorch DataLoader
DataLoader with source domain training data with weak augmentations.
source_dataloader_strong: PyTorch DataLoader
DataLoader with source domain training data with strong augmentations.
target_dataloader_weak: PyTorch DataLoader
DataLoader with target domain training data with weak augmentations.
THIS DATALOADER'S BATCH SIZE MUST BE 3 * SOURCE_DATALOADER_BATCH_SIZE.
target_dataloader_strong: PyTorch DataLoader
DataLoader with target domain training data with strong augmentations.
THIS DATALOADER'S BATCH SIZE MUST BE 3 * SOURCE_DATALOADER_BATCH_SIZE.
target_dataloader_test: PyTorch DataLoader
DataLoader with target domain validation data, used for early stopping.
epochs: int
Amount of epochs to train the model for.
hyperparams: dict
Dictionary containing hyperparameters for this algorithm. Check `data/hyperparams.py`.
save_path: str
Path to store model weights.
Returns:
--------
encoder: PyTorch neural network
Neural network that receives images and encodes them into an array of size X.
classifier: PyTorch neural network
Neural network that receives an array of size X and classifies it into N classes.
"""
# configure hyperparameters
lr = hyperparams['learning_rate']
wd = hyperparams['weight_decay']
step_scheduler = hyperparams['step_scheduler']
tau = hyperparams['tau']
iters = max(len(source_dataloader_weak), len(source_dataloader_strong), len(target_dataloader_weak), len(target_dataloader_strong))
# mu related stuff
steps_per_epoch = iters
total_steps = epochs * steps_per_epoch
current_step = 0
# configure optimizer and scheduler
optimizer = optim.Adam(list(self.encoder.parameters()) + list(self.classifier.parameters()), lr=lr, weight_decay=wd)
if step_scheduler:
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=20)
# early stopping variables
start_epoch = 0
best_acc = 0.0
patience = 20
bad_epochs = 0
self.history = {'epoch_loss': [], 'accuracy_source': [], 'accuracy_target': []}
# training loop
for epoch in range(start_epoch, epochs):
running_loss = 0.0
# set network to training mode
self.encoder.train()
self.classifier.train()
dataset = zip(source_dataloader_weak, source_dataloader_strong, target_dataloader_weak, target_dataloader_strong)
# this is where the unsupervised learning comes in, as such, we're not interested in labels
for (data_source_weak, labels_source), (data_source_strong, _), (data_target_weak, _), (data_target_strong, _) in dataset:
data_source_weak = data_source_weak.to(self.device)
labels_source = labels_source.to(self.device)
data_source_strong = data_source_strong.to(self.device)
data_target_weak = data_target_weak.to(self.device)
data_target_strong = data_target_strong.to(self.device)
# concatenate data (in case of low GPU power this could be done after classifying with the model)
data_combined = torch.cat([data_source_weak, data_source_strong, data_target_weak, data_target_strong], 0)
source_combined = torch.cat([data_source_weak, data_source_strong], 0)
# get source data limit (useful for slicing later)
source_total = source_combined.size(0)
# zero gradients
optimizer.zero_grad()
# forward pass: calls the model once for both source and target and once for source only
logits_combined = self.classifier(self.encoder(data_combined))
logits_source_p = logits_combined[:source_total]
# from https://github.com/yizhe-ang/AdaMatch-PyTorch/blob/main/trainers/adamatch.py
self._disable_batchnorm_tracking(self.encoder)
self._disable_batchnorm_tracking(self.classifier)
logits_source_pp = self.classifier(self.encoder(source_combined))
self._enable_batchnorm_tracking(self.encoder)
self._enable_batchnorm_tracking(self.classifier)
# perform random logit interpolation
lambd = torch.rand_like(logits_source_p).to(self.device)
final_logits_source = (lambd * logits_source_p) + ((1-lambd) * logits_source_pp)
# distribution allignment
## softmax for logits of weakly augmented source images
logits_source_weak = final_logits_source[:data_source_weak.size(0)]
pseudolabels_source = F.softmax(logits_source_weak, 1)
## softmax for logits of weakly augmented target images
logits_target = logits_combined[source_total:]
logits_target_weak = logits_target[:data_target_weak.size(0)]
pseudolabels_target = F.softmax(logits_target_weak, 1)
## allign target label distribtion to source label distribution
expectation_ratio = (1e-6 + torch.mean(pseudolabels_source)) / (1e-6 + torch.mean(pseudolabels_target))
final_pseudolabels = F.normalize((pseudolabels_target * expectation_ratio), p=2, dim=1) # L2 normalization
# perform relative confidence thresholding
row_wise_max, _ = torch.max(pseudolabels_source, dim=1)
final_sum = torch.mean(row_wise_max, 0)
## define relative confidence threshold
c_tau = tau * final_sum
max_values, _ = torch.max(final_pseudolabels, dim=1)
mask = (max_values >= c_tau).float()
# compute loss
source_loss = self._compute_source_loss(logits_source_weak, final_logits_source[data_source_weak.size(0):], labels_source)
final_pseudolabels = torch.max(final_pseudolabels, 1)[1] # argmax
target_loss = self._compute_target_loss(final_pseudolabels, logits_target[data_target_weak.size(0):], mask)
## compute target loss weight (mu)
pi = torch.tensor(np.pi, dtype=torch.float).to(self.device)
step = torch.tensor(current_step, dtype=torch.float).to(self.device)
mu = 0.5 - torch.cos(torch.minimum(pi, (2*pi*step) / total_steps)) / 2
## get total loss
loss = source_loss + (mu * target_loss)
current_step += 1
# backpropagate and update weights
loss.backward()
optimizer.step()
# metrics
running_loss += loss.item()
# get losses
epoch_loss = running_loss / iters
self.history['epoch_loss'].append(epoch_loss)
# self.evaluate on testing data (target domain)
epoch_accuracy_source = self.evaluate(source_dataloader_weak)
epoch_accuracy_target = self.evaluate(target_dataloader_weak)
test_epoch_accuracy = self.evaluate(target_dataloader_test)
self.history['accuracy_source'].append(epoch_accuracy_source)
self.history['accuracy_target'].append(epoch_accuracy_target)
# save checkpoint
if test_epoch_accuracy > best_acc:
torch.save({'encoder_weights': self.encoder.state_dict(),
'classifier_weights': self.classifier.state_dict()
}, save_path)
best_acc = test_epoch_accuracy
bad_epochs = 0
else:
bad_epochs += 1
print('[Epoch {}/{}] loss: {:.6f}; accuracy source: {:.6f}; accuracy target: {:.6f}; val accuracy: {:.6f};'.format(epoch+1, epochs, epoch_loss, epoch_accuracy_source, epoch_accuracy_target, test_epoch_accuracy))
if bad_epochs >= patience:
print(f"reached {bad_epochs} bad epochs, stopping training with best val accuracy of {best_acc}!")
break
# scheduler step
if step_scheduler:
scheduler.step()
best = torch.load(save_path)
self.encoder.load_state_dict(best['encoder_weights'])
self.classifier.load_state_dict(best['classifier_weights'])
return self.encoder, self.classifier
def evaluate(self, dataloader, return_lists_roc=False):
"""
Evaluates model on `dataloader`.
Arguments:
----------
dataloader: PyTorch DataLoader
DataLoader with test data.
return_lists_roc: bool
If True returns also list of labels, a list of outputs and a list of predictions.
Useful for some metrics.
Returns:
--------
accuracy: float
Accuracy achieved over `dataloader`.
"""
# set network to evaluation mode
self.encoder.eval()
self.classifier.eval()
labels_list = []
outputs_list = []
preds_list = []
with torch.no_grad():
for data, labels in dataloader:
data = data.to(self.device)
labels = labels.to(self.device)
# predict
outputs = F.softmax(self.classifier(self.encoder(data)), dim=1)
# numpify
labels_numpy = labels.detach().cpu().numpy()
outputs_numpy = outputs.detach().cpu().numpy() # probs (AUROC)
preds = np.argmax(outputs_numpy, axis=1) # accuracy
# append
labels_list.append(labels_numpy)
outputs_list.append(outputs_numpy)
preds_list.append(preds)
labels_list = np.concatenate(labels_list)
outputs_list = np.concatenate(outputs_list)
preds_list = np.concatenate(preds_list)
# metrics
#auc = sklearn.metrics.roc_auc_score(labels_list, outputs_list, multi_class='ovr')
accuracy = sklearn.metrics.accuracy_score(labels_list, preds_list)
if return_lists_roc:
return accuracy, labels_list, outputs_list, preds_list
return accuracy
def plot_metrics(self):
"""
Plots the training metrics (only usable after calling .train()).
"""
# plot metrics for losses n stuff
fig, axs = plt.subplots(1, 3, figsize=(18,5), dpi=200)
epochs = len(self.history['epoch_loss'])
axs[0].plot(range(1, epochs+1), self.history['epoch_loss'])
axs[0].set_xlabel('Epochs')
axs[0].set_ylabel('Loss')
axs[0].set_title('Entropy loss')
axs[1].plot(range(1, epochs+1), self.history['accuracy_source'])
axs[1].set_xlabel('Epochs')
axs[1].set_ylabel('Accuracy')
axs[1].set_title('Accuracy on weakly augmented source')
axs[2].plot(range(1, epochs+1), self.history['accuracy_target'])
axs[2].set_xlabel('Epochs')
axs[2].set_ylabel('Accuracy')
axs[2].set_title('Accuracy on weakly augmented target')
plt.show()
def plot_cm_roc(self, dataloader, n_classes=10):
"""
Plots the confusion matrix and ROC curves of the model on `dataloader`.
Arguments:
----------
dataloader: PyTorch DataLoader
DataLoader with test data.
n_classes: int
Number of classes.
"""
cmap = sns.diverging_palette(220, 20, as_cmap=True)
self.encoder.eval()
self.classifier.eval()
accuracy, labels_list, outputs_list, preds_list = self.evaluate(dataloader, return_lists_roc=True)
# plot confusion matrix
cm = sklearn.metrics.confusion_matrix(labels_list, preds_list)
group_counts = ['{0:0.0f}'.format(value) for value in cm.flatten()]
group_percentages = ['({0:.2%})'.format(value) for value in cm.flatten()/np.sum(cm)]
labels = [f'{v1}\n{v2}' for v1, v2 in zip(group_counts, group_percentages)]
labels = np.asarray(labels).reshape(n_classes,n_classes)
#tn, fp, fn, tp = cm.ravel()
plt.figure(figsize=(10,10), dpi=200)
sns.heatmap(cm, annot=labels, cmap=cmap, fmt="")
plt.title("Confusion matrix")
plt.ylabel("Actual label")
plt.xlabel("Predicted label")
plt.show()
# plot roc
## one hot encode data
onehot = np.zeros((labels_list.size, labels_list.max()+1))
onehot[np.arange(labels_list.size),labels_list] = 1
onehot = onehot.astype('int')
fpr = dict()
tpr = dict()
roc_auc = dict()
## get roc curve and auroc for each class
for i in range(n_classes):
fpr[i], tpr[i], _ = sklearn.metrics.roc_curve(onehot[:, i], outputs_list[:, i])
roc_auc[i] = sklearn.metrics.auc(fpr[i], tpr[i])
## get macro average auroc
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
mean_tpr += np.interp(all_fpr, fpr[i], tpr[i])
mean_tpr /= n_classes
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = sklearn.metrics.auc(fpr["macro"], tpr["macro"])
plt.figure(figsize=(9,9), dpi=200)
plt.plot([0, 1], [0, 1], color='black', linestyle='--')
for i in range(n_classes):
plt.plot(fpr[i], tpr[i], label=f"AUC class {i} = {roc_auc[i]:.4f}")
plt.plot(fpr["macro"], tpr["macro"], label=f"macro-average AUC = {roc_auc['macro']:.4f}", color='deeppink', linewidth=2)
plt.title('Receiver Operating Characteristic (ROC)')
plt.xlabel('False Positives')
plt.ylabel('True Positives')
ax = plt.gca()
ax.set_aspect('equal')
plt.legend(loc='lower right')
plt.show()
@staticmethod
def _disable_batchnorm_tracking(model):
def fn(module):
if isinstance(module, nn.modules.batchnorm._BatchNorm):
module.track_running_stats = False
model.apply(fn)
@staticmethod
def _enable_batchnorm_tracking(model):
def fn(module):
if isinstance(module, nn.modules.batchnorm._BatchNorm):
module.track_running_stats = True
model.apply(fn)
@staticmethod
def _compute_source_loss(logits_weak, logits_strong, labels):
"""
Receives logits as input (dense layer outputs with no activation function)
"""
loss_function = nn.CrossEntropyLoss() # default: `reduction="mean"`
weak_loss = loss_function(logits_weak, labels)
strong_loss = loss_function(logits_strong, labels)
#return weak_loss + strong_loss
return (weak_loss + strong_loss) / 2
@staticmethod
def _compute_target_loss(pseudolabels, logits_strong, mask):
"""
Receives logits as input (dense layer outputs with no activation function).
`pseudolabels` are treated as ground truth (standard SSL practice).
"""
loss_function = nn.CrossEntropyLoss(reduction="none")
pseudolabels = pseudolabels.detach() # remove from backpropagation
loss = loss_function(logits_strong, pseudolabels)
return (loss * mask).mean()