-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent_loss_helpers.py
51 lines (40 loc) · 1.9 KB
/
event_loss_helpers.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
import torch
import math
import random
def lin_log(x, threshold=20):
"""
linear mapping + logarithmic mapping.
:param x: float or ndarray the input linear value in range 0-255
:param threshold: float threshold 0-255 the threshold for transisition from linear to log mapping
"""
# converting x into np.float32.
if x.dtype is not torch.float64:
x = x.double()
f = (1./threshold) * math.log(threshold)
y = torch.where(x <= threshold, x*f, torch.log(x))
return y.float()
def event_loss_call(all_rgb, event_data, select_coords, combination, rgb2gray, resolution_h, resolution_w):
'''
simulate the generation of event stream and calculate the event loss
'''
if rgb2gray == "rgb":
rgb2grey = torch.tensor([0.299,0.587,0.114])
elif rgb2gray == "ave":
rgb2grey = torch.tensor([1/3, 1/3, 1/3])
loss = []
chose = random.sample(combination, 10)
for its in range(10):
start = chose[its][0]
end = chose[its][1]
thres_pos = (lin_log(torch.mv(all_rgb[end], rgb2grey) * 255) - lin_log(torch.mv(all_rgb[start], rgb2grey) * 255)) / 0.3
thres_neg = (lin_log(torch.mv(all_rgb[end], rgb2grey) * 255) - lin_log(torch.mv(all_rgb[start], rgb2grey) * 255)) / 0.2
event_cur = event_data[start].view(resolution_h, resolution_w)[select_coords[:, 0], select_coords[:, 1]]
for j in range(start + 1, end):
event_cur += event_data[j].view(resolution_h, resolution_w)[select_coords[:, 0], select_coords[:, 1]]
pos = event_cur > 0
neg = event_cur < 0
loss_pos = torch.mean(((thres_pos * pos) - ((event_cur + 0.5) * pos)) ** 2)
loss_neg = torch.mean(((thres_neg * neg) - ((event_cur - 0.5) * neg)) ** 2)
loss.append(loss_pos + loss_neg)
event_loss = torch.mean(torch.stack(loss, dim=0), dim=0)
return event_loss