-
Notifications
You must be signed in to change notification settings - Fork 19
/
vpr_model.py
252 lines (208 loc) · 9.48 KB
/
vpr_model.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
import pytorch_lightning as pl
import torch
from torch.optim import lr_scheduler, optimizer
import utils
from models import helper
class VPRModel(pl.LightningModule):
"""This is the main model for Visual Place Recognition
we use Pytorch Lightning for modularity purposes.
Args:
pl (_type_): _description_
"""
def __init__(self,
#---- Backbone
backbone_arch='resnet50',
backbone_config={},
#---- Aggregator
agg_arch='ConvAP',
agg_config={},
#---- Train hyperparameters
lr=0.03,
optimizer='sgd',
weight_decay=1e-3,
momentum=0.9,
lr_sched='linear',
lr_sched_args = {
'start_factor': 1,
'end_factor': 0.2,
'total_iters': 4000,
},
#----- Loss
loss_name='MultiSimilarityLoss',
miner_name='MultiSimilarityMiner',
miner_margin=0.1,
faiss_gpu=False
):
super().__init__()
# Backbone
self.encoder_arch = backbone_arch
self.backbone_config = backbone_config
# Aggregator
self.agg_arch = agg_arch
self.agg_config = agg_config
# Train hyperparameters
self.lr = lr
self.optimizer = optimizer
self.weight_decay = weight_decay
self.momentum = momentum
self.lr_sched = lr_sched
self.lr_sched_args = lr_sched_args
# Loss
self.loss_name = loss_name
self.miner_name = miner_name
self.miner_margin = miner_margin
self.save_hyperparameters() # write hyperparams into a file
self.loss_fn = utils.get_loss(loss_name)
self.miner = utils.get_miner(miner_name, miner_margin)
self.batch_acc = [] # we will keep track of the % of trivial pairs/triplets at the loss level
self.faiss_gpu = faiss_gpu
# ----------------------------------
# get the backbone and the aggregator
self.backbone = helper.get_backbone(backbone_arch, backbone_config)
self.aggregator = helper.get_aggregator(agg_arch, agg_config)
# For validation in Lightning v2.0.0
self.val_outputs = []
# the forward pass of the lightning model
def forward(self, x):
x = self.backbone(x)
x = self.aggregator(x)
return x
# configure the optimizer
def configure_optimizers(self):
if self.optimizer.lower() == 'sgd':
optimizer = torch.optim.SGD(
self.parameters(),
lr=self.lr,
weight_decay=self.weight_decay,
momentum=self.momentum
)
elif self.optimizer.lower() == 'adamw':
optimizer = torch.optim.AdamW(
self.parameters(),
lr=self.lr,
weight_decay=self.weight_decay
)
elif self.optimizer.lower() == 'adam':
optimizer = torch.optim.AdamW(
self.parameters(),
lr=self.lr,
weight_decay=self.weight_decay
)
else:
raise ValueError(f'Optimizer {self.optimizer} has not been added to "configure_optimizers()"')
if self.lr_sched.lower() == 'multistep':
scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=self.lr_sched_args['milestones'], gamma=self.lr_sched_args['gamma'])
elif self.lr_sched.lower() == 'cosine':
scheduler = lr_scheduler.CosineAnnealingLR(optimizer, self.lr_sched_args['T_max'])
elif self.lr_sched.lower() == 'linear':
scheduler = lr_scheduler.LinearLR(
optimizer,
start_factor=self.lr_sched_args['start_factor'],
end_factor=self.lr_sched_args['end_factor'],
total_iters=self.lr_sched_args['total_iters']
)
return [optimizer], [scheduler]
# configure the optizer step, takes into account the warmup stage
def optimizer_step(self, epoch, batch_idx, optimizer, optimizer_closure):
# warm up lr
optimizer.step(closure=optimizer_closure)
self.lr_schedulers().step()
# The loss function call (this method will be called at each training iteration)
def loss_function(self, descriptors, labels):
# we mine the pairs/triplets if there is an online mining strategy
if self.miner is not None:
miner_outputs = self.miner(descriptors, labels)
loss = self.loss_fn(descriptors, labels, miner_outputs)
# calculate the % of trivial pairs/triplets
# which do not contribute in the loss value
nb_samples = descriptors.shape[0]
nb_mined = len(set(miner_outputs[0].detach().cpu().numpy()))
batch_acc = 1.0 - (nb_mined/nb_samples)
else: # no online mining
loss = self.loss_fn(descriptors, labels)
batch_acc = 0.0
if type(loss) == tuple:
# somes losses do the online mining inside (they don't need a miner objet),
# so they return the loss and the batch accuracy
# for example, if you are developping a new loss function, you might be better
# doing the online mining strategy inside the forward function of the loss class,
# and return a tuple containing the loss value and the batch_accuracy (the % of valid pairs or triplets)
loss, batch_acc = loss
# keep accuracy of every batch and later reset it at epoch start
self.batch_acc.append(batch_acc)
# log it
self.log('b_acc', sum(self.batch_acc) /
len(self.batch_acc), prog_bar=True, logger=True)
return loss
# This is the training step that's executed at each iteration
def training_step(self, batch, batch_idx):
places, labels = batch
# Note that GSVCities yields places (each containing N images)
# which means the dataloader will return a batch containing BS places
BS, N, ch, h, w = places.shape
# reshape places and labels
images = places.view(BS*N, ch, h, w)
labels = labels.view(-1)
# Feed forward the batch to the model
descriptors = self(images) # Here we are calling the method forward that we defined above
if torch.isnan(descriptors).any():
raise ValueError('NaNs in descriptors')
loss = self.loss_function(descriptors, labels) # Call the loss_function we defined above
self.log('loss', loss.item(), logger=True, prog_bar=True)
return {'loss': loss}
def on_train_epoch_end(self):
# we empty the batch_acc list for next epoch
self.batch_acc = []
# For validation, we will also iterate step by step over the validation set
# this is the way Pytorch Lghtning is made. All about modularity, folks.
def validation_step(self, batch, batch_idx, dataloader_idx=None):
places, _ = batch
descriptors = self(places)
self.val_outputs[dataloader_idx].append(descriptors.detach().cpu())
return descriptors.detach().cpu()
def on_validation_epoch_start(self):
# reset the outputs list
self.val_outputs = [[] for _ in range(len(self.trainer.datamodule.val_datasets))]
def on_validation_epoch_end(self):
"""this return descriptors in their order
depending on how the validation dataset is implemented
for this project (MSLS val, Pittburg val), it is always references then queries
[R1, R2, ..., Rn, Q1, Q2, ...]
"""
val_step_outputs = self.val_outputs
dm = self.trainer.datamodule
# The following line is a hack: if we have only one validation set, then
# we need to put the outputs in a list (Pytorch Lightning does not do it presently)
if len(dm.val_datasets)==1: # we need to put the outputs in a list
val_step_outputs = [val_step_outputs]
for i, (val_set_name, val_dataset) in enumerate(zip(dm.val_set_names, dm.val_datasets)):
feats = torch.concat(val_step_outputs[i], dim=0)
if 'pitts' in val_set_name:
# split to ref and queries
num_references = val_dataset.dbStruct.numDb
positives = val_dataset.getPositives()
elif 'msls' in val_set_name:
# split to ref and queries
num_references = val_dataset.num_references
positives = val_dataset.pIdx
else:
print(f'Please implement validation_epoch_end for {val_set_name}')
raise NotImplemented
r_list = feats[ : num_references]
q_list = feats[num_references : ]
pitts_dict = utils.get_validation_recalls(
r_list=r_list,
q_list=q_list,
k_values=[1, 5, 10, 15, 20, 50, 100],
gt=positives,
print_results=True,
dataset_name=val_set_name,
faiss_gpu=self.faiss_gpu
)
del r_list, q_list, feats, num_references, positives
self.log(f'{val_set_name}/R1', pitts_dict[1], prog_bar=False, logger=True)
self.log(f'{val_set_name}/R5', pitts_dict[5], prog_bar=False, logger=True)
self.log(f'{val_set_name}/R10', pitts_dict[10], prog_bar=False, logger=True)
print('\n\n')
# reset the outputs list
self.val_outputs = []