-
Notifications
You must be signed in to change notification settings - Fork 1
/
loss.py
206 lines (173 loc) · 7.86 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
def conv3x3(in_channels, out_channels, stride=1):
return nn.Conv2d(in_channels, out_channels, kernel_size=3,
stride=stride, padding=1, bias=True)
class Discriminator(nn.Module):
def __init__(self, in_size=160):
super(Discriminator, self).__init__()
self.conv1 = conv3x3(3, 32)
self.LReLU1 = nn.LeakyReLU(0.2)
self.conv2 = conv3x3(32, 32, 2)
self.LReLU2 = nn.LeakyReLU(0.2)
self.conv3 = conv3x3(32, 64)
self.LReLU3 = nn.LeakyReLU(0.2)
self.conv4 = conv3x3(64, 64, 2)
self.LReLU4 = nn.LeakyReLU(0.2)
self.conv5 = conv3x3(64, 128)
self.LReLU5 = nn.LeakyReLU(0.2)
self.conv6 = conv3x3(128, 128, 2)
self.LReLU6 = nn.LeakyReLU(0.2)
self.conv7 = conv3x3(128, 256)
self.LReLU7 = nn.LeakyReLU(0.2)
self.conv8 = conv3x3(256, 256, 2)
self.LReLU8 = nn.LeakyReLU(0.2)
self.conv9 = conv3x3(256, 512)
self.LReLU9 = nn.LeakyReLU(0.2)
self.conv10 = conv3x3(512, 512, 2)
self.LReLU10 = nn.LeakyReLU(0.2)
self.fc1 = nn.Linear(in_size//32 * in_size//32 * 512, 1024)
self.LReLU11 = nn.LeakyReLU(0.2)
self.fc2 = nn.Linear(1024, 1)
def forward(self, x):
x = self.LReLU1(self.conv1(x))
x = self.LReLU2(self.conv2(x))
x = self.LReLU3(self.conv3(x))
x = self.LReLU4(self.conv4(x))
x = self.LReLU5(self.conv5(x))
x = self.LReLU6(self.conv6(x))
x = self.LReLU7(self.conv7(x))
x = self.LReLU8(self.conv8(x))
x = self.LReLU9(self.conv9(x))
x = self.LReLU10(self.conv10(x))
x = x.view(x.size(0), -1)
x = self.LReLU11(self.fc1(x))
x = self.fc2(x)
return x
class ReconstructionLoss(nn.Module):
def __init__(self, type='l1'):
super(ReconstructionLoss, self).__init__()
if (type == 'l1'):
self.loss = nn.L1Loss()
elif (type == 'l2'):
self.loss = nn.MSELoss()
def forward(self, sr, hr):
return self.loss(sr, hr)
class PerceptualLoss(nn.Module):
def __init__(self):
super(PerceptualLoss, self).__init__()
def forward(self, sr_relu5_1, hr_relu5_1):
loss = F.mse_loss(sr_relu5_1, hr_relu5_1)
return loss
class TPerceptualLoss(nn.Module):
def __init__(self, use_S=True, type='l2'):
super(TPerceptualLoss, self).__init__()
self.use_S = use_S
self.type = type
def gram_matrix(self, x):
b, ch, h, w = x.size()
f = x.view(b, ch, h*w)
f_T = f.transpose(1, 2)
G = f.bmm(f_T) / (h * w * ch)
return G
def forward(self, map_lv3, map_lv2, map_lv1, S, T_lv3, T_lv2, T_lv1):
### S.size(): [N, 1, h, w]
if (self.use_S):
S_lv3 = torch.sigmoid(S)
S_lv2 = torch.sigmoid(F.interpolate(S, size=(S.size(-2)*2, S.size(-1)*2), mode='bicubic'))
S_lv1 = torch.sigmoid(F.interpolate(S, size=(S.size(-2)*4, S.size(-1)*4), mode='bicubic'))
else:
S_lv3, S_lv2, S_lv1 = 1., 1., 1.
if (self.type == 'l1'):
loss_texture = F.l1_loss(map_lv3 * S_lv3, T_lv3 * S_lv3)
loss_texture += F.l1_loss(map_lv2 * S_lv2, T_lv2 * S_lv2)
loss_texture += F.l1_loss(map_lv1 * S_lv1, T_lv1 * S_lv1)
loss_texture /= 3.
elif (self.type == 'l2'):
loss_texture = F.mse_loss(map_lv3 * S_lv3, T_lv3 * S_lv3)
loss_texture += F.mse_loss(map_lv2 * S_lv2, T_lv2 * S_lv2)
loss_texture += F.mse_loss(map_lv1 * S_lv1, T_lv1 * S_lv1)
loss_texture /= 3.
return loss_texture
class AdversarialLoss(nn.Module):
def __init__(self, use_cpu=False, num_gpu=1, gan_type='WGAN_GP', gan_k=1,
lr_dis=1e-4, train_crop_size=40):
super(AdversarialLoss, self).__init__()
self.gan_type = gan_type
self.gan_k = gan_k
self.device = torch.device('cpu' if use_cpu else 'cuda')
self.discriminator = discriminator.Discriminator(train_crop_size*4).to(self.device)
if (num_gpu > 1):
self.discriminator = nn.DataParallel(self.discriminator, list(range(num_gpu)))
if (gan_type in ['WGAN_GP', 'GAN']):
self.optimizer = optim.Adam(
self.discriminator.parameters(),
betas=(0, 0.9), eps=1e-8, lr=lr_dis
)
else:
raise SystemExit('Error: no such type of GAN!')
self.bce_loss = torch.nn.BCELoss().to(self.device)
# if (D_path):
# self.logger.info('load_D_path: ' + D_path)
# D_state_dict = torch.load(D_path)
# self.discriminator.load_state_dict(D_state_dict['D'])
# self.optimizer.load_state_dict(D_state_dict['D_optim'])
def forward(self, fake, real):
fake_detach = fake.detach()
for _ in range(self.gan_k):
self.optimizer.zero_grad()
d_fake = self.discriminator(fake_detach)
d_real = self.discriminator(real)
if (self.gan_type.find('WGAN') >= 0):
loss_d = (d_fake - d_real).mean()
if self.gan_type.find('GP') >= 0:
epsilon = torch.rand(real.size(0), 1, 1, 1).to(self.device)
epsilon = epsilon.expand(real.size())
hat = fake_detach.mul(1 - epsilon) + real.mul(epsilon)
hat.requires_grad = True
d_hat = self.discriminator(hat)
gradients = torch.autograd.grad(
outputs=d_hat.sum(), inputs=hat,
retain_graph=True, create_graph=True, only_inputs=True
)[0]
gradients = gradients.view(gradients.size(0), -1)
gradient_norm = gradients.norm(2, dim=1)
gradient_penalty = 10 * gradient_norm.sub(1).pow(2).mean()
loss_d += gradient_penalty
elif (self.gan_type == 'GAN'):
valid_score = torch.ones(real.size(0), 1).to(self.device)
fake_score = torch.zeros(real.size(0), 1).to(self.device)
real_loss = self.bce_loss(torch.sigmoid(d_real), valid_score)
fake_loss = self.bce_loss(torch.sigmoid(d_fake), fake_score)
loss_d = (real_loss + fake_loss) / 2.
# Discriminator update
loss_d.backward()
self.optimizer.step()
d_fake_for_g = self.discriminator(fake)
if (self.gan_type.find('WGAN') >= 0):
loss_g = -d_fake_for_g.mean()
elif (self.gan_type == 'GAN'):
loss_g = self.bce_loss(torch.sigmoid(d_fake_for_g), valid_score)
# Generator loss
return loss_g
def state_dict(self):
D_state_dict = self.discriminator.state_dict()
D_optim_state_dict = self.optimizer.state_dict()
return D_state_dict, D_optim_state_dict
def get_loss_dict(args):
loss = {}
if (abs(args.rec_w - 0) <= 1e-8):
raise SystemExit('NotImplementError: ReconstructionLoss must exist!')
else:
loss['rec_loss'] = ReconstructionLoss(type='l1')
if (abs(args.per_w - 0) > 1e-8):
loss['per_loss'] = PerceptualLoss()
if (abs(args.tpl_w - 0) > 1e-8):
loss['tpl_loss'] = TPerceptualLoss(use_S=args.tpl_use_S, type=args.tpl_type)
if (abs(args.adv_w - 0) > 1e-8):
loss['adv_loss'] = AdversarialLoss( use_cpu=args.cpu, num_gpu=args.num_gpu,
gan_type=args.GAN_type, gan_k=args.GAN_k, lr_dis=args.lr_rate_dis,
train_crop_size=args.train_crop_size)
return loss