-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain_util.py
175 lines (127 loc) · 5.59 KB
/
main_util.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tqdm import tqdm
import numpy as np
from matplotlib import pyplot as plt
from losses import *
def train_one_epoch(args, net, train_loader, opt, mode):
if mode=='train':
net.train()
elif mode=='val':
net.eval()
num_examples = 0
total_loss = 0
if args.model=='raflow' or args.model == 'raflow_vod':
loss_items={
'Loss': [],
'chamferLoss': [],
'veloLoss':[],
'smoothnessLoss':[],
}
for i, data in tqdm(enumerate(train_loader), total = len(train_loader)):
pc1, pc2, ft1, ft2, _, gt , mask, interval= data
pc1 = pc1.cuda().transpose(2,1).contiguous()
pc2 = pc2.cuda().transpose(2,1).contiguous()
ft1 = ft1.cuda().transpose(2,1).contiguous()
ft2 = ft2.cuda().transpose(2,1).contiguous()
mask = mask.cuda()
interval = interval.cuda().float()
gt = gt.cuda().float()
batch_size = pc1.size(0)
num_examples += batch_size
vel1 = ft1[:,0]
if args.model=='raflow' or args.model == 'raflow_vod':
_, agg_f, _,_ = net(pc1, pc2, ft1, ft2, interval)
loss, items = computeloss(pc1,pc2, agg_f, vel1,interval, args)
if mode=='train':
opt.zero_grad()
loss.backward()
opt.step()
total_loss += loss.item() * batch_size
for l in loss_items:
loss_items[l].append(items[l])
total_loss=total_loss*1.0/num_examples
for l in loss_items:
loss_items[l]=np.mean(np.array(loss_items[l]))
return total_loss, loss_items
def plot_loss_epoch(train_items_iter, args, epoch):
plt.clf()
plt.plot(np.array(train_items_iter['Loss']).T, 'b')
plt.plot(np.array(train_items_iter['chamferLoss']).T, 'r')
plt.plot(np.array(train_items_iter['veloLoss']).T, 'g')
plt.plot(np.array(train_items_iter['smoothnessLoss']).T, 'c')
plt.legend(['Total','chamferLoss','veloLoss','smoothness'], loc='upper right')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.savefig('checkpoints/%s/loss_train_%s.png' %(args.exp_name,epoch),dpi=500)
def get_carterian_res(pc, sensor, args):
## measure resolution for r/theta/phi
if sensor == 'radar':
if args.dataset == 'saicDataset': # LRR30
r_res = 0.2 # m
theta_res = 1 * np.pi/180 # radian
phi_res = 1.6 *np.pi/180 # radian
if args.dataset == 'vodDataset': # ZF FRGen21
r_res: 0.2 # m
theta_res: 1.5 * np.pi/180 # radian
phi_res: 1.5 *np.pi/180 # radian
if sensor == 'lidar': # HDL-64E
r_res = 0.04 # m
theta_res = 0.4 * np.pi/180 # radian
phi_res = 0.08 *np.pi/180 # radian
res = np.array([r_res, theta_res, phi_res])
## x y z
x = pc[:,0]
y = pc[:,1]
z = pc[:,2]
## from xyz to r/theta/phi (range/elevation/azimuth)
r = np.sqrt(x**2+y**2+z**2)
theta = np.arcsin(z/r)
phi = np.arctan2(y,x)
## compute xyz's gradient about r/theta/phi
grad_x = np.stack((np.cos(phi)*np.cos(theta), -r*np.sin(theta)*np.cos(phi), -r*np.cos(theta)*np.sin(phi)),axis=2)
grad_y = np.stack((np.sin(phi)*np.cos(theta), -r*np.sin(phi)*np.sin(theta), r*np.cos(theta)*np.cos(phi)),axis=2)
grad_z = np.stack((np.sin(theta), r*np.cos(theta), np.zeros((np.size(x,0),np.size(x,1)))),axis=2)
## measure resolution for xyz (different positions have different resolution)
x_res = np.sum(abs(grad_x) * res,axis=2)
y_res = np.sum(abs(grad_y) * res,axis=2)
z_res = np.sum(abs(grad_z) * res,axis=2)
xyz_res = np.stack((x_res,y_res,z_res),axis=2)
return xyz_res
def eval_scene_flow(pc, pred, labels, mask, args):
pc = pc.cpu().numpy()
pred = pred.cpu().detach().numpy()
labels = labels.cpu().numpy()
mask = mask.cpu().numpy()
error = np.sqrt(np.sum((pred - labels)**2, 2) + 1e-20)
epe = np.mean(error)
gtflow_len = np.sqrt(np.sum(labels*labels, 2) + 1e-20)
## obtain x y z measure resolution for each point (radar lidar)
xyz_res_r = get_carterian_res(pc, 'radar', args)
res_r = np.sqrt(np.sum(xyz_res_r,2)+1e-20)
xyz_res_l = get_carterian_res(pc, 'lidar', args)
res_l = np.sqrt(np.sum(xyz_res_l,2)+1e-20)
## calcualte Resolution Normalized Error
re_error = error/(res_r/res_l)
rne = np.mean(re_error)
mov_rne = np.sum(re_error[mask==0])/(np.sum(mask==0)+1e-6)
stat_rne = np.mean(re_error[mask==1])
avg_rne = (mov_rne+stat_rne)/2
## calculate Strict/Relaxed Accuracy Score
sas = np.sum(np.logical_or((re_error <= 0.10), (re_error/gtflow_len <= 0.10)))/(np.size(pred,0)*np.size(pred,1))
ras = np.sum(np.logical_or((re_error <= 0.20), (re_error/gtflow_len <= 0.20)))/(np.size(pred,0)*np.size(pred,1))
sf_metric = {'rne':rne, '50-50 rne': avg_rne, 'mov_rne': mov_rne, 'stat_rne': stat_rne,\
'sas': sas, 'ras': ras, 'epe':epe}
return sf_metric
def eval_motion_seg(pre, gt):
pre = pre.cpu().detach().numpy()
gt = gt.cpu().numpy()
tp = np.logical_and((pre==1),(gt==1)).sum()
tn = np.logical_and((pre==0),(gt==0)).sum()
fp = np.logical_and((pre==1),(gt==0)).sum()
fn = np.logical_and((pre==0),(gt==1)).sum()
acc = (tp+tn)/(tp+tn+fp+fn)
sen = tp/(tp+fn)
miou = 0.5*(tp/(tp+fp+fn+1e-4)+tn/(tn+fp+fn+1e-4))
seg_metric = {'acc': acc, 'miou': miou, 'sen': sen}
return seg_metric