-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet.py
368 lines (304 loc) · 15.2 KB
/
net.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
from ops.spectral_norm import spectral_norm as SpectralNorm
# Defines the GAN loss which uses either LSGAN or the regular GAN.
# When LSGAN is used, it is basically same as MSELoss,
# but it abstracts away the need to create the target label tensor
# that has the same size as the input
class Discriminator(nn.Module):
"""Discriminator. PatchGAN."""
def __init__(self, image_size=128, conv_dim=64, repeat_num=3, norm='SN'):
super(Discriminator, self).__init__()
layers = []
if norm == 'SN':
layers.append(SpectralNorm(nn.Conv2d(3, conv_dim, kernel_size=4, stride=2, padding=1)))
else:
layers.append(nn.Conv2d(3, conv_dim, kernel_size=4, stride=2, padding=1))
layers.append(nn.LeakyReLU(0.01, inplace=True))
curr_dim = conv_dim
for i in range(1, repeat_num):
if norm == 'SN':
layers.append(SpectralNorm(nn.Conv2d(curr_dim, curr_dim * 2, kernel_size=4, stride=2, padding=1)))
else:
layers.append(nn.Conv2d(curr_dim, curr_dim * 2, kernel_size=4, stride=2, padding=1))
layers.append(nn.LeakyReLU(0.01, inplace=True))
curr_dim = curr_dim * 2
# k_size = int(image_size / np.power(2, repeat_num))
if norm == 'SN':
layers.append(SpectralNorm(nn.Conv2d(curr_dim, curr_dim * 2, kernel_size=4, stride=1, padding=1)))
else:
layers.append(nn.Conv2d(curr_dim, curr_dim * 2, kernel_size=4, stride=1, padding=1))
layers.append(nn.LeakyReLU(0.01, inplace=True))
curr_dim = curr_dim * 2
self.main = nn.Sequential(*layers)
if norm == 'SN':
self.conv1 = SpectralNorm(nn.Conv2d(curr_dim, 1, kernel_size=4, stride=1, padding=1, bias=False))
else:
self.conv1 = nn.Conv2d(curr_dim, 1, kernel_size=4, stride=1, padding=1, bias=False)
# conv1 remain the last square size, 256*256-->30*30
# self.conv2 = SpectralNorm(nn.Conv2d(curr_dim, 1, kernel_size=k_size, bias=False))
# conv2 output a single number
def forward(self, x):
h = self.main(x)
out_makeup = self.conv1(h)
return out_makeup.squeeze()
class VGG(nn.Module):
def __init__(self, pool='max'):
super(VGG, self).__init__()
# vgg modules
self.conv1_1 = nn.Conv2d(3, 64, kernel_size=3, padding=1)
self.conv1_2 = nn.Conv2d(64, 64, kernel_size=3, padding=1)
self.conv2_1 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
self.conv2_2 = nn.Conv2d(128, 128, kernel_size=3, padding=1)
self.conv3_1 = nn.Conv2d(128, 256, kernel_size=3, padding=1)
self.conv3_2 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.conv3_3 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.conv3_4 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.conv4_1 = nn.Conv2d(256, 512, kernel_size=3, padding=1)
self.conv4_2 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.conv4_3 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.conv4_4 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.conv5_1 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.conv5_2 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.conv5_3 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
self.conv5_4 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
if pool == 'max':
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2)
self.pool5 = nn.MaxPool2d(kernel_size=2, stride=2)
elif pool == 'avg':
self.pool1 = nn.AvgPool2d(kernel_size=2, stride=2)
self.pool2 = nn.AvgPool2d(kernel_size=2, stride=2)
self.pool3 = nn.AvgPool2d(kernel_size=2, stride=2)
self.pool4 = nn.AvgPool2d(kernel_size=2, stride=2)
self.pool5 = nn.AvgPool2d(kernel_size=2, stride=2)
def forward(self, x, out_keys):
out = {'r11': F.relu(self.conv1_1(x))}
out['r12'] = F.relu(self.conv1_2(out['r11']))
out['p1'] = self.pool1(out['r12'])
out['r21'] = F.relu(self.conv2_1(out['p1']))
out['r22'] = F.relu(self.conv2_2(out['r21']))
out['p2'] = self.pool2(out['r22'])
out['r31'] = F.relu(self.conv3_1(out['p2']))
out['r32'] = F.relu(self.conv3_2(out['r31']))
out['r33'] = F.relu(self.conv3_3(out['r32']))
out['r34'] = F.relu(self.conv3_4(out['r33']))
out['p3'] = self.pool3(out['r34'])
out['r41'] = F.relu(self.conv4_1(out['p3']))
out['r42'] = F.relu(self.conv4_2(out['r41']))
out['r43'] = F.relu(self.conv4_3(out['r42']))
out['r44'] = F.relu(self.conv4_4(out['r43']))
out['p4'] = self.pool4(out['r44'])
out['r51'] = F.relu(self.conv5_1(out['p4']))
out['r52'] = F.relu(self.conv5_2(out['r51']))
out['r53'] = F.relu(self.conv5_3(out['r52']))
out['r54'] = F.relu(self.conv5_4(out['r53']))
out['p5'] = self.pool5(out['r54'])
return [out[key] for key in out_keys]
class ResidualBlock(nn.Module):
"""Residual Block."""
def __init__(self, dim_in, dim_out, net_mode=None):
if net_mode == 'p' or (net_mode is None):
use_affine = True
elif net_mode == 't':
use_affine = False
super(ResidualBlock, self).__init__()
self.main = nn.Sequential(
nn.Conv2d(dim_in, dim_out, kernel_size=3, stride=1, padding=1, bias=False),
nn.InstanceNorm2d(dim_out, affine=use_affine),
nn.ReLU(inplace=True),
nn.Conv2d(dim_out, dim_out, kernel_size=3, stride=1, padding=1, bias=False),
nn.InstanceNorm2d(dim_out, affine=use_affine)
)
def forward(self, x):
return x + self.main(x)
class GetMatrix(nn.Module):
def __init__(self, dim_in, dim_out):
super(GetMatrix, self).__init__()
self.get_gamma = nn.Conv2d(dim_in, dim_out, kernel_size=1, stride=1, padding=0, bias=False)
self.get_beta = nn.Conv2d(dim_in, dim_out, kernel_size=1, stride=1, padding=0, bias=False)
def forward(self, x):
gamma = self.get_gamma(x)
beta = self.get_beta(x)
return x, gamma, beta
class NONLocalBlock2D(nn.Module):
def __init__(self):
super(NONLocalBlock2D, self).__init__()
self.g = nn.Conv2d(in_channels=1, out_channels=1,
kernel_size=1, stride=1, padding=0)
def forward(self, source, weight):
"""(b, c, h, w)
src_diff: (3, 136, 32, 32)
"""
batch_size = source.size(0)
g_source = source.view(batch_size, 1, -1) # (N, C, H*W)
g_source = g_source.permute(0, 2, 1) # (N, H*W, C)
y = torch.bmm(weight.to_dense(), g_source)
y = y.permute(0, 2, 1).contiguous() # (N, C, H*W)
y = y.view(batch_size, 1, *source.size()[2:])
return y
class Generator(nn.Module):
"""Generator. Encoder-Decoder Architecture."""
def __init__(self):
super(Generator, self).__init__()
# -------------------------- PNet(MDNet) for obtaining makeup matrices --------------------------
layers = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, stride=1, padding=3, bias=False),
nn.InstanceNorm2d(64, affine=True),
nn.ReLU(inplace=True)
)
self.pnet_in = layers
# Down-Sampling
curr_dim = 64
for i in range(2):
layers = nn.Sequential(
nn.Conv2d(curr_dim, curr_dim * 2, kernel_size=4, stride=2, padding=1, bias=False),
nn.InstanceNorm2d(curr_dim * 2, affine=True),
nn.ReLU(inplace=True),
)
setattr(self, f'pnet_down_{i + 1}', layers)
curr_dim = curr_dim * 2
# Bottleneck. All bottlenecks share the same attention module
self.atten_bottleneck_g = NONLocalBlock2D()
self.atten_bottleneck_b = NONLocalBlock2D()
self.simple_spade = GetMatrix(curr_dim, 1) # get the makeup matrix
for i in range(3):
setattr(self, f'pnet_bottleneck_{i + 1}', ResidualBlock(dim_in=curr_dim, dim_out=curr_dim, net_mode='p'))
# --------------------------- TNet(MANet) for applying makeup transfer ----------------------------
self.tnet_in_conv = nn.Conv2d(3, 64, kernel_size=7, stride=1, padding=3, bias=False)
self.tnet_in_spade = nn.InstanceNorm2d(64, affine=False)
self.tnet_in_relu = nn.ReLU(inplace=True)
# Down-Sampling
curr_dim = 64
for i in range(2):
setattr(self, f'tnet_down_conv_{i + 1}',
nn.Conv2d(curr_dim, curr_dim * 2, kernel_size=4, stride=2, padding=1, bias=False))
setattr(self, f'tnet_down_spade_{i + 1}', nn.InstanceNorm2d(curr_dim * 2, affine=False))
setattr(self, f'tnet_down_relu_{i + 1}', nn.ReLU(inplace=True))
curr_dim = curr_dim * 2
# Bottleneck
for i in range(6):
setattr(self, f'tnet_bottleneck_{i + 1}', ResidualBlock(dim_in=curr_dim, dim_out=curr_dim, net_mode='t'))
# Up-Sampling
for i in range(2):
setattr(self, f'tnet_up_conv_{i + 1}',
nn.ConvTranspose2d(curr_dim, curr_dim // 2, kernel_size=4, stride=2, padding=1, bias=False))
setattr(self, f'tnet_up_spade_{i + 1}', nn.InstanceNorm2d(curr_dim // 2, affine=False))
setattr(self, f'tnet_up_relu_{i + 1}', nn.ReLU(inplace=True))
curr_dim = curr_dim // 2
layers = nn.Sequential(
nn.Conv2d(curr_dim, 3, kernel_size=7, stride=1, padding=3, bias=False),
nn.Tanh()
)
self.tnet_out = layers
@staticmethod
def atten_feature(mask_s, weight, gamma_s, beta_s, atten_module_g, atten_module_b):
"""
feature size: (1, c, h, w)
mask_c(s): (3, 1, h, w)
diff_c: (1, 138, 256, 256)
return: (1, c, h, w)
"""
channel_num = gamma_s.shape[1]
mask_s_re = F.interpolate(mask_s, size=gamma_s.shape[2:]).repeat(1, channel_num, 1, 1)
gamma_s_re = gamma_s.repeat(3, 1, 1, 1)
gamma_s = gamma_s_re * mask_s_re # (3, c, h, w)
beta_s_re = beta_s.repeat(3, 1, 1, 1)
beta_s = beta_s_re * mask_s_re
gamma = atten_module_g(gamma_s, weight) # (3, c, h, w)
beta = atten_module_b(beta_s, weight)
gamma = (gamma[0] + gamma[1] + gamma[2]).unsqueeze(0) # (c, h, w) combine the three parts
beta = (beta[0] + beta[1] + beta[2]).unsqueeze(0)
return gamma, beta
@staticmethod
def get_weight(mask_c, mask_s, fea_c, fea_s, diff_c, diff_s, mode='train'):
""" s --> source; c --> target
feature size: (1, 256, 64, 64)
diff: (3, 136, 32, 32)
"""
HW = 64 * 64
batch_size = 3
assert fea_s is not None # fea_s when i==3
# get 3 part fea using mask
channel_num = fea_s.shape[1]
mask_c_re = F.interpolate(mask_c, size=64).repeat(1, channel_num, 1, 1) # (3, c, h, w)
fea_c = fea_c.repeat(3, 1, 1, 1) # (3, c, h, w)
# 这里的mask难道是分开三个区域?为了论文中公式的同一个facial region
fea_c = fea_c * mask_c_re # (3, c, h, w) 3 stands for 3 parts
mask_s_re = F.interpolate(mask_s, size=64).repeat(1, channel_num, 1, 1)
fea_s = fea_s.repeat(3, 1, 1, 1)
fea_s = fea_s * mask_s_re
theta_input = torch.cat((fea_c * 0.01, diff_c), dim=1)
phi_input = torch.cat((fea_s * 0.01, diff_s), dim=1)
theta_target = theta_input.view(batch_size, -1, HW) # (N, C+136, H*W)
theta_target = theta_target.permute(0, 2, 1) # (N, H*W, C+136)
phi_source = phi_input.view(batch_size, -1, HW) # (N, C+136, H*W)
weight = torch.bmm(theta_target, phi_source) # (3, HW, HW)
if mode == 'train':
weight = weight.cpu()
weight_ind = torch.LongTensor(weight.detach().numpy().nonzero())
weight = weight.cuda()
weight_ind = weight_ind.cuda()
else:
weight_ind = torch.LongTensor(weight.numpy().nonzero())
weight *= 200 # hyper parameters for visual feature
weight = F.softmax(weight, dim=-1)
weight = weight[weight_ind[0], weight_ind[1], weight_ind[2]]
return torch.sparse.FloatTensor(weight_ind, weight, torch.Size([3, HW, HW]))
def forward_atten(self, c, s, mask_c, mask_s, diff_c, diff_s, gamma=None, beta=None, ret=False, mode='train'):
"""attention version
c: content, stands for source image. shape: (b, c, h, w)
s: style, stands for reference image. shape: (b, c, h, w)
mask_list_c: lip, skin, eye. (b, 1, h, w)
"""
# forward c in tnet(MANet)
c_tnet = self.tnet_in_conv(c)
s = self.pnet_in(s)
c_tnet = self.tnet_in_spade(c_tnet)
c_tnet = self.tnet_in_relu(c_tnet)
# down-sampling
for i in range(2):
if gamma is None:
cur_pnet_down = getattr(self, f'pnet_down_{i + 1}')
s = cur_pnet_down(s)
cur_tnet_down_conv = getattr(self, f'tnet_down_conv_{i + 1}')
cur_tnet_down_spade = getattr(self, f'tnet_down_spade_{i + 1}')
cur_tnet_down_relu = getattr(self, f'tnet_down_relu_{i + 1}')
c_tnet = cur_tnet_down_conv(c_tnet)
c_tnet = cur_tnet_down_spade(c_tnet)
c_tnet = cur_tnet_down_relu(c_tnet)
# bottleneck
for i in range(6):
if gamma is None and i <= 2:
cur_pnet_bottleneck = getattr(self, f'pnet_bottleneck_{i + 1}')
cur_tnet_bottleneck = getattr(self, f'tnet_bottleneck_{i + 1}')
# get s_pnet from p and transform
if i == 3:
if gamma is None: # not in test_mix
s, gamma, beta = self.simple_spade(s)
weight = self.get_weight(mask_c, mask_s, c_tnet, s, diff_c, diff_s, mode)
gamma, beta = self.atten_feature(mask_s, weight, gamma, beta, self.atten_bottleneck_g,
self.atten_bottleneck_b)
if ret:
return [gamma, beta]
# else: # in test mode
# gamma, beta = param_A[0]*w + param_B[0]*(1-w), param_A[1]*w + param_B[1]*(1-w)
c_tnet = c_tnet * (1 + gamma) + beta # apply makeup transfer using makeup matrices
if gamma is None and i <= 2:
s = cur_pnet_bottleneck(s)
c_tnet = cur_tnet_bottleneck(c_tnet)
# up-sampling
for i in range(2):
cur_tnet_up_conv = getattr(self, f'tnet_up_conv_{i + 1}')
cur_tnet_up_spade = getattr(self, f'tnet_up_spade_{i + 1}')
cur_tnet_up_relu = getattr(self, f'tnet_up_relu_{i + 1}')
c_tnet = cur_tnet_up_conv(c_tnet)
c_tnet = cur_tnet_up_spade(c_tnet)
c_tnet = cur_tnet_up_relu(c_tnet)
c_tnet = self.tnet_out(c_tnet)
return c_tnet