-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathHardNetHPatchesSplits.py
710 lines (616 loc) · 28.7 KB
/
HardNetHPatchesSplits.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
#!/usr/bin/python2 utt
# -*- coding: utf-8 -*-
"""
This is HardNet local patch descriptor. The training code is based on PyTorch TFeat implementation
https://github.com/edgarriba/examples/tree/master/triplet
by Edgar Riba.
If you use this code, please cite
@article{HardNet2017,
author = {Anastasiya Mishchuk, Dmytro Mishkin, Filip Radenovic, Jiri Matas},
title = "{Working hard to know your neighbor's margins:Local descriptor learning loss}",
year = 2017}
(c) 2017 by Anastasiia Mishchuk, Dmytro Mishkin
"""
from __future__ import division, print_function
import sys
from copy import deepcopy
import argparse
import torch
import torch.nn.init
import torch.nn as nn
import torch.optim as optim
import torchvision.datasets as dset
import torchvision.transforms as transforms
from torch.autograd import Variable
import torch.backends.cudnn as cudnn
import os
from tqdm import tqdm
import numpy as np
import random
import cv2
import PIL
import math
import copy
from EvalMetrics import ErrorRateAt95Recall#, ErrorRateFDRAt95Recall, convertFDR2FPR, convertFPR2FDR
from Losses import loss_HardNet, loss_random_sampling, loss_L2Net, global_orthogonal_regularization
from W1BS import w1bs_extract_descs_and_save
from Utils import L2Norm, cv2_scale, np_reshape
from Utils import str2bool
import torch.nn as nn
import torch.utils.data as data
class CorrelationPenaltyLoss(nn.Module):
def __init__(self):
super(CorrelationPenaltyLoss, self).__init__()
def forward(self, input):
mean1 = torch.mean(input, dim=0)
zeroed = input - mean1.expand_as(input)
cor_mat = torch.bmm(torch.t(zeroed).unsqueeze(0), zeroed.unsqueeze(0)).squeeze(0)
d = torch.diag(torch.diag(cor_mat))
no_diag = cor_mat - d
d_sq = no_diag * no_diag
return torch.sqrt(d_sq.sum()) / input.size(0)
# Training settings
parser = argparse.ArgumentParser(description='PyTorch HardNet')
# Model options
parser.add_argument('--w1bsroot', type=str,
default='data/sets/wxbs-descriptors-benchmark/code/',
help='path to dataset')
parser.add_argument('--hpatches-split', type=str,
default='data/sets/',
help='path to HPatches split generated by HPatchesDatasetCreator')
parser.add_argument('--dataroot', type=str,
default='data/sets/',
help='path to Brown datasets for testing')
parser.add_argument('--enable-logging', type=str2bool, default=False,
help='output to tensorlogger')
parser.add_argument('--log-dir', default='data/logs/',
help='folder to output log')
parser.add_argument('--model-dir', default='data/models/',
help='folder to output model checkpoints')
parser.add_argument('--experiment-name', default='/multiple_datasets_all/',
help='experiment path')
parser.add_argument('--training-set', default='all',
help='Other options: notredame, yosemite')
parser.add_argument('--loss', default='triplet_margin',
help='Other options: softmax, contrastive')
parser.add_argument('--batch-reduce', default='min',
help='Other options: average, random, random_global, L2Net')
parser.add_argument('--num-workers', default=1, type=int,
help='Number of workers to be created')
parser.add_argument('--pin-memory', type=bool, default=True,
help='')
parser.add_argument('--decor', type=str2bool, default=False,
help='L2Net decorrelation penalty')
parser.add_argument('--anchorave', type=str2bool, default=False,
help='anchorave')
parser.add_argument('--imageSize', type=int, default=32,
help='the height / width of the input image to network')
parser.add_argument('--mean-image', type=float, default=0.443728476019,
help='mean of train dataset for normalization')
parser.add_argument('--std-image', type=float, default=0.20197947209,
help='std of train dataset for normalization')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to latest checkpoint (default: none)')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
help='manual epoch number (useful on restarts)')
parser.add_argument('--epochs', type=int, default=10, metavar='E',
help='number of epochs to train (default: 10)')
parser.add_argument('--anchorswap', type=bool, default=True,
help='turns on anchor swap')
parser.add_argument('--batch-size', type=int, default=1024, metavar='BS',
help='input batch size for training (default: 1024)')
parser.add_argument('--test-batch-size', type=int, default=1024, metavar='BST',
help='input batch size for testing (default: 1024)')
parser.add_argument('--n-triplets', type=int, default=15000000, metavar='N',
help='how many triplets will generate from the dataset')
parser.add_argument('--margin', type=float, default=1.0, metavar='MARGIN',
help='the margin value for the triplet loss function (default: 1.0')
parser.add_argument('--gor', type=str2bool, default=False,
help='use gor')
parser.add_argument('--alpha', type=float, default=1.0, metavar='ALPHA',
help='gor parameter')
parser.add_argument('--act-decay', type=float, default=0,
help='activity L2 decay, default 0')
parser.add_argument('--lr', type=float, default=10.0, metavar='LR',
help='learning rate (default: 10.0)')
parser.add_argument('--fliprot', type=str2bool, default=True,
help='turns on flip and 90deg rotation augmentation')
parser.add_argument('--augmentation', type=str2bool, default=False,
help='turns on shift and small scale rotation augmentation')
parser.add_argument('--lr-decay', default=1e-6, type=float, metavar='LRD',
help='learning rate decay ratio (default: 1e-6')
parser.add_argument('--wd', default=1e-4, type=float,
metavar='W', help='weight decay (default: 1e-4)')
parser.add_argument('--optimizer', default='sgd', type=str,
metavar='OPT', help='The optimizer to use (default: SGD)')
# Device options
parser.add_argument('--no-cuda', action='store_true', default=False,
help='enables CUDA training')
parser.add_argument('--gpu-id', default='0', type=str,
help='id(s) for CUDA_VISIBLE_DEVICES')
parser.add_argument('--seed', type=int, default=0, metavar='S',
help='random seed (default: 0)')
parser.add_argument('--log-interval', type=int, default=10, metavar='LI',
help='how many batches to wait before logging training status')
args = parser.parse_args()
suffix = '{}_{}'.format(args.training_set, args.batch_reduce)
if args.gor:
suffix = suffix + '_gor_alpha{:1.1f}'.format(args.alpha)
if args.anchorswap:
suffix = suffix + '_as'
if args.anchorave:
suffix = suffix + '_av'
triplet_flag = (args.batch_reduce == 'random_global') or args.gor
dataset_names = ['liberty', 'notredame', 'yosemite']
TEST_ON_W1BS = False
# check if path to w1bs dataset testing module exists
if os.path.isdir(args.w1bsroot):
sys.path.insert(0, args.w1bsroot)
import utils.w1bs as w1bs
TEST_ON_W1BS = True
# set the device to use by setting CUDA_VISIBLE_DEVICES env variable in
# order to prevent any memory allocation on unused GPUs
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id
args.cuda = not args.no_cuda and torch.cuda.is_available()
if args.cuda:
cudnn.benchmark = True
torch.cuda.manual_seed_all(args.seed)
# create loggin directory
if not os.path.exists(args.log_dir):
os.makedirs(args.log_dir)
# set random seeds
torch.manual_seed(args.seed)
np.random.seed(args.seed)
class TotalDatasetsLoader(data.Dataset):
def __init__(self, datasets_path, train = True, transform = None, batch_size = None, n_triplets = 5000000, fliprot = False, *arg, **kw):
super(TotalDatasetsLoader, self).__init__()
#datasets_path = [os.path.join(datasets_path, dataset) for dataset in os.listdir(datasets_path) if '.pt' in dataset]
datasets_path = [datasets_path]
datasets = [torch.load(dataset) for dataset in datasets_path]
print (datasets_path)
data, labels = datasets[0][0], datasets[0][1]
for i in range(1,len(datasets)):
data = torch.cat([data,datasets[i][0]])
labels = torch.cat([labels, datasets[i][1]+torch.max(labels)+1])
del datasets
self.data, self.labels = data, labels
self.transform = transform
self.train = train
self.n_triplets = n_triplets
self.batch_size = batch_size
self.fliprot = fliprot
if self.train:
print('Generating {} triplets'.format(self.n_triplets))
self.triplets = self.generate_triplets(self.labels, self.n_triplets, self.batch_size)
@staticmethod
def generate_triplets(labels, num_triplets, batch_size):
def create_indices(_labels):
inds = dict()
for idx, ind in enumerate(_labels):
if ind not in inds:
inds[ind] = []
inds[ind].append(idx)
return inds
triplets = []
indices = create_indices(labels.numpy())
unique_labels = np.unique(labels.numpy())
n_classes = unique_labels.shape[0]
# add only unique indices in batch
already_idxs = set()
for x in tqdm(range(num_triplets)):
if len(already_idxs) >= batch_size:
already_idxs = set()
c1 = np.random.randint(0, n_classes)
while c1 in already_idxs:
c1 = np.random.randint(0, n_classes)
already_idxs.add(c1)
c2 = np.random.randint(0, n_classes)
while c1 == c2:
c2 = np.random.randint(0, n_classes)
if len(indices[c1]) == 2: # hack to speed up process
n1, n2 = 0, 1
else:
n1 = np.random.randint(0, len(indices[c1]))
n2 = np.random.randint(0, len(indices[c1]))
while n1 == n2:
n2 = np.random.randint(0, len(indices[c1]))
n3 = np.random.randint(0, len(indices[c2]))
triplets.append([indices[c1][n1], indices[c1][n2], indices[c2][n3]])
return torch.LongTensor(np.array(triplets))
def __getitem__(self, index):
def transform_img(img):
if self.transform is not None:
img = self.transform(img.numpy())
return img
t = self.triplets[index]
a, p, n = self.data[t[0]], self.data[t[1]], self.data[t[2]]
img_a = transform_img(a)
img_p = transform_img(p)
# transform images if required
if self.fliprot:
do_flip = random.random() > 0.5
do_rot = random.random() > 0.5
if do_rot:
img_a = img_a.permute(0,2,1)
img_p = img_p.permute(0,2,1)
if do_flip:
img_a = torch.from_numpy(deepcopy(img_a.numpy()[:,:,::-1]))
img_p = torch.from_numpy(deepcopy(img_p.numpy()[:,:,::-1]))
return img_a, img_p
def __len__(self):
if self.train:
return self.triplets.size(0)
class TripletPhotoTour(dset.PhotoTour):
"""
From the PhotoTour Dataset it generates triplet samples
note: a triplet is composed by a pair of matching images and one of
different class.
"""
def __init__(self, train=True, transform=None, n_triplets = 1000, batch_size=None, load_random_triplets=False, *arg, **kw):
super(TripletPhotoTour, self).__init__(*arg, **kw)
self.transform = transform
self.out_triplets = load_random_triplets
self.train = train
self.n_triplets = n_triplets
self.batch_size = batch_size
if self.train:
print('Generating {} triplets'.format(self.n_triplets))
self.triplets = self.generate_triplets(self.labels, self.n_triplets)
@staticmethod
def generate_triplets(labels, num_triplets):
def create_indices(_labels):
inds = dict()
for idx, ind in enumerate(_labels):
if ind not in inds:
inds[ind] = []
inds[ind].append(idx)
return inds
triplets = []
indices = create_indices(labels.numpy())
unique_labels = np.unique(labels.numpy())
n_classes = unique_labels.shape[0]
# add only unique indices in batch
already_idxs = set()
for x in tqdm(range(num_triplets)):
if len(already_idxs) >= args.batch_size:
already_idxs = set()
c1 = np.random.randint(0, n_classes - 1)
while c1 in already_idxs:
c1 = np.random.randint(0, n_classes - 1)
already_idxs.add(c1)
c2 = np.random.randint(0, n_classes - 1)
while c1 == c2:
c2 = np.random.randint(0, n_classes - 1)
if len(indices[c1]) == 2: # hack to speed up process
n1, n2 = 0, 1
else:
n1 = np.random.randint(0, len(indices[c1]) - 1)
n2 = np.random.randint(0, len(indices[c1]) - 1)
while n1 == n2:
n2 = np.random.randint(0, len(indices[c1]) - 1)
n3 = np.random.randint(0, len(indices[c2]) - 1)
triplets.append([indices[c1][n1], indices[c1][n2], indices[c2][n3]])
return torch.LongTensor(np.array(triplets))
def __getitem__(self, index):
def transform_img(img):
if self.transform is not None:
img = self.transform(img.numpy())
return img
if not self.train:
m = self.matches[index]
img1 = transform_img(self.data[m[0]])
img2 = transform_img(self.data[m[1]])
return img1, img2, m[2]
t = self.triplets[index]
a, p, n = self.data[t[0]], self.data[t[1]], self.data[t[2]]
img_a = transform_img(a)
img_p = transform_img(p)
img_n = None
if self.out_triplets:
img_n = transform_img(n)
# transform images if required
if args.fliprot:
do_flip = random.random() > 0.5
do_rot = random.random() > 0.5
if do_rot:
img_a = img_a.permute(0, 2, 1)
img_p = img_p.permute(0, 2, 1)
if self.out_triplets:
img_n = img_n.permute(0, 2, 1)
if do_flip:
img_a = torch.from_numpy(deepcopy(img_a.numpy()[:, :, ::-1]))
img_p = torch.from_numpy(deepcopy(img_p.numpy()[:, :, ::-1]))
if self.out_triplets:
img_n = torch.from_numpy(deepcopy(img_n.numpy()[:, :, ::-1]))
if self.out_triplets:
return (img_a, img_p, img_n)
else:
return (img_a, img_p)
def __len__(self):
if self.train:
return self.triplets.size(0)
else:
return self.matches.size(0)
class HardNet(nn.Module):
"""HardNet model definition
"""
def __init__(self):
super(HardNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(32, affine=False),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(32, affine=False),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(64, affine=False),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(64, affine=False),
nn.ReLU(),
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(128, affine=False),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(128, affine=False),
nn.ReLU(),
nn.Dropout(0.3),
nn.Conv2d(128, 128, kernel_size=8, bias=False),
nn.BatchNorm2d(128, affine=False),
)
self.features.apply(weights_init)
return
def input_norm(self, x):
flat = x.view(x.size(0), -1)
mp = torch.mean(flat, dim=1)
sp = torch.std(flat, dim=1) + 1e-7
return (x - mp.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1).expand_as(x)) / sp.unsqueeze(-1).unsqueeze(
-1).unsqueeze(1).expand_as(x)
def forward(self, input):
x_features = self.features(self.input_norm(input))
x = x_features.view(x_features.size(0), -1)
return L2Norm()(x)
def weights_init(m):
if isinstance(m, nn.Conv2d):
nn.init.orthogonal(m.weight.data, gain=0.6)
try:
nn.init.constant(m.bias.data, 0.01)
except:
pass
return
def create_loaders(load_random_triplets=False):
test_dataset_names = copy.copy(dataset_names)
#test_dataset_names.remove(args.training_set)
kwargs = {'num_workers': args.num_workers, 'pin_memory': args.pin_memory} if args.cuda else {}
np_reshape64 = lambda x: np.reshape(x, (64, 64, 1))
transform_test = transforms.Compose([
transforms.Lambda(np_reshape64),
transforms.ToPILImage(),
transforms.Resize(32),
transforms.ToTensor()])
transform_train = transforms.Compose([
transforms.Lambda(np_reshape64),
transforms.ToPILImage(),
transforms.RandomRotation(5,PIL.Image.BILINEAR),
transforms.RandomResizedCrop(32, scale = (0.9,1.0),ratio = (0.9,1.1)),
transforms.Resize(32),
transforms.ToTensor()])
transform = transforms.Compose([
transforms.Lambda(cv2_scale),
transforms.Lambda(np_reshape),
transforms.ToTensor(),
transforms.Normalize((args.mean_image,), (args.std_image,))])
if not args.augmentation:
transform_train = transform
transform_test = transform
train_loader = torch.utils.data.DataLoader(
TotalDatasetsLoader(train=True,
load_random_triplets=load_random_triplets,
batch_size=args.batch_size,
datasets_path=args.hpatches_split,
fliprot=args.fliprot,
n_triplets=args.n_triplets,
name=args.training_set,
download=True,
transform=transform_train),
batch_size=args.batch_size,
shuffle=False, **kwargs)
test_loaders = [{'name': name,
'dataloader': torch.utils.data.DataLoader(
TripletPhotoTour(train=False,
batch_size=args.test_batch_size,
n_triplets = 1000,
root=args.dataroot,
name=name,
download=True,
transform=transform_test),
batch_size=args.test_batch_size,
shuffle=False, **kwargs)}
for name in test_dataset_names]
return train_loader, test_loaders
def train(train_loader, model, optimizer, epoch, logger, load_triplets=False):
# switch to train mode
model.train()
pbar = tqdm(enumerate(train_loader))
for batch_idx, data in pbar:
if load_triplets:
data_a, data_p, data_n = data
else:
data_a, data_p = data
if args.cuda:
data_a, data_p = data_a.cuda(), data_p.cuda()
data_a, data_p = Variable(data_a), Variable(data_p)
out_a = model(data_a)
out_p = model(data_p)
if load_triplets:
data_n = data_n.cuda()
data_n = Variable(data_n)
out_n = model(data_n)
if args.batch_reduce == 'L2Net':
loss = loss_L2Net(out_a, out_p, anchor_swap=args.anchorswap,
margin=args.margin, loss_type=args.loss)
elif args.batch_reduce == 'random_global':
loss = loss_random_sampling(out_a, out_p, out_n,
margin=args.margin,
anchor_swap=args.anchorswap,
loss_type=args.loss)
else:
loss = loss_HardNet(out_a, out_p,
margin=args.margin,
anchor_swap=args.anchorswap,
anchor_ave=args.anchorave,
batch_reduce=args.batch_reduce,
loss_type=args.loss)
if args.decor:
loss += CorrelationPenaltyLoss()(out_a)
if args.gor:
loss += args.alpha * global_orthogonal_regularization(out_a, out_n)
optimizer.zero_grad()
loss.backward()
optimizer.step()
adjust_learning_rate(optimizer)
if batch_idx % args.log_interval == 0:
pbar.set_description(
'Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data_a), len(train_loader.dataset),
100. * batch_idx / len(train_loader),
loss.data[0]))
if (args.enable_logging):
logger.log_value('loss', loss.data[0]).step()
try:
os.stat('{}{}'.format(args.model_dir, suffix))
except:
os.makedirs('{}{}'.format(args.model_dir, suffix))
torch.save({'epoch': epoch + 1, 'state_dict': model.state_dict()},
'{}{}/checkpoint_{}.pth'.format(args.model_dir, suffix, epoch))
def test(test_loader, model, epoch, logger, logger_test_name):
# switch to evaluate mode
model.eval()
labels, distances = [], []
pbar = tqdm(enumerate(test_loader))
for batch_idx, (data_a, data_p, label) in pbar:
if args.cuda:
data_a, data_p = data_a.cuda(), data_p.cuda()
data_a, data_p, label = Variable(data_a, volatile=True), \
Variable(data_p, volatile=True), Variable(label)
out_a = model(data_a)
out_p = model(data_p)
dists = torch.sqrt(torch.sum((out_a - out_p) ** 2, 1)) # euclidean distance
distances.append(dists.data.cpu().numpy().reshape(-1, 1))
ll = label.data.cpu().numpy().reshape(-1, 1)
labels.append(ll)
if batch_idx % args.log_interval == 0:
pbar.set_description(logger_test_name + ' Test Epoch: {} [{}/{} ({:.0f}%)]'.format(
epoch, batch_idx * len(data_a), len(test_loader.dataset),
100. * batch_idx / len(test_loader)))
num_tests = test_loader.dataset.matches.size(0)
labels = np.vstack(labels).reshape(num_tests)
distances = np.vstack(distances).reshape(num_tests)
fpr95 = ErrorRateAt95Recall(labels, 1.0 / (distances + 1e-8))
#fdr95 = ErrorRateFDRAt95Recall(labels, 1.0 / (distances + 1e-8))
#fpr2 = convertFDR2FPR(fdr95, 0.95, 50000, 50000)
#fpr2fdr = convertFPR2FDR(fpr2, 0.95, 50000, 50000)
#print('\33[91mTest set: Accuracy(FDR95): {:.8f}\n\33[0m'.format(fdr95))
print('\33[91mTest set: Accuracy(FPR95): {:.8f}\n\33[0m'.format(fpr95))
#print('\33[91mTest set: Accuracy(FDR2FPR): {:.8f}\n\33[0m'.format(fpr2))
#print('\33[91mTest set: Accuracy(FPR2FDR): {:.8f}\n\33[0m'.format(fpr2fdr))
#fpr2 = convertFDR2FPR(round(fdr95,2), 0.95, 50000, 50000)
#fpr2fdr = convertFPR2FDR(round(fpr2,2), 0.95, 50000, 50000)
#print('\33[91mTest set: Accuracy(FDR2FPR): {:.8f}\n\33[0m'.format(fpr2))
#print('\33[91mTest set: Accuracy(FPR2FDR): {:.8f}\n\33[0m'.format(fpr2fdr))
if (args.enable_logging):
logger.log_value(logger_test_name + ' fpr95', fpr95)
return
def adjust_learning_rate(optimizer):
"""Updates the learning rate given the learning rate decay.
The routine has been implemented according to the original Lua SGD optimizer
"""
for group in optimizer.param_groups:
if 'step' not in group:
group['step'] = 0.
else:
group['step'] += 1.
group['lr'] = args.lr * (
1.0 - float(group['step']) * float(args.batch_size) / (args.n_triplets * float(args.epochs)))
return
def create_optimizer(model, new_lr):
# setup optimizer
if args.optimizer == 'sgd':
optimizer = optim.SGD(model.parameters(), lr=new_lr,
momentum=0.9, dampening=0.9,
weight_decay=args.wd)
elif args.optimizer == 'adam':
optimizer = optim.Adam(model.parameters(), lr=new_lr,
weight_decay=args.wd)
else:
raise Exception('Not supported optimizer: {0}'.format(args.optimizer))
return optimizer
def main(train_loader, test_loaders, model, logger, file_logger):
# print the experiment configuration
print('\nparsed options:\n{}\n'.format(vars(args)))
# if (args.enable_logging):
# file_logger.log_string('logs.txt', '\nparsed options:\n{}\n'.format(vars(args)))
if args.cuda:
model.cuda()
optimizer1 = create_optimizer(model.features, args.lr)
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print('=> loading checkpoint {}'.format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
checkpoint = torch.load(args.resume)
model.load_state_dict(checkpoint['state_dict'])
else:
print('=> no checkpoint found at {}'.format(args.resume))
start = args.start_epoch
end = start + args.epochs
for epoch in range(start, end):
# iterate over test loaders and test results
#train_loader, test_loaders2 = create_loaders(load_random_triplets=triplet_flag)
train(train_loader, model, optimizer1, epoch, logger, triplet_flag)
for test_loader in test_loaders:
test(test_loader['dataloader'], model, epoch, logger, test_loader['name'])
if TEST_ON_W1BS:
# print(weights_path)
patch_images = w1bs.get_list_of_patch_images(
DATASET_DIR=args.w1bsroot.replace('/code', '/data/W1BS'))
desc_name = 'curr_desc' # + str(random.randint(0,100))
DESCS_DIR = LOG_DIR + '/temp_descs/' # args.w1bsroot.replace('/code', "/data/out_descriptors")
OUT_DIR = DESCS_DIR.replace('/temp_descs/', "/out_graphs/")
for img_fname in patch_images:
w1bs_extract_descs_and_save(img_fname, model, desc_name, cuda=args.cuda,
mean_img=args.mean_image,
std_img=args.std_image, out_dir=DESCS_DIR)
force_rewrite_list = [desc_name]
w1bs.match_descriptors_and_save_results(DESC_DIR=DESCS_DIR, do_rewrite=True,
dist_dict={},
force_rewrite_list=force_rewrite_list)
if (args.enable_logging):
w1bs.draw_and_save_plots_with_loggers(DESC_DIR=DESCS_DIR, OUT_DIR=OUT_DIR,
methods=["SNN_ratio"],
descs_to_draw=[desc_name],
logger=file_logger,
tensor_logger=logger)
else:
w1bs.draw_and_save_plots(DESC_DIR=DESCS_DIR, OUT_DIR=OUT_DIR,
methods=["SNN_ratio"],
descs_to_draw=[desc_name])
if __name__ == '__main__':
LOG_DIR = args.log_dir
if not os.path.isdir(LOG_DIR):
os.makedirs(LOG_DIR)
LOG_DIR = os.path.join(args.log_dir, suffix)
DESCS_DIR = os.path.join(LOG_DIR, 'temp_descs')
if TEST_ON_W1BS:
if not os.path.isdir(DESCS_DIR):
os.makedirs(DESCS_DIR)
logger, file_logger = None, None
model = HardNet()
if (args.enable_logging):
from Loggers import Logger, FileLogger
logger = Logger(LOG_DIR)
# file_logger = FileLogger(./log/+suffix)
train_loader, test_loaders = create_loaders(load_random_triplets=triplet_flag)
main(train_loader, test_loaders, model, logger, file_logger)