-
Notifications
You must be signed in to change notification settings - Fork 6
/
simple_inference_waymo.py
215 lines (185 loc) · 7.91 KB
/
simple_inference_waymo.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
# modified from the single_inference.py by @muzi2045
# from spconv.utils import VoxelGenerator as VoxelGenerator
from itertools import count
from det3d.core import anchor
from det3d.core.input.voxel_generator import VoxelGenerator
from det3d.datasets.pipelines.loading import read_single_waymo
from det3d.datasets.pipelines.loading import get_obj
from det3d.torchie.trainer import load_checkpoint
from det3d.models import build_detector
from det3d.torchie import Config
from tqdm import tqdm
import numpy as np
import pickle
import open3d as o3d
import argparse
import torch
import time
import os
import re
voxel_generator = None
model = None
device = None
def initialize_model(args):
global model, voxel_generator
cfg = Config.fromfile(args.config)
model = build_detector(cfg.S_model, train_cfg=None, test_cfg=cfg.test_cfg)
if args.checkpoint is not None:
load_checkpoint(model, args.checkpoint, map_location="cpu")
# print(model)
if args.fp16:
print("cast model to fp16")
model = model.half()
model = model.cuda()
model.eval()
global device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
range = cfg.voxel_generator.range
voxel_size = cfg.voxel_generator.voxel_size
max_points_in_voxel = cfg.voxel_generator.max_points_in_voxel
max_voxel_num = cfg.voxel_generator.max_voxel_num
voxel_generator = VoxelGenerator(
voxel_size=voxel_size,
point_cloud_range=range,
max_num_points=max_points_in_voxel,
max_voxels=max_voxel_num
)
return model
def voxelization(points, voxel_generator):
voxel_output = voxel_generator.generate(points)
voxels, coords, num_points = \
voxel_output['voxels'], voxel_output['coordinates'], voxel_output['num_points_per_voxel']
return voxels, coords, num_points
def _process_inputs(points, fp16):
voxels, coords, num_points = voxel_generator.generate(points)
with open('./anchors.pkl','rb') as f:
anchors = pickle.load(f)
num_voxels = np.array([voxels.shape[0]], dtype=np.int32)
grid_size = voxel_generator.grid_size
coords = np.pad(coords, ((0, 0), (1, 0)), mode='constant', constant_values = 0)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# print(device)
voxels = torch.tensor(voxels, dtype=torch.float32, device=device)
coords = torch.tensor(coords, dtype=torch.int32, device=device)
num_points = torch.tensor(num_points, dtype=torch.int32, device=device)
num_voxels = torch.tensor(num_voxels, dtype=torch.int32, device=device)
anchors = torch.tensor(anchors[None,None], dtype=torch.float32, device=device)
if fp16:
voxels = voxels.half()
inputs = dict(
voxels = voxels,
num_points = num_points,
num_voxels = num_voxels,
coordinates = coords,
shape = [grid_size],
anchors = anchors
)
return inputs
def run_model(points, fp16=False):
with torch.no_grad():
data_dict = _process_inputs(points, fp16)
torch.cuda.synchronize()
start = time.time()
outputs, x_fea, comp_fea = model(data_dict, return_loss=False, return_feature= True)
# outputs = model(data_dict, return_loss=False, return_feature= False)
torch.cuda.synchronize()
end = time.time()
return {'boxes': outputs[0]['box3d_lidar'].cpu().numpy(),
'scores': outputs[0]['scores'].cpu().numpy(),
'classes': outputs[0]['label_preds'].cpu().numpy(),
'time':end-start,}
# 'x_fea':x_fea.cpu().numpy(),
# 'comp_fea': comp_fea.cpu().numpy()}
def process_example(points, fp16=False):
output = run_model(points, fp16)
# assert len(output) == 6
# assert set(output.keys()) == set(('boxes', 'scores', 'classes', 'time','x_fea','comp_fea'))
num_objs = output['boxes'].shape[0]
assert output['scores'].shape[0] == num_objs
assert output['classes'].shape[0] == num_objs
return output
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
'''
return [ atoi(c) for c in re.split(r'_(\d+).pkl', text) ]
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="CenterPoint")
parser.add_argument("config", help="path to config file")
parser.add_argument(
"--checkpoint", help="the path to checkpoint which the model read from", default=None, type=str
)
parser.add_argument('--input_data_dir', type=str, required=True)
parser.add_argument('--output_dir', type=str, required=True)
parser.add_argument('--fp16', action='store_true')
parser.add_argument('--threshold', default=0.5)
parser.add_argument('--visual', action='store_true')
parser.add_argument("--online", action='store_true')
parser.add_argument('--num_frame', default=-1, type=int)
args = parser.parse_args()
print("Please prepare your point cloud in waymo format and save it as a pickle dict with points key into the {}".format(args.input_data_dir))
print("One point cloud should be saved in one pickle file.")
print("Download and save the pretrained model at {}".format(args.checkpoint))
# Run any user-specified initialization code for their submission.
model = initialize_model(args)
latencies = []
visual_dicts = []
pred_dicts = {}
counter = 0
times = 0
lists = list(os.listdir(args.input_data_dir))
lists.sort(key=natural_keys)
for frame_name in tqdm(lists):
if counter == args.num_frame:
break
else:
counter += 1
pc_name = os.path.join(args.input_data_dir, frame_name)
points = read_single_waymo(get_obj(pc_name))
anno = get_obj('../data/Waymo/val/annos/'+frame_name)
gt_boxes = np.array([ o['box'] for o in anno['objects'] ])
gt_num_points = np.array([ o['num_points'] for o in anno['objects'] ])
indx = gt_num_points.nonzero()
gt_class = np.array([ o['label'] for o in anno['objects'] ])
try:
gt_boxes = gt_boxes[:,[0,1,2,3,4,5,-1]][indx]
gt_boxes[:,-1] = -gt_boxes[:,-1]
gt_class = np.ones([gt_boxes.shape[0]],dtype='int')*4
gt_score = np.ones([gt_boxes.shape[0]])
except:
gt_boxes = []
gt_class = []
gt_score = []
detections = process_example(points, args.fp16)
if len(gt_boxes) != 0:
detections['boxes'] = np.concatenate([detections['boxes'], gt_boxes],0)
detections['scores'] = np.concatenate([detections['scores'],gt_score], 0)
detections['classes'] = np.concatenate([detections['classes'],gt_class],0)
else:
detections['boxes'] = None
detections['scores'] = None
detections['classes'] = None
detections['name'] = frame_name
if counter > 5:
times += detections['time']
if args.visual and args.online:
pcd = o3d.geometry.PointCloud()
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points[:, :3])
visual = [pcd]
num_dets = detections['scores'].shape[0]
visual += plot_boxes(detections, args.threshold)
o3d.visualization.draw_geometries(visual)
elif args.visual:
visual_dicts.append({'points': points, 'detections': detections})
pred_dicts.update({frame_name: detections})
if args.visual:
with open(os.path.join(args.output_dir, 'visualization.pkl'), 'wb') as f:
pickle.dump(visual_dicts, f)
with open(os.path.join(args.output_dir, 'detections.pkl'), 'wb') as f:
pickle.dump(pred_dicts, f)
print("time: {}".format(1/(times/(len(os.listdir(args.input_data_dir))))))