-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathloss.py
More file actions
361 lines (264 loc) · 11.9 KB
/
Copy pathloss.py
File metadata and controls
361 lines (264 loc) · 11.9 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import torch
import torch.nn as nn
from torch import log as thLog
from torch.autograd import Variable
from torch import Tensor, mul, dot, ones
from torch.nn import functional as F
class DoobNetLoss(nn.Module):
# Based on Wang et al.
def __init__(self, beta, gamma, sigma):
super(DoobNetLoss, self).__init__()
self.alpha = Variable(Tensor([0]))
self.beta = beta
self.gamma = gamma
self.sigma = sigma
def forward(self, b_pred, b_gt):
N = b_gt.shape[0]
b_pred = b_pred.view(-1, 1)
b_gt = b_gt.view(-1, 1)
b_gt = b_gt.float()
sm = b_gt.sum()
sz = b_gt.size()[0]
self.alpha = 1.0 - sm.float() / float(sz)
alfa = self.alpha * b_gt + (1.0 - self.alpha) * (1.0 - b_gt)
pt = mul(b_gt, b_pred) + mul(1.0 - b_gt, 1.0 - b_pred)
clamp_val = 1e-7 # to avoid exploding gradients when taking torch.log
pt = pt.clamp(min=clamp_val, max=1.0-clamp_val)
logpt = thLog(pt)
power_pt = (1.0 - pt) ** self.gamma
power_pt = power_pt * self.beta * logpt
loss = -1.0 * alfa * power_pt
loss = loss.sum()
return (1.0 / N) * loss
class SharpNetLoss(nn.Module):
def __init__(self, lamb, mu, use_depth=False, use_normals=False,
use_boundary=False, use_geo_consensus=False):
super(SharpNetLoss, self).__init__()
self.lamb = lamb
self.mu = mu
self.use_normals = use_normals
self.use_depth = use_depth
self.use_boundary_loss = use_boundary
self.use_geo_consensus = use_geo_consensus
self.masked_spatial_gradients_loss = SpatialGradientsLoss(clamp_value=1e-7,
size_average=True,
gradient_loss_on=True,
smooth_error=True)
if self.use_depth:
self.masked_depth_loss = LainaBerHuLoss(use_logs=True)
if self.use_boundary_loss:
self.boundary_loss = DoobNetLoss(beta=4, gamma=0.5, sigma=3)
if self.use_geo_consensus:
self.norm_depth_bound_consensus_loss = NormalDepthConsensusLoss()
self.depth_bound_consensus_loss = DepthBoundaryConsensusLoss()
def forward(self, mask_gt,
d_pred=None, d_gt=None,
n_pred=None, n_gt=None,
b_pred=None, b_gt=None,
val=False, use_grad=False):
d_loss = 0
n_loss = 0
grad_loss = 0
b_loss = 0
geo_loss = 0
if len(mask_gt.shape) != 4:
mask_gt = mask_gt.unsqueeze(1)
mask_gt_valid = mask_gt[:, 0, ...].unsqueeze(1)
if d_pred is not None:
d_gt = d_gt.unsqueeze(1)
d_loss = self.masked_depth_loss(d_pred, d_gt, mask_gt_valid)
if use_grad:
grad_loss = self.masked_spatial_gradients_loss(d_pred, d_gt, mask_gt_valid)
else:
grad_loss = 0
if n_pred is not None:
n_loss = normals_loss(n_pred, n_gt, mask_gt_valid)
if self.use_boundary_loss:
b_loss = self.boundary_loss(b_pred, b_gt)
b_loss = 0.01 * b_loss
if self.use_geo_consensus:
db_loss = 0
ndb_loss = 0
if d_pred is not None and b_pred is not None:
db_loss = self.depth_bound_consensus_loss(d_pred, b_pred)
if n_pred is not None and d_pred is not None and b_pred is not None:
ndb_loss = self.norm_depth_bound_consensus_loss(n_pred, d_pred, b_pred)
geo_loss = db_loss + ndb_loss
return d_loss, grad_loss, n_loss, b_loss, geo_loss
class LainaBerHuLoss(nn.Module):
# Based on Laina et al.
def __init__(self, size_average=True, use_logs=True, clamp_val=1e-9):
super(LainaBerHuLoss, self).__init__()
self.size_average = size_average
self.use_log = use_logs
self.clamp_val = clamp_val
def forward(self, input, target, mask):
if self.use_log:
n = thLog(input.clamp(min=self.clamp_val)) - thLog(target.clamp(min=self.clamp_val))
else:
n = input - target
n = torch.abs(n)
n = mul(n, mask)
n = n.squeeze(1)
c = 0.2 * n.max()
cond = n < c
loss = torch.where(cond, n, (n ** 2 + c ** 2) / (2 * c + 1e-9))
loss = loss.sum()
if self.size_average:
return loss / mask.sum()
return loss
class HuberLoss(nn.Module):
def __init__(self, size_average=True, use_logs=True, sigma=1):
super(HuberLoss, self).__init__()
self.size_average = size_average
self.sigma = sigma
def forward(self, input, target, mask=None):
n = torch.abs(input - target)
if mask is not None:
n = mul(n, mask)
cond = n < 1 / (self.sigma ** 2)
loss = torch.where(cond, 0.5 * (self.sigma * n) ** 2, n - 0.5 / (self.sigma ** 2))
if self.size_average:
if mask is not None:
return loss.sum() / mask.sum()
else:
return loss.mean()
return loss.sum()
def normals_loss(input, target, mask=None):
if input is None or target is None:
return 0
else:
prod = mul(input, target)
if mask is not None:
n = mask.sum().float()
prod = mul(prod, mask)
else:
n = target.numel().float()
prod = 1.0 - (1.0 / n) * prod.sum()
prod = prod.clamp(min=0)
return prod
class SpatialGradientsLoss(nn.Module):
def __init__(self, kernel_size=3, use_logs=True, clamp_value=1e-7, size_average=False,
smooth_error=True,
gradient_loss_on=True):
super(SpatialGradientsLoss, self).__init__()
self.size_average = size_average
self.kernel_size = kernel_size
self.clamp_value = clamp_value
self.use_logs = use_logs
self.smooth_error = smooth_error
self.gradient_loss_on = gradient_loss_on
if gradient_loss_on:
self.masked_huber_loss = HuberLoss(sigma=3)
def forward(self, input, target, mask=None):
repeat_channels = target.shape[1]
sobel_x = torch.Tensor([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
sobel_x = sobel_x.view((1, 1, 3, 3))
sobel_x = torch.autograd.Variable(sobel_x.cuda())
sobel_y = torch.Tensor([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
sobel_y = sobel_y.view((1, 1, 3, 3))
sobel_y = torch.autograd.Variable(sobel_y.cuda())
if repeat_channels != 1:
sobel_x = sobel_x.repeat(1, repeat_channels, 1, 1)
sobel_y = sobel_y.repeat(1, repeat_channels, 1, 1)
smooth_loss = 0
grad_loss = 0
if self.smooth_error:
diff = thLog(input.clamp(min=self.clamp_value)) - thLog(target.clamp(min=self.clamp_value))
gx_diff = F.conv2d(diff, (1.0 / 8.0) * sobel_x, padding=1)
gy_diff = F.conv2d(diff, (1.0 / 8.0) * sobel_y, padding=1)
gradients_diff = torch.pow(gx_diff, 2) + torch.pow(gy_diff, 2)
if mask is None:
smooth_loss = gradients_diff.sum()
if self.size_average:
smooth_loss = smooth_loss * (1.0 / gradients_diff.numel())
else:
gradients_diff = mul(gradients_diff, mask.repeat(1, 3, 1, 1))
smooth_loss = gradients_diff.sum()
if self.size_average:
smooth_loss = smooth_loss * (1.0 / mask.sum())
if self.gradient_loss_on:
input = thLog(input.clamp(min=self.clamp_value))
target = thLog(target.clamp(min=self.clamp_value))
gx_input = F.conv2d(input, (1.0 / 8.0) * sobel_x, padding=1)
gy_input = F.conv2d(input, (1.0 / 8.0) * sobel_y, padding=1)
gx_target = F.conv2d(target, (1.0 / 8.0) * sobel_x, padding=1)
gy_target = F.conv2d(target, (1.0 / 8.0) * sobel_y, padding=1)
gradients_input = torch.pow(gx_input, 2) + torch.pow(gy_input, 2)
gradients_target = torch.pow(gx_target, 2) + torch.pow(gy_target, 2)
grad_loss = self.masked_huber_loss(gradients_input, gradients_target, mask)
return smooth_loss + grad_loss
class DepthBoundaryConsensusLoss(nn.Module):
def __init__(self, kernel_size=3, use_logs=True, clamp_value=1e-7, size_average=False):
super(DepthBoundaryConsensusLoss, self).__init__()
self.size_average = size_average
self.kernel_size = kernel_size
self.clamp_value = clamp_value
def forward(self, depth, boundary, mask=None):
repeat_channels = depth.shape[1]
sobel_x = torch.Tensor([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
sobel_x = sobel_x.view((1, 1, 3, 3))
sobel_x = torch.autograd.Variable(sobel_x.cuda())
sobel_y = torch.Tensor([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
sobel_y = sobel_y.view((1, 1, 3, 3))
sobel_y = torch.autograd.Variable(sobel_y.cuda())
lap = torch.Tensor([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
lap = lap.view((1, 1, 3, 3))
lap = torch.autograd.Variable(lap.cuda())
if repeat_channels != 1:
sobel_x = sobel_x.repeat(1, repeat_channels, 1, 1)
sobel_y = sobel_y.repeat(1, repeat_channels, 1, 1)
lap = lap.repeat(1, repeat_channels, 1, 1)
lap_depth = F.conv2d(depth, (1 / 8.0) * lap, padding=1)
gx = F.conv2d(depth, (1.0 / 8.0) * sobel_x, padding=1)
gy = F.conv2d(depth, (1.0 / 8.0) * sobel_y, padding=1)
g_depth = torch.pow(gx, 2) + torch.pow(gy, 2)
boundary = boundary.clamp(min=self.clamp_value, max=1 - self.clamp_value)
loss = torch.abs(mul(mul(g_depth, thLog(boundary)), lap_depth))
loss = loss + 0.0001 * torch.abs(mul(thLog(1 - boundary), torch.exp(-lap_depth)))
loss = loss + 0.0001 * torch.abs(boundary)
if mask is None:
return loss.sum() / (float(depth.numel()))
else:
loss = mul(loss, mask)
return loss.sum() / float(mask.sum())
class NormalDepthConsensusLoss(nn.Module):
def __init__(self, kernel_size=3, clamp_value=1e-7, size_average=False):
super(NormalDepthConsensusLoss, self).__init__()
self.size_average = size_average
self.kernel_size = kernel_size
self.clamp_value = clamp_value
def forward(self, normals, depth, boundary):
repeat_channels = depth.shape[1]
sobel_x = torch.Tensor([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
sobel_x = sobel_x.view((1, 1, 3, 3))
sobel_x = torch.autograd.Variable(sobel_x.cuda())
sobel_y = torch.Tensor([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
sobel_y = sobel_y.view((1, 1, 3, 3))
sobel_y = torch.autograd.Variable(sobel_y.cuda())
if repeat_channels != 1:
sobel_x = sobel_x.repeat(1, repeat_channels, 1, 1)
sobel_y = sobel_y.repeat(1, repeat_channels, 1, 1)
gx_depth = F.conv2d(depth, (1.0 / 8.0) * sobel_x, padding=1)
gy_depth = F.conv2d(depth, (1.0 / 8.0) * sobel_y, padding=1)
g_depth = torch.cat((gx_depth, gy_depth), 1)
g_depth = F.normalize(g_depth, p=2, dim=1)
normal2d = normals[:, :2, ...]
normal2d = F.normalize(normal2d, p=2, dim=1)
prod = mul(g_depth, normal2d)
prod = prod.sum(1).unsqueeze(1)
prod = (1.0 - prod).clamp(min=0)
prod = torch.abs(mul(prod, (-1.0) * thLog(boundary.clamp(min=self.clamp_value))))
return prod.mean()