-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexp_opt.py
197 lines (152 loc) · 10.1 KB
/
exp_opt.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
import time
import numpy as np
import time
from multiprocessing import Pool
import threading
from pathlib import Path
from tqdm import tqdm
from o3d_util import O3dUtil as o3d_util
from my_util import MyUtil as my_util
from o3d_sampling import O3dSampling as o3d_sampling
import copy
import argparse
from exp_spatial_interpolation import add_undefined_points, get_yps_by_sector_list, get_xyzs_by_ypds
from exp_temporal_interpolation import save_first_frame
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KDTree
import torch
from PointINet.models.models import PointINet
from eval_util import EvalUtil as eval_util
def fun_lidar_error_concealment_optimal(exp_path, packet_loss_rate, pro_name, args):
point_count = args["point_count"]
sector_size = args["sector_size"]
recursion = args["recursion"]
# 2DNN config
lidar_range = args["lidar_range"]
lidar_coordinate = args["lidar_coordinate"]
azimuth_size = args["azimuth_size"]
channel_size = args["channel_size"]
v_fov = args["v_fov"]
v_fov_start = args["v_fov_start"]
v_fov_end = args["v_fov_end"]
angular_resolution = args["angular_resolution"]
# PointINet config
step_t = args["step_t"]
sampling_type = args["sampling_type"]
remove_outlier = args["remove_outlier"]
net = PointINet()
net.load_state_dict(torch.load(args["model_path"]))
net.flow.load_state_dict(torch.load(args["flow_model_path"]))
net.eval()
net.cuda()
velodyne_compression_dir = Path() / exp_path / "velodyne_compression"
save_dir = Path() / exp_path / "cache_frame" / "receiver_{}_{}".format(pro_name, packet_loss_rate)
my_util.create_dir(save_dir)
none_dir = Path() / exp_path / "cache_frame" / "receiver_none_{}".format(packet_loss_rate)
file_names = sorted(none_dir.glob("*.bin"))
file_count = len(file_names)
assert file_count > 0, "bin files not exists: {}".format(none_dir)
first_file_name = Path() / exp_path / "velodyne_compression" / "{}.bin".format(file_names[0].stem)
save_first_frame(first_file_name, point_count, save_dir, sector_size)
for i, file_name in tqdm(enumerate(file_names), desc="{} {}".format(pro_name, packet_loss_rate), total=len(file_names)):
if i == 0 or i + 1 >= file_count:
continue
start_time = time.process_time()
tp_queue, si_queue, ti_queue = [], [], []
points_xyzi = np.fromfile(file_name, dtype=np.float32, count=-1).reshape([-1, 4])
tp_queue.append(points_xyzi)
si_queue.append(points_xyzi)
ti_queue.append(points_xyzi)
sector_list_p = np.load(file_name.parent / "{}_log.npy".format(file_names[i-1].stem), allow_pickle=True).item()["sector_list"]
sector_list = np.load(file_name.parent / "{}_log.npy".format(file_name.stem), allow_pickle=True).item()["sector_list"]
sector_list_n = np.load(file_name.parent / "{}_log.npy".format(file_names[i+1].stem), allow_pickle=True).item()["sector_list"]
all_sector_list = range(sector_size)
drop_sector_list = list(set(all_sector_list).difference(set(sector_list)))
sector_list_rate_p = 1 - (len(sector_list_p) / sector_size)
sector_lost_rate = 1 - (len(sector_list) / sector_size)
sector_lost_rate_n = 1 - (len(sector_list_n) / sector_size)
# TP
npy_file_name = file_name.parent.parent.parent / "location" / "{}.npy".format(file_name.stem)
assert npy_file_name.exists(), "npy file not exists: {}".format(npy_file_name)
npy_location = np.load(npy_file_name, allow_pickle=True).item()
previous_id = int(file_names[i-1].stem)
if recursion:
previous_log_file_name = save_dir / "{0:06}_log.npy".format(previous_id)
previous_bin_file_name = save_dir / "{0:06}.bin".format(previous_id)
else:
previous_log_file_name = file_name.parent / "{0:06}_log.npy".format(previous_id)
previous_bin_file_name = file_name.parent / "{0:06}.bin".format(previous_id)
previous_locaton_file_name = file_name.parent.parent.parent / "location" / "{0:06}.npy".format(previous_id)
assert previous_log_file_name.exists() and previous_bin_file_name.exists() and previous_locaton_file_name.exists() , "previous file not exists: {}".format(previous_bin_file_name)
previous_points_xyzi = np.fromfile(previous_bin_file_name, dtype=np.float32, count=-1).reshape([-1, 4])
previous_location = np.load(previous_locaton_file_name, allow_pickle=True).item()
new_offset_xyzrs = np.asarray(np.array([npy_location["x"], npy_location["y"], npy_location["z"], npy_location["rx"], npy_location["ry"], npy_location["rz"]])-np.array([previous_location["x"], previous_location["y"], previous_location["z"], previous_location["rx"], previous_location["ry"], previous_location["rz"]]))
previous_points_xyzi = my_util.split_sector_xyzi_by_sector_list(previous_points_xyzi, sector_size, drop_sector_list)
tp_queue.append(my_util.split_sector_xyzi_by_sector_list(o3d_util.translate_by_matrix(o3d_util.rotate_by_matrix(previous_points_xyzi, o3d_util.get_rotation_matrix_from_angles(new_offset_xyzrs[3], new_offset_xyzrs[4], new_offset_xyzrs[5])), new_offset_xyzrs[0:3]), sector_size, drop_sector_list))
# SI
added_points_ypdac = add_undefined_points(points_xyzi, v_fov, v_fov_start, v_fov_end, channel_size, azimuth_size, angular_resolution, lidar_coordinate)
received_points_ypd = my_util.split_sector_ypd_by_sector_list(added_points_ypdac[:, :3], sector_size, sector_list)
dropped_points_yp = get_yps_by_sector_list(sector_size, drop_sector_list, azimuth_size, v_fov, v_fov_start, v_fov_end, channel_size)
if dropped_points_yp.shape[0] > 0:
tree_2d = KDTree(received_points_ypd[:, :2])
distances, indices = tree_2d.query(dropped_points_yp)
predicted_d = received_points_ypd[indices, 2]
predicted_points_ypd = np.concatenate((dropped_points_yp, np.array(predicted_d).reshape((-1, 1))), axis=1)
predicted_points_ypd = predicted_points_ypd[np.where((predicted_points_ypd[:, 2] > 0) & (predicted_points_ypd[:, 2] < lidar_range))]
si_queue.append(np.concatenate((get_xyzs_by_ypds(predicted_points_ypd), -np.ones([predicted_points_ypd.shape[0], 1])), axis=1))
# TI
fn1_bin = file_names[i - 1]
fn2_bin = file_names[i + 1]
with torch.no_grad():
pc1, color1 = o3d_sampling.get_lidar_by_bin_multi_type(o3d_sampling.bin2xyzi(fn1_bin), 16384, sampling_type, remove_outlier=remove_outlier)
pc2, color2 = o3d_sampling.get_lidar_by_bin_multi_type(o3d_sampling.bin2xyzi(fn2_bin), 16384, sampling_type, remove_outlier=remove_outlier)
t = torch.tensor([step_t]).cuda().float()
pred_mid_pc = net(pc1, pc2, color1, color2, t)
pred_mid_pc = pred_mid_pc.squeeze(0).permute(1, 0).cpu().numpy()
ti_queue.append(my_util.split_sector_xyzi_by_sector_list(pred_mid_pc, sector_size, drop_sector_list))
tp_all_points_xyzi = np.concatenate(tp_queue, axis=0)
si_all_points_xyzi = np.concatenate(si_queue, axis=0)
ti_all_points_xyzi = np.concatenate(ti_queue, axis=0)
if point_count != -1:
tp_all_points_xyzi = o3d_sampling.adaptive_sampling_by_xyzi(tp_all_points_xyzi, point_count)
si_all_points_xyzi = o3d_sampling.adaptive_sampling_by_xyzi(si_all_points_xyzi, point_count)
ti_all_points_xyzi = o3d_sampling.adaptive_sampling_by_xyzi(ti_all_points_xyzi, point_count)
gt_frame_file_name = velodyne_compression_dir / "{}.bin".format(file_name.stem)
assert gt_frame_file_name.exists(), "gt frame not exist: {}".format(gt_frame_file_name)
gt_frame_points_xyzi = np.fromfile(gt_frame_file_name, dtype=np.float32, count=-1).reshape([-1, 4])
tp_cd = eval_util.cal_cd_by_xyzis(tp_all_points_xyzi, gt_frame_points_xyzi)
si_cd = eval_util.cal_cd_by_xyzis(si_all_points_xyzi, gt_frame_points_xyzi)
ti_cd = eval_util.cal_cd_by_xyzis(ti_all_points_xyzi, gt_frame_points_xyzi)
opm_algo_index = np.argmin(np.array([tp_cd, si_cd, ti_cd]))
if opm_algo_index == 0:
opm_points_xyzi = tp_all_points_xyzi
elif opm_algo_index == 1:
opm_points_xyzi = si_all_points_xyzi
elif opm_algo_index == 2:
opm_points_xyzi = ti_all_points_xyzi
else:
assert False, "NOT OPM ALGO"
running_time = time.process_time() - start_time
opm_points_xyzi.astype(np.float32).tofile(str(save_dir / "{}.bin".format(file_name.stem)))
o3d_util.save_ply_by_xyzi(opm_points_xyzi, str(save_dir / "{}.ply".format(file_name.stem)))
np.save(save_dir / "{}_log.npy".format(file_name.stem), {"id": i + 1, "file_name": str(file_name), "model_type": "optimal", "concealment_types": opm_algo_index, "sector_size": sector_size, "sector_list": sector_list + drop_sector_list, "concealed_sector_list": drop_sector_list, "time": time.time(), "running_time": running_time})
def main(args):
m_config = my_util.get_config(args.c)
execution_name = "lidar_error_concealment_optimal"
exp_path = m_config["exp"]["path"]
packet_loss_rates = m_config["exp"]["packet_loss_rates"]
if m_config["exp"][execution_name]["enable"]:
for packet_loss_rate in packet_loss_rates:
globals()["fun_{}".format(execution_name)](exp_path, packet_loss_rate, execution_name, copy.deepcopy(m_config["exp"][execution_name]))
print("MAIN THREAD STOP")
def parse_args():
parser = argparse.ArgumentParser(description='demo')
parser.add_argument('--c', type=str, default="config.yaml")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
start_time = time.time()
main(args)
print("Total Running Time: {}".format(time.time() - start_time))