-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlosses.py
331 lines (262 loc) · 10.5 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
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
import torch
from kornia import rgb_to_hls
import math
__all__ = ["GlobalRelativeSimilarityLoss", "FeatureMSE", "RelativeMSE", "SMAPE", "TonemappedMSE", "TonemappedRelativeMSE"]
class FeatureMSE(torch.nn.Module):
"""Feature Mean-Squared Error. Path disentangling loss
"""
def __init__(self, color='rgb', non_local=True):
super(FeatureMSE, self).__init__()
self.color = color
self.non_local = non_local
print('FeatureMSE locality: %s'%('Non-local' if non_local else 'Local'))
def intra_pixel_dist(self, p_buffer, ref):
b, s, c, h, w = p_buffer.shape
idx = torch.randperm(s)
ref_1 = ref.permute(0, 3, 4, 1, 2).reshape(b * h * w, s, 3)
ref_2 = ref_1[:, idx, :]
mse_rad_inter = 0.5 * torch.sum(torch.pow(ref_1 - ref_2, 2), dim=2)
p_1 = p_buffer.permute(0, 1, 3, 4, 2).reshape(b, s * h * w, c)
p_2 = p_1[:, idx, :]
mse_p_inter = 0.5 * torch.sum(torch.pow(p_1 - p_2, 2), dim=2)
loss = 0.5 * torch.mean(torch.pow(mse_p_inter - mse_rad_inter, 2))
return loss
def intra_patch_dist(self, p_buffer, ref):
b, s, c, h, w = p_buffer.shape
idx = torch.randperm(s * h * w)
ref_1 = ref.permute(0, 1, 3, 4, 2).reshape(b, s * h * w, 3)
ref_2 = ref_1[:, idx, :]
mse_rad_inter = 0.5 * torch.sum(torch.pow(ref_1 - ref_2, 2), dim=2)
p_1 = p_buffer.permute(0, 1, 3, 4, 2).reshape(b, s * h * w, c)
p_2 = p_1[:, idx, :]
mse_p_inter = 0.5 * torch.sum(torch.pow(p_1 - p_2, 2), dim=2)
loss = 0.5 * torch.mean(torch.pow(mse_p_inter - mse_rad_inter, 2))
return loss
def intra_batch_dist(self, p_buffer, ref):
b, s, c, h, w = p_buffer.shape
idx = torch.randperm(b * s * h * w)
ref_1 = ref.permute(0, 1, 3, 4, 2).reshape(-1, 3)
ref_2 = ref_1[idx, :]
mse_rad_inter = 0.5 * torch.sum(torch.pow(ref_1 - ref_2, 2), dim=1)
p_1 = p_buffer.permute(0, 1, 3, 4, 2).reshape(-1, c)
p_2 = p_1[idx, :]
mse_p_inter = 0.5 * torch.sum(torch.pow(p_1 - p_2, 2), dim=1)
loss = 0.5 * torch.mean(torch.pow(mse_p_inter - mse_rad_inter, 2))
return loss
def _tonemap_gamma(self, img):
img = torch.clamp(img, min=0)
return (img / (1 + img)) ** 0.454545
def _preprocess(self, img):
"""Convert to the Cartesian HLS coordinates
Args:
img(torch.Tensor): three channel image. (*, C=3, H, W)
"""
img = rgb_to_hls(self._tonemap_gamma(img))
theta = img[...,0,:,:].clone()
r = img[...,2,:,:].clone()
img[...,0,:,:] = r * torch.cos(theta)
img[...,1,:,:] *= 2
img[...,2,:,:] = r * torch.sin(theta)
return img
def forward(self, p_buffer, ref):
"""Evaluate the metric.
Args:
p_buffer(torch.Tensor): embedded path. (B, S, C=3, H, W)
ref(torch.Tensor): reference radiance. (B, C=3, H, W)
"""
# convert to the same HLS space
if self.color == 'hls':
p_buffer = self._preprocess(p_buffer)
ref = self._preprocess(ref)
else:
#p_buffer = self._tonemap_gamma(p_buffer)
ref = self._tonemap_gamma(ref)
_, s, _, _, _ = p_buffer.shape
ref = torch.stack((ref,) * s, dim=1)
if not torch.isfinite(p_buffer).all():
raise RuntimeError("Infinite loss at train time.")
if not torch.isfinite(ref).all():
raise RuntimeError("Infinite loss at train time.")
# intra-patch
loss_p = self.intra_patch_dist(p_buffer, ref)
# intra-batch
if self.non_local:
loss_b = self.intra_batch_dist(p_buffer, ref)
else:
loss_b = loss_p
return loss_p + loss_b
class GlobalRelativeSimilarityLoss(torch.nn.Module):
"""Global Relative Similarity Loss.
Focused on uniformly reducing the norm of feature errors.
But doesn't give superior improvements.
Contrastive2: z = x^2 + y^2
Contrastive1: z = |x| + |y|
GRS : z = ln(1 + exp(x) + exp(-x) + exp(y) + exp(-y))
Inspired by
RelocNet [Balntas et al. ECCV 2018]
Multi-Similarity Loss [Wang et al. CVPR 2019]
"""
def __init__(self, alpha=2, color='rgb'):
super(GlobalRelativeSimilarityLoss, self).__init__()
self.color = color
self.alpha = alpha
def intra_patch_dist(self, p_buffer, ref):
b, s, c, h, w = p_buffer.shape
idx = torch.randperm(s * h * w)
ref_1 = ref.permute(0, 1, 3, 4, 2).reshape(b, s * h * w, 3)
ref_2 = ref_1[:, idx, :]
mse_rad_inter = 0.5 * torch.sum(torch.pow(ref_1 - ref_2, 2), dim=2)
p_1 = p_buffer.permute(0, 1, 3, 4, 2).reshape(b, s * h * w, c)
p_2 = p_1[:, idx, :]
mse_p_inter = 0.5 * torch.sum(torch.pow(p_1 - p_2, 2), dim=2)
disp = mse_p_inter - mse_rad_inter # displacement
disp = disp.reshape(b * s * h * w)
return disp
def intra_batch_dist(self, p_buffer, ref):
b, s, c, h, w = p_buffer.shape
idx = torch.randperm(b * s * h * w)
ref_1 = ref.permute(0, 1, 3, 4, 2).reshape(-1, 3)
ref_2 = ref_1[idx, :]
mse_rad_inter = 0.5 * torch.sum(torch.pow(ref_1 - ref_2, 2), dim=1)
p_1 = p_buffer.permute(0, 1, 3, 4, 2).reshape(-1, c)
p_2 = p_1[idx, :]
mse_p_inter = 0.5 * torch.sum(torch.pow(p_1 - p_2, 2), dim=1)
disp = mse_p_inter - mse_rad_inter # displacement
return disp
def _tmap1(self, img):
# img channel 수 무관
img = torch.clamp(img, min=0)
return (img / (1 + img)) ** 0.454545
def _tmap2(self, img):
# img channel = 3
img = torch.clamp(img, min=0)
lum = 0.2126 * img[...,0:1,:,:] + 0.7152 * img[...,1:2,:,:] + \
0.0722 * img[...,2:3,:,:]
return (img / (1 + lum)) ** 0.454545
def _tmap3(self, img):
img = torch.clamp(img, min=0)
lum = img[...,0:1,:,:] + img[...,1:2,:,:] + img[...,2:3,:,:]
lum /= 3.0
return (img / (1 + lum)) ** 0.454545
def forward(self, p_buffer, ref):
"""Evaluate the metric.
Args:
p_buffer(torch.Tensor): embedded path. (B, S, C, H, W)
ref(torch.Tensor): reference radiance. (B, C=3, H, W)
"""
if not torch.isfinite(p_buffer).all():
raise RuntimeError("Infinite loss at train time.")
if not torch.isfinite(ref).all():
raise RuntimeError("Infinite loss at train time.")
#p_buffer = self._tmap1(p_buffer)
ref = self._tmap1(ref)
b, s, c, h, w = p_buffer.shape
ref = torch.stack((ref,) * s, dim=1)
disp_p = self.intra_patch_dist(p_buffer, ref)
disp_b = self.intra_batch_dist(p_buffer, ref)
zero = torch.zeros((1), dtype=p_buffer.dtype, device=p_buffer.device)
exp = self.alpha * torch.cat([disp_p, disp_b, -disp_p, -disp_b, zero], dim=0)
output = torch.logsumexp(exp, dim=0) - math.log(1 + 4 * b * s * h * w)
output /= math.sqrt(self.alpha)
return output
"""
The below code is written by Michaël Gharbi.
"""
# Sample-based Monte Carlo Denoising using a Kernel-Splatting Network
# Michaël Gharbi Tzu-Mao Li Miika Aittala Jaakko Lehtinen Frédo Durand
# Siggraph 2019
#
# Copyright (c) 2019 Michaël Gharbi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
def _tonemap(im):
"""Helper Reinhards tonemapper.
Args:
im(torch.Tensor): image to tonemap.
Returns:
(torch.Tensor) tonemaped image.
"""
im = torch.clamp(im, min=0)
return im / (1+im)
class RelativeMSE(torch.nn.Module):
"""Relative Mean-Squared Error.
:math:`0.5 * \\frac{(x - y)^2}{y^2 + \epsilon}`
Args:
eps(float): small number to avoid division by 0.
"""
def __init__(self, eps=1e-2):
super(RelativeMSE, self).__init__()
self.eps = eps
def forward(self, im, ref):
"""Evaluate the metric.
Args:
im(torch.Tensor): image.
ref(torch.Tensor): reference.
"""
mse = torch.pow(im-ref, 2)
loss = mse/(torch.pow(ref, 2) + self.eps)
loss = 0.5*torch.mean(loss)
return loss
class SMAPE(torch.nn.Module):
"""Symmetric Mean Absolute error.
:math:`\\frac{|x - y|} {|x| + |y| + \epsilon}`
Args:
eps(float): small number to avoid division by 0.
"""
def __init__(self, eps=1e-2):
super(SMAPE, self).__init__()
self.eps = eps
def forward(self, im, ref):
# NOTE: the denominator is used to scale the loss, but does not
# contribute gradients, hence the '.detach()' call.
loss = (torch.abs(im-ref) / (
self.eps + torch.abs(im.detach()) + torch.abs(ref.detach()))).mean()
return loss
class TonemappedMSE(torch.nn.Module):
"""Mean-squared error on tonemaped images.
Args:
eps(float): small number to avoid division by 0.
"""
def __init__(self, eps=1e-2):
super(TonemappedMSE, self).__init__()
self.eps = eps # avoid division by zero
def forward(self, im, ref):
im = _tonemap(im)
ref = _tonemap(ref)
loss = torch.pow(im-ref, 2)
loss = 0.5*torch.mean(loss)
return loss
class TonemappedRelativeMSE(torch.nn.Module):
"""Relative mean-squared error on tonemaped images.
Args:
eps(float): small number to avoid division by 0.
"""
def __init__(self, eps=1e-2):
super(TonemappedRelativeMSE, self).__init__()
self.eps = eps # avoid division by zero
def forward(self, im, ref):
im = _tonemap(im)
ref = _tonemap(ref)
mse = torch.pow(im-ref, 2)
loss = mse/(torch.pow(ref, 2) + self.eps)
loss = 0.5*torch.mean(loss)
return loss
def _tonemap(im):
"""Helper Reinhards tonemapper.
Args:
im(torch.Tensor): image to tonemap.
Returns:
(torch.Tensor) tonemaped image.
"""
im = torch.clamp(im, min=0)
return im / (1+im)