-
Notifications
You must be signed in to change notification settings - Fork 20
/
training_3DMatch.py
145 lines (135 loc) · 5.88 KB
/
training_3DMatch.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
import os
import time
import shutil
import json
from config import get_config
from easydict import EasyDict as edict
from datasets.ThreeDMatch import ThreeDMatchDataset, ThreeDMatchTestset
from trainer import Trainer
from models.architectures import KPFCNN
# from models.D3Feat import KPFCNN
from datasets.dataloader import get_dataloader
from utils.loss import ContrastiveLoss, DetLoss, CircleLoss
from torch import optim
from torch import nn
import torch
if __name__ == '__main__':
config = get_config()
dconfig = vars(config)
for k in dconfig:
print(f" {k}: {dconfig[k]}")
config = edict(dconfig)
os.makedirs(config.snapshot_dir, exist_ok=True)
os.makedirs(config.tboard_dir, exist_ok=True)
os.makedirs(config.save_dir, exist_ok=True)
shutil.copy2(os.path.join('.', 'training_3DMatch.py'), os.path.join(config.snapshot_dir, 'train.py'))
shutil.copy2(os.path.join('.', 'trainer.py'), os.path.join(config.snapshot_dir, 'trainer.py'))
shutil.copy2(os.path.join('models', 'architectures.py'), os.path.join(config.snapshot_dir, 'model.py')) # for the model setting.
shutil.copy2(os.path.join('models', 'blocks.py'), os.path.join(config.snapshot_dir, 'conv.py')) # for the conv implementation.
shutil.copy2(os.path.join('utils', 'loss.py'), os.path.join(config.snapshot_dir, 'loss.py'))
shutil.copy2(os.path.join('datasets', 'ThreeDMatch.py'), os.path.join(config.snapshot_dir, 'dataset.py'))
json.dump(
config,
open(os.path.join(config.snapshot_dir, 'config.json'), 'w'),
indent=4,
)
if config.gpu_mode:
config.device = torch.device('cuda')
else:
config.device = torch.device('cpu')
# create model
config.architecture = [
'simple',
'resnetb',
]
for i in range(config.num_layers-1):
config.architecture.append('resnetb_strided')
config.architecture.append('resnetb')
config.architecture.append('resnetb')
for i in range(config.num_layers-2):
config.architecture.append('nearest_upsample')
config.architecture.append('unary')
config.architecture.append('nearest_upsample')
config.architecture.append('last_unary')
print("Network Architecture:\n", "".join([layer+'\n' for layer in config.architecture]))
config.model = KPFCNN(config)
# create optimizer
if config.optimizer == 'SGD':
config.optimizer = optim.SGD(
config.model.parameters(),
lr=config.lr,
momentum=config.momentum,
weight_decay=config.weight_decay,
)
elif config.optimizer == 'ADAM':
config.optimizer = optim.Adam(
config.model.parameters(),
lr=config.lr,
betas=(0.9, 0.999),
# momentum=config.momentum,
weight_decay=config.weight_decay,
)
config.scheduler = optim.lr_scheduler.ExponentialLR(
config.optimizer,
gamma=config.scheduler_gamma,
)
# create dataset and dataloader
train_set = ThreeDMatchDataset(root=config.root,
split='train',
downsample=config.downsample,
self_augment=config.self_augment,
num_node=config.num_node,
augment_noise=config.augment_noise,
augment_axis=config.augment_axis,
augment_rotation=config.augment_rotation,
augment_translation=config.augment_translation,
config=config,
)
val_set = ThreeDMatchDataset(root=config.root,
split='val',
num_node=64,
downsample=config.downsample,
self_augment=config.self_augment,
augment_noise=config.augment_noise,
augment_axis=config.augment_axis,
augment_rotation=config.augment_rotation,
augment_translation=config.augment_translation,
config=config,
)
config.train_loader, neighborhood_limits = get_dataloader(dataset=train_set,
batch_size=config.batch_size,
shuffle=True,
num_workers=config.num_workers,
)
config.val_loader,_ = get_dataloader(dataset=val_set,
batch_size=config.batch_size,
shuffle=True,
num_workers=config.num_workers,
neighborhood_limits=neighborhood_limits
)
# create evaluation
if config.desc_loss == 'contrastive':
desc_loss = ContrastiveLoss(
pos_margin=config.pos_margin,
neg_margin=config.neg_margin,
metric='euclidean',
safe_radius=config.safe_radius
)
else:
desc_loss = CircleLoss(
dist_type=config.dist_type,
log_scale=config.log_scale,
safe_radius=config.safe_radius,
pos_margin=config.pos_margin,
neg_margin=config.neg_margin,
)
config.evaluation_metric = {
'desc_loss': desc_loss,
'det_loss': DetLoss(metric='euclidean'),
}
config.metric_weight = {
'desc_loss': config.desc_loss_weight,
'det_loss': config.det_loss_weight,
}
trainer = Trainer(config)
trainer.train()