This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
kitti_predict.py
227 lines (197 loc) · 8.11 KB
/
kitti_predict.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
import argparse
import os
import json
import numpy as np
import tensorflow as tf
import open3d
import time
import model
from dataset.kitti_dataset import KittiDataset
from tf_ops.tf_interpolate import interpolate_label_with_color
def interpolate_dense_labels(sparse_points, sparse_labels, dense_points, k=3):
sparse_pcd = open3d.PointCloud()
sparse_pcd.points = open3d.Vector3dVector(sparse_points)
sparse_pcd_tree = open3d.KDTreeFlann(sparse_pcd)
dense_labels = []
for dense_point in dense_points:
result_k, sparse_indexes, _ = sparse_pcd_tree.search_knn_vector_3d(
dense_point, k
)
knn_sparse_labels = sparse_labels[sparse_indexes]
dense_label = np.bincount(knn_sparse_labels).argmax()
dense_labels.append(dense_label)
return dense_labels
class PredictInterpolator:
def __init__(self, checkpoint_path, num_classes, hyper_params):
# Get ops from graph
with tf.device("/gpu:0"):
# Placeholders
pl_sparse_points_centered_batched, _, _ = model.get_placeholders(
hyper_params["num_point"], hyperparams=hyper_params
)
pl_is_training = tf.placeholder(tf.bool, shape=())
# Prediction
pred, _ = model.get_model(
pl_sparse_points_centered_batched,
pl_is_training,
num_classes,
hyperparams=hyper_params,
)
sparse_labels_batched = tf.argmax(pred, axis=2)
# (1, num_sparse_points) -> (num_sparse_points,)
sparse_labels = tf.reshape(sparse_labels_batched, [-1])
sparse_labels = tf.cast(sparse_labels, tf.int32)
# Saver
saver = tf.train.Saver()
# Graph for interpolating labels
# Assuming batch_size == 1 for simplicity
pl_sparse_points_batched = tf.placeholder(tf.float32, (None, None, 3))
sparse_points = tf.reshape(pl_sparse_points_batched, [-1, 3])
pl_dense_points = tf.placeholder(tf.float32, (None, 3))
pl_knn = tf.placeholder(tf.int32, ())
dense_labels, dense_colors = interpolate_label_with_color(
sparse_points, sparse_labels, pl_dense_points, pl_knn
)
self.ops = {
"pl_sparse_points_centered_batched": pl_sparse_points_centered_batched,
"pl_sparse_points_batched": pl_sparse_points_batched,
"pl_dense_points": pl_dense_points,
"pl_is_training": pl_is_training,
"pl_knn": pl_knn,
"dense_labels": dense_labels,
"dense_colors": dense_colors,
}
# Restore checkpoint to session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.allow_soft_placement = True
config.log_device_placement = False
self.sess = tf.Session(config=config)
saver.restore(self.sess, checkpoint_path)
print("Model restored")
def predict_and_interpolate(
self,
sparse_points_centered_batched,
sparse_points_batched,
dense_points,
run_metadata=None,
run_options=None,
):
dense_labels_val, dense_colors_val = self.sess.run(
[self.ops["dense_labels"], self.ops["dense_colors"]],
feed_dict={
self.ops[
"pl_sparse_points_centered_batched"
]: sparse_points_centered_batched,
self.ops["pl_sparse_points_batched"]: sparse_points_batched,
self.ops["pl_dense_points"]: dense_points,
self.ops["pl_knn"]: 3,
self.ops["pl_is_training"]: False,
},
)
return dense_labels_val, dense_colors_val
if __name__ == "__main__":
np.random.seed(0)
# Parser
parser = argparse.ArgumentParser()
parser.add_argument(
"--num_samples",
type=int,
default=8,
help="# samples, each contains num_point points",
)
parser.add_argument("--ckpt", default="", help="Checkpoint file")
parser.add_argument("--save", action="store_true", default=False)
parser.add_argument(
"--kitti_root", default="", help="Checkpoint file", required=True
)
flags = parser.parse_args()
hyper_params = json.loads(open("semantic_no_color.json").read())
# Create output dir
sparse_output_dir = os.path.join("result", "sparse")
dense_output_dir = os.path.join("result", "dense")
os.makedirs(sparse_output_dir, exist_ok=True)
os.makedirs(dense_output_dir, exist_ok=True)
# Dataset
dataset = KittiDataset(
num_points_per_sample=hyper_params["num_point"],
base_dir=flags.kitti_root,
dates=["2011_09_26"],
# drives=["0095", "0001"],
drives=["0095"],
box_size_x=hyper_params["box_size_x"],
box_size_y=hyper_params["box_size_y"],
)
# Model
max_batch_size = 128 # The more the better, limited by memory size
predictor = PredictInterpolator(
checkpoint_path=flags.ckpt,
num_classes=dataset.num_classes,
hyper_params=hyper_params,
)
# Init visualizer
dense_pcd = open3d.PointCloud()
vis = open3d.Visualizer()
vis.create_window()
vis.add_geometry(dense_pcd)
render_option = vis.get_render_option()
render_option.point_size = 0.05
to_reset_view_point = True
for kitti_file_data in dataset.list_file_data:
timer = {
"load_data": 0,
"predict_interpolate": 0,
"visualize": 0,
"write_data": 0,
"total": 0,
}
global_start_time = time.time()
# Predict for num_samples times
points_collector = []
pd_labels_collector = []
# Get data
start_time = time.time()
points_centered, points = kitti_file_data.get_batch_of_one_z_box_from_origin(
num_points_per_sample=hyper_params["num_point"]
)
if len(points_centered) > max_batch_size:
raise NotImplementedError("TODO: iterate batches if > max_batch_size")
timer["load_data"] += time.time() - start_time
# Predict and interpolate
start_time = time.time()
dense_points = kitti_file_data.points
dense_labels, dense_colors = predictor.predict_and_interpolate(
sparse_points_centered_batched=points_centered, # (batch_size, num_sparse_points, 3)
sparse_points_batched=points, # (batch_size, num_sparse_points, 3)
dense_points=dense_points, # (num_dense_points, 3)
)
timer["predict_interpolate"] += time.time() - start_time
# Visualize
start_time = time.time()
dense_pcd.points = open3d.Vector3dVector(dense_points)
dense_pcd.colors = open3d.Vector3dVector(dense_colors.astype(np.float64))
vis.update_geometry()
if to_reset_view_point:
vis.reset_view_point(True)
to_reset_view_point = False
vis.poll_events()
vis.update_renderer()
timer["visualize"] += time.time() - start_time
# Save dense point cloud with predicted labels
if flags.save:
start_time = time.time()
file_prefix = os.path.basename(kitti_file_data.file_path_without_ext)
dense_pcd = open3d.PointCloud()
dense_pcd.points = open3d.Vector3dVector(dense_points.reshape((-1, 3)))
dense_pcd_path = os.path.join(dense_output_dir, file_prefix + ".pcd")
open3d.write_point_cloud(dense_pcd_path, dense_pcd)
print("Exported dense_pcd to {}".format(dense_pcd_path))
dense_labels_path = os.path.join(dense_output_dir, file_prefix + ".labels")
np.savetxt(dense_labels_path, dense_labels, fmt="%d")
print("Exported dense_labels to {}".format(dense_labels_path))
timer["write_data"] += time.time() - start_time
timer["total"] += time.time() - global_start_time
# Print timer
fmt_string = "[{:5.2f} FPS] " + ": {:.04f}, ".join(timer.keys()) + ": {:.04f}"
fmt_values = [1.0 / timer["total"]] + list(timer.values())
print(fmt_string.format(*fmt_values))