-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss.py
212 lines (165 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class SigmaLoss:
def __init__(self, N_samples, perturb, raw_noise_std):
super(SigmaLoss, self).__init__()
self.N_samples = N_samples
self.perturb = perturb
self.raw_noise_std = raw_noise_std
def calculate_loss(self, rays_o, rays_d, viewdirs, near, far, depths, run_func, network):
# print(near.mean(), depths[0], far.mean())
# assert near.mean() <= depths[0] and depths[0] <= far.mean()
N_rays = rays_o.shape[0]
t_vals = torch.linspace(0., 1., steps=self.N_samples).to(device)
t_vals = t_vals.expand([N_rays, self.N_samples])
z_vals = near * (1.-t_vals) + depths[:,None] * (t_vals)
if self.perturb > 0.:
# get intervals between samples
mids = .5 * (z_vals[...,1:] + z_vals[...,:-1])
upper = torch.cat([mids, z_vals[...,-1:]], -1)
lower = torch.cat([z_vals[...,:1], mids], -1)
# stratified samples in those intervals
t_rand = torch.rand(z_vals.shape).to(device)
z_vals = lower + (upper - lower) * t_rand
pts = rays_o[...,None,:] + rays_d[...,None,:] * z_vals[...,:,None] # [N_rays, N_samples, 3]
raw = run_func(pts, viewdirs, network)
noise = 0.
if self.raw_noise_std > 0.:
noise = torch.randn(raw[...,3].shape) * self.raw_noise_std
sigma = F.relu(raw[...,3] + noise)
# sigma_sigmoid = torch.sigmoid(sigma) # [N_rays, N_samples]
# assert sigma_sigmoid.shape[0] == N_rays and sigma_sigmoid.shape[1] == self.N_samples
# # sigma_sigmoid = torch.mean(sigma_sigmoid, axis=0)
# loss = torch.sum(sigma_sigmoid[:,:-1], axis=1) - sigma_sigmoid[:,-1]
loss = -torch.exp(sigma[:,-1]) / (torch.sum(torch.exp(sigma), axis=1) + 1)
return loss
import torch
import torch.nn as nn
import torch.nn.functional as F
# Based on
# https://github.com/tensorflow/models/blob/master/research/struct2depth/model.py#L625-L641
class InverseDepthSmoothnessLoss(nn.Module):
r"""Criterion that computes image-aware inverse depth smoothness loss.
.. math::
\text{loss} = \left | \partial_x d_{ij} \right | e^{-\left \|
\partial_x I_{ij} \right \|} + \left |
\partial_y d_{ij} \right | e^{-\left \| \partial_y I_{ij} \right \|}
Shape:
- Inverse Depth: :math:`(N, 1, H, W)`
- Image: :math:`(N, 3, H, W)`
- Output: scalar
Examples::
>>> idepth = torch.rand(1, 1, 4, 5)
>>> image = torch.rand(1, 3, 4, 5)
>>> smooth = InverseDepthSmoothnessLoss()
>>> loss = smooth(idepth, image)
"""
def __init__(self) -> None:
super(InverseDepthSmoothnessLoss, self).__init__()
@staticmethod
def gradient_x(img: torch.Tensor) -> torch.Tensor:
assert len(img.shape) == 4, img.shape
return img[:, :, :, :-1] - img[:, :, :, 1:]
@staticmethod
def gradient_y(img: torch.Tensor) -> torch.Tensor:
assert len(img.shape) == 4, img.shape
return img[:, :, :-1, :] - img[:, :, 1:, :]
def forward(
self,
idepth: torch.Tensor,
image: torch.Tensor) -> torch.Tensor:
if not torch.is_tensor(idepth):
raise TypeError("Input idepth type is not a torch.Tensor. Got {}"
.format(type(idepth)))
if not torch.is_tensor(image):
raise TypeError("Input image type is not a torch.Tensor. Got {}"
.format(type(image)))
if not len(idepth.shape) == 4:
raise ValueError("Invalid idepth shape, we expect BxCxHxW. Got: {}"
.format(idepth.shape))
if not len(image.shape) == 4:
raise ValueError("Invalid image shape, we expect BxCxHxW. Got: {}"
.format(image.shape))
if not idepth.shape[-2:] == image.shape[-2:]:
raise ValueError("idepth and image shapes must be the same. Got: {}"
.format(idepth.shape, image.shape))
if not idepth.device == image.device:
raise ValueError(
"idepth and image must be in the same device. Got: {}" .format(
idepth.device, image.device))
if not idepth.dtype == image.dtype:
raise ValueError(
"idepth and image must be in the same dtype. Got: {}" .format(
idepth.dtype, image.dtype))
# compute the gradients
idepth_dx: torch.Tensor = self.gradient_x(idepth)
idepth_dy: torch.Tensor = self.gradient_y(idepth)
image_dx: torch.Tensor = self.gradient_x(image)
image_dy: torch.Tensor = self.gradient_y(image)
# compute image weights
weights_x: torch.Tensor = torch.exp(
-torch.mean(torch.abs(image_dx), dim=1, keepdim=True))
weights_y: torch.Tensor = torch.exp(
-torch.mean(torch.abs(image_dy), dim=1, keepdim=True))
# apply image weights to depth
smoothness_x: torch.Tensor = torch.abs(idepth_dx * weights_x)
smoothness_y: torch.Tensor = torch.abs(idepth_dy * weights_y)
return torch.mean(smoothness_x) + torch.mean(smoothness_y)
import torch
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
from math import exp
def gaussian(window_size, sigma):
gauss = torch.Tensor([exp(-(x - window_size // 2) ** 2 / float(2 * sigma ** 2)) for x in range(window_size)])
return gauss / gauss.sum()
def create_window(window_size, channel):
_1D_window = gaussian(window_size, 1.5).unsqueeze(1)
_2D_window = _1D_window.mm(_1D_window.t()).float().unsqueeze(0).unsqueeze(0)
window = Variable(_2D_window.expand(channel, 1, window_size, window_size).contiguous())
return window
def _ssim(img1, img2, window, window_size, channel, size_average=True):
mu1 = F.conv2d(img1, window, padding=window_size // 2, groups=channel)
mu2 = F.conv2d(img2, window, padding=window_size // 2, groups=channel)
mu1_sq = mu1.pow(2)
mu2_sq = mu2.pow(2)
mu1_mu2 = mu1 * mu2
sigma1_sq = F.conv2d(img1 * img1, window, padding=window_size // 2, groups=channel) - mu1_sq
sigma2_sq = F.conv2d(img2 * img2, window, padding=window_size // 2, groups=channel) - mu2_sq
sigma12 = F.conv2d(img1 * img2, window, padding=window_size // 2, groups=channel) - mu1_mu2
C1 = 0.01 ** 2
C2 = 0.03 ** 2
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2))
if size_average:
return ssim_map.mean()
else:
return ssim_map.mean(1).mean(1).mean(1)
class SSIM(torch.nn.Module):
def __init__(self, window_size=11, size_average=True):
super(SSIM, self).__init__()
self.window_size = window_size
self.size_average = size_average
self.channel = 1
self.window = create_window(window_size, self.channel)
def forward(self, img1, img2):
(_, channel, _, _) = img1.size()
if channel == self.channel and self.window.data.type() == img1.data.type():
window = self.window
else:
window = create_window(self.window_size, channel)
if img1.is_cuda:
window = window.cuda(img1.get_device())
window = window.type_as(img1)
self.window = window
self.channel = channel
return _ssim(img1, img2, window, self.window_size, channel, self.size_average)
def ssim(img1, img2, window_size=11, size_average=True):
(_, channel, _, _) = img1.size()
window = create_window(window_size, channel)
if img1.is_cuda:
window = window.cuda(img1.get_device())
window = window.type_as(img1)
return _ssim(img1, img2, window, window_size, channel, size_average)