Update loss.py
Browse files
loss.py
CHANGED
@@ -1,307 +1,309 @@
|
|
1 |
-
import torch
|
2 |
-
import torch.nn as nn
|
3 |
-
import torch.nn.functional as F
|
4 |
-
import numpy as np
|
5 |
-
import scipy.stats as st
|
6 |
-
from utils import pair_downsampler,calculate_local_variance,LocalMean
|
7 |
-
|
8 |
-
EPS = 1e-9
|
9 |
-
PI = 22.0 / 7.0
|
10 |
-
|
11 |
-
|
12 |
-
class LossFunction(nn.Module):
|
13 |
-
def __init__(self):
|
14 |
-
super(LossFunction, self).__init__()
|
15 |
-
self._l2_loss = nn.MSELoss()
|
16 |
-
self._l1_loss = nn.L1Loss()
|
17 |
-
self.smooth_loss = SmoothLoss()
|
18 |
-
self.texture_difference=TextureDifference()
|
19 |
-
self.local_mean=LocalMean(patch_size=5)
|
20 |
-
self.L_TV_loss=L_TV()
|
21 |
-
|
22 |
-
|
23 |
-
def forward(self,input,L_pred1,L_pred2,L2,s2,s21,s22,H2,H11,H12,H13,s13,H14,s14,H3,s3,H3_pred,H4_pred,L_pred1_L_pred2_diff,H3_denoised1_H3_denoised2_diff,H2_blur,H3_blur):
|
24 |
-
eps = 1e-9
|
25 |
-
input = input + eps
|
26 |
-
|
27 |
-
input_Y = L2.detach()[:, 2, :, :] * 0.299 + L2.detach()[:, 1, :, :] * 0.587 + L2.detach()[:, 0, :, :] * 0.144
|
28 |
-
input_Y_mean = torch.mean(input_Y, dim=(1, 2))
|
29 |
-
enhancement_factor = 0.5/ (input_Y_mean + eps)
|
30 |
-
enhancement_factor = enhancement_factor.unsqueeze(1).unsqueeze(2).unsqueeze(3)
|
31 |
-
enhancement_factor = torch.clamp(enhancement_factor, 1, 25)
|
32 |
-
adjustment_ratio = torch.pow(0.7, -enhancement_factor) / enhancement_factor
|
33 |
-
adjustment_ratio = adjustment_ratio.repeat(1, 3, 1, 1)
|
34 |
-
normalized_low_light_layer = L2.detach() / s2
|
35 |
-
normalized_low_light_layer = torch.clamp(normalized_low_light_layer, eps, 0.8)
|
36 |
-
enhanced_brightness=torch.pow(L2.detach()*enhancement_factor, enhancement_factor)
|
37 |
-
clamped_enhanced_brightness = torch.clamp(enhanced_brightness * adjustment_ratio, eps, 1)
|
38 |
-
clamped_adjusted_low_light = torch.clamp(L2.detach() * enhancement_factor,eps,1)
|
39 |
-
loss = 0
|
40 |
-
#Enhance_loss
|
41 |
-
loss += self._l2_loss(s2, clamped_enhanced_brightness) *700
|
42 |
-
loss += self._l2_loss(normalized_low_light_layer, clamped_adjusted_low_light) *1000
|
43 |
-
loss += self.smooth_loss(L2.detach(), s2) *5
|
44 |
-
loss += self.L_TV_loss(s2)*1600
|
45 |
-
#Loss_res_1
|
46 |
-
L11, L12 = pair_downsampler(input)
|
47 |
-
loss += self._l2_loss(L11, L_pred2) * 1000
|
48 |
-
loss += self._l2_loss(L12, L_pred1) * 1000
|
49 |
-
denoised1, denoised2 = pair_downsampler(L2)
|
50 |
-
loss += self._l2_loss(L_pred1, denoised1) * 1000
|
51 |
-
loss += self._l2_loss(L_pred2, denoised2) * 1000
|
52 |
-
# Loss_res_2
|
53 |
-
loss += self._l2_loss(H3_pred, torch.cat([H12.detach(), s22.detach()], 1)) * 1000
|
54 |
-
loss += self._l2_loss(H4_pred, torch.cat([H11.detach(), s21.detach()], 1)) * 1000
|
55 |
-
H3_denoised1, H3_denoised2 = pair_downsampler(H3)
|
56 |
-
loss += self._l2_loss(H3_pred[:, 0:3, :, :], H3_denoised1) * 1000
|
57 |
-
loss += self._l2_loss(H4_pred[:, 0:3, :, :], H3_denoised2) * 1000
|
58 |
-
#Loss_color
|
59 |
-
loss += self._l2_loss(H2_blur.detach(), H3_blur) * 10000
|
60 |
-
#Loss_ill
|
61 |
-
loss += self._l2_loss(s2.detach(), s3) * 1000
|
62 |
-
#Loss_cons
|
63 |
-
local_mean1 = self.local_mean(H3_denoised1)
|
64 |
-
local_mean2 = self.local_mean(H3_denoised2)
|
65 |
-
weighted_diff1 = (1 - H3_denoised1_H3_denoised2_diff) * local_mean1+H3_denoised1*H3_denoised1_H3_denoised2_diff
|
66 |
-
weighted_diff2 = (1 - H3_denoised1_H3_denoised2_diff) * local_mean2+H3_denoised1*H3_denoised1_H3_denoised2_diff
|
67 |
-
loss += self._l2_loss(H3_denoised1,weighted_diff1)* 10000
|
68 |
-
loss += self._l2_loss(H3_denoised2, weighted_diff2)* 10000
|
69 |
-
#Loss_Var
|
70 |
-
noise_std = calculate_local_variance(H3 - H2)
|
71 |
-
H2_var = calculate_local_variance(H2)
|
72 |
-
loss += self._l2_loss(H2_var, noise_std) * 1000
|
73 |
-
return loss
|
74 |
-
|
75 |
-
def local_mean(self, image):
|
76 |
-
padding = self.patch_size // 2
|
77 |
-
image = F.pad(image, (padding, padding, padding, padding), mode='reflect')
|
78 |
-
patches = image.unfold(2, self.patch_size, 1).unfold(3, self.patch_size, 1)
|
79 |
-
return patches.mean(dim=(4, 5))
|
80 |
-
|
81 |
-
def gauss_kernel(kernlen=21, nsig=3, channels=1):
|
82 |
-
interval = (2 * nsig + 1.) / (kernlen)
|
83 |
-
x = np.linspace(-nsig - interval / 2., nsig + interval / 2., kernlen + 1)
|
84 |
-
kern1d = np.diff(st.norm.cdf(x))
|
85 |
-
kernel_raw = np.sqrt(np.outer(kern1d, kern1d))
|
86 |
-
kernel = kernel_raw / kernel_raw.sum()
|
87 |
-
out_filter = np.array(kernel, dtype=np.float32)
|
88 |
-
out_filter = out_filter.reshape((kernlen, kernlen, 1, 1))
|
89 |
-
out_filter = np.repeat(out_filter, channels, axis=2)
|
90 |
-
|
91 |
-
return out_filter
|
92 |
-
|
93 |
-
|
94 |
-
class TextureDifference(nn.Module):
|
95 |
-
def __init__(self, patch_size=5, constant_C=1e-5,threshold=0.975):
|
96 |
-
super(TextureDifference, self).__init__()
|
97 |
-
self.patch_size = patch_size
|
98 |
-
self.constant_C = constant_C
|
99 |
-
self.threshold = threshold
|
100 |
-
|
101 |
-
def forward(self, image1, image2):
|
102 |
-
# Convert RGB images to grayscale
|
103 |
-
image1 = self.rgb_to_gray(image1)
|
104 |
-
image2 = self.rgb_to_gray(image2)
|
105 |
-
|
106 |
-
stddev1 = self.local_stddev(image1)
|
107 |
-
stddev2 = self.local_stddev(image2)
|
108 |
-
numerator = 2 * stddev1 * stddev2
|
109 |
-
denominator = stddev1 ** 2 + stddev2 ** 2 + self.constant_C
|
110 |
-
diff = numerator / denominator
|
111 |
-
|
112 |
-
# Apply threshold to diff tensor
|
113 |
-
binary_diff = torch.where(diff > self.threshold, torch.tensor(1.0, device=diff.device),
|
114 |
-
torch.tensor(0.0, device=diff.device))
|
115 |
-
|
116 |
-
return binary_diff
|
117 |
-
|
118 |
-
def local_stddev(self, image):
|
119 |
-
padding = self.patch_size // 2
|
120 |
-
image = F.pad(image, (padding, padding, padding, padding), mode='reflect')
|
121 |
-
patches = image.unfold(2, self.patch_size, 1).unfold(3, self.patch_size, 1)
|
122 |
-
mean = patches.mean(dim=(4, 5), keepdim=True)
|
123 |
-
squared_diff = (patches - mean) ** 2
|
124 |
-
local_variance = squared_diff.mean(dim=(4, 5))
|
125 |
-
local_stddev = torch.sqrt(local_variance+1e-9)
|
126 |
-
return local_stddev
|
127 |
-
|
128 |
-
def rgb_to_gray(self, image):
|
129 |
-
# Convert RGB image to grayscale using the luminance formula
|
130 |
-
gray_image = 0.144 * image[:, 0, :, :] + 0.5870 * image[:, 1, :, :] + 0.299 * image[:, 2, :, :]
|
131 |
-
return gray_image.unsqueeze(1) # Add a channel dimension for compatibility
|
132 |
-
|
133 |
-
|
134 |
-
class L_TV(nn.Module):
|
135 |
-
def __init__(self,TVLoss_weight=1):
|
136 |
-
super(L_TV,self).__init__()
|
137 |
-
self.TVLoss_weight = TVLoss_weight
|
138 |
-
|
139 |
-
def forward(self,x):
|
140 |
-
batch_size = x.size()[0]
|
141 |
-
h_x = x.size()[2]
|
142 |
-
w_x = x.size()[3]
|
143 |
-
count_h = (x.size()[2]-1) * x.size()[3]
|
144 |
-
count_w = x.size()[2] * (x.size()[3] - 1)
|
145 |
-
h_tv = torch.pow((x[:,:,1:,:]-x[:,:,:h_x-1,:]),2).sum()
|
146 |
-
w_tv = torch.pow((x[:,:,:,1:]-x[:,:,:,:w_x-1]),2).sum()
|
147 |
-
return self.TVLoss_weight*2*(h_tv/count_h+w_tv/count_w)/batch_size
|
148 |
-
|
149 |
-
class Blur(nn.Module):
|
150 |
-
def __init__(self, nc):
|
151 |
-
super(Blur, self).__init__()
|
152 |
-
self.nc = nc
|
153 |
-
kernel = gauss_kernel(kernlen=21, nsig=3, channels=self.nc)
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
self.
|
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 |
-
keepdim=True) * sigma_color)
|
197 |
-
|
198 |
-
keepdim=True) * sigma_color)
|
199 |
-
|
200 |
-
keepdim=True) * sigma_color)
|
201 |
-
|
202 |
-
keepdim=True) * sigma_color)
|
203 |
-
|
204 |
-
keepdim=True) * sigma_color)
|
205 |
-
|
206 |
-
keepdim=True) * sigma_color)
|
207 |
-
|
208 |
-
keepdim=True) * sigma_color)
|
209 |
-
|
210 |
-
keepdim=True) * sigma_color)
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
keepdim=True) * sigma_color)
|
215 |
-
|
216 |
-
keepdim=True) * sigma_color)
|
217 |
-
|
218 |
-
keepdim=True) * sigma_color)
|
219 |
-
|
220 |
-
keepdim=True) * sigma_color)
|
221 |
-
|
222 |
-
keepdim=True) * sigma_color)
|
223 |
-
|
224 |
-
keepdim=True) * sigma_color)
|
225 |
-
|
226 |
-
keepdim=True) * sigma_color)
|
227 |
-
|
228 |
-
keepdim=True) * sigma_color)
|
229 |
-
|
230 |
-
keepdim=True) * sigma_color)
|
231 |
-
|
232 |
-
keepdim=True) * sigma_color)
|
233 |
-
|
234 |
-
keepdim=True) * sigma_color)
|
235 |
-
|
236 |
-
keepdim=True) * sigma_color)
|
237 |
-
|
238 |
-
keepdim=True) * sigma_color)
|
239 |
-
|
240 |
-
keepdim=True) * sigma_color)
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
keepdim=True)
|
259 |
-
|
260 |
-
keepdim=True)
|
261 |
-
|
262 |
-
keepdim=True)
|
263 |
-
|
264 |
-
keepdim=True)
|
265 |
-
|
266 |
-
keepdim=True)
|
267 |
-
|
268 |
-
keepdim=True)
|
269 |
-
|
270 |
-
keepdim=True)
|
271 |
-
|
272 |
-
keepdim=True)
|
273 |
-
|
274 |
-
keepdim=True)
|
275 |
-
|
276 |
-
keepdim=True)
|
277 |
-
|
278 |
-
keepdim=True)
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
+ torch.mean(
|
284 |
-
+ torch.mean(
|
285 |
-
+ torch.mean(
|
286 |
-
+ torch.mean(
|
287 |
-
+ torch.mean(
|
288 |
-
+ torch.mean(
|
289 |
-
+ torch.mean(
|
290 |
-
+ torch.mean(
|
291 |
-
+ torch.mean(
|
292 |
-
+ torch.mean(
|
293 |
-
+ torch.mean(
|
294 |
-
+ torch.mean(
|
295 |
-
+ torch.mean(
|
296 |
-
+ torch.mean(
|
297 |
-
+ torch.mean(
|
298 |
-
+ torch.mean(
|
299 |
-
+ torch.mean(
|
300 |
-
+ torch.mean(
|
301 |
-
+ torch.mean(
|
302 |
-
+ torch.mean(
|
303 |
-
+ torch.mean(
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
import numpy as np
|
5 |
+
import scipy.stats as st
|
6 |
+
from utils import pair_downsampler,calculate_local_variance,LocalMean
|
7 |
+
|
8 |
+
EPS = 1e-9
|
9 |
+
PI = 22.0 / 7.0
|
10 |
+
|
11 |
+
|
12 |
+
class LossFunction(nn.Module):
|
13 |
+
def __init__(self):
|
14 |
+
super(LossFunction, self).__init__()
|
15 |
+
self._l2_loss = nn.MSELoss()
|
16 |
+
self._l1_loss = nn.L1Loss()
|
17 |
+
self.smooth_loss = SmoothLoss()
|
18 |
+
self.texture_difference=TextureDifference()
|
19 |
+
self.local_mean=LocalMean(patch_size=5)
|
20 |
+
self.L_TV_loss=L_TV()
|
21 |
+
|
22 |
+
|
23 |
+
def forward(self,input,L_pred1,L_pred2,L2,s2,s21,s22,H2,H11,H12,H13,s13,H14,s14,H3,s3,H3_pred,H4_pred,L_pred1_L_pred2_diff,H3_denoised1_H3_denoised2_diff,H2_blur,H3_blur):
|
24 |
+
eps = 1e-9
|
25 |
+
input = input + eps
|
26 |
+
|
27 |
+
input_Y = L2.detach()[:, 2, :, :] * 0.299 + L2.detach()[:, 1, :, :] * 0.587 + L2.detach()[:, 0, :, :] * 0.144
|
28 |
+
input_Y_mean = torch.mean(input_Y, dim=(1, 2))
|
29 |
+
enhancement_factor = 0.5/ (input_Y_mean + eps)
|
30 |
+
enhancement_factor = enhancement_factor.unsqueeze(1).unsqueeze(2).unsqueeze(3)
|
31 |
+
enhancement_factor = torch.clamp(enhancement_factor, 1, 25)
|
32 |
+
adjustment_ratio = torch.pow(0.7, -enhancement_factor) / enhancement_factor
|
33 |
+
adjustment_ratio = adjustment_ratio.repeat(1, 3, 1, 1)
|
34 |
+
normalized_low_light_layer = L2.detach() / s2
|
35 |
+
normalized_low_light_layer = torch.clamp(normalized_low_light_layer, eps, 0.8)
|
36 |
+
enhanced_brightness=torch.pow(L2.detach()*enhancement_factor, enhancement_factor)
|
37 |
+
clamped_enhanced_brightness = torch.clamp(enhanced_brightness * adjustment_ratio, eps, 1)
|
38 |
+
clamped_adjusted_low_light = torch.clamp(L2.detach() * enhancement_factor,eps,1)
|
39 |
+
loss = 0
|
40 |
+
#Enhance_loss
|
41 |
+
loss += self._l2_loss(s2, clamped_enhanced_brightness) *700
|
42 |
+
loss += self._l2_loss(normalized_low_light_layer, clamped_adjusted_low_light) *1000
|
43 |
+
loss += self.smooth_loss(L2.detach(), s2) *5
|
44 |
+
loss += self.L_TV_loss(s2)*1600
|
45 |
+
#Loss_res_1
|
46 |
+
L11, L12 = pair_downsampler(input)
|
47 |
+
loss += self._l2_loss(L11, L_pred2) * 1000
|
48 |
+
loss += self._l2_loss(L12, L_pred1) * 1000
|
49 |
+
denoised1, denoised2 = pair_downsampler(L2)
|
50 |
+
loss += self._l2_loss(L_pred1, denoised1) * 1000
|
51 |
+
loss += self._l2_loss(L_pred2, denoised2) * 1000
|
52 |
+
# Loss_res_2
|
53 |
+
loss += self._l2_loss(H3_pred, torch.cat([H12.detach(), s22.detach()], 1)) * 1000
|
54 |
+
loss += self._l2_loss(H4_pred, torch.cat([H11.detach(), s21.detach()], 1)) * 1000
|
55 |
+
H3_denoised1, H3_denoised2 = pair_downsampler(H3)
|
56 |
+
loss += self._l2_loss(H3_pred[:, 0:3, :, :], H3_denoised1) * 1000
|
57 |
+
loss += self._l2_loss(H4_pred[:, 0:3, :, :], H3_denoised2) * 1000
|
58 |
+
#Loss_color
|
59 |
+
loss += self._l2_loss(H2_blur.detach(), H3_blur) * 10000
|
60 |
+
#Loss_ill
|
61 |
+
loss += self._l2_loss(s2.detach(), s3) * 1000
|
62 |
+
#Loss_cons
|
63 |
+
local_mean1 = self.local_mean(H3_denoised1)
|
64 |
+
local_mean2 = self.local_mean(H3_denoised2)
|
65 |
+
weighted_diff1 = (1 - H3_denoised1_H3_denoised2_diff) * local_mean1+H3_denoised1*H3_denoised1_H3_denoised2_diff
|
66 |
+
weighted_diff2 = (1 - H3_denoised1_H3_denoised2_diff) * local_mean2+H3_denoised1*H3_denoised1_H3_denoised2_diff
|
67 |
+
loss += self._l2_loss(H3_denoised1,weighted_diff1)* 10000
|
68 |
+
loss += self._l2_loss(H3_denoised2, weighted_diff2)* 10000
|
69 |
+
#Loss_Var
|
70 |
+
noise_std = calculate_local_variance(H3 - H2)
|
71 |
+
H2_var = calculate_local_variance(H2)
|
72 |
+
loss += self._l2_loss(H2_var, noise_std) * 1000
|
73 |
+
return loss
|
74 |
+
|
75 |
+
def local_mean(self, image):
|
76 |
+
padding = self.patch_size // 2
|
77 |
+
image = F.pad(image, (padding, padding, padding, padding), mode='reflect')
|
78 |
+
patches = image.unfold(2, self.patch_size, 1).unfold(3, self.patch_size, 1)
|
79 |
+
return patches.mean(dim=(4, 5))
|
80 |
+
|
81 |
+
def gauss_kernel(kernlen=21, nsig=3, channels=1):
|
82 |
+
interval = (2 * nsig + 1.) / (kernlen)
|
83 |
+
x = np.linspace(-nsig - interval / 2., nsig + interval / 2., kernlen + 1)
|
84 |
+
kern1d = np.diff(st.norm.cdf(x))
|
85 |
+
kernel_raw = np.sqrt(np.outer(kern1d, kern1d))
|
86 |
+
kernel = kernel_raw / kernel_raw.sum()
|
87 |
+
out_filter = np.array(kernel, dtype=np.float32)
|
88 |
+
out_filter = out_filter.reshape((kernlen, kernlen, 1, 1))
|
89 |
+
out_filter = np.repeat(out_filter, channels, axis=2)
|
90 |
+
|
91 |
+
return out_filter
|
92 |
+
|
93 |
+
|
94 |
+
class TextureDifference(nn.Module):
|
95 |
+
def __init__(self, patch_size=5, constant_C=1e-5,threshold=0.975):
|
96 |
+
super(TextureDifference, self).__init__()
|
97 |
+
self.patch_size = patch_size
|
98 |
+
self.constant_C = constant_C
|
99 |
+
self.threshold = threshold
|
100 |
+
|
101 |
+
def forward(self, image1, image2):
|
102 |
+
# Convert RGB images to grayscale
|
103 |
+
image1 = self.rgb_to_gray(image1)
|
104 |
+
image2 = self.rgb_to_gray(image2)
|
105 |
+
|
106 |
+
stddev1 = self.local_stddev(image1)
|
107 |
+
stddev2 = self.local_stddev(image2)
|
108 |
+
numerator = 2 * stddev1 * stddev2
|
109 |
+
denominator = stddev1 ** 2 + stddev2 ** 2 + self.constant_C
|
110 |
+
diff = numerator / denominator
|
111 |
+
|
112 |
+
# Apply threshold to diff tensor
|
113 |
+
binary_diff = torch.where(diff > self.threshold, torch.tensor(1.0, device=diff.device),
|
114 |
+
torch.tensor(0.0, device=diff.device))
|
115 |
+
|
116 |
+
return binary_diff
|
117 |
+
|
118 |
+
def local_stddev(self, image):
|
119 |
+
padding = self.patch_size // 2
|
120 |
+
image = F.pad(image, (padding, padding, padding, padding), mode='reflect')
|
121 |
+
patches = image.unfold(2, self.patch_size, 1).unfold(3, self.patch_size, 1)
|
122 |
+
mean = patches.mean(dim=(4, 5), keepdim=True)
|
123 |
+
squared_diff = (patches - mean) ** 2
|
124 |
+
local_variance = squared_diff.mean(dim=(4, 5))
|
125 |
+
local_stddev = torch.sqrt(local_variance+1e-9)
|
126 |
+
return local_stddev
|
127 |
+
|
128 |
+
def rgb_to_gray(self, image):
|
129 |
+
# Convert RGB image to grayscale using the luminance formula
|
130 |
+
gray_image = 0.144 * image[:, 0, :, :] + 0.5870 * image[:, 1, :, :] + 0.299 * image[:, 2, :, :]
|
131 |
+
return gray_image.unsqueeze(1) # Add a channel dimension for compatibility
|
132 |
+
|
133 |
+
|
134 |
+
class L_TV(nn.Module):
|
135 |
+
def __init__(self,TVLoss_weight=1):
|
136 |
+
super(L_TV,self).__init__()
|
137 |
+
self.TVLoss_weight = TVLoss_weight
|
138 |
+
|
139 |
+
def forward(self,x):
|
140 |
+
batch_size = x.size()[0]
|
141 |
+
h_x = x.size()[2]
|
142 |
+
w_x = x.size()[3]
|
143 |
+
count_h = (x.size()[2]-1) * x.size()[3]
|
144 |
+
count_w = x.size()[2] * (x.size()[3] - 1)
|
145 |
+
h_tv = torch.pow((x[:,:,1:,:]-x[:,:,:h_x-1,:]),2).sum()
|
146 |
+
w_tv = torch.pow((x[:,:,:,1:]-x[:,:,:,:w_x-1]),2).sum()
|
147 |
+
return self.TVLoss_weight*2*(h_tv/count_h+w_tv/count_w)/batch_size
|
148 |
+
|
149 |
+
class Blur(nn.Module):
|
150 |
+
def __init__(self, nc):
|
151 |
+
super(Blur, self).__init__()
|
152 |
+
self.nc = nc
|
153 |
+
kernel = gauss_kernel(kernlen=21, nsig=3, channels=self.nc)
|
154 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
155 |
+
kernel = torch.from_numpy(kernel).permute(2, 3, 0, 1).to(device)
|
156 |
+
self.weight = nn.Parameter(data=kernel, requires_grad=False).to(device)
|
157 |
+
|
158 |
+
def forward(self, x):
|
159 |
+
if x.size(1) != self.nc:
|
160 |
+
raise RuntimeError(
|
161 |
+
"The channel of input [%d] does not match the preset channel [%d]" % (x.size(1), self.nc))
|
162 |
+
|
163 |
+
x = F.conv2d(x, self.weight, stride=1, padding=10, groups=self.nc)
|
164 |
+
return x
|
165 |
+
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
class SmoothLoss(nn.Module):
|
170 |
+
def __init__(self):
|
171 |
+
super(SmoothLoss, self).__init__()
|
172 |
+
self.sigma = 10
|
173 |
+
|
174 |
+
def rgb2yCbCr(self, input_im):
|
175 |
+
|
176 |
+
im_flat = input_im.contiguous().view(-1, 3).float()
|
177 |
+
# [w,h,3] => [w*h,3]
|
178 |
+
device = input_im.device # Use same device as input
|
179 |
+
mat = torch.Tensor([[0.257, -0.148, 0.439], [0.564, -0.291, -0.368], [0.098, 0.439, -0.071]]).to(device)
|
180 |
+
# [3,3]
|
181 |
+
bias = torch.Tensor([16.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0]).to(device)
|
182 |
+
# [1,3]
|
183 |
+
temp = im_flat.mm(mat) + bias
|
184 |
+
# [w*h,3]*[3,3]+[1,3] => [w*h,3]
|
185 |
+
out = temp.view(input_im.shape[0], 3, input_im.shape[2], input_im.shape[3])
|
186 |
+
return out
|
187 |
+
|
188 |
+
# output: output input:input
|
189 |
+
def forward(self, input, output):
|
190 |
+
|
191 |
+
|
192 |
+
self.output = output
|
193 |
+
self.input = self.rgb2yCbCr(input)
|
194 |
+
sigma_color = -1.0 / (2 * self.sigma * self.sigma)
|
195 |
+
w1 = torch.exp(torch.sum(torch.pow(self.input[:, :, 1:, :] - self.input[:, :, :-1, :], 2), dim=1,
|
196 |
+
keepdim=True) * sigma_color)
|
197 |
+
w2 = torch.exp(torch.sum(torch.pow(self.input[:, :, :-1, :] - self.input[:, :, 1:, :], 2), dim=1,
|
198 |
+
keepdim=True) * sigma_color)
|
199 |
+
w3 = torch.exp(torch.sum(torch.pow(self.input[:, :, :, 1:] - self.input[:, :, :, :-1], 2), dim=1,
|
200 |
+
keepdim=True) * sigma_color)
|
201 |
+
w4 = torch.exp(torch.sum(torch.pow(self.input[:, :, :, :-1] - self.input[:, :, :, 1:], 2), dim=1,
|
202 |
+
keepdim=True) * sigma_color)
|
203 |
+
w5 = torch.exp(torch.sum(torch.pow(self.input[:, :, :-1, :-1] - self.input[:, :, 1:, 1:], 2), dim=1,
|
204 |
+
keepdim=True) * sigma_color)
|
205 |
+
w6 = torch.exp(torch.sum(torch.pow(self.input[:, :, 1:, 1:] - self.input[:, :, :-1, :-1], 2), dim=1,
|
206 |
+
keepdim=True) * sigma_color)
|
207 |
+
w7 = torch.exp(torch.sum(torch.pow(self.input[:, :, 1:, :-1] - self.input[:, :, :-1, 1:], 2), dim=1,
|
208 |
+
keepdim=True) * sigma_color)
|
209 |
+
w8 = torch.exp(torch.sum(torch.pow(self.input[:, :, :-1, 1:] - self.input[:, :, 1:, :-1], 2), dim=1,
|
210 |
+
keepdim=True) * sigma_color)
|
211 |
+
w9 = torch.exp(torch.sum(torch.pow(self.input[:, :, 2:, :] - self.input[:, :, :-2, :], 2), dim=1,
|
212 |
+
keepdim=True) * sigma_color)
|
213 |
+
w10 = torch.exp(torch.sum(torch.pow(self.input[:, :, :-2, :] - self.input[:, :, 2:, :], 2), dim=1,
|
214 |
+
keepdim=True) * sigma_color)
|
215 |
+
w11 = torch.exp(torch.sum(torch.pow(self.input[:, :, :, 2:] - self.input[:, :, :, :-2], 2), dim=1,
|
216 |
+
keepdim=True) * sigma_color)
|
217 |
+
w12 = torch.exp(torch.sum(torch.pow(self.input[:, :, :, :-2] - self.input[:, :, :, 2:], 2), dim=1,
|
218 |
+
keepdim=True) * sigma_color)
|
219 |
+
w13 = torch.exp(torch.sum(torch.pow(self.input[:, :, :-2, :-1] - self.input[:, :, 2:, 1:], 2), dim=1,
|
220 |
+
keepdim=True) * sigma_color)
|
221 |
+
w14 = torch.exp(torch.sum(torch.pow(self.input[:, :, 2:, 1:] - self.input[:, :, :-2, :-1], 2), dim=1,
|
222 |
+
keepdim=True) * sigma_color)
|
223 |
+
w15 = torch.exp(torch.sum(torch.pow(self.input[:, :, 2:, :-1] - self.input[:, :, :-2, 1:], 2), dim=1,
|
224 |
+
keepdim=True) * sigma_color)
|
225 |
+
w16 = torch.exp(torch.sum(torch.pow(self.input[:, :, :-2, 1:] - self.input[:, :, 2:, :-1], 2), dim=1,
|
226 |
+
keepdim=True) * sigma_color)
|
227 |
+
w17 = torch.exp(torch.sum(torch.pow(self.input[:, :, :-1, :-2] - self.input[:, :, 1:, 2:], 2), dim=1,
|
228 |
+
keepdim=True) * sigma_color)
|
229 |
+
w18 = torch.exp(torch.sum(torch.pow(self.input[:, :, 1:, 2:] - self.input[:, :, :-1, :-2], 2), dim=1,
|
230 |
+
keepdim=True) * sigma_color)
|
231 |
+
w19 = torch.exp(torch.sum(torch.pow(self.input[:, :, 1:, :-2] - self.input[:, :, :-1, 2:], 2), dim=1,
|
232 |
+
keepdim=True) * sigma_color)
|
233 |
+
w20 = torch.exp(torch.sum(torch.pow(self.input[:, :, :-1, 2:] - self.input[:, :, 1:, :-2], 2), dim=1,
|
234 |
+
keepdim=True) * sigma_color)
|
235 |
+
w21 = torch.exp(torch.sum(torch.pow(self.input[:, :, :-2, :-2] - self.input[:, :, 2:, 2:], 2), dim=1,
|
236 |
+
keepdim=True) * sigma_color)
|
237 |
+
w22 = torch.exp(torch.sum(torch.pow(self.input[:, :, 2:, 2:] - self.input[:, :, :-2, :-2], 2), dim=1,
|
238 |
+
keepdim=True) * sigma_color)
|
239 |
+
w23 = torch.exp(torch.sum(torch.pow(self.input[:, :, 2:, :-2] - self.input[:, :, :-2, 2:], 2), dim=1,
|
240 |
+
keepdim=True) * sigma_color)
|
241 |
+
w24 = torch.exp(torch.sum(torch.pow(self.input[:, :, :-2, 2:] - self.input[:, :, 2:, :-2], 2), dim=1,
|
242 |
+
keepdim=True) * sigma_color)
|
243 |
+
p = 1.0
|
244 |
+
|
245 |
+
pixel_grad1 = w1 * torch.norm((self.output[:, :, 1:, :] - self.output[:, :, :-1, :]), p, dim=1, keepdim=True)
|
246 |
+
pixel_grad2 = w2 * torch.norm((self.output[:, :, :-1, :] - self.output[:, :, 1:, :]), p, dim=1, keepdim=True)
|
247 |
+
pixel_grad3 = w3 * torch.norm((self.output[:, :, :, 1:] - self.output[:, :, :, :-1]), p, dim=1, keepdim=True)
|
248 |
+
pixel_grad4 = w4 * torch.norm((self.output[:, :, :, :-1] - self.output[:, :, :, 1:]), p, dim=1, keepdim=True)
|
249 |
+
pixel_grad5 = w5 * torch.norm((self.output[:, :, :-1, :-1] - self.output[:, :, 1:, 1:]), p, dim=1, keepdim=True)
|
250 |
+
pixel_grad6 = w6 * torch.norm((self.output[:, :, 1:, 1:] - self.output[:, :, :-1, :-1]), p, dim=1, keepdim=True)
|
251 |
+
pixel_grad7 = w7 * torch.norm((self.output[:, :, 1:, :-1] - self.output[:, :, :-1, 1:]), p, dim=1, keepdim=True)
|
252 |
+
pixel_grad8 = w8 * torch.norm((self.output[:, :, :-1, 1:] - self.output[:, :, 1:, :-1]), p, dim=1, keepdim=True)
|
253 |
+
pixel_grad9 = w9 * torch.norm((self.output[:, :, 2:, :] - self.output[:, :, :-2, :]), p, dim=1, keepdim=True)
|
254 |
+
pixel_grad10 = w10 * torch.norm((self.output[:, :, :-2, :] - self.output[:, :, 2:, :]), p, dim=1, keepdim=True)
|
255 |
+
pixel_grad11 = w11 * torch.norm((self.output[:, :, :, 2:] - self.output[:, :, :, :-2]), p, dim=1, keepdim=True)
|
256 |
+
pixel_grad12 = w12 * torch.norm((self.output[:, :, :, :-2] - self.output[:, :, :, 2:]), p, dim=1, keepdim=True)
|
257 |
+
pixel_grad13 = w13 * torch.norm((self.output[:, :, :-2, :-1] - self.output[:, :, 2:, 1:]), p, dim=1,
|
258 |
+
keepdim=True)
|
259 |
+
pixel_grad14 = w14 * torch.norm((self.output[:, :, 2:, 1:] - self.output[:, :, :-2, :-1]), p, dim=1,
|
260 |
+
keepdim=True)
|
261 |
+
pixel_grad15 = w15 * torch.norm((self.output[:, :, 2:, :-1] - self.output[:, :, :-2, 1:]), p, dim=1,
|
262 |
+
keepdim=True)
|
263 |
+
pixel_grad16 = w16 * torch.norm((self.output[:, :, :-2, 1:] - self.output[:, :, 2:, :-1]), p, dim=1,
|
264 |
+
keepdim=True)
|
265 |
+
pixel_grad17 = w17 * torch.norm((self.output[:, :, :-1, :-2] - self.output[:, :, 1:, 2:]), p, dim=1,
|
266 |
+
keepdim=True)
|
267 |
+
pixel_grad18 = w18 * torch.norm((self.output[:, :, 1:, 2:] - self.output[:, :, :-1, :-2]), p, dim=1,
|
268 |
+
keepdim=True)
|
269 |
+
pixel_grad19 = w19 * torch.norm((self.output[:, :, 1:, :-2] - self.output[:, :, :-1, 2:]), p, dim=1,
|
270 |
+
keepdim=True)
|
271 |
+
pixel_grad20 = w20 * torch.norm((self.output[:, :, :-1, 2:] - self.output[:, :, 1:, :-2]), p, dim=1,
|
272 |
+
keepdim=True)
|
273 |
+
pixel_grad21 = w21 * torch.norm((self.output[:, :, :-2, :-2] - self.output[:, :, 2:, 2:]), p, dim=1,
|
274 |
+
keepdim=True)
|
275 |
+
pixel_grad22 = w22 * torch.norm((self.output[:, :, 2:, 2:] - self.output[:, :, :-2, :-2]), p, dim=1,
|
276 |
+
keepdim=True)
|
277 |
+
pixel_grad23 = w23 * torch.norm((self.output[:, :, 2:, :-2] - self.output[:, :, :-2, 2:]), p, dim=1,
|
278 |
+
keepdim=True)
|
279 |
+
pixel_grad24 = w24 * torch.norm((self.output[:, :, :-2, 2:] - self.output[:, :, 2:, :-2]), p, dim=1,
|
280 |
+
keepdim=True)
|
281 |
+
|
282 |
+
ReguTerm1 = torch.mean(pixel_grad1) \
|
283 |
+
+ torch.mean(pixel_grad2) \
|
284 |
+
+ torch.mean(pixel_grad3) \
|
285 |
+
+ torch.mean(pixel_grad4) \
|
286 |
+
+ torch.mean(pixel_grad5) \
|
287 |
+
+ torch.mean(pixel_grad6) \
|
288 |
+
+ torch.mean(pixel_grad7) \
|
289 |
+
+ torch.mean(pixel_grad8) \
|
290 |
+
+ torch.mean(pixel_grad9) \
|
291 |
+
+ torch.mean(pixel_grad10) \
|
292 |
+
+ torch.mean(pixel_grad11) \
|
293 |
+
+ torch.mean(pixel_grad12) \
|
294 |
+
+ torch.mean(pixel_grad13) \
|
295 |
+
+ torch.mean(pixel_grad14) \
|
296 |
+
+ torch.mean(pixel_grad15) \
|
297 |
+
+ torch.mean(pixel_grad16) \
|
298 |
+
+ torch.mean(pixel_grad17) \
|
299 |
+
+ torch.mean(pixel_grad18) \
|
300 |
+
+ torch.mean(pixel_grad19) \
|
301 |
+
+ torch.mean(pixel_grad20) \
|
302 |
+
+ torch.mean(pixel_grad21) \
|
303 |
+
+ torch.mean(pixel_grad22) \
|
304 |
+
+ torch.mean(pixel_grad23) \
|
305 |
+
+ torch.mean(pixel_grad24)
|
306 |
+
|
307 |
+
total_term = ReguTerm1
|
308 |
+
return total_term
|
309 |
+
|