-
Notifications
You must be signed in to change notification settings - Fork 124
/
train_val.py
244 lines (199 loc) · 10.2 KB
/
train_val.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
import torch
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
from pytorch_lightning.core import LightningModule
from torch.nn import CrossEntropyLoss
import evaluate_utils
import head
import net
import numpy as np
import utils
class Trainer(LightningModule):
def __init__(self, **kwargs):
super(Trainer, self).__init__()
self.save_hyperparameters() # sets self.hparams
self.class_num = utils.get_num_class(self.hparams)
print('classnum: {}'.format(self.class_num))
self.model = net.build_model(model_name=self.hparams.arch)
self.head = head.build_head(head_type=self.hparams.head,
embedding_size=512,
class_num=self.class_num,
m=self.hparams.m,
h=self.hparams.h,
t_alpha=self.hparams.t_alpha,
s=self.hparams.s,
)
self.cross_entropy_loss = CrossEntropyLoss()
if self.hparams.start_from_model_statedict:
ckpt = torch.load(self.hparams.start_from_model_statedict)
self.model.load_state_dict({key.replace('model.', ''):val
for key,val in ckpt['state_dict'].items() if 'model.' in key})
def get_current_lr(self):
scheduler = None
if scheduler is None:
try:
# pytorch lightning >= 1.8
scheduler = self.trainer.lr_scheduler_configs[0].scheduler
except:
pass
if scheduler is None:
# pytorch lightning <=1.7
try:
scheduler = self.trainer.lr_schedulers[0]['scheduler']
except:
pass
if scheduler is None:
raise ValueError('lr calculation not successful')
if isinstance(scheduler, lr_scheduler._LRScheduler):
lr = scheduler.get_last_lr()[0]
else:
lr = scheduler.get_epoch_values(self.current_epoch)[0]
return lr
def forward(self, images, labels):
embeddings, norms = self.model(images)
cos_thetas = self.head(embeddings, norms, labels)
if isinstance(cos_thetas, tuple):
cos_thetas, bad_grad = cos_thetas
labels[bad_grad.squeeze(-1)] = -100 # ignore_index
return cos_thetas, norms, embeddings, labels
def training_step(self, batch, batch_idx):
images, labels = batch
cos_thetas, norms, embeddings, labels = self.forward(images, labels)
loss_train = self.cross_entropy_loss(cos_thetas, labels)
lr = self.get_current_lr()
# log
self.log('lr', lr, on_step=True, on_epoch=True, logger=True)
self.log('train_loss', loss_train, on_step=True, on_epoch=True, logger=True)
return loss_train
def training_epoch_end(self, outputs):
return None
def validation_step(self, batch, batch_idx):
images, labels, dataname, image_index = batch
embeddings, norms = self.model(images)
fliped_images = torch.flip(images, dims=[3])
flipped_embeddings, flipped_norms = self.model(fliped_images)
stacked_embeddings = torch.stack([embeddings, flipped_embeddings], dim=0)
stacked_norms = torch.stack([norms, flipped_norms], dim=0)
embeddings, norms = utils.fuse_features_with_norm(stacked_embeddings, stacked_norms)
if self.hparams.distributed_backend == 'ddp':
# to save gpu memory
return {
'output': embeddings.to('cpu'),
'norm': norms.to('cpu'),
'target': labels.to('cpu'),
'dataname': dataname.to('cpu'),
'image_index': image_index.to('cpu')
}
else:
# dp requires the tensor to be cuda
return {
'output': embeddings,
'norm': norms,
'target': labels,
'dataname': dataname,
'image_index': image_index
}
def validation_epoch_end(self, outputs):
all_output_tensor, all_norm_tensor, all_target_tensor, all_dataname_tensor = self.gather_outputs(outputs)
dataname_to_idx = {"agedb_30": 0, "cfp_fp": 1, "lfw": 2, "cplfw": 3, "calfw": 4}
idx_to_dataname = {val: key for key, val in dataname_to_idx.items()}
val_logs = {}
for dataname_idx in all_dataname_tensor.unique():
dataname = idx_to_dataname[dataname_idx.item()]
# per dataset evaluation
embeddings = all_output_tensor[all_dataname_tensor == dataname_idx].to('cpu').numpy()
labels = all_target_tensor[all_dataname_tensor == dataname_idx].to('cpu').numpy()
issame = labels[0::2]
tpr, fpr, accuracy, best_thresholds = evaluate_utils.evaluate(embeddings, issame, nrof_folds=10)
acc, best_threshold = accuracy.mean(), best_thresholds.mean()
num_val_samples = len(embeddings)
val_logs[f'{dataname}_val_acc'] = acc
val_logs[f'{dataname}_best_threshold'] = best_threshold
val_logs[f'{dataname}_num_val_samples'] = num_val_samples
val_logs['val_acc'] = np.mean([
val_logs[f'{dataname}_val_acc'] for dataname in dataname_to_idx.keys() if f'{dataname}_val_acc' in val_logs
])
val_logs['epoch'] = self.current_epoch
for k, v in val_logs.items():
# self.log(name=k, value=v, rank_zero_only=True)
self.log(name=k, value=v)
return None
def test_step(self, batch, batch_idx):
return self.validation_step(batch, batch_idx)
def test_epoch_end(self, outputs):
all_output_tensor, all_norm_tensor, all_target_tensor, all_dataname_tensor = self.gather_outputs(outputs)
dataname_to_idx = {"agedb_30": 0, "cfp_fp": 1, "lfw": 2, "cplfw": 3, "calfw": 4}
idx_to_dataname = {val: key for key, val in dataname_to_idx.items()}
test_logs = {}
for dataname_idx in all_dataname_tensor.unique():
dataname = idx_to_dataname[dataname_idx.item()]
# per dataset evaluation
embeddings = all_output_tensor[all_dataname_tensor == dataname_idx].to('cpu').numpy()
labels = all_target_tensor[all_dataname_tensor == dataname_idx].to('cpu').numpy()
issame = labels[0::2]
tpr, fpr, accuracy, best_thresholds = evaluate_utils.evaluate(embeddings, issame, nrof_folds=10)
acc, best_threshold = accuracy.mean(), best_thresholds.mean()
num_test_samples = len(embeddings)
test_logs[f'{dataname}_test_acc'] = acc
test_logs[f'{dataname}_test_best_threshold'] = best_threshold
test_logs[f'{dataname}_num_test_samples'] = num_test_samples
test_logs['test_acc'] = np.mean([
test_logs[f'{dataname}_test_acc'] for dataname in dataname_to_idx.keys()
if f'{dataname}_test_acc' in test_logs
])
test_logs['epoch'] = self.current_epoch
for k, v in test_logs.items():
# self.log(name=k, value=v, rank_zero_only=True)
self.log(name=k, value=v)
return None
def gather_outputs(self, outputs):
if self.hparams.distributed_backend == 'ddp':
# gather outputs across gpu
outputs_list = []
_outputs_list = utils.all_gather(outputs)
for _outputs in _outputs_list:
outputs_list.extend(_outputs)
else:
outputs_list = outputs
# if self.trainer.is_global_zero:
all_output_tensor = torch.cat([out['output'] for out in outputs_list], axis=0).to('cpu')
all_norm_tensor = torch.cat([out['norm'] for out in outputs_list], axis=0).to('cpu')
all_target_tensor = torch.cat([out['target'] for out in outputs_list], axis=0).to('cpu')
all_dataname_tensor = torch.cat([out['dataname'] for out in outputs_list], axis=0).to('cpu')
all_image_index = torch.cat([out['image_index'] for out in outputs_list], axis=0).to('cpu')
# get rid of duplicate index outputs
unique_dict = {}
for _out, _nor, _tar, _dat, _idx in zip(all_output_tensor, all_norm_tensor, all_target_tensor,
all_dataname_tensor, all_image_index):
unique_dict[_idx.item()] = {'output': _out, 'norm': _nor, 'target': _tar, 'dataname': _dat}
unique_keys = sorted(unique_dict.keys())
all_output_tensor = torch.stack([unique_dict[key]['output'] for key in unique_keys], axis=0)
all_norm_tensor = torch.stack([unique_dict[key]['norm'] for key in unique_keys], axis=0)
all_target_tensor = torch.stack([unique_dict[key]['target'] for key in unique_keys], axis=0)
all_dataname_tensor = torch.stack([unique_dict[key]['dataname'] for key in unique_keys], axis=0)
return all_output_tensor, all_norm_tensor, all_target_tensor, all_dataname_tensor
def configure_optimizers(self):
# paras_only_bn, paras_wo_bn = self.separate_bn_paras(self.model)
paras_wo_bn, paras_only_bn = self.split_parameters(self.model)
optimizer = optim.SGD([{
'params': paras_wo_bn + [self.head.kernel],
'weight_decay': 5e-4
}, {
'params': paras_only_bn
}],
lr=self.hparams.lr,
momentum=self.hparams.momentum)
scheduler = lr_scheduler.MultiStepLR(optimizer,
milestones=self.hparams.lr_milestones,
gamma=self.hparams.lr_gamma)
return [optimizer], [scheduler]
def split_parameters(self, module):
params_decay = []
params_no_decay = []
for m in module.modules():
if isinstance(m, torch.nn.modules.batchnorm._BatchNorm):
params_no_decay.extend([*m.parameters()])
elif len(list(m.children())) == 0:
params_decay.extend([*m.parameters()])
assert len(list(module.parameters())) == len(params_decay) + len(params_no_decay)
return params_decay, params_no_decay