-
Notifications
You must be signed in to change notification settings - Fork 26
/
custom_train.py
213 lines (191 loc) · 8.87 KB
/
custom_train.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
import argparse
import numpy as np
import open3d
import os
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
from data import CustomData
from models import IterativeBenchmark
from loss import EMDLosspy
from metrics import compute_metrics, summary_metrics, print_train_info
from utils import time_calc
def setup_seed(seed):
torch.backends.cudnn.deterministic = True
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
def config_params():
parser = argparse.ArgumentParser(description='Configuration Parameters')
## dataset
parser.add_argument('--root', required=True, help='the data path')
parser.add_argument('--train_npts', type=int, required=True,
help='the points number of each pc for training')
## models training
parser.add_argument('--seed', type=int, default=1234)
parser.add_argument('--gn', action='store_true',
help='whether to use group normalization')
parser.add_argument('--epoches', type=int, default=400)
parser.add_argument('--batchsize', type=int, default=16)
parser.add_argument('--num_workers', type=int, default=4)
parser.add_argument('--in_dim', type=int, default=3,
help='3 for (x, y, z) or 6 for (x, y, z, nx, ny, nz)')
parser.add_argument('--niters', type=int, default=8,
help='iteration nums in one model forward')
parser.add_argument('--lr', type=float, default=0.0001,
help='initial learning rate')
parser.add_argument('--milestones', type=list, default=[50, 250],
help='lr decays when epoch in milstones')
parser.add_argument('--gamma', type=float, default=0.1,
help='lr decays to gamma * lr every decay epoch')
# logs
parser.add_argument('--saved_path', default='work_dirs/models',
help='the path to save training logs and checkpoints')
parser.add_argument('--saved_frequency', type=int, default=10,
help='the frequency to save the logs and checkpoints')
args = parser.parse_args()
return args
def compute_loss(ref_cloud, pred_ref_clouds, loss_fn):
losses = []
discount_factor = 0.5
for i in range(8):
loss = loss_fn(ref_cloud[..., :3].contiguous(),
pred_ref_clouds[i][..., :3].contiguous())
losses.append(discount_factor**(8 - i)*loss)
return torch.sum(torch.stack(losses))
@time_calc
def train_one_epoch(train_loader, model, loss_fn, optimizer):
losses = []
r_mse, r_mae, t_mse, t_mae, r_isotropic, t_isotropic = [], [], [], [], [], []
for ref_cloud, src_cloud, gtR, gtt in tqdm(train_loader):
ref_cloud, src_cloud, gtR, gtt = ref_cloud.cuda(), src_cloud.cuda(), \
gtR.cuda(), gtt.cuda()
optimizer.zero_grad()
R, t, pred_ref_clouds = model(src_cloud.permute(0, 2, 1).contiguous(),
ref_cloud.permute(0, 2, 1).contiguous())
loss = compute_loss(ref_cloud, pred_ref_clouds, loss_fn)
loss.backward()
optimizer.step()
cur_r_mse, cur_r_mae, cur_t_mse, cur_t_mae, cur_r_isotropic, \
cur_t_isotropic = compute_metrics(R, t, gtR, gtt)
losses.append(loss.item())
r_mse.append(cur_r_mse)
r_mae.append(cur_r_mae)
t_mse.append(cur_t_mse)
t_mae.append(cur_t_mae)
r_isotropic.append(cur_r_isotropic.cpu().detach().numpy())
t_isotropic.append(cur_t_isotropic.cpu().detach().numpy())
r_mse, r_mae, t_mse, t_mae, r_isotropic, t_isotropic = \
summary_metrics(r_mse, r_mae, t_mse, t_mae, r_isotropic, t_isotropic)
results = {
'loss': np.mean(losses),
'r_mse': r_mse,
'r_mae': r_mae,
't_mse': t_mse,
't_mae': t_mae,
'r_isotropic': r_isotropic,
't_isotropic': t_isotropic
}
return results
@time_calc
def test_one_epoch(test_loader, model, loss_fn):
model.eval()
losses = []
r_mse, r_mae, t_mse, t_mae, r_isotropic, t_isotropic = [], [], [], [], [], []
with torch.no_grad():
for ref_cloud, src_cloud, gtR, gtt in tqdm(test_loader):
ref_cloud, src_cloud, gtR, gtt = ref_cloud.cuda(), src_cloud.cuda(), \
gtR.cuda(), gtt.cuda()
R, t, pred_ref_clouds = model(src_cloud.permute(0, 2, 1).contiguous(),
ref_cloud.permute(0, 2, 1).contiguous())
loss = compute_loss(ref_cloud, pred_ref_clouds, loss_fn)
cur_r_mse, cur_r_mae, cur_t_mse, cur_t_mae, cur_r_isotropic, \
cur_t_isotropic = compute_metrics(R, t, gtR, gtt)
losses.append(loss.item())
r_mse.append(cur_r_mse)
r_mae.append(cur_r_mae)
t_mse.append(cur_t_mse)
t_mae.append(cur_t_mae)
r_isotropic.append(cur_r_isotropic.cpu().detach().numpy())
t_isotropic.append(cur_t_isotropic.cpu().detach().numpy())
model.train()
r_mse, r_mae, t_mse, t_mae, r_isotropic, t_isotropic = \
summary_metrics(r_mse, r_mae, t_mse, t_mae, r_isotropic, t_isotropic)
results = {
'loss': np.mean(losses),
'r_mse': r_mse,
'r_mae': r_mae,
't_mse': t_mse,
't_mae': t_mae,
'r_isotropic': r_isotropic,
't_isotropic': t_isotropic
}
return results
def main():
args = config_params()
print(args)
setup_seed(args.seed)
if not os.path.exists(args.saved_path):
os.makedirs(args.saved_path)
summary_path = os.path.join(args.saved_path, 'summary')
if not os.path.exists(summary_path):
os.makedirs(summary_path)
checkpoints_path = os.path.join(args.saved_path, 'checkpoints')
if not os.path.exists(checkpoints_path):
os.makedirs(checkpoints_path)
train_set = CustomData(args.root, args.train_npts)
test_set = CustomData(args.root, args.train_npts, False)
train_loader = DataLoader(train_set, batch_size=args.batchsize,
shuffle=True, num_workers=args.num_workers)
test_loader = DataLoader(test_set, batch_size=args.batchsize, shuffle=False,
num_workers=args.num_workers)
model = IterativeBenchmark(in_dim=args.in_dim, niters=args.niters, gn = args.gn)
model = model.cuda()
loss_fn = EMDLosspy()
loss_fn = loss_fn.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,
milestones=args.milestones,
gamma=args.gamma,
last_epoch=-1)
writer = SummaryWriter(summary_path)
test_min_loss, test_min_r_mse_error, test_min_rot_error = \
float('inf'), float('inf'), float('inf')
for epoch in range(args.epoches):
print('=' * 20, epoch + 1, '=' * 20)
train_results = train_one_epoch(train_loader, model, loss_fn, optimizer)
print_train_info(train_results)
test_results = test_one_epoch(test_loader, model, loss_fn)
print_train_info(test_results)
if epoch % args.saved_frequency == 0:
writer.add_scalar('Loss/train', train_results['loss'], epoch + 1)
writer.add_scalar('Loss/test', test_results['loss'], epoch + 1)
writer.add_scalar('RError/train', train_results['r_mse'], epoch + 1)
writer.add_scalar('RError/test', test_results['r_mse'], epoch + 1)
writer.add_scalar('rotError/train', train_results['r_isotropic'],
epoch + 1)
writer.add_scalar('rotError/test', test_results['r_isotropic'],
epoch + 1)
writer.add_scalar('Lr', optimizer.param_groups[0]['lr'], epoch + 1)
test_loss, test_r_error, test_rot_error = \
test_results['loss'], test_results['r_mse'], test_results[
'r_isotropic']
if test_loss < test_min_loss:
saved_path = os.path.join(checkpoints_path, "test_min_loss.pth")
torch.save(model.state_dict(), saved_path)
test_min_loss = test_loss
if test_r_error < test_min_r_mse_error:
saved_path = os.path.join(checkpoints_path,
"test_min_rmse_error.pth")
torch.save(model.state_dict(), saved_path)
test_min_r_mse_error = test_r_error
if test_rot_error < test_min_rot_error:
saved_path = os.path.join(checkpoints_path,
"test_min_rot_error.pth")
torch.save(model.state_dict(), saved_path)
test_min_rot_error = test_rot_error
scheduler.step()
if __name__ == '__main__':
main()