-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss.py
executable file
·197 lines (157 loc) · 7.84 KB
/
loss.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
#!/usr/bin/env python3
import torch
import torch.nn as nn
import torch.nn.functional as F
class SmoothLoss(nn.Module):
def __init__(self, device):
super(SmoothLoss, self).__init__()
gradx = torch.FloatTensor([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]]).to(device)
grady = torch.FloatTensor([[-1, -2, -1],
[0, 0, 2],
[1, 0, 1]]).to(device)
self.disp_gradx_ry = gradx.unsqueeze(0).unsqueeze(0)
self.disp_grady_ry = grady.unsqueeze(0).unsqueeze(0)
self.disp_gradx = self.disp_gradx_ry.repeat(1, 128, 1, 1) # NOTE: batch_size is hard coded
self.disp_grady = self.disp_grady_ry.repeat(1, 128, 1, 1) # batch_size is hard coded
# print(self.disp_gradx.shape, self.disp_grady.shape)
self.img_gradx = self.disp_gradx_ry.repeat(128, 1, 1, 1) # NOTE: batch_size is hard coded
self.img_grady = self.disp_grady_ry.repeat(128, 1, 1, 1) # batch_size is hard coded
# print(self.img_gradx.shape, self.img_grady.shape)
# self.min_depth = 15.
# self.max_depth = 75.
def get_smooth_loss(self, disp, img):
"""Computes the smoothness loss for a disparity image
The color image is used for edge-aware smoothness
"""
grad_disp_x = torch.abs(F.conv2d(disp, self.disp_gradx, padding=1, stride=1))
grad_disp_y = torch.abs(F.conv2d(disp, self.disp_grady, padding=1, stride=1))
grad_img_x = torch.abs(torch.mean(F.conv2d(img, self.img_gradx, padding=1, stride=1), dim=1, keepdim=True))
grad_img_y = torch.abs(torch.mean(F.conv2d(img, self.img_grady, padding=1, stride=1), dim=1, keepdim=True))
grad_disp_x *= torch.exp(-grad_img_x)
grad_disp_y *= torch.exp(-grad_img_y)
loss_x = 10 * (torch.sqrt(torch.var(grad_disp_x) + 0.15 * torch.pow(torch.mean(grad_disp_x), 2)))
loss_y = 10 * (torch.sqrt(torch.var(grad_disp_y) + 0.15 * torch.pow(torch.mean(grad_disp_y), 2)))
return loss_x + loss_y
def forward(self, rgb, disp):
N, C, H, W = rgb.shape
# disp = self.compute_disp(depth)
loss = self.get_smooth_loss(disp, rgb)
return loss
class pyramid_SIDL(nn.Module):
def __init__(self):
super(pyramid_SIDL, self).__init__()
self.n = 4 # number of pyramid levels
def build_pyramid(self, img, n):
if len(img.shape) == 3:
img = img.unsqueeze(1)
pyramid = [img]
h = img.shape[2]
w = img.shape[3]
for i in range(n - 1):
ratio = 2 ** (i + 1)
nh = h // ratio
nw = w // ratio
pyramid.append(F.interpolate(pyramid[i], (nh, nw), mode='bilinear', align_corners=True))
# print(pyramid[i].shape)
return pyramid
def x_grad(self, img):
if len(img.shape) == 2:
img = img.unsqueeze(0).unsqueeze(0)
img = F.pad(img, (1, 0, 0, 0), mode='replicate')
grad_x = img[:, :, :, :-1] - img[:, :, :, 1:]
return grad_x
def y_grad(self, img):
if len(img.shape) == 2:
img = img.unsqueeze(0).unsqueeze(0)
img = F.pad(img, (0, 0, 0, 1), mode='replicate')
grad_y = img[:, :, :-1, :] - img[:, :, 1:, :]
return grad_y
def disp_smoothness(self, disp, pyramid):
disp_x_grad = [self.x_grad(i) for i in disp]
disp_y_grad = [self.y_grad(j) for j in disp]
image_x_grad = [self.x_grad(i) for i in pyramid]
image_y_grad = [self.y_grad(j) for j in pyramid]
#e^(-|x|) weights, gradient negatively exponential to weights
#average over all pixels in C dimension
weights_x = [torch.exp(-torch.mean(torch.abs(i), 1, keepdim=True)) for i in image_x_grad]
weights_y = [torch.exp(-torch.mean(torch.abs(j), 1, keepdim=True)) for j in image_y_grad]
smoothness_x = []
for i in range(self.n):
# print(disp_x_grad[i].shape, weights_x[i].shape)
element_wise = disp_x_grad[i] * weights_x[i]
smoothness_x.append(element_wise)
smoothness_y = [disp_y_grad[j] * weights_y[j] for j in range(self.n)]
smoothness = [torch.mean(torch.abs(smoothness_x[i]) + torch.abs(smoothness_y[i])) / 2 ** i for i in range(self.n)]
return smoothness
def forward(self, rgb, disp):
N, C, H, W = rgb.shape
pyramid = self.build_pyramid(rgb, self.n)
disp_pyramid = self.build_pyramid(disp, self.n)
loss = self.disp_smoothness(disp_pyramid, pyramid)
return sum(loss)
# sidl = pyramid_SIDL().to('cuda')
# sidl_loss = sidl(torch.rand(64, 3, 224, 224), torch.rand(64, 224, 224))
# print(sidl_loss)
# class Hessian2DNorm():
# def __init__(self):
# pass
# def __call__(self, img):
# # Compute Individual derivatives
# fxx = img[..., 1:-1, :-2] + img[..., 1:-1, 2:] - 2*img[..., 1:-1, 1:-1]
# fyy = img[..., :-2, 1:-1] + img[..., 2:, 1:-1] - 2*img[..., 1:-1, 1:-1]
# fxy = img[..., :-1, :-1] + img[..., 1:, 1:] - \
# img[..., 1:, :-1] - img[..., :-1, 1:]
# return torch.sqrt(fxx.abs().pow(2) +\
# 2*fxy[..., :-1, :-1].abs().pow(2) +\
# fyy.abs().pow(2)).sum()
# class Hessian3DNorm():
# def __init__(self):
# pass
# def __call__(self, img):
# # Compute Individual derivatives
# fxx = img[...,1:-1, 1:-1, :-2] + img[...,1:-1, 1:-1, 2:] - 2*img[...,1:-1, 1:-1, 1:-1]
# fyy = img[...,1:-1, :-2, 1:-1] + img[...,1:-1, 2:, 1:-1] - 2*img[...,1:-1, 1:-1, 1:-1]
# fxy = img[...,1:-1, :-1, :-1] + img[...,1:-1, 1:, 1:] - \
# img[...,1:-1, 1:, :-1] - img[...,1:-1, :-1, 1:]
# fzz = img[...,:-2, 1:-1, 1:-1] + img[...,2:, 1:-1, 1:-1] - 2*img[...,1:-1, 1:-1, 1:-1]
# fxz = img[...,:-1, 1:-1, :-1] + img[...,1:, 1:-1, 1:] - \
# img[...,1:, 1:-1, :-1] - img[...,:-1, 1:-1, 1:]
# fyz = img[...,:-1, :-1, 1:-1] + img[...,1:, 1:, 1:-1] - \
# img[...,1:, :-1, 1:-1] - img[...,:-1, 1:, 1:-1]
# return torch.sqrt(fxx.abs().pow(2) +\
# 2*fxy[..., :-1, :-1].abs().pow(2) +\
# fyy.abs().pow(2) + fzz.abs().pow(2) +\
# 2*fxz[...,:-1, :, :-1].abs().pow(2) + 2*fyz[...,:-1,:-1,:].abs().pow(2) ).sum()
# class TV2DNorm():
# def __init__(self, mode='l1'):
# self.mode = mode
# def __call__(self, img):
# grad_x = img[..., 1:, 1:] - img[..., 1:, :-1]
# grad_y = img[..., 1:, 1:] - img[..., :-1, 1:]
# if self.mode == 'isotropic':
# #return torch.sqrt(grad_x.abs().pow(2) + grad_y.abs().pow(2)).mean()
# return torch.sqrt(grad_x**2 + grad_y**2).sum()
# elif self.mode == 'l1':
# return abs(grad_x).sum() + abs(grad_y).sum()
# elif self.mode == 'hessian':
# return Hessian2DNorm()(img)
# else:
# return (grad_x.pow(2) + grad_y.pow(2)).sum()
# class TV3DNorm():
# def __init__(self, mode='l1'):
# self.mode = mode
# def __call__(self, img):
# grad_x = img[...,1:, 1:, 1:] - img[...,1:, 1:, :-1]
# grad_y = img[...,1:, 1:, 1:] - img[...,1:, :-1, 1:]
# grad_z = img[...,1:, 1:, 1:] - img[...,:-1, 1:, 1:]
# if self.mode == 'isotropic':
# #return torch.sqrt(grad_x.abs().pow(2) + grad_y.abs().pow(2)).mean()
# return torch.sqrt(grad_x**2 + grad_y**2 + grad_z**2).sum()
# elif self.mode == 'l1':
# return abs(grad_x).sum() + abs(grad_y).sum() + abs(grad_z).sum()
# elif self.mode == 'hessian':
# return Hessian3DNorm()(img)
# else:
# return (grad_x.pow(2) + grad_y.pow(2) + grad_z.pow(2)).sum()