-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtrain_superpoint.py
186 lines (156 loc) · 5.66 KB
/
train_superpoint.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
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import datetime
import logging
import os
import time
import argparse
import yaml
import copy
import torch
import torch.distributed as dist
import torch.optim as optim
from torch.autograd import Variable
from torch.optim import lr_scheduler
from torchvision.models.detection.backbone_utils import resnet_fpn_backbone
from torch.utils.data import DataLoader
from model.build_model import build_superpoint_model
from model.superpoint.superpoint_loss import SuperPointLoss
from datasets.utils.build_data import coco_loader
from datasets.synthetic.synthetic import SyntheticDataset
from datasets.utils.batch_collator import BatchCollator
from datasets.utils.preprocess import preprocess_superpoint_train_data
os.environ["CUDA_VISIBLE_DEVICES"] = "2, 3"
def update_gaussian_radius(gaussian_radius, iter, gaussian_gamma, gaussian_milestones):
r = gaussian_radius
if r < 0:
return 1, gaussian_radius
for i in range(len(gaussian_milestones)):
if iter > gaussian_milestones[i]:
r = r * gaussian_gamma
else:
break
r = int(r)
if r < 2:
gaussian_radius = -1
return r, gaussian_radius
def train(configs):
# read configs
## command line config
use_gpu = configs['use_gpu']
model_dir = configs['model_dir']
data_root = configs['data_root']
## data cofig
data_config = configs['data']
dataset_name = data_config['name']
## superpoint model config
superpoint_model_config = configs['model']['superpoint']
train_batch_size = superpoint_model_config['train']['batch_size']
epochs = superpoint_model_config['train']['epochs']
lr = superpoint_model_config['train']['lr']
momentum = superpoint_model_config['train']['momentum']
w_decay = superpoint_model_config['train']['w_decay']
milestones = superpoint_model_config['train']['milestones']
gamma = superpoint_model_config['train']['gamma']
gaussian_region = superpoint_model_config['train']['gaussian_region']
gaussian_radius = gaussian_region['radius']
gaussian_gamma = gaussian_region['gamma']
gaussian_milestones = gaussian_region['milestones']
train_batch_size = superpoint_model_config['train']['batch_size']
checkpoint = superpoint_model_config['train']['checkpoint']
## others
configs['num_gpu'] = [0, 1]
# data
if 'coco' in dataset_name:
train_data_name = data_config['TRAIN']
train_loader = coco_loader(
data_root=data_root, name=train_data_name, config=data_config, batch_size=train_batch_size,
remove_images_without_annotations=True)
elif 'synthetic' in dataset_name:
train_dataset = SyntheticDataset(data_root=data_root, use_for='training')
sampler = torch.utils.data.sampler.RandomSampler(train_dataset)
batch_sampler = torch.utils.data.sampler.BatchSampler(sampler=sampler, batch_size=train_batch_size, drop_last=True)
collator = BatchCollator()
train_loader = DataLoader(train_dataset, batch_sampler=batch_sampler, collate_fn=collator, num_workers=8)
# model
model = build_superpoint_model(configs)
model.train()
# optimizer
optimizer = optim.RMSprop(model.parameters(), lr=lr, momentum=momentum, weight_decay=w_decay)
scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=milestones, gamma=gamma)
# loss
criterion = SuperPointLoss(config=superpoint_model_config)
sum_iter = 0
r = gaussian_radius
for _ in range(epochs):
for iter, batch in enumerate(train_loader):
optimizer.zero_grad()
batch = preprocess_superpoint_train_data(batch, use_gpu, r, data_config)
if use_gpu:
for key in batch:
if key == 'image_name':
continue
batch[key] = batch[key].cuda()
outputs = model(batch['image'])
batch_outputs = {'outputs': outputs}
if 'warped_image' in batch:
warped_outputs = model(batch['warped_image'])
batch_outputs['warped_outputs'] = warped_outputs
loss, loss_dict = criterion(batch, batch_outputs)
loss = loss / train_batch_size
for k in loss_dict:
loss_dict[k] = loss_dict[k].cpu().item() / train_batch_size
loss.backward()
optimizer.step()
if iter%10 == 0:
print("sum_iter = {}, gaussian_radius={}, loss = {}".format(sum_iter, r, loss.item()))
sum_iter += 1
r, gaussian_radius = update_gaussian_radius(gaussian_radius, sum_iter, gaussian_gamma, gaussian_milestones)
scheduler.step()
if sum_iter % checkpoint == 0:
model_saving_path = os.path.join(model_dir, "superpoint_iter{}.pth".format(sum_iter))
torch.save(model.state_dict(), model_saving_path)
print("saving model to {}".format(model_saving_path))
def main():
parser = argparse.ArgumentParser(description="Training")
parser.add_argument(
"-c", "--config_file",
dest = "config_file",
type = str,
default = ""
)
parser.add_argument(
"-g", "--gpu",
dest = "gpu",
type = int,
default = 0
)
parser.add_argument(
"-s", "--save_dir",
dest = "save_dir",
type = str,
default = ""
)
parser.add_argument(
"-d", "--data_root",
dest = "data_root",
type = str,
default = ""
)
parser.add_argument(
"-m", "--model_path",
dest = "pretrained_model_path",
type = str,
default = ""
)
args = parser.parse_args()
config_file = args.config_file
f = open(config_file, 'r', encoding='utf-8')
configs = f.read()
configs = yaml.load(configs)
configs['use_gpu'] = args.gpu
configs['model_dir'] = args.save_dir
configs['data_root'] = args.data_root
configs['superpoint_model_path'] = args.pretrained_model_path
train(configs)
if __name__ == "__main__":
main()