-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment_v4.py
812 lines (661 loc) · 30.6 KB
/
experiment_v4.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
from collections import Counter
import numpy as np
import torch.nn as nn
import torch
import torch.optim as optim
from torch.optim.lr_scheduler import StepLR
from torch.utils.data import TensorDataset, DataLoader
import matplotlib.pyplot as plt
import random
from utils import TripletLossWeighted, TripletLoss, TripletDataset, calc_embeddings
import pandas as pd
import torch.nn.functional as F
from torch.utils.data.sampler import BatchSampler
from itertools import combinations
from torchvision import transforms
dt_name_to_cols_to_encode = {
'cmc': [1,2,6,7],
'dermatology': list(range(0,10)) + list(range(10,33)),
'hayes-roth':[0,1,2,3],
'new_vehicle': [],
'new_yeast': [5],
'1czysty-cut': [],
'2delikatne-cut': [],
'3mocniej-cut': [],
'4delikatne-bezover-cut': [],
'balance-scale': [0,1,2,3],
'cleveland': [2,6,10,11,12],
'cleveland_v2': [2, 6, 10, 11, 12],
'glass': [],
'new_ecoli': [],
'new_led7digit': [],
'new_winequality-red': [],
'thyroid-newthyroid': []
}
dt_name_minority_classes = {
'1czysty-cut': [1, 2],
'2delikatne-cut': [1,2],
'3mocniej-cut': [1,2],
'4delikatne-bezover-cut': [1,2],
'balance-scale': [0],
'cleveland': [1,2,3,4],
'cleveland_v2': [1,2,3],
'cmc': [1],
'dermatology': [5],
'glass': [5,2,4],
'hayes-roth': [2],
'new_vehicle': [0,2],
'new_yeast': [2,3,4,5,6],
'new_ecoli': [4,2,3],
'new_led7digit': [1,4],
'new_winequality-red': [3,2],
'thyroid-newthyroid': [2,1]
}
def config_tuned_for_lda(config):
config['cmc'] = {'nn_config': {'units_1st_layer': 17,
'units_2nd_layer': 256,
'units_3rd_layer': 128,
'units_latent_layer': 8},
'weighted_triplet_loss': True,
'lr': 0.0001,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
config['dermatology'] = {'nn_config': {'units_1st_layer': 97,
'units_2nd_layer': 512,
'units_3rd_layer': 256,
'units_latent_layer': 16},
'weighted_triplet_loss': True,
'lr': 0.0015,
'batch_size': 16,
'gamma': 0.99,
'epochs': 150}
config['hayes-roth'] = {'nn_config': {'units_1st_layer': 11,
'units_2nd_layer': 128,
'units_3rd_layer': 64,
'units_latent_layer': 16},
'weighted_triplet_loss': True,
'lr': 0.0015,
'batch_size': 16,
'gamma': 0.99,
'epochs': 300}
config['new_vehicle'] = {'nn_config': {'units_1st_layer': 18,
'units_2nd_layer': 256,
'units_3rd_layer': 128,
'units_latent_layer': 16},
'weighted_triplet_loss': True,
'lr': 0.003,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
config['new_yeast'] = {'nn_config': {'units_1st_layer': 9,
'units_2nd_layer': 300,
'units_3rd_layer': 200,
'units_latent_layer': 12},
'weighted_triplet_loss': True,
'lr': 0.0004,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
config['balance-scale'] = {'nn_config': {'units_1st_layer': 16,
'units_2nd_layer': 256,
'units_3rd_layer': 128,
'units_latent_layer': 10},
'weighted_triplet_loss': True,
'lr': 0.004,
'batch_size': 16,
'gamma': 0.99,
'epochs': 200}
config['cleveland'] = {'nn_config': {'units_1st_layer': 24,
'units_2nd_layer': 72,
'units_3rd_layer': 48,
'units_latent_layer': 16},
'weighted_triplet_loss': True,
'lr': 0.0005,
'batch_size': 16,
'gamma': 0.99,
'epochs': 150}
config['cleveland_v2'] = {'nn_config': {'units_1st_layer': 23,
'units_2nd_layer': 256,
'units_3rd_layer': 128,
'units_latent_layer': 16},
'weighted_triplet_loss': True,
'lr': 0.0005,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
config['glass'] = {'nn_config': {'units_1st_layer': 9,
'units_2nd_layer': 256,
'units_3rd_layer': 128,
'units_latent_layer': 12},
'weighted_triplet_loss': True,
'lr': 0.0005,
'batch_size': 16,
'gamma': 0.99,
'epochs': 150}
config['thyroid-newthyroid'] = {'nn_config': {'units_1st_layer': 5,
'units_2nd_layer': 64,
'units_3rd_layer': 32,
'units_latent_layer': 8},
'weighted_triplet_loss': True,
'lr': 0.004,
'batch_size': 16,
'gamma': 0.99,
'epochs': 150}
config['new_ecoli'] = {'nn_config': {'units_1st_layer': 7,
'units_2nd_layer': 128,
'units_3rd_layer': 64,
'units_latent_layer': 12},
'weighted_triplet_loss': True,
'lr': 0.0005,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
config['new_led7digit'] = {'nn_config': {'units_1st_layer': 7,
'units_2nd_layer': 64,
'units_3rd_layer': 32,
'units_latent_layer': 8},
'weighted_triplet_loss': True,
'lr': 0.001,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
config['new_winequality-red'] = {'nn_config': {'units_1st_layer': 11,
'units_2nd_layer': 128,
'units_3rd_layer': 64,
'units_latent_layer': 12},
'weighted_triplet_loss': True,
'lr': 0.003,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
config['4delikatne-bezover-cut'] = {'nn_config': {'units_1st_layer': 2,
'units_2nd_layer': 128,
'units_3rd_layer': 64,
'units_latent_layer': 8},
'weighted_triplet_loss': True,
'lr': 0.003,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
config['3mocniej-cut'] = {'nn_config': {'units_1st_layer': 2,
'units_2nd_layer': 128,
'units_3rd_layer': 64,
'units_latent_layer': 10},
'weighted_triplet_loss': True,
'lr': 0.003,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
config['1czysty-cut'] = {'nn_config': {'units_1st_layer': 2,
'units_2nd_layer': 64,
'units_3rd_layer': 32,
'units_latent_layer': 8},
'weighted_triplet_loss': True,
'lr': 0.003,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
config['2delikatne-cut'] = {'nn_config': {'units_1st_layer': 2,
'units_2nd_layer': 128,
'units_3rd_layer': 64,
'units_latent_layer': 12},
'weighted_triplet_loss': True,
'lr': 0.003,
'batch_size': 16,
'gamma': 0.99,
'epochs': 100}
return config
def one_hot_encode_all(datasets):
ds_names = list(dt_name_to_cols_to_encode.keys())
for ds_name in ds_names:
k = ds_name
df = pd.DataFrame(data=datasets[k]['data'])
encoded = pd.get_dummies(df, columns=dt_name_to_cols_to_encode[ds_name], drop_first=True)
datasets[f"{k}_encoded"] = {'data': encoded.values, 'target': datasets[k]['target']}
return datasets
def config_calculation_strategy1(datasets):
config = {}
for dataset_name in datasets:
data, target = datasets[dataset_name]['data'], datasets[dataset_name]['target']
neural_net_config = {
"units_1st_layer": data.shape[1],
"units_2nd_layer": data.shape[1] * 2,
"units_3rd_layer": data.shape[1],
"units_latent_layer": data.shape[1] // 2
}
config[dataset_name] = {
"nn_config": neural_net_config,
"weighted_triplet_loss": True
}
return config
def config_calculation_strategy2(datasets):
config = {}
for dataset_name in datasets:
data, target = datasets[dataset_name]['data'], datasets[dataset_name]['target']
neural_net_config = {
"units_1st_layer": data.shape[1],
"units_2nd_layer": max(16, data.shape[1] * 3),
"units_3rd_layer": max(data.shape[1] * 2, 8),
"units_latent_layer": max(4, data.shape[1] // 2)
}
config[dataset_name] = {
"nn_config": neural_net_config,
"weighted_triplet_loss": True,
"lr": 1e-4,
"batch_size": 32,
"gamma": 0.95,
"epochs": 100
}
return config
def config_calculation_strategy3(datasets):
config = {}
for dataset_name in datasets:
data, target = datasets[dataset_name]['data'], datasets[dataset_name]['target']
neural_net_config = {
"units_1st_layer": data.shape[1],
"units_2nd_layer": max(16, data.shape[1] * 3),
"units_3rd_layer": max(data.shape[1] * 2, 8),
"units_latent_layer": max(4, data.shape[1] // 2),
"units_decision_layer": np.unique(target).size
}
config[dataset_name] = {
"nn_config": neural_net_config,
"weighted_triplet_loss": True,
"lr": 0.003,
"batch_size": 16,
"gamma": 0.99,
"epochs": 100
}
return config
def weights_calculation_strategy1(X_train, y_train):
# Inverse class frequencies, normalized
cards = Counter(y_train)
# weights = {c: (1/v) * 100 for c,v in cards.items()}
weights = {c: 1/v for c, v in cards.items()}
weights_normalized = {c: weights[c]/sum(weights.values()) for c in cards.keys()}
print(f"Class cardinalities: {cards}")
print(f"Weights: {weights_normalized}")
return weights_normalized
class EmbeddingNet(nn.Module):
def __init__(self, nn_config):
super(EmbeddingNet, self).__init__()
self.fc = nn.Sequential(nn.Linear(nn_config["units_1st_layer"], nn_config["units_2nd_layer"]),
nn.PReLU(),
nn.Linear(nn_config["units_2nd_layer"], nn_config["units_3rd_layer"]),
nn.PReLU(),
nn.Linear(nn_config["units_3rd_layer"], nn_config["units_latent_layer"])
)
def forward(self, x):
output = self.fc(x)
return output
def embed(self, x):
return self.forward(x)
class TripletNet(nn.Module):
def __init__(self, embedding_net):
super(TripletNet, self).__init__()
self.embedding_net = embedding_net
def forward(self, x1, x2, x3):
output1 = self.embedding_net(x1)
output2 = self.embedding_net(x2)
output3 = self.embedding_net(x3)
return output1, output2, output3
def embed(self, x):
return self.embedding_net(x)
class ClassificationNet(nn.Module):
def __init__(self, embedding_net, nn_config):
super(ClassificationNet, self).__init__()
self.embedding_net = embedding_net
self.n_classes = nn_config["units_decision_layer"]
self.nonlinear = nn.PReLU()
self.fc1 = nn.Linear(nn_config["units_latent_layer"], nn_config["units_decision_layer"])
def forward(self, x):
output = self.embedding_net(x)
output = self.nonlinear(output)
scores = F.log_softmax(self.fc1(output), dim=-1)
return scores
def embed(self, x):
return self.embedding_net(x)
def train_classification_net(model, device, train_loader, optimizer):
model.train()
train_loss = []
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target.long())
loss.backward()
optimizer.step()
train_loss.append(loss.item())
return np.mean(train_loss)
def test_classification_net(model, device, test_loader):
model.eval()
test_loss = []
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
loss = F.nll_loss(output, target.long())
test_loss.append(loss.item())
return np.mean(test_loss)
def train_tripletnet(model, device, train_loader, optimizer, epoch, weights, nn_config, log_interval=10, dry_run=False):
model.train()
train_loss = []
for batch_idx, (data, target) in enumerate(train_loader):
data = (data,)
data = tuple(d.cuda() for d in data)
target = target.cuda()
optimizer.zero_grad()
outputs = model(*data)
outputs = (outputs,)
loss_inputs = outputs
target = (target,)
loss_inputs += target
margin = 1.0
loss_fn = OnlineTripletLoss(margin=margin, triplet_selector=SemihardNegativeTripletSelector(margin))
loss_outputs = loss_fn(*loss_inputs)
loss = loss_outputs[0] if type(loss_outputs) in (tuple, list) else loss_outputs
loss.backward()
optimizer.step()
train_loss.append(loss.item())
return np.mean(train_loss)
def test_tripletnet(model, device, test_loader, weights, nn_config):
model.eval()
test_loss = []
correct = 0
with torch.no_grad():
for data, target in test_loader:
# print(data)
# print(target)
data = (data,)
data = tuple(d.cuda() for d in data)
target = target.cuda()
outputs = model(*data)
outputs = (outputs,)
loss_inputs = outputs
target = (target,)
loss_inputs += target
margin = 1.0
loss_fn = OnlineTripletLoss(margin=margin, triplet_selector=SemihardNegativeTripletSelector(margin))
loss_outputs = loss_fn(*loss_inputs)
loss = loss_outputs[0] if type(loss_outputs) in (tuple, list) else loss_outputs
test_loss.append(loss.item())
# print(test_loss)
return np.mean(test_loss)
def train_triplets(X_train, y_train, X_test, y_test, weights, cfg):
seed = 3
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed = 7
batch_size = cfg["batch_size"]
test_batch_size = cfg["batch_size"]
use_cuda = True
lr = cfg["lr"]
gamma = cfg["gamma"]
epochs = cfg["epochs"]
save_model = True
log_interval = 20
nn_config = cfg["nn_config"]
torch.manual_seed(seed)
device = torch.device("cuda" if use_cuda else "cpu")
train_kwargs, test_kwargs = {}, {}
if use_cuda:
cuda_kwargs = {'num_workers': 1,
'pin_memory': True}
train_kwargs.update(cuda_kwargs)
test_kwargs.update(cuda_kwargs)
dataset1 = TensorDataset(torch.Tensor(X_train), torch.Tensor(y_train))
dataset1.train_data = torch.Tensor(X_train)
dataset1.train_labels = torch.Tensor(y_train)
dataset1.train = True
dataset2 = TensorDataset(torch.Tensor(X_test), torch.Tensor(y_test))
dataset2.test_data = torch.Tensor(X_test)
dataset2.test_labels = torch.Tensor(y_test)
dataset2.train = False
triplet_train_dataset = TripletDataset(dataset1)
triplet_test_dataset = TripletDataset(dataset2)
triplet_train_loader = torch.utils.data.DataLoader(triplet_train_dataset, **train_kwargs)
triplet_test_loader = torch.utils.data.DataLoader(triplet_test_dataset, **test_kwargs)
n_classes = np.unique(y_train).size
train_batch_sampler = BalancedBatchSampler(dataset1.train_labels, n_classes=n_classes, n_samples=max(1,batch_size//n_classes), name="train")
test_batch_sampler = BalancedBatchSampler(dataset2.test_labels, n_classes=n_classes, n_samples=max(1,test_batch_size//n_classes), name="test")
online_train_loader = torch.utils.data.DataLoader(dataset1, batch_sampler=train_batch_sampler, **train_kwargs)
online_test_loader = torch.utils.data.DataLoader(dataset2, batch_sampler=test_batch_sampler, **test_kwargs)
embedding_net = EmbeddingNet(nn_config)
# model = TripletNet(embedding_net).to(device)
model = embedding_net.to(device)
optimizer = optim.Adam(model.parameters(), lr=lr)
scheduler = StepLR(optimizer, step_size=1, gamma=gamma)
test_losses = []
train_losses = []
for epoch in range(1, epochs + 1):
train_losses.append(train_tripletnet(model, device, online_train_loader, optimizer, epoch, weights, nn_config, log_interval))
test_losses.append(test_tripletnet(model, device, online_test_loader, weights, nn_config))
scheduler.step()
if save_model:
torch.save(model.state_dict(), "mnist_cnn_triplet.pt")
test_loader = torch.utils.data.DataLoader(dataset2, batch_size=1)
train_loader = torch.utils.data.DataLoader(dataset1, batch_size=1)
embeddings_train, _ = calc_embeddings(model, device, train_loader)
embeddings_test, _ = calc_embeddings(model, device, test_loader)
plt.plot(test_losses, label="test losses")
plt.plot(train_losses, label="train losses")
plt.legend()
plt.show()
return embeddings_train, embeddings_test
def train_classification(X_train, y_train, X_test, y_test, cfg):
seed = 3
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed = 7
batch_size = cfg["batch_size"]
test_batch_size = cfg["batch_size"]
use_cuda = True
lr = cfg["lr"]
gamma = cfg["gamma"]
epochs = cfg["epochs"]
save_model = True
log_interval = 20
nn_config = cfg["nn_config"]
torch.manual_seed(seed)
device = torch.device("cuda" if use_cuda else "cpu")
train_kwargs = {'batch_size': batch_size}
test_kwargs = {'batch_size': test_batch_size}
if use_cuda:
cuda_kwargs = {'num_workers': 1,
'pin_memory': True,
'shuffle': True}
train_kwargs.update(cuda_kwargs)
test_kwargs.update(cuda_kwargs)
dataset1 = TensorDataset(torch.Tensor(X_train), torch.Tensor(y_train))
dataset1.train_data = torch.Tensor(X_train)
dataset1.train_labels = torch.Tensor(y_train)
dataset1.train = True
dataset2 = TensorDataset(torch.Tensor(X_test), torch.Tensor(y_test))
dataset2.test_data = torch.Tensor(X_test)
dataset2.test_labels = torch.Tensor(y_test)
dataset2.train = False
train_loader = torch.utils.data.DataLoader(dataset1, **train_kwargs)
test_loader = torch.utils.data.DataLoader(dataset2, **test_kwargs)
embedding_net = EmbeddingNet(nn_config)
model = ClassificationNet(embedding_net, nn_config).to(device)
optimizer = optim.Adam(model.parameters(), lr=lr)
scheduler = StepLR(optimizer, step_size=1, gamma=gamma)
test_losses = []
train_losses = []
for epoch in range(1, epochs + 1):
train_losses.append(train_classification_net(model, device, train_loader, optimizer))
test_losses.append(test_classification_net(model, device, test_loader))
scheduler.step()
if save_model:
torch.save(model.state_dict(), "mnist_cnn.pt")
plt.plot(test_losses, label="test losses")
plt.plot(train_losses, label="train losses")
plt.legend()
plt.show()
return model
class BalancedBatchSampler(BatchSampler):
"""
samples n_classes and within these classes samples n_samples
Returns batches of size n_classes * n_samples
"""
def __init__(self, labels, n_classes, n_samples, name):
self.name=name
self.labels = labels
self.labels_set = list(set(self.labels.numpy()))
self.label_to_indices = {label: np.where(self.labels.numpy() == label)[0]
for label in self.labels_set}
for l in self.labels_set:
np.random.shuffle(self.label_to_indices[l])
self.used_label_indices_count = {label: 0 for label in self.labels_set}
self.count = 0
self.n_classes = n_classes
self.n_samples = n_samples
self.n_dataset = len(self.labels)
# print(f"{self.name}: {self.n_dataset}")
# print(f"{self.name}: {self.n_classes} x {self.n_samples}")
self.batch_size = self.n_samples * self.n_classes
def __iter__(self):
self.count = 0
while self.count + self.batch_size < self.n_dataset:
# print(self.name)
classes = np.random.choice(self.labels_set, self.n_classes, replace=False)
indices = []
for class_ in classes:
indices.extend(self.label_to_indices[class_][
self.used_label_indices_count[class_]:self.used_label_indices_count[
class_] + self.n_samples])
self.used_label_indices_count[class_] += self.n_samples
if self.used_label_indices_count[class_] + self.n_samples > len(self.label_to_indices[class_]):
np.random.shuffle(self.label_to_indices[class_])
self.used_label_indices_count[class_] = 0
yield indices
self.count += self.n_classes * self.n_samples
def __len__(self):
return self.n_dataset // self.batch_size
class OnlineTripletLoss(nn.Module):
def __init__(self, margin, triplet_selector):
super(OnlineTripletLoss, self).__init__()
self.margin = margin
self.triplet_selector = triplet_selector
def forward(self, embeddings, target):
triplets = self.triplet_selector.get_triplets(embeddings, target)
if embeddings.is_cuda:
triplets = triplets.cuda()
ap_distances = (embeddings[triplets[:, 0]] - embeddings[triplets[:, 1]]).pow(2).sum(1) # .pow(.5)
an_distances = (embeddings[triplets[:, 0]] - embeddings[triplets[:, 2]]).pow(2).sum(1) # .pow(.5)
losses = F.relu(ap_distances - an_distances + self.margin)
return losses.mean(), len(triplets)
def pdist(vectors):
distance_matrix = -2 * vectors.mm(torch.t(vectors)) + vectors.pow(2).sum(dim=1).view(1, -1) + vectors.pow(2).sum(
dim=1).view(-1, 1)
return distance_matrix
class TripletSelector:
"""
Implementation should return indices of anchors, positive and negative samples
return np array of shape [N_triplets x 3]
"""
def __init__(self):
pass
def get_triplets(self, embeddings, labels):
raise NotImplementedError
class AllTripletSelector(TripletSelector):
"""
Returns all possible triplets
May be impractical in most cases
"""
def __init__(self):
super(AllTripletSelector, self).__init__()
def get_triplets(self, embeddings, labels):
labels = labels.cpu().data.numpy()
triplets = []
for label in set(labels):
label_mask = (labels == label)
label_indices = np.where(label_mask)[0]
if len(label_indices) < 2:
continue
negative_indices = np.where(np.logical_not(label_mask))[0]
anchor_positives = list(combinations(label_indices, 2)) # All anchor-positive pairs
# Add all negatives for all positive pairs
temp_triplets = [[anchor_positive[0], anchor_positive[1], neg_ind] for anchor_positive in anchor_positives
for neg_ind in negative_indices]
triplets += temp_triplets
return torch.LongTensor(np.array(triplets))
def hardest_negative(loss_values):
hard_negative = np.argmax(loss_values)
return hard_negative if loss_values[hard_negative] > 0 else None
def random_hard_negative(loss_values):
hard_negatives = np.where(loss_values > 0)[0]
return np.random.choice(hard_negatives) if len(hard_negatives) > 0 else None
def semihard_negative(loss_values, margin):
semihard_negatives = np.where(np.logical_and(loss_values < margin, loss_values > 0))[0]
return np.random.choice(semihard_negatives) if len(semihard_negatives) > 0 else None
class FunctionNegativeTripletSelector(TripletSelector):
"""
For each positive pair, takes the hardest negative sample (with the greatest triplet loss value) to create a triplet
Margin should match the margin used in triplet loss.
negative_selection_fn should take array of loss_values for a given anchor-positive pair and all negative samples
and return a negative index for that pair
"""
def __init__(self, margin, negative_selection_fn, cpu=True):
super(FunctionNegativeTripletSelector, self).__init__()
self.cpu = cpu
self.margin = margin
self.negative_selection_fn = negative_selection_fn
def get_triplets(self, embeddings, labels):
if self.cpu:
embeddings = embeddings.cpu()
distance_matrix = pdist(embeddings)
distance_matrix = distance_matrix.cpu()
labels = labels.cpu().data.numpy()
triplets = []
for label in set(labels):
label_mask = (labels == label)
label_indices = np.where(label_mask)[0]
if len(label_indices) < 2:
continue
negative_indices = np.where(np.logical_not(label_mask))[0]
anchor_positives = list(combinations(label_indices, 2)) # All anchor-positive pairs
anchor_positives = np.array(anchor_positives)
ap_distances = distance_matrix[anchor_positives[:, 0], anchor_positives[:, 1]]
for anchor_positive, ap_distance in zip(anchor_positives, ap_distances):
loss_values = ap_distance - distance_matrix[torch.LongTensor(np.array([anchor_positive[0]])), torch.LongTensor(negative_indices)] + self.margin
loss_values = loss_values.data.cpu().numpy()
hard_negative = self.negative_selection_fn(loss_values)
if hard_negative is not None:
hard_negative = negative_indices[hard_negative]
triplets.append([anchor_positive[0], anchor_positive[1], hard_negative])
if len(triplets) == 0:
triplets.append([anchor_positive[0], anchor_positive[1], negative_indices[0]])
triplets = np.array(triplets)
return torch.LongTensor(triplets)
def HardestNegativeTripletSelector(margin, cpu=False): return FunctionNegativeTripletSelector(margin=margin,
negative_selection_fn=hardest_negative,
cpu=cpu)
def RandomNegativeTripletSelector(margin, cpu=False): return FunctionNegativeTripletSelector(margin=margin,
negative_selection_fn=random_hard_negative,
cpu=cpu)
def SemihardNegativeTripletSelector(margin, cpu=False): return FunctionNegativeTripletSelector(margin=margin,
negative_selection_fn=lambda x: semihard_negative(x, margin),
cpu=cpu)
class AverageNonzeroTripletsMetric:
'''
Counts average number of nonzero triplets found in minibatches
'''
def __init__(self):
self.values = []
def __call__(self, outputs, target, loss):
self.values.append(loss[1])
return self.value()
def reset(self):
self.values = []
def value(self):
return np.mean(self.values)
def name(self):
return 'Average nonzero triplets'