-
Notifications
You must be signed in to change notification settings - Fork 1
/
inference.py
160 lines (135 loc) · 5.64 KB
/
inference.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
import os
import cv2
import numpy as np
import scipy.misc as m
import torch
import pdb
import argparse
import sys
from sys import exit
from torch.utils.data import DataLoader
from torchvision import transforms
from sklearn.linear_model import LinearRegression
#User Define modules
sys.path.append('./train')
import models
import data
import paths
from visualize import testdemo, deproject_row_points
class Test:
def __init__(self, path_weight):
self.device = torch.device('cuda' if \
torch.cuda.is_available() else 'cpu')
# Initialize model
weight = torch.load(path_weight, map_location = self.device)
self.network = models.unet()
self.network.load_state_dict(weight)
self.network.eval()
self.network.to(self.device)
def denoise(self, img):
"""
Args:
numpy binary image (positive means traversible path)
Returns:
removed noise by keeping the max contour only*
*assuming the traversible path is indeed the largest contour
"""
contours,_ = cv2.findContours((img > 0 ).astype('uint8'),
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
out = np.zeros_like(img).astype(np.uint8)
idx = None
amax = -1
for i , ctr in enumerate(contours):
if cv2.contourArea(ctr) > amax:
amax = cv2.contourArea(ctr)
idx = i
if (idx==None):
return img
cv2.drawContours(out, contours, idx, 255, -1)
return out
def get_line(self, points):
'''
Args:
point: N X 3 numpy array representing pointcloud,
each row is (x, y, z)
Returns:
degree of the least-square-fitted line
'''
lr = LinearRegression()
xs = points[:, 0].reshape(-1, 1)
ys = points[:, 1].reshape(-1, 1)
lr.fit(xs, ys)
xmin = np.min(xs)
xmax = np.max(xs)
xs = np.array([xmin, xmax])
ys = np.array([lr.predict([[xmin]]), lr.predict([[xmax]])])
ys = ys.flatten()
return xs, ys, lr.coef_
def success(self, prediction, labels, rimg):
"""
Args:
prediction H X W numpy gray scale image, traversible is 255
label H X W numpy binary image
rimg H X W numpy gray scale range image
Returns:
whether predicted line is within error bound compared
to labeled line
"""
pc_res = deproject_row_points(prediction, rimg.copy())
pc_lbl = deproject_row_points(labels, rimg.copy())
# two set of vertex points that define a line
x_res, y_res, coef_res = self.get_line(pc_res)
x_lbl, y_lbl, coef_lbl = self.get_line(pc_lbl)
# compute orientation from gradient
deg_res = np.rad2deg(np.arctan2(y_res[-1] - y_res[0], x_res[-1] - x_res[0]))
deg_lbl = np.rad2deg(np.arctan2(y_lbl[-1] - y_lbl[0], x_lbl[-1] - x_lbl[0]))
# compute distance base on labeled-line's two vertices
y_res_dw = coef_res[0] * (x_lbl[0] - x_res[0]) + y_res[0]
y_res_up = coef_res[0] * (x_lbl[1] - x_res[1]) + y_res[1]
y_dist = (np.linalg.norm(np.array([y_res_dw, y_res_up]).flatten() -y_lbl))
#print(y_dist)
return abs(deg_res - deg_lbl) < 3.0 and y_dist <= 0.5
def evaluate(self, test_loader, visualize_flag):
res_iou = []
res_align = []
for num, (rimg, feats, labels) in enumerate(test_loader):
feats, labels = feats.to(self.device), labels.to(self.device)
seg_im = self.network(feats)
# Convert tensor to numpy
to_numpy = lambda x : x[0].cpu().detach().numpy()
(rimg, seg_im, labels) = map(to_numpy, (rimg, seg_im, labels))
# Compute range images
imax = seg_im.argmax(axis = 0)
prediction = self.denoise(imax==1)
# Compute iou
intersec = ( labels > 0 ) & (prediction > 0)
union = ( labels > 0 ) | (prediction > 0)
iou = np.sum(intersec)/np.sum(union)
res_iou.append(iou)
# Determine success of alignment between predict line & annotated line
success = self.success(prediction, labels, rimg)
res_align.append(success)
# Visualize model output and deprojected pointcloud
if(visualize_flag == "all"):
#testdemo(prediction, labels, rimg, birdview=False)
testdemo(prediction, labels, rimg, birdview=True)
elif(visualize_flag == "failed" and not success):
print("failed id = {}".format(num))
testdemo(prediction, labels, rimg, birdview=False)
testdemo(prediction, labels, rimg, birdview=True)
#print(res_iou)
print("iou = {:.3f}, success rate = {:.2f}".format(\
np.mean(res_iou), np.mean(res_align)))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Evalutate results of range image unet.')
parser.add_argument("-m", "--model", required=True, nargs="+", help="paths to deep learning segmentation model")
parser.add_argument("-s", "--show", choices = ["all", "failed", "none"], default="none",
help="whether or not to visualize results")
args = vars(parser.parse_args())
test_dataset = data.RangeViewDataset(mode = 'test')
test_loader = DataLoader(test_dataset)
for path in (args["model"]):
print(path)
test = Test(path) #best 0406_RIU03_60
test.evaluate(test_loader, args["show"])