-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
231 lines (189 loc) · 7.02 KB
/
losses.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
228
229
230
231
import theano.tensor as T
import numpy as np
def berhu(predictions, targets,s=0.2,l=0.5,m=1.2):
# Compute mask
mask = T.gt(targets, l) * T.lt(targets,m)
# Compute n of valid pixels
n_valid = T.sum(mask)
# Redundant mult here
r = (predictions - targets) * mask
c = s * T.max(T.abs_(r))
a_r = T.abs_(r)
b = T.switch(T.lt(a_r, c), a_r, ((r**2) + (c**2))/(2*c))
return T.sum(b)/n_valid
def berhu_spatial_low_thresh(predictions, targets, s=0.2, l=0., m=1.2, gw=0.5):
# Compute mask
mask = T.gt(targets, l) * T.lt(targets,m)
# Compute n of valid pixels
n_valid = T.sum(mask)
r = (predictions - targets) * mask
c = s * T.max(T.abs_(r))
a_r = T.abs_(r)
b = T.switch(T.lt(a_r, c), a_r, ((r**2) + (c**2))/(2*c))
pixel_cost = T.sum(b)/n_valid
# Gradient cost
h = 1
pred = predictions
target = targets
if pred.ndim == 4:
pred = pred[:,0,:,:]
if target.ndim == 4:
target = target[:,0,:,:]
# Recompute mask
mask = T.gt(target, l) * T.lt(target,m)
p_di = (pred[:,h:,:] - pred[:,:-h,:]) * (1 / np.float32(h))
p_dj = (pred[:,:,h:] - pred[:,:,:-h]) * (1 / np.float32(h))
t_di = (target[:,h:,:] - target[:,:-h,:]) * (1 / np.float32(h))
t_dj = (target[:,:,h:] - target[:,:,:-h]) * (1 / np.float32(h))
m_di = T.and_(mask[:,h:,:], mask[:,:-h,:])
m_dj = T.and_(mask[:,:,h:], mask[:,:,:-h])
# Define spatial grad cost
grad_cost = T.sum(m_di * T.abs_(p_di - t_di)) / T.sum(m_di) + T.sum(m_dj * T.abs_(p_dj - t_dj)) / T.sum(m_dj)
return gw * grad_cost + pixel_cost
def berhu_spatial(predictions, targets, s=0.2, l=0., m=10., gw=0.5):
# Compute mask
mask = T.gt(targets, l) * T.lt(targets,m)
# Compute n of valid pixels
n_valid = T.sum(mask)
r = (predictions - targets) * mask
c = s * T.max(T.abs_(r))
a_r = T.abs_(r)
b = T.switch(T.lt(a_r, c), a_r, ((r**2) + (c**2))/(2*c))
pixel_cost = T.sum(b)/n_valid
# Gradient cost
h = 1
pred = predictions
target = targets
if pred.ndim == 4:
pred = pred[:,0,:,:]
if target.ndim == 4:
target = target[:,0,:,:]
# Recompute mask
mask = T.gt(target, l) * T.lt(target,m)
p_di = (pred[:,h:,:] - pred[:,:-h,:]) * (1 / np.float32(h))
p_dj = (pred[:,:,h:] - pred[:,:,:-h]) * (1 / np.float32(h))
t_di = (target[:,h:,:] - target[:,:-h,:]) * (1 / np.float32(h))
t_dj = (target[:,:,h:] - target[:,:,:-h]) * (1 / np.float32(h))
m_di = T.and_(mask[:,h:,:], mask[:,:-h,:])
m_dj = T.and_(mask[:,:,h:], mask[:,:,:-h])
# Define spatial grad cost
grad_cost = T.sum(m_di * T.abs_(p_di - t_di)) / T.sum(m_di) + T.sum(m_dj * T.abs_(p_dj - t_dj)) / T.sum(m_dj)
return gw * grad_cost + pixel_cost
def mse(predictions, targets):
mask = T.gt(targets, 0.)
mp = mask * predictions
mt = mask * targets
# Compute n of valid pixels
n_valid = T.sum(mask)
# Apply mask
d = (mp - mt)
return T.sum((d)**2) / n_valid
def tukey_biweight(predictions, targets, c=4.685, s=1.4826):
"""
Tukey's biweight function expressed in theano as in
:param predictions: Prediction tensor
:param targets: Target tensor
:param c: Tukey tuning constant
:param s: Consistence scale parameter
:return: Cost function
"""
# Flatten input to make calc easier
pred = predictions.flatten(2)
target = targets.flatten(2)
# Compute mask
mask = T.gt(target, 0)
# Compute n of valid pixels
n_valid = T.sum(mask, axis=1)
# Apply mask and log transform
m_pred = pred * mask
m_t = T.switch(mask, target, 0)
def median(tensor):
"""
MAD tensor from https://groups.google.com/forum/#!topic/theano-users/I4eHjbAetEQ
:param tensor: Input tensor
:return: Median expression
"""
tensor = tensor.flatten(1)
return T.switch(T.eq((tensor.shape[0] % 2), 0),
# if even vector
T.mean(T.sort(tensor)[((tensor.shape[0] / 2) - 1): ((tensor.shape[0] / 2) + 1)]),
# if odd vector
T.sort(tensor)[tensor.shape[0] // 2])
def mad(tensor):
"""
Median absolute deviation
:param tensor: Input tensor
:return: MAD
"""
med = median(tensor=tensor)
return median(T.abs_(tensor - med))
# Residual
r_i = (m_pred - m_t)
# r_i = r_i / (s * mad(r_i))
r_i = r_i / r_i.std()
# Compute the masking vectors
tukey_mask = T.gt(T.abs_(r_i), c)
# Cost
cost = (c ** 2 / 6) * (1-(1 - (r_i / c) ** 2) ** 3)
# Aggregate
return T.sum(T.sum(T.switch(tukey_mask, (c ** 2) / 6., cost), axis=1)) / T.maximum((T.sum(n_valid)), 1)
def spatial_gradient(prediction, target, l=0.1,m=2.):
# Flatten input to make calc easier
pred = prediction
pred_v = pred.flatten(2)
target_v = target.flatten(2)
# Compute mask
mask = T.gt(target_v,0.)
# Compute n of valid pixels
n_valid = T.sum(mask, axis=1)
# Apply mask and log transform
m_pred = pred_v * mask
m_t = T.switch(mask, T.log(target_v),0.)
d = m_pred - m_t
# Define scale invariant cost
scale_invariant_cost = (T.sum(n_valid * T.sum(d**2, axis=1)) - l*T.sum(T.sum(d, axis=1)**2))/ T.maximum(T.sum(n_valid**2), 1)
# Add spatial gradient components from D. Eigen DNL
# Squeeze in case
if pred.ndim == 4:
pred = pred[:,0,:,:]
if target.ndim == 4:
target = target[:,0,:,:]
# Mask in tensor form
mask_tensor = T.gt(target,0.)
# Project into log space
target = T.switch(mask_tensor, T.log(target),0.)
# Stepsize
h = 1
# Compute spatial gradients symbolically
p_di = (pred[:,h:,:] - pred[:,:-h,:]) * (1 / np.float32(h))
p_dj = (pred[:,:,h:] - pred[:,:,:-h]) * (1 / np.float32(h))
t_di = (target[:,h:,:] - target[:,:-h,:]) * (1 / np.float32(h))
t_dj = (target[:,:,h:] - target[:,:,:-h]) * (1 / np.float32(h))
m_di = T.and_(mask_tensor[:,h:,:], mask_tensor[:,:-h,:])
m_dj = T.and_(mask_tensor[:,:,h:], mask_tensor[:,:,:-h])
# Define spatial grad cost
grad_cost = T.sum(m_di * (p_di - t_di)**2) / T.sum(m_di) + T.sum(m_dj * (p_dj - t_dj)**2) / T.sum(m_dj)
# Compute final expression
return scale_invariant_cost + grad_cost
def scale_invariant_error(predictions, targets):
"""
Scale invariant error in log space
:param predictions: Prediction tensor
:param targets: Target tensor
:return: theano expression
"""
_lambda_ = 0.5
# Flatten input to make calc easier
pred = predictions.flatten(2)
target = targets.flatten(2)
# Compute mask
mask = T.gt(target, 0)
# Compute n of valid pixels
n_valid = T.sum(mask, axis=1)
# Apply mask and log transform
m_pred = pred * mask
m_t = T.switch(mask, T.log(target), 0)
d = m_pred - m_t
# Define cost
return (T.sum(n_valid * T.sum(d ** 2, axis=1)) - _lambda_ * T.sum(T.sum(d, axis=1) ** 2)) / T.maximum(
T.sum(n_valid ** 2), 1)