-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_pcd.py
144 lines (108 loc) · 5.44 KB
/
train_pcd.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
# -*- coding: utf-8 -*-
from platform import machine
import torch,math
import torch.nn as nn
import torch.nn.functional as F
import os, sys
sys.path.append("/media/fangxu/Disk4T/fangxuPrj/Depth_Point_Location_Attention")
from tqdm import tqdm
from dataset import make_dataloaders
from model import Fuse_PPNet, Pose_Depth_Net, Pose_Pcd_Net , standard_pose_loss
from mmcv import Config
from utils import median, quaternion_angular_error, load_state_dict
import logging, time
import numpy as np
## step 1: config
if len(sys.argv)==2:
config_path = sys.argv[1]
else:
config_path = '/media/fangxu/Disk4T/fangxuPrj/Depth_Point_Location_Attention/config/conf_pcd.py'
assert os.path.exists(config_path)==True
config = Config.fromfile(config_path)
dtype = config.dtype # torch.cuda.FloatTensor if cuda else torch.FloatTensor
torch.cuda.manual_seed(1)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
savedir = os.path.join(config.save_dir, config.save_prj, config.scene ) #'/media/fangxu/Disk4T/LQ/'+scene
if not os.path.exists(savedir):
os.makedirs( savedir )
# step 2: logging setting, 输出到屏幕和日志
logger = logging.getLogger()
logger.setLevel(level = logging.INFO)
log_path = os.path.join( savedir, str(int( time.time() ))+".log" )
handler = logging.FileHandler( log_path )
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logger.addHandler(handler)
#logger.addHandler(console)
## step 2: data load and model construct
train_loader , test_loader = make_dataloaders(config)
model = Pose_Pcd_Net(config)
if config.pretrain_weight is not None:
model.load_state_dict( torch.load(config.pretrain_weight), strict= False )
data_length = len(train_loader)
criterion = standard_pose_loss(config)
criterion.to(device)
model.to(device)
# error calculate
optimizer = torch.optim.Adam(model.parameters(), lr=config.learning_rate, betas=(0.9, 0.999), weight_decay=1e-5)
Best_Pos_error = 9999.0
Best_Ort_error = 9999.0
pdist = nn.PairwiseDistance(2)
for epoch in range(1,config.epochs+1):
model.train()
loss_t_acc, loss_q_acc, loss_acc = [], [], []
for i, ( _ , pcd_base, base_t,base_q) in enumerate(train_loader):
#depth_input = depth_base.to(device)
pcd_input = {e: pcd_base[e].to(device) for e in pcd_base}
t_target, q_target = base_t.to(device), base_q.to(device)
optimizer.zero_grad()
t_pred, q_pred = model(pcd_input)
loss , loss_t_item, loss_q_item = criterion(t_pred, q_pred, t_target, q_target)
loss.backward()
optimizer.step()
loss_t_acc.append( loss_t_item )
loss_q_acc.append( loss_q_item )
loss_acc.append( loss.item() )
if (i+1) % config.print_every == 0:
logger.info('epoch {}, batch:{}/{}, loss:{}, loss_T:{}, loss_Q:{} '.format(epoch, i+1 , data_length ,
round(loss.item(), 5),
round(loss_t_item, 5),
round(loss_q_item, 5) ))
logger.info('Epoch:{}, Average translation loss over epoch = {}'.format(epoch, round( np.average(loss_t_acc) , 5) ))
logger.info('Epoch:{}, Average orientation loss over epoch = {}'.format(epoch, round( np.average(loss_q_acc) , 5) ))
logger.info('Epoch:{}, Average loss over epoch = {}'.format(epoch, round( np.average(loss_acc), 5 ) ))
if (epoch > -1 and epoch % config.interval == 0):
model.eval()
with torch.no_grad():
dis_Err_Count, ort_Err_count = [], []
for _ , (_ , pcd_base, base_t,base_q) in enumerate(tqdm(test_loader)):
#depth_input = depth_base.to(device)
pcd_input = {e: pcd_base[e].to(device) for e in pcd_base}
t_gt, q_gt = base_t.to(device) , base_q.to(device)
t_infer, q_infer = model(pcd_input)
dis_Err = pdist(t_infer, t_gt).cpu().numpy()
dis_Err_Count = dis_Err_Count + list(dis_Err) # 合并为大 list , list + 号运算,非数值相加
q_infer = F.normalize(q_infer, p=2, dim=1 )
ort_Err = quaternion_angular_error( q_infer, q_gt).cpu().numpy()
ort_Err_count = ort_Err_count + list(ort_Err) # 合并为大 list , list + 号运算,非数值相加
pos_Err_e = median(dis_Err_Count)
ort_Err_e = median(ort_Err_count)
logger.info('Eval: Media distance error= {}, Median orientation error = {}'.format( round(pos_Err_e,5), round(ort_Err_e, 5) ))
if pos_Err_e < Best_Pos_error:
Best_Pos_error = pos_Err_e
Best_Ort_error = ort_Err_e
save_best_path = os.path.join(savedir, 'best_epoch_{}.pth'.format(epoch))
logger.info('### save the best params in epoch {} ###'.format(epoch))
#torch.save(model.state_dict(), save_best_path )
checkpoint_dict = {'epoch': epoch, 'model_state_dict': model.state_dict(), 'optim_state_dict': optimizer.state_dict() }
torch.save(checkpoint_dict, save_best_path) # epoch
# if __name__=="__main__":
# print("x")
# main()
#isExists = os.path.exists( save_best_path )
#if (isExists):
#os.remove(save_best_path )
#