-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathtrain_triplet_loss.py
521 lines (455 loc) · 20.1 KB
/
train_triplet_loss.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import numpy as np
import argparse
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torch.nn.modules.distance import PairwiseDistance
from datasets.LFWDataset import LFWDataset
from losses.triplet_loss import TripletLoss
from datasets.TripletLossDataset import TripletFaceDataset
from validate_on_LFW import evaluate_lfw
from plot import plot_roc_lfw, plot_accuracy_lfw
from tqdm import tqdm
from models.inceptionresnetv2 import InceptionResnetV2Triplet
from models.mobilenetv2 import MobileNetV2Triplet
from models.resnet import (
Resnet18Triplet,
Resnet34Triplet,
Resnet50Triplet,
Resnet101Triplet,
Resnet152Triplet
)
parser = argparse.ArgumentParser(description="Training a FaceNet facial recognition model using Triplet Loss.")
parser.add_argument('--dataroot', '-d', type=str, required=True,
help="(REQUIRED) Absolute path to the training dataset folder"
)
parser.add_argument('--lfw', type=str, required=True,
help="(REQUIRED) Absolute path to the labeled faces in the wild dataset folder"
)
parser.add_argument('--training_dataset_csv_path', type=str, default='datasets/glint360k.csv',
help="Path to the csv file containing the image paths of the training dataset"
)
parser.add_argument('--epochs', default=150, type=int,
help="Required training epochs (default: 150)"
)
parser.add_argument('--iterations_per_epoch', default=5000, type=int,
help="Number of training iterations per epoch (default: 5000)"
)
parser.add_argument('--model_architecture', type=str, default="resnet34", choices=["resnet18", "resnet34", "resnet50", "resnet101", "resnet152", "inceptionresnetv2", "mobilenetv2"],
help="The required model architecture for training: ('resnet18','resnet34', 'resnet50', 'resnet101', 'resnet152', 'inceptionresnetv2', 'mobilenetv2'), (default: 'resnet34')"
)
parser.add_argument('--pretrained', default=False, type=bool,
help="Download a model pretrained on the ImageNet dataset (Default: False)"
)
parser.add_argument('--embedding_dimension', default=512, type=int,
help="Dimension of the embedding vector (default: 512)"
)
parser.add_argument('--num_human_identities_per_batch', default=32, type=int,
help="Number of set human identities per generated triplets batch. (Default: 32)."
)
parser.add_argument('--batch_size', default=544, type=int,
help="Batch size (default: 544)"
)
parser.add_argument('--lfw_batch_size', default=200, type=int,
help="Batch size for LFW dataset (6000 pairs) (default: 200)"
)
parser.add_argument('--resume_path', default='', type=str,
help='path to latest model checkpoint: (model_training_checkpoints/model_resnet34_epoch_1.pt file) (default: None)'
)
parser.add_argument('--num_workers', default=4, type=int,
help="Number of workers for data loaders (default: 4)"
)
parser.add_argument('--optimizer', type=str, default="adagrad", choices=["sgd", "adagrad", "rmsprop", "adam"],
help="Required optimizer for training the model: ('sgd','adagrad','rmsprop','adam'), (default: 'adagrad')"
)
parser.add_argument('--learning_rate', default=0.075, type=float,
help="Learning rate for the optimizer (default: 0.075)"
)
parser.add_argument('--margin', default=0.2, type=float,
help='margin for triplet loss (default: 0.2)'
)
parser.add_argument('--image_size', default=140, type=int,
help='Input image size (default: 140 (140x140))'
)
parser.add_argument('--use_semihard_negatives', default=False, type=bool,
help="If True: use semihard negative triplet selection. Else: use hard negative triplet selection (Default: False)"
)
parser.add_argument('--training_triplets_path', default=None, type=str,
help="Path to training triplets numpy file in 'datasets/generated_triplets' folder to skip training triplet generation step for the first epoch."
)
args = parser.parse_args()
def set_model_architecture(model_architecture, pretrained, embedding_dimension):
if model_architecture == "resnet18":
model = Resnet18Triplet(
embedding_dimension=embedding_dimension,
pretrained=pretrained
)
elif model_architecture == "resnet34":
model = Resnet34Triplet(
embedding_dimension=embedding_dimension,
pretrained=pretrained
)
elif model_architecture == "resnet50":
model = Resnet50Triplet(
embedding_dimension=embedding_dimension,
pretrained=pretrained
)
elif model_architecture == "resnet101":
model = Resnet101Triplet(
embedding_dimension=embedding_dimension,
pretrained=pretrained
)
elif model_architecture == "resnet152":
model = Resnet152Triplet(
embedding_dimension=embedding_dimension,
pretrained=pretrained
)
elif model_architecture == "inceptionresnetv2":
model = InceptionResnetV2Triplet(
embedding_dimension=embedding_dimension,
pretrained=pretrained
)
elif model_architecture == "mobilenetv2":
model = MobileNetV2Triplet(
embedding_dimension=embedding_dimension,
pretrained=pretrained
)
print("Using {} model architecture.".format(model_architecture))
return model
def set_model_gpu_mode(model):
flag_train_gpu = torch.cuda.is_available()
flag_train_multi_gpu = False
if flag_train_gpu and torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
model.cuda()
flag_train_multi_gpu = True
print('Using multi-gpu training.')
elif flag_train_gpu and torch.cuda.device_count() == 1:
model.cuda()
print('Using single-gpu training.')
return model, flag_train_multi_gpu
def set_optimizer(optimizer, model, learning_rate):
if optimizer == "sgd":
optimizer_model = optim.SGD(
params=model.parameters(),
lr=learning_rate,
momentum=0.9,
dampening=0,
nesterov=False,
weight_decay=1e-5
)
elif optimizer == "adagrad":
optimizer_model = optim.Adagrad(
params=model.parameters(),
lr=learning_rate,
lr_decay=0,
initial_accumulator_value=0.1,
eps=1e-10,
weight_decay=1e-5
)
elif optimizer == "rmsprop":
optimizer_model = optim.RMSprop(
params=model.parameters(),
lr=learning_rate,
alpha=0.99,
eps=1e-08,
momentum=0,
centered=False,
weight_decay=1e-5
)
elif optimizer == "adam":
optimizer_model = optim.Adam(
params=model.parameters(),
lr=learning_rate,
betas=(0.9, 0.999),
eps=1e-08,
amsgrad=False,
weight_decay=1e-5
)
return optimizer_model
def validate_lfw(model, lfw_dataloader, model_architecture, epoch):
model.eval()
with torch.no_grad():
l2_distance = PairwiseDistance(p=2)
distances, labels = [], []
print("Validating on LFW! ...")
progress_bar = enumerate(tqdm(lfw_dataloader))
for batch_index, (data_a, data_b, label) in progress_bar:
data_a = data_a.cuda()
data_b = data_b.cuda()
output_a, output_b = model(data_a), model(data_b)
distance = l2_distance.forward(output_a, output_b) # Euclidean distance
distances.append(distance.cpu().detach().numpy())
labels.append(label.cpu().detach().numpy())
labels = np.array([sublabel for label in labels for sublabel in label])
distances = np.array([subdist for distance in distances for subdist in distance])
true_positive_rate, false_positive_rate, precision, recall, accuracy, roc_auc, best_distances, \
tar, far = evaluate_lfw(
distances=distances,
labels=labels,
far_target=1e-3
)
# Print statistics and add to log
print("Accuracy on LFW: {:.4f}+-{:.4f}\tPrecision {:.4f}+-{:.4f}\tRecall {:.4f}+-{:.4f}\t"
"ROC Area Under Curve: {:.4f}\tBest distance threshold: {:.2f}+-{:.2f}\t"
"TAR: {:.4f}+-{:.4f} @ FAR: {:.4f}".format(
np.mean(accuracy),
np.std(accuracy),
np.mean(precision),
np.std(precision),
np.mean(recall),
np.std(recall),
roc_auc,
np.mean(best_distances),
np.std(best_distances),
np.mean(tar),
np.std(tar),
np.mean(far)
)
)
with open('logs/lfw_{}_log_triplet.txt'.format(model_architecture), 'a') as f:
val_list = [
epoch,
np.mean(accuracy),
np.std(accuracy),
np.mean(precision),
np.std(precision),
np.mean(recall),
np.std(recall),
roc_auc,
np.mean(best_distances),
np.std(best_distances),
np.mean(tar)
]
log = '\t'.join(str(value) for value in val_list)
f.writelines(log + '\n')
try:
# Plot ROC curve
plot_roc_lfw(
false_positive_rate=false_positive_rate,
true_positive_rate=true_positive_rate,
figure_name="plots/roc_plots/roc_{}_epoch_{}_triplet.png".format(model_architecture, epoch)
)
# Plot LFW accuracies plot
plot_accuracy_lfw(
log_file="logs/lfw_{}_log_triplet.txt".format(model_architecture),
epochs=epoch,
figure_name="plots/accuracies_plots/lfw_accuracies_{}_epoch_{}_triplet.png".format(model_architecture, epoch)
)
except Exception as e:
print(e)
return best_distances
def forward_pass(imgs, model, batch_size):
imgs = imgs.cuda()
embeddings = model(imgs)
# Split the embeddings into Anchor, Positive, and Negative embeddings
anc_embeddings = embeddings[:batch_size]
pos_embeddings = embeddings[batch_size: batch_size * 2]
neg_embeddings = embeddings[batch_size * 2:]
return anc_embeddings, pos_embeddings, neg_embeddings, model
def main():
dataroot = args.dataroot
lfw_dataroot = args.lfw
training_dataset_csv_path = args.training_dataset_csv_path
epochs = args.epochs
iterations_per_epoch = args.iterations_per_epoch
model_architecture = args.model_architecture
pretrained = args.pretrained
embedding_dimension = args.embedding_dimension
num_human_identities_per_batch = args.num_human_identities_per_batch
batch_size = args.batch_size
lfw_batch_size = args.lfw_batch_size
resume_path = args.resume_path
num_workers = args.num_workers
optimizer = args.optimizer
learning_rate = args.learning_rate
margin = args.margin
image_size = args.image_size
use_semihard_negatives = args.use_semihard_negatives
training_triplets_path = args.training_triplets_path
flag_training_triplets_path = False
start_epoch = 0
if training_triplets_path is not None:
flag_training_triplets_path = True # Load triplets file for the first training epoch
# Define image data pre-processing transforms
# ToTensor() normalizes pixel values between [0, 1]
# Normalize(mean=[0.6071, 0.4609, 0.3944], std=[0.2457, 0.2175, 0.2129]) according to the calculated glint360k
# dataset with tightly-cropped faces dataset RGB channels' mean and std values by
# calculate_glint360k_rgb_mean_std.py in 'datasets' folder.
data_transforms = transforms.Compose([
transforms.Resize(size=image_size),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(degrees=5),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.6071, 0.4609, 0.3944],
std=[0.2457, 0.2175, 0.2129]
)
])
lfw_transforms = transforms.Compose([
transforms.Resize(size=image_size),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.6071, 0.4609, 0.3944],
std=[0.2457, 0.2175, 0.2129]
)
])
lfw_dataloader = torch.utils.data.DataLoader(
dataset=LFWDataset(
dir=lfw_dataroot,
pairs_path='datasets/LFW_pairs.txt',
transform=lfw_transforms
),
batch_size=lfw_batch_size,
num_workers=num_workers,
shuffle=False
)
# Instantiate model
model = set_model_architecture(
model_architecture=model_architecture,
pretrained=pretrained,
embedding_dimension=embedding_dimension
)
# Load model to GPU or multiple GPUs if available
model, flag_train_multi_gpu = set_model_gpu_mode(model)
# Set optimizer
optimizer_model = set_optimizer(
optimizer=optimizer,
model=model,
learning_rate=learning_rate
)
# Resume from a model checkpoint
if resume_path:
if os.path.isfile(resume_path):
print("Loading checkpoint {} ...".format(resume_path))
checkpoint = torch.load(resume_path)
start_epoch = checkpoint['epoch'] + 1
optimizer_model.load_state_dict(checkpoint['optimizer_model_state_dict'])
# In order to load state dict for optimizers correctly, model has to be loaded to gpu first
if flag_train_multi_gpu:
model.module.load_state_dict(checkpoint['model_state_dict'])
else:
model.load_state_dict(checkpoint['model_state_dict'])
print("Checkpoint loaded: start epoch from checkpoint = {}".format(start_epoch))
else:
print("WARNING: No checkpoint found at {}!\nTraining from scratch.".format(resume_path))
if use_semihard_negatives:
print("Using Semi-Hard negative triplet selection!")
else:
print("Using Hard negative triplet selection!")
start_epoch = start_epoch
print("Training using triplet loss starting for {} epochs:\n".format(epochs - start_epoch))
for epoch in range(start_epoch, epochs):
num_valid_training_triplets = 0
l2_distance = PairwiseDistance(p=2)
_training_triplets_path = None
if flag_training_triplets_path:
_training_triplets_path = training_triplets_path
flag_training_triplets_path = False # Only load triplets file for the first epoch
# Re-instantiate training dataloader to generate a triplet list for this training epoch
train_dataloader = torch.utils.data.DataLoader(
dataset=TripletFaceDataset(
root_dir=dataroot,
training_dataset_csv_path=training_dataset_csv_path,
num_triplets=iterations_per_epoch * batch_size,
num_human_identities_per_batch=num_human_identities_per_batch,
triplet_batch_size=batch_size,
epoch=epoch,
training_triplets_path=_training_triplets_path,
transform=data_transforms
),
batch_size=batch_size,
num_workers=num_workers,
shuffle=False # Shuffling for triplets with set amount of human identities per batch is not required
)
# Training pass
model.train()
progress_bar = enumerate(tqdm(train_dataloader))
for batch_idx, (batch_sample) in progress_bar:
# Forward pass - compute embeddings
anc_imgs = batch_sample['anc_img']
pos_imgs = batch_sample['pos_img']
neg_imgs = batch_sample['neg_img']
# Concatenate the input images into one tensor because doing multiple forward passes would create
# weird GPU memory allocation behaviours later on during training which would cause GPU Out of Memory
# issues
all_imgs = torch.cat((anc_imgs, pos_imgs, neg_imgs)) # Must be a tuple of Torch Tensors
anc_embeddings, pos_embeddings, neg_embeddings, model = forward_pass(
imgs=all_imgs,
model=model,
batch_size=batch_size
)
pos_dists = l2_distance.forward(anc_embeddings, pos_embeddings)
neg_dists = l2_distance.forward(anc_embeddings, neg_embeddings)
if use_semihard_negatives:
# Semi-Hard Negative triplet selection
# (negative_distance - positive_distance < margin) AND (positive_distance < negative_distance)
# Based on: https://github.com/davidsandberg/facenet/blob/master/src/train_tripletloss.py#L295
first_condition = (neg_dists - pos_dists < margin).cpu().numpy().flatten()
second_condition = (pos_dists < neg_dists).cpu().numpy().flatten()
all = (np.logical_and(first_condition, second_condition))
valid_triplets = np.where(all == 1)
else:
# Hard Negative triplet selection
# (negative_distance - positive_distance < margin)
# Based on: https://github.com/davidsandberg/facenet/blob/master/src/train_tripletloss.py#L296
all = (neg_dists - pos_dists < margin).cpu().numpy().flatten()
valid_triplets = np.where(all == 1)
anc_valid_embeddings = anc_embeddings[valid_triplets]
pos_valid_embeddings = pos_embeddings[valid_triplets]
neg_valid_embeddings = neg_embeddings[valid_triplets]
# Calculate triplet loss
triplet_loss = TripletLoss(margin=margin).forward(
anchor=anc_valid_embeddings,
positive=pos_valid_embeddings,
negative=neg_valid_embeddings
)
# Calculating number of triplets that met the triplet selection method during the epoch
num_valid_training_triplets += len(anc_valid_embeddings)
# Backward pass
optimizer_model.zero_grad()
triplet_loss.backward()
optimizer_model.step()
# Print training statistics for epoch and add to log
print('Epoch {}:\tNumber of valid training triplets in epoch: {}'.format(
epoch,
num_valid_training_triplets
)
)
with open('logs/{}_log_triplet.txt'.format(model_architecture), 'a') as f:
val_list = [
epoch,
num_valid_training_triplets
]
log = '\t'.join(str(value) for value in val_list)
f.writelines(log + '\n')
# Evaluation pass on LFW dataset
best_distances = validate_lfw(
model=model,
lfw_dataloader=lfw_dataloader,
model_architecture=model_architecture,
epoch=epoch
)
# Save model checkpoint
state = {
'epoch': epoch,
'embedding_dimension': embedding_dimension,
'batch_size_training': batch_size,
'model_state_dict': model.state_dict(),
'model_architecture': model_architecture,
'optimizer_model_state_dict': optimizer_model.state_dict(),
'best_distance_threshold': np.mean(best_distances)
}
# For storing data parallel model's state dictionary without 'module' parameter
if flag_train_multi_gpu:
state['model_state_dict'] = model.module.state_dict()
# Save model checkpoint
torch.save(state, 'model_training_checkpoints/model_{}_triplet_epoch_{}.pt'.format(
model_architecture,
epoch
)
)
if __name__ == '__main__':
main()