-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgen2channel_detec.py
169 lines (141 loc) · 6.62 KB
/
gen2channel_detec.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
import os
import sys
import cv2
import torch
import skimage.transform
import numpy as np
import torch.nn.functional as F
from tqdm.auto import tqdm
from kitti_utils import generate_depth_map
from multiprocessing import Process, Queue, Pool, cpu_count
regenerate = False
if sys.argv[1] == 'regen':
regenerate = True
print("regenerating, will clean previous files")
if sys.argv[2] == 'r200':
input_folder = 'random200'
output_folder = 'r200_2cha'
print("for random 200 points sample")
elif sys.argv[2] == '4beam':
input_folder = '4beam'
output_folder = '2channel'
print("for 4-beams sample")
def get_detec_calib(folder, frame_idx):
img_path = os.path.join(folder, "image_02/data/{:06d}.png".format(int(frame_idx)))
imshape = cv2.imread(img_path).shape[:2]
if imshape == (375, 1242):
return 'kitti_data/2011_09_26'
if imshape == (370, 1224):
return 'kitti_data/2011_09_28'
if imshape == (374, 1238):
return 'kitti_data/2011_09_29'
if imshape == (370, 1226):
return 'kitti_data/2011_09_30'
if imshape == (376, 1241):
return 'kitti_data/2011_10_03'
def get_4beam(folder, frame_index, side, do_flip):
side_map = {"2": 2, "3": 3, "l": 2, "r": 3}
calib_path = get_detec_calib(folder, frame_index)
velo_filename = os.path.join(
folder,
"{}/{:06d}.bin".format(input_folder, int(frame_index)))
depth_gt = generate_depth_map(calib_path, velo_filename, side_map[side], shape=[384, 1280])
depth_gt = F.max_pool2d(torch.tensor(depth_gt).unsqueeze(0),
2, ceil_mode=True).squeeze().numpy()
if do_flip:
depth_gt = np.fliplr(depth_gt)
return depth_gt
def get_4beam_2channel(fourbeam, height=192, width=640, expand=2):
expanded_depth = torch.zeros([height, width], dtype=torch.float32)
confidence_map = torch.zeros([height, width], dtype=torch.float32)
accumulate = torch.zeros([height, width], dtype=torch.float32)
for i in range(76, 190):
for j in range(2, 638):
if fourbeam[i][j] != 0:
expanded_depth[i][j] = fourbeam[i][j]
confidence_map[i][j] = 1
accumulate[i][j] = 1
for dis in range(1, expand+1):
confidence = 1/(dis+1)
for horizontal in range(1, dis+1):
x = horizontal
y = dis - horizontal
if accumulate[i+x][j+y] == 0 or confidence_map[i+x][j+y] < confidence:
expanded_depth[i+x][j+y] = fourbeam[i][j]
confidence_map[i+x][j+y] = confidence
accumulate[i+x][j+y] = 1
elif confidence_map[i+x][j+y] == confidence:
expanded_depth[i + x][j + y] += fourbeam[i][j]
accumulate[i + x][j + y] += 1
if x != 0:
x = -horizontal
y = dis - horizontal
if accumulate[i + x][j + y] == 0 or confidence_map[i + x][j + y] < confidence:
expanded_depth[i + x][j + y] = fourbeam[i][j]
confidence_map[i + x][j + y] = confidence
accumulate[i + x][j + y] = 1
elif confidence_map[i + x][j + y] == confidence:
expanded_depth[i + x][j + y] += fourbeam[i][j]
accumulate[i + x][j + y] += 1
if y != 0:
x = horizontal
y = horizontal - dis
if accumulate[i + x][j + y] == 0 or confidence_map[i + x][j + y] < confidence:
expanded_depth[i + x][j + y] = fourbeam[i][j]
confidence_map[i + x][j + y] = confidence
accumulate[i + x][j + y] = 1
elif confidence_map[i + x][j + y] == confidence:
expanded_depth[i + x][j + y] += fourbeam[i][j]
accumulate[i + x][j + y] += 1
if x != 0 and y != 0:
x = -horizontal
y = horizontal - dis
if accumulate[i + x][j + y] == 0 or confidence_map[i + x][j + y] < confidence:
expanded_depth[i + x][j + y] = fourbeam[i][j]
confidence_map[i + x][j + y] = confidence
accumulate[i + x][j + y] = 1
elif confidence_map[i + x][j + y] == confidence:
expanded_depth[i + x][j + y] += fourbeam[i][j]
accumulate[i + x][j + y] += 1
accumulate[accumulate == 0] = 1
expanded_depth = torch.div(expanded_depth, accumulate)
return expanded_depth, confidence_map
data_path = 'kitti_data/'
def gen2channel(line):
folder = data_path + line[:-1].split()[0]
idx = int(line[:-1].split()[1])
side = line[:-1].split()[2]
out_path = folder+'/{}'.format(output_folder)
if not os.path.exists(out_path):
os.mkdir(out_path)
if not regenerate:
if os.path.isfile(out_path + '/{}_{}_{}.npy'.format(idx, side, False)):
if os.path.isfile(out_path + '/{}_{}_{}.npy'.format(idx, side, True)):
return
four_beam = get_4beam(folder, idx, side, False)
flip_four_beam = get_4beam(folder, idx, side, True)
four_beam = torch.from_numpy(four_beam.astype(np.float32))/100.0
flip_four_beam = torch.from_numpy(flip_four_beam.astype(np.float32))/100.0
expanded_depth, confidence_map = get_4beam_2channel(four_beam)
two_channel = torch.stack([expanded_depth, confidence_map]).numpy()
expanded_depth, confidence_map = get_4beam_2channel(flip_four_beam)
flip_two_channel = torch.stack([expanded_depth, confidence_map]).numpy()
np.save(out_path+'/{}_{}_{}.npy'.format(idx, side, False), two_channel)
np.save(out_path + '/{}_{}_{}.npy'.format(idx, side, True), flip_two_channel)
#print(out_path+'/{}_{}'.format(idx, side))
def update(*a):
pbar.update()
test_file_path = 'splits/detection/test.txt'
test_file = open(test_file_path, 'r')
lines = test_file.readlines()
test_file.close()
print("using {} cpu cores".format(cpu_count()))
pool = Pool(cpu_count())
pbar = tqdm(total=len(lines))
for line in lines:
#gen2channel(line)
pool.apply_async(gen2channel, args=(line,), callback=update)
pool.close()
pool.join()
pbar.clear(nolock=False)
pbar.close()