forked from anshulpaigwar/GndNet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate_SemanticKITTI.py
executable file
·282 lines (221 loc) · 9.95 KB
/
evaluate_SemanticKITTI.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
"""
Author: Anshul Paigwar
email: [email protected]
"""
import argparse
import os
import shutil
import yaml
import time
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
# from modules import gnd_est_Loss
from model import GroundEstimatorNet
from modules.loss_func import MaskedHuberLoss
from dataset_utils.dataset_provider import get_train_loader, get_valid_loader
from utils.utils import lidar_to_img, lidar_to_heightmap, segment_cloud
from utils.point_cloud_ops import points_to_voxel
import ipdb as pdb
import matplotlib.pyplot as plt
import numba
from numba import jit,types
use_cuda = torch.cuda.is_available()
if use_cuda:
print('setting gpu on gpu_id: 0') #TODO: find the actual gpu id being used
#############################################xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#######################################
parser = argparse.ArgumentParser()
parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to latest checkpoint (default: none)')
parser.add_argument('--config', default='config/config_kittiSem.yaml', type=str, metavar='PATH', help='path to config file (default: none)')
parser.add_argument('-v', '--visualize', default=False, dest='visualize', action='store_true', help='visualize model on validation set')
parser.add_argument('-gnd', '--visualize_gnd',default=False, dest='visualize_gnd', action='store_true', help='visualize ground elevation')
parser.add_argument('--data_dir', default="/home/anshul/es3cap/semkitti_gndnet/kitti_semantic/dataset/sequences/07/",
type=str, metavar='PATH', help='path to dataset (default: none)')
parser.add_argument('--logdir', default=None,
type=str, metavar='PATH', help='path to save pred (default: none)')
args = parser.parse_args()
if os.path.isfile(args.config):
print("using config file:", args.config)
with open(args.config) as f:
config_dict = yaml.load(f, Loader=yaml.FullLoader)
class ConfigClass:
def __init__(self, **entries):
self.__dict__.update(entries)
cfg = ConfigClass(**config_dict) # convert python dict to class for ease of use
else:
print("=> no config file found at '{}'".format(args.config))
print("setting batch_size to 1")
cfg.batch_size = 1
'''
if args.visualize:
# Ros Includes
import rospy
from utils.ros_utils import np2ros_pub_2, gnd_marker_pub
from sensor_msgs.msg import PointCloud2
from visualization_msgs.msg import Marker
rospy.init_node('gnd_data_provider', anonymous=True)
pcl_pub = rospy.Publisher("/kitti/velo/pointcloud", PointCloud2, queue_size=10)
marker_pub_2 = rospy.Publisher("/kitti/gnd_marker_pred", Marker, queue_size=10)
'''
model = GroundEstimatorNet(cfg).cuda()
optimizer = optim.SGD(model.parameters(), lr=cfg.lr, momentum=0.9, weight_decay=0.0005)
def get_GndSeg(sem_label, GndClasses):
index = np.isin(sem_label, GndClasses)
GndSeg = np.ones(sem_label.shape)
GndSeg[index] = 0
index = np.isin(sem_label, [0,1])
GndSeg[index] = -1
return GndSeg
@jit(nopython=True)
def remove_outliers(pred_GndSeg, GndSeg): # removes the points outside grid and unlabled points
index = pred_GndSeg >= 0
pred_GndSeg = pred_GndSeg[index]
GndSeg = GndSeg[index]
index = GndSeg >=0
pred_GndSeg = pred_GndSeg[index]
GndSeg = GndSeg[index]
return 1-pred_GndSeg, 1-GndSeg
@jit(nopython=True)
def _shift_cloud(cloud, height):
cloud += np.array([0,0,height,0], dtype=np.float32)
return cloud
def get_target_gnd(cloud, sem_label):
if cloud.shape[0] != sem_label.shape[0]:
raise Exception('Points and label MisMatch')
index = np.isin(sem_label, [40, 44, 48, 49,60,72])
gnd = cloud[index]
gnd_mask = lidar_to_img(np.copy(gnd), np.asarray(cfg.grid_range), cfg.voxel_size[0], fill = 1)
gnd_heightmap = lidar_to_heightmap(np.copy(gnd), np.asarray(cfg.grid_range), cfg.voxel_size[0], max_points = 100)
return gnd_heightmap, gnd_mask
def InferGround(cloud):
cloud = _shift_cloud(cloud[:,:4], cfg.lidar_height)
voxels, coors, num_points = points_to_voxel(cloud, cfg.voxel_size, cfg.pc_range, cfg.max_points_voxel, True, cfg.max_voxels)
voxels = torch.from_numpy(voxels).float().cuda()
coors = torch.from_numpy(coors)
coors = F.pad(coors, (1,0), 'constant', 0).float().cuda()
num_points = torch.from_numpy(num_points).float().cuda()
with torch.no_grad():
output = model(voxels, coors, num_points)
return output
# if args.visualize:
# plt.ion()
# fig = plt.figure()
def evaluate_SemanticKITTI(data_dir):
velodyne_dir = data_dir + "velodyne/"
label_dir = data_dir + 'labels/'
frames = os.listdir(velodyne_dir)
# calibration = parse_calibration(os.path.join(data_dir, "calib.txt"))
# poses = parse_poses(os.path.join(data_dir, "poses.txt"), calibration)
# rate = rospy.Rate(2) # 10hz
# angle = 0
# increase = True
iou_score = 0
mse_score = 0
prec_score = 0
recall_score = 0
print(len(frames))
for f in range(len(frames)):
points_path = os.path.join(velodyne_dir, "%06d.bin" % f)
points = np.fromfile(points_path, dtype=np.float32).reshape(-1, 4)
label_path = os.path.join(label_dir, "%06d.label" % f)
sem_label = np.fromfile(label_path, dtype=np.uint32)
sem_label = sem_label.reshape((-1))
pred_gnd = InferGround(points)
pred_gnd = pred_gnd.cpu().numpy()
# TODO: Remove the points which are very below the ground
pred_GndSeg = segment_cloud(points.copy(),np.asarray(cfg.grid_range), cfg.voxel_size[0], elevation_map = pred_gnd.T, threshold = 0.2)
GndSeg = get_GndSeg(sem_label, GndClasses = [40, 44, 48, 49,60,72])
'''
if args.visualize:
np2ros_pub_2(points, pcl_pub, None, pred_GndSeg)
if args.visualize_gnd:
gnd_marker_pub(pred_gnd, marker_pub_2, cfg, color = "red")
# pdb.set_trace()
'''
pred_GndSeg, GndSeg = remove_outliers(pred_GndSeg, GndSeg)
intersection = np.logical_and(GndSeg, pred_GndSeg)
union = np.logical_or(GndSeg, pred_GndSeg)
iou = np.sum(intersection) / np.sum(union)
prec = np.sum(intersection)/pred_GndSeg.sum()
recall = np.sum(intersection)/GndSeg.sum()
# tn = np.count_nonzero(pred_GndSeg==0) - np.sum(intersection)
# iou = np.count_nonzero(pred_GndSeg==0)/(np.count_nonzero(GndSeg==0)+tn)
iou_score += iou
prec_score += prec
recall_score += recall
target_gnd, gnd_mask = get_target_gnd(points, sem_label)
# if args.visualize:
# fig.clear()
# fig.add_subplot(1, 3, 1)
# plt.imshow(gnd_mask, interpolation='nearest')
# fig.add_subplot(1, 3, 2)
# cs = plt.imshow(target_gnd*gnd_mask, interpolation='nearest')
# cbar = fig.colorbar(cs)
# fig.add_subplot(1, 3, 3)
# cs = plt.imshow(pred_gnd.T*gnd_mask, interpolation='nearest')
# cbar = fig.colorbar(cs)
# plt.show()
mse = (np.square(target_gnd - pred_gnd.T)*gnd_mask).sum()
mse = mse/gnd_mask.sum()
mse_score += mse
print(f, iou, mse, prec, recall)
iou_score = iou_score/len(frames)
mse_score = mse_score/len(frames)
recall_score = recall_score/len(frames)
prec_score = prec_score/len(frames)
print(iou_score, mse_score, prec_score, recall_score)
def infer(data_dir):
dataset_dir = os.path.join(data_dir)
sequences = os.listdir(dataset_dir)
sequences.sort()
for seq in sequences:
velodyne_dir = os.path.join(dataset_dir, seq, 'velodyne')
frames = os.listdir(velodyne_dir)
frames.sort()
print(seq)
print(len(frames))
for file in frames:
points_path = os.path.join(velodyne_dir, file)
points = np.fromfile(points_path, dtype=np.float32).reshape(-1, 4)
#get ground labels
pred_gnd = InferGround(points)
pred_gnd = pred_gnd.cpu().numpy()
pred_GndSeg = segment_cloud(points.copy(),np.asarray(cfg.grid_range), cfg.voxel_size[0], elevation_map = pred_gnd.T, threshold = 0.2)
#pred_GndSeg, GndSeg = remove_outliers(pred_GndSeg, GndSeg)
#write to file
pred_GndSeg = pred_GndSeg.reshape((-1)).astype(np.int16)
path = os.path.join(args.logdir, 'ground_removal', seq, "predictions", file)
if not os.path.exists(os.path.join(args.logdir, 'ground_removal')):
os.makedirs(os.path.join(args.logdir, 'ground_removal'))
if not os.path.exists(os.path.join(args.logdir, 'ground_removal', seq)):
os.makedirs(os.path.join(args.logdir, 'ground_removal', seq))
if not os.path.exists(os.path.join(args.logdir, 'ground_removal', seq, "predictions")):
os.makedirs(os.path.join(args.logdir, 'ground_removal', seq, "predictions"))
pred_GndSeg.tofile(path)
print('{}{}'.format(seq, ' done'))
def main():
# rospy.init_node('pcl2_pub_example', anonymous=True)
global args
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
lowest_loss = checkpoint['lowest_loss']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.resume))
else:
raise Exception('please specify checkpoint to load')
#evaluate_SemanticKITTI(args.data_dir)
infer(args.data_dir)
if __name__ == '__main__':
main()