-
Notifications
You must be signed in to change notification settings - Fork 23
/
loss.py
270 lines (199 loc) · 9.64 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
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
"""
This part of the code is built based on the project:
https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix
"""
import torch
torch.cuda.current_device()
import torch.nn as nn
import utils
import matplotlib.pyplot as plt
import numpy as np
# Decide which device we want to run on
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class MinimumPixelLoss():
def __init__(self, opt=2):
self.criterion = None
if opt == 1:
self.criterion = nn.L1Loss(reduce=False)
elif opt == 2:
self.criterion = nn.MSELoss(reduce=False)
else:
raise NotImplementedError('opt expected to be 1 (L1 loss) or '
'2 (L2 loss) but received %d' % opt)
def forward(self, batch, G_pred1, G_pred2):
target_1 = (batch['gt1']).to(device)
target_2 = (batch['gt2']).to(device)
# compute loss on each img
loss_1 = torch.mean(self.criterion(G_pred1, target_1), dim=[1, 2, 3]) \
+ torch.mean(self.criterion(G_pred2, target_2), dim=[1, 2, 3])
# exchange order and compute loss
loss_2 = torch.mean(self.criterion(G_pred1, target_2), dim=[1, 2, 3]) \
+ torch.mean(self.criterion(G_pred2, target_1), dim=[1, 2, 3])
loss_min = torch.min(loss_1, loss_2)
return torch.mean(loss_min)
class PixelLoss():
def __init__(self, opt=2):
self.criterion = None
if opt == 1:
self.criterion = nn.L1Loss()
elif opt == 2:
self.criterion = nn.MSELoss()
else:
raise NotImplementedError('opt expected to be 1 (L1 loss) or '
'2 (L2 loss) but received %d' % opt)
def forward(self, batch, G_pred1, G_pred2):
target_1 = (batch['gt1']).to(device)
target_2 = (batch['gt2']).to(device)
# compute loss on each img
loss = self.criterion(G_pred1, target_1) + self.criterion(G_pred2, target_2)
return loss
class ExclusionLoss(nn.Module):
def __init__(self, level=3):
"""
Loss on the gradient. based on:
http://openaccess.thecvf.com/content_cvpr_2018/papers/Zhang_Single_Image_Reflection_CVPR_2018_paper.pdf
"""
super(ExclusionLoss, self).__init__()
self.level = level
self.avg_pool = torch.nn.AvgPool2d(2, stride=2).type(torch.cuda.FloatTensor)
self.sigmoid = nn.Sigmoid().type(torch.cuda.FloatTensor)
def get_gradients(self, img1, img2):
gradx_loss = []
grady_loss = []
for l in range(self.level):
gradx1, grady1 = self.compute_gradient(img1)
gradx2, grady2 = self.compute_gradient(img2)
# alphax = 2.0 * torch.mean(torch.abs(gradx1)) / torch.mean(torch.abs(gradx2))
# alphay = 2.0 * torch.mean(torch.abs(grady1)) / torch.mean(torch.abs(grady2))
alphay = 1
alphax = 1
gradx1_s = (self.sigmoid(gradx1) * 2) - 1
grady1_s = (self.sigmoid(grady1) * 2) - 1
gradx2_s = (self.sigmoid(gradx2 * alphax) * 2) - 1
grady2_s = (self.sigmoid(grady2 * alphay) * 2) - 1
# gradx_loss.append(torch.mean(((gradx1_s ** 2) * (gradx2_s ** 2))) ** 0.25)
# grady_loss.append(torch.mean(((grady1_s ** 2) * (grady2_s ** 2))) ** 0.25)
gradx_loss += self._all_comb(gradx1_s, gradx2_s)
grady_loss += self._all_comb(grady1_s, grady2_s)
img1 = self.avg_pool(img1)
img2 = self.avg_pool(img2)
return gradx_loss, grady_loss
def _all_comb(self, grad1_s, grad2_s):
v = []
for i in range(3):
for j in range(3):
v.append(torch.mean(((grad1_s[:, j, :, :] ** 2) * (grad2_s[:, i, :, :] ** 2))) ** 0.25)
return v
def compute_gradient(self, img):
gradx = img[:, :, 1:, :] - img[:, :, :-1, :]
grady = img[:, :, :, 1:] - img[:, :, :, :-1]
return gradx, grady
def forward(self, G_pred1, G_pred2):
img1 = G_pred1.to(device)
img2 = G_pred2.to(device)
gradx_loss, grady_loss = self.get_gradients(img1, img2)
loss_gradxy = sum(gradx_loss) / (self.level * 9) + sum(grady_loss) / (self.level * 9)
return loss_gradxy / 2.0
class KurtosisLoss():
def __init__(self):
return
def kurtosis(self, img):
y = img - torch.mean(img)
a = torch.mean(torch.pow(y, 4))
b = torch.mean(y ** 2) ** 2
k = a / (b + 1e-9)
return k / 50.
def forward(self, G_pred1, G_pred2):
k1 = self.kurtosis(G_pred1)
k2 = self.kurtosis(G_pred2)
loss = k1 + k2
return loss
class GANLoss(nn.Module):
"""Define different GAN objectives.
The GANLoss class abstracts away the need to create the target label tensor
that has the same size as the input.
"""
def __init__(self, gan_mode, target_real_label=1.0, target_fake_label=0.0):
""" Initialize the GANLoss class.
Parameters:
gan_mode (str) - - the type of GAN objective. It currently supports vanilla, lsgan, and wgangp.
target_real_label (bool) - - label for a real image
target_fake_label (bool) - - label of a fake image
Note: Do not use sigmoid as the last layer of Discriminator.
LSGAN needs no sigmoid. vanilla GANs will handle it with BCEWithLogitsLoss.
"""
super(GANLoss, self).__init__()
self.register_buffer('real_label', torch.tensor(target_real_label))
self.register_buffer('fake_label', torch.tensor(target_fake_label))
self.gan_mode = gan_mode
if gan_mode == 'lsgan':
self.loss = nn.MSELoss()
elif gan_mode == 'vanilla':
self.loss = nn.BCEWithLogitsLoss()
elif gan_mode in ['wgangp']:
self.loss = None
else:
raise NotImplementedError('gan mode %s not implemented' % gan_mode)
def get_target_tensor(self, prediction, target_is_real):
"""Create label tensors with the same size as the input.
Parameters:
prediction (tensor) - - tpyically the prediction from a discriminator
target_is_real (bool) - - if the ground truth label is for real images or fake images
Returns:
A label tensor filled with ground truth label, and with the size of the input
"""
if target_is_real:
target_tensor = self.real_label
else:
target_tensor = self.fake_label
return target_tensor.expand_as(prediction)
def __call__(self, prediction, target_is_real):
"""Calculate loss given Discriminator's output and grount truth labels.
Parameters:
prediction (tensor) - - tpyically the prediction output from a discriminator
target_is_real (bool) - - if the ground truth label is for real images or fake images
Returns:
the calculated loss.
"""
if self.gan_mode in ['lsgan', 'vanilla']:
target_tensor = self.get_target_tensor(prediction, target_is_real)
loss = self.loss(prediction, target_tensor)
elif self.gan_mode == 'wgangp':
if target_is_real:
loss = -prediction.mean()
else:
loss = prediction.mean()
return loss
def cal_gradient_penalty(netD, real_data, fake_data, device, type='mixed', constant=1.0, lambda_gp=10.0):
"""Calculate the gradient penalty loss, used in WGAN-GP paper https://arxiv.org/abs/1704.00028
Arguments:
netD (network) -- discriminator network
real_data (tensor array) -- real images
fake_data (tensor array) -- generated images from the generator
device (str) -- GPU / CPU: from torch.device('cuda:{}'.format(self.gpu_ids[0])) if self.gpu_ids else torch.device('cpu')
type (str) -- if we mix real and fake data or not [real | fake | mixed].
constant (float) -- the constant used in formula ( | |gradient||_2 - constant)^2
lambda_gp (float) -- weight for this loss
Returns the gradient penalty loss
"""
if lambda_gp > 0.0:
if type == 'real': # either use real images, fake images, or a linear interpolation of two.
interpolatesv = real_data
elif type == 'fake':
interpolatesv = fake_data
elif type == 'mixed':
alpha = torch.rand(real_data.shape[0], 1, device=device)
alpha = alpha.expand(real_data.shape[0], real_data.nelement() // real_data.shape[0]).contiguous().view(*real_data.shape)
interpolatesv = alpha * real_data + ((1 - alpha) * fake_data)
else:
raise NotImplementedError('{} not implemented'.format(type))
interpolatesv.requires_grad_(True)
disc_interpolates = netD(interpolatesv)
gradients = torch.autograd.grad(outputs=disc_interpolates, inputs=interpolatesv,
grad_outputs=torch.ones(disc_interpolates.size()).to(device),
create_graph=True, retain_graph=True, only_inputs=True)
gradients = gradients[0].view(real_data.size(0), -1) # flat the data
gradient_penalty = (((gradients + 1e-16).norm(2, dim=1) - constant) ** 2).mean() * lambda_gp # added eps
return gradient_penalty, gradients
else:
return 0.0, None