-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
187 lines (148 loc) · 6.74 KB
/
main.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
import os
import argparse
from model import IDAM, FPFH, GNN
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import MultiStepLR
from data import ModelNet40
from util import npmat2euler
import numpy as np
from tqdm import tqdm
torch.backends.cudnn.enabled = False # fix cudnn non-contiguous error
def test_one_epoch(args, net, test_loader):
net.eval()
R_list = []
t_list = []
R_pred_list = []
t_pred_list = []
euler_list = []
for src, target, R, t, euler in tqdm(test_loader):
src = src.cuda()
target = target.cuda()
R = R.cuda()
t = t.cuda()
R_pred, t_pred, *_ = net(src, target)
R_list.append(R.detach().cpu().numpy())
t_list.append(t.detach().cpu().numpy())
R_pred_list.append(R_pred.detach().cpu().numpy())
t_pred_list.append(t_pred.detach().cpu().numpy())
euler_list.append(euler.numpy())
R = np.concatenate(R_list, axis=0)
t = np.concatenate(t_list, axis=0)
R_pred = np.concatenate(R_pred_list, axis=0)
t_pred = np.concatenate(t_pred_list, axis=0)
euler = np.concatenate(euler_list, axis=0)
euler_pred = npmat2euler(R_pred)
r_mse = np.mean((euler_pred - np.degrees(euler)) ** 2)
r_rmse = np.sqrt(r_mse)
r_mae = np.mean(np.abs(euler_pred - np.degrees(euler)))
t_mse = np.mean((t - t_pred) ** 2)
t_rmse = np.sqrt(t_mse)
t_mae = np.mean(np.abs(t - t_pred))
return r_rmse, r_mae, t_rmse, t_mae
def train_one_epoch(args, net, train_loader, opt):
net.train()
R_list = []
t_list = []
R_pred_list = []
t_pred_list = []
euler_list = []
for src, target, R, t, euler in tqdm(train_loader):
src = src.cuda()
target = target.cuda()
R = R.cuda()
t = t.cuda()
opt.zero_grad()
R_pred, t_pred, loss = net(src, target, R, t)
R_list.append(R.detach().cpu().numpy())
t_list.append(t.detach().cpu().numpy())
R_pred_list.append(R_pred.detach().cpu().numpy())
t_pred_list.append(t_pred.detach().cpu().numpy())
euler_list.append(euler.numpy())
loss.backward()
opt.step()
R = np.concatenate(R_list, axis=0)
t = np.concatenate(t_list, axis=0)
R_pred = np.concatenate(R_pred_list, axis=0)
t_pred = np.concatenate(t_pred_list, axis=0)
euler = np.concatenate(euler_list, axis=0)
euler_pred = npmat2euler(R_pred)
r_mse = np.mean((euler_pred - np.degrees(euler)) ** 2)
r_rmse = np.sqrt(r_mse)
r_mae = np.mean(np.abs(euler_pred - np.degrees(euler)))
t_mse = np.mean((t - t_pred) ** 2)
t_rmse = np.sqrt(t_mse)
t_mae = np.mean(np.abs(t - t_pred))
return r_rmse, r_mae, t_rmse, t_mae
def train(args, net, train_loader, test_loader):
opt = optim.Adam(net.parameters(), lr=0.0001, weight_decay=0.001)
scheduler = MultiStepLR(opt, milestones=[30], gamma=0.1)
for epoch in range(args.epochs):
train_stats = train_one_epoch(args, net, train_loader, opt)
test_stats = test_one_epoch(args, net, test_loader)
print('===== EPOCH %d =====' % (epoch+1))
print('TRAIN, rot_RMSE: %f, rot_MAE: %f, trans_RMSE: %f, trans_MAE: %f' % train_stats)
print('TEST, rot_RMSE: %f, rot_MAE: %f, trans_RMSE: %f, trans_MAE: %f' % test_stats)
torch.save(net.state_dict(), 'checkpoints/%s/models/model.%d.t7' % (args.exp_name, epoch))
scheduler.step()
def main():
arg_bool = lambda x: x.lower() in ['true', 't', '1']
parser = argparse.ArgumentParser(description='Point Cloud Registration')
parser.add_argument('--exp_name', type=str, default='exp', metavar='N',
help='Name of the experiment')
parser.add_argument('--num_iter', type=int, default=3, metavar='N',
help='Number of iteration inside the network')
parser.add_argument('--emb_nn', type=str, default='GNN', metavar='N',
help='Feature extraction method. [GNN, FPFH]')
parser.add_argument('--emb_dims', type=int, default=64, metavar='N',
help='Dimension of embeddings. Must be 33 if emb_nn == FPFH')
parser.add_argument('--batch_size', type=int, default=16, metavar='batch_size',
help='Size of batch)')
parser.add_argument('--test_batch_size', type=int, default=16, metavar='batch_size',
help='Size of batch)')
parser.add_argument('--epochs', type=int, default=40, metavar='N',
help='number of episode to train ')
parser.add_argument('--unseen', type=arg_bool, default='False',
help='Test on unseen categories')
parser.add_argument('--gaussian_noise', type=arg_bool, default='False',
help='Wheter to add gaussian noise')
parser.add_argument('--alpha', type=float, default=0.75, metavar='N',
help='Fraction of points when sampling partial point cloud')
parser.add_argument('--factor', type=float, default=4, metavar='N',
help='Divided factor for rotations')
args = parser.parse_args()
print(args)
##### make checkpoint directory and backup #####
if not os.path.exists('checkpoints'):
os.makedirs('checkpoints')
if not os.path.exists('checkpoints/' + args.exp_name):
os.makedirs('checkpoints/' + args.exp_name)
if not os.path.exists('checkpoints/' + args.exp_name + '/' + 'models'):
os.makedirs('checkpoints/' + args.exp_name + '/' + 'models')
os.system('cp main.py checkpoints' + '/' + args.exp_name + '/' + 'main.py.backup')
os.system('cp model.py checkpoints' + '/' + args.exp_name + '/' + 'model.py.backup')
os.system('cp data.py checkpoints' + '/' + args.exp_name + '/' + 'data.py.backup')
##### make checkpoint directory and backup #####
##### load data #####
train_loader = DataLoader(
ModelNet40(partition='train', alpha=args.alpha, gaussian_noise=args.gaussian_noise, unseen=args.unseen, factor=args.factor),
batch_size=args.batch_size, shuffle=True, drop_last=True, num_workers=8)
test_loader = DataLoader(
ModelNet40(partition='test', alpha=args.alpha, gaussian_noise=args.gaussian_noise, unseen=args.unseen, factor=args.factor),
batch_size=args.test_batch_size, shuffle=False, drop_last=False, num_workers=8)
##### load data #####
##### load model #####
if args.emb_nn == 'GNN':
net = IDAM(GNN(args.emb_dims), args).cuda()
elif args.emb_nn == 'FPFH':
args.emb_dims = 33
net = IDAM(FPFH(), args).cuda()
##### load model #####
##### train #####
train(args, net, train_loader, test_loader)
##### train #####
if __name__ == '__main__':
main()