-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
322 lines (294 loc) · 11.6 KB
/
utils.py
File metadata and controls
322 lines (294 loc) · 11.6 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import cv2
from tqdm import tqdm
import numpy as np
import time
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
import random
import torch
from torch.utils.data import ConcatDataset
import os
import argparse
class Timer:
"""
count running time
"""
def __init__(self, msg='Time elapsed'):
self.msg = msg
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
duration = self.end - self.start
print(f'{self.msg}: {duration:.2f}s')
def binary_search_h5_dset(dset, x, l=None, r=None, side='left'):
"""
Binary search for a timestamp in an HDF5 event file, without
loading the entire file into RAM
@param dset The HDF5 dataset
@param x The timestamp being searched for
@param l Starting guess for the left side (0 if None is chosen)
@param r Starting guess for the right side (-1 if None is chosen)
@param side Which side to take final result for if exact match is not found
@returns Index of nearest event to 'x'
"""
l = 0 if l is None else l
r = len(dset)-1 if r is None else r
while l <= r:
mid = l + (r - l)//2;
midval = dset[mid]
if midval == x:
return mid
elif midval < x:
l = mid + 1
else:
r = mid - 1
if side == 'left':
return l
return r
def filter_events_time(events, start_time, end_time, voxel_type='gopro_7'):
"""
select time range
type1 : dataset from cross attention model
type2 : dataset from zhangxiang
"""
if voxel_type == 'EFNet':
start_index = binary_search_h5_dset(events['ts'],start_time)
end_index = binary_search_h5_dset(events['ts'],end_time)
x = events['xs'][start_index:end_index]
y = events['ys'][start_index:end_index]
p = events['ps'][start_index:end_index].astype(int)
p[p == 0] = -1
t = events['ts'][start_index:end_index]
elif voxel_type == 'gopro_7' :
start_index = binary_search_h5_dset(events[3],start_time)
end_index = binary_search_h5_dset(events[3],end_time)
x = events[0][start_index:end_index].astype(np.int16)
y = events[1][start_index:end_index].astype(np.int16)
p = events[2][start_index:end_index].astype(int)
t = events[3][start_index:end_index]
elif voxel_type == 'EDI':
start_index = binary_search_h5_dset(events[3],start_time)
end_index = binary_search_h5_dset(events[3],end_time)
x = events[0][start_index:end_index].astype(np.int16)
y = events[1][start_index:end_index].astype(np.int16)
p = events[2][start_index:end_index].astype(int)
t = events[3][start_index:end_index]
p[p==0] = -1
return x, y, p, t
def inv_normalize_time(t,start_time,end_time):
inv_t = start_time + t * (end_time - start_time)
return np.clip(inv_t,start_time,end_time)
def event2tensor_key(events, img_size, start_time, end_time, num_bin, keypoints):
"""
event streams to [num_bin, C, H, W] event tensor, C=2 indicates polarity
split T into N parts.Each part has the same time length.
Return:
E -> tensor [num_bin, C, H, W]
events_bedding -> array [num_bedding] stores the events number information
keypoints: shape:[num_bin] normalized to [0,...,1]
"""
keypoints = inv_normalize_time(keypoints,start_time,end_time)
E = np.zeros((num_bin, 2, img_size[0], img_size[1]))
x, y, p, t = filter_events_time(events, start_time, end_time)
events_num = len(t)
events_bedding = np.zeros(num_bin)
idx = np.zeros_like(t).astype(np.int8)
for i in range(len(keypoints)-1):
start_index = binary_search_h5_dset(t,keypoints[i])
end_index = binary_search_h5_dset(t,keypoints[i+1])
idx[start_index:end_index] = i
index = (t == keypoints[i+1])
idx[index] = i
# idx范围为[0,num_bin],因此需要限制最大值的idx减1
# # p[(T0_p,T0_n),(T1_p,T1_n),...,(T15_p,T15_n)] 总共32长
p[p == 1] = 0 # 正极性在第一个通道上
p[p == -1] = 1 # 负极性在第二个通道上
np.add.at(E, (idx, p, y, x), 1)
events_bedding[:] = np.sum(E, axis=(1, 2, 3))
return E, events_bedding
def event2tensor_time(events, img_size, start_time, end_time, num_bin,voxel_type ='gopro_7'):
"""
event streams to [num_bin, C, H, W] event tensor, C=2 indicates polarity
split T into N parts.Each part has the same time length.
Return:
E -> tensor [num_bin, C, H, W]
events_bedding -> array [num_bedding] stores the events number information
"""
interval_time = (end_time - start_time) / num_bin
E = np.zeros((num_bin, 2, img_size[0], img_size[1]))
x, y, p, t = filter_events_time(events, start_time, end_time,voxel_type)
events_num = len(t)
events_bedding = np.zeros(num_bin)
new_t = t - start_time
idx = np.floor(new_t/interval_time).astype(np.int8)
idx[idx == num_bin] -= 1 # idx范围为[0,num_bin],因此需要限制最大值的idx减1
# p[(T0_p,T0_n),(T1_p,T1_n),...,(T15_p,T15_n)] 总共32长
p[p == 1] = 0 # 正极性在第一个通道上
p[p == -1] = 1 # 负极性在第二个通道上
np.add.at(E, (idx, p, y, x), 1)
events_bedding[:] = np.sum(E, axis=(1, 2, 3))
return E, events_bedding
def event2tensor_space(events, img_size, start_time, end_time, num_bin,voxel_type ='gopro_7'):
"""
datasource: event data from start_time to end_time
event streams to [T, C, H, W] event tensor, C=2 indicates polarity
split events' number into N parts.Each part has the same events number.
Return:
E -> tensor [num_bin, C, H, W]
time_bedding -> array [num_bedding] : stores the time information in the middle of events stream
"""
E = np.zeros((num_bin, 2, img_size[0], img_size[1]))
x, y, p, t = filter_events_time(events, start_time, end_time,voxel_type)
events_num = len(t)
interval_space = events_num//num_bin
time_bedding = np.zeros(num_bin)
for idx in range(num_bin):
if idx == num_bin - 1:
x_ = x[idx*interval_space:]
y_ = y[idx*interval_space:]
p_ = p[idx*interval_space:]
t_ = t[idx*interval_space:]
else:
x_ = x[idx*interval_space:(idx+1)*interval_space]
y_ = y[idx*interval_space:(idx+1)*interval_space]
p_ = p[idx*interval_space:(idx+1)*interval_space]
t_ = t[idx*interval_space:(idx+1)*interval_space]
p_[p_ == 1] = 0 # 正极性在第一个通道上
p_[p_ == -1] = 1 # 负极性在第二个通道上
np.add.at(E, (idx, p_, y_, x_), 1)
time_bedding[idx] = t_[-1]
return E, time_bedding
def normalize_image(image, percentile_lower=0, percentile_upper=100):
"""
normalize image to [0,1]
"""
mini, maxi = np.percentile(
image, (percentile_lower, percentile_upper)) # mini,maxi = 1 if all pixes are the same
if mini == maxi:
return 0 * image + 0.5 # gray image
return np.clip((image - mini) / (maxi - mini + 1e-5), 0, 1)
def animate(images, fig_title=' ', fps=30):
fig = plt.figure(figsize=(0.1, 0.1)) # don't take up room initially
fig.suptitle(fig_title)
fig.set_size_inches(7.2, 5.4, forward=False) # resize but don't update gui
ims = []
for image in tqdm(images):
im = plt.imshow(normalize_image(image), cmap='gray',
vmin=0, vmax=1, animated=True)
ims.append([im])
ani = ArtistAnimation(fig, ims, interval=50, blit=False, repeat_delay=1000)
ani.save(f'result/{fig_title}.gif', fps=fps)
plt.close(ani._fig)
def set_random_seed(seed):
"""Set random seeds."""
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.cuda.manual_seed(seed)
def concatenate_h5_datasets(dataset, path,select_path = None, **args):
"""concat all h5 file under path
Args:
dataset (class): dataset class eg: REBlur
path (str): path that contains the h5 file
"""
file_folder_path = path
h5_file_path = [os.path.join(file_folder_path, s)
for s in os.listdir(file_folder_path)]
print('Found {} h5 files in {}'.format(
len(h5_file_path), file_folder_path))
datasets = []
for h5_file in h5_file_path:
print(h5_file)
if select_path == None:
datasets.append(dataset(h5_file, **args))
else:
if h5_file == select_path:
datasets.append(dataset(h5_file, **args))
return ConcatDataset(datasets)
def save_plot(title, x_data, y_data):
plt.clf()
plt.title(title)
plt.grid(linestyle=":")
plt.plot(x_data, y_data)
plt.subplots_adjust(wspace=0.2, hspace=0.4)
plt.tight_layout()
plt.savefig('Result/'+title+'.png')
def save_image(img,title = 'test',nor = False,rgb = False):
# nor or save the result directly
if nor:
img = cv2.normalize(img, None, 255, 0, cv2.NORM_MINMAX, cv2.CV_8UC1)
else:
img = img.clip(0,1)
img = np.array(img * 255).astype(np.uint8)
# rgb save or not
if rgb == True:
img = np.transpose(img, (1, 2, 0))
elif img.shape[0] == 1:
img = img[0]
cv2.imwrite('Result/' + title + '.png', img)
def save_res_image(img,title = 'test',nor = False):
if nor:
img = cv2.normalize(img, None, 255, 0, cv2.NORM_MINMAX, cv2.CV_8UC1)
img = np.transpose(img, (1, 2, 0))
else:
img = cv2.normalize(img, None, 255, 0, cv2.NORM_MINMAX, cv2.CV_8UC1)
cv2.imwrite('Result/' + title + '.png', img)
def save_event_image(voxel, title,mode='red-blue'):
if mode == 'red-blue':
# Red-blue mode
# positive events: blue, negative events: red
event_preview = np.zeros((voxel.shape[0], voxel.shape[1], 3), dtype=np.uint8)
b = event_preview[:, :, 0]
r = event_preview[:, :, 2]
b[voxel > 0] = 255
r[voxel < 0] = 255
else:
# Grayscale mode
# normalize event image to [0, 255] for display
m, M = voxel.min(), voxel.max()
event_preview = np.clip((255.0 * (voxel - m) / (M - m)).astype(np.uint8), 0, 255)
cv2.imwrite('Result/' + title + '.png', event_preview)
def show_image(img, title='test', wait=True):
image = cv2.normalize(img, None, 255, 0, cv2.NORM_MINMAX, cv2.CV_8UC1)
cv2.imshow(title, image)
if wait:
cv2.waitKey(0)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.count += n
self.sum += val * n
self.avg = self.sum / self.count
def save_event_image(voxel, title,mode='red-blue'):
if mode == 'red-blue':
# Red-blue mode
# positive events: blue, negative events: red
event_preview = np.zeros((voxel.shape[0], voxel.shape[1], 3), dtype=np.uint8)
b = event_preview[:, :, 0]
g = event_preview[:, :, 1]
r = event_preview[:, :, 2]
event_preview.fill(255)
b[voxel > 0] = 255
g[voxel > 0] = 0
r[voxel > 0] = 0
b[voxel < 0] = 0
r[voxel < 0] = 255
g[voxel < 0] = 0
else:
# Grayscale mode
# normalize event image to [0, 255] for display
m, M = voxel.min(), voxel.max()
event_preview = np.clip((255.0 * (voxel - m) / (M - m)).astype(np.uint8), 0, 255)
cv2.imwrite('Result/' + title + '.png', event_preview)