-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_1.py
294 lines (239 loc) · 9.38 KB
/
model_1.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
import torch.nn as nn
import torch.nn.parallel
import torch
import pdb
import torch.nn as nn
import torch.nn.functional as F
from model_ip.light_cnn import LightCNN_29Layers_v2
from model_ip import common
dd = pdb.set_trace
v_siz = 0
z_siz = 256 - v_siz
class FeatureExtractor(nn.Module):
def __init__(self, cnn, feature_layer=11):
super(FeatureExtractor, self).__init__()
self.features = nn.Sequential(*list(cnn.features.children())[:(feature_layer + 1)])
def forward(self, x):
return self.features(x)
class conv_mean_pool(nn.Module):
def __init__(self, inplanes, outplanes):
super(conv_mean_pool, self).__init__()
self.conv = nn.Conv2d(inplanes, outplanes, 3, 1, 1)
self.pooling = nn.AvgPool2d(2)
def forward(self, x):
out = x
out = self.conv(out)
out = self.pooling(out)
return out
class mean_pool_conv(nn.Module):
def __init__(self, inplanes, outplanes):
super(mean_pool_conv, self).__init__()
self.conv = nn.Conv2d(inplanes, outplanes, 3, 1, 1)
self.pooling = nn.AvgPool2d(2)
def forward(self, x):
out = x
out = self.pooling(out)
out = self.conv(out)
return out
class upsample_conv(nn.Module):
def __init__(self, inplanes, outplanes):
super(upsample_conv, self).__init__()
self.upsample = nn.Upsample(scale_factor=2, mode='nearest')
self.conv = nn.Conv2d(inplanes, outplanes, 3, 1, 1)
def forward(self, x):
out = x
out = self.upsample(out)
out = self.conv(out)
return out
class residualBlock_down(nn.Module): # for discriminator, no batchnorm
def __init__(self, inplanes, outplanes):
super(residualBlock_down, self).__init__()
self.conv_shortcut = mean_pool_conv(inplanes, outplanes)
self.conv1 = nn.Conv2d(inplanes, outplanes, 3, 1, 1)
self.conv2 = conv_mean_pool(outplanes, outplanes)
self.ReLU = nn.ReLU()
def forward(self, x):
shortcut = self.conv_shortcut(x)
out = x
out = self.ReLU(out)
out = self.conv1(out)
out = self.ReLU(out)
out = self.conv2(out)
return shortcut + out
class residualBlock_up(nn.Module):
def __init__(self, inplanes, outplanes):
super(residualBlock_up, self).__init__()
self.conv_shortcut = upsample_conv(inplanes, outplanes)
self.conv1 = upsample_conv(inplanes, outplanes)
self.conv2 = nn.Conv2d(outplanes, outplanes, 3, 1, 1)
self.bn1 = nn.BatchNorm2d(inplanes)
self.bn2 = nn.BatchNorm2d(outplanes)
self.ReLU = nn.ReLU()
def forward(self, x):
shortcut = self.conv_shortcut(x)
out = x
out = self.bn1(out)
out = self.ReLU(out)
out = self.conv1(out)
out = self.bn2(out)
out = self.ReLU(out)
out = self.conv2(out)
return shortcut + out
class _G_xvz(nn.Module):
def __init__(self):
super(_G_xvz, self).__init__()
# self.conv = nn.Conv2d(3, 64, 3, 1, 1) 64*64 resolution implementation
self.conv = nn.Conv2d(3, 64, 3, 1, 1) # 3*128*128 --> 64*128*128
self.resBlock0 = residualBlock_down(64, 64) # 64*128*128 --> 64*64*64
self.resBlock1 = residualBlock_down(64, 128)
self.resBlock2 = residualBlock_down(128, 256)
self.resBlock3 = residualBlock_down(256, 512)
self.resBlock4 = residualBlock_down(512, 512)
self.fc_v = nn.Linear(512 * 4 * 4, v_siz)
self.fc_z = nn.Linear(512 * 4 * 4, z_siz)
self.softmax = nn.Softmax()
def forward(self, x):
out = self.conv(x)
out = self.resBlock0(out)
out = self.resBlock1(out)
out = self.resBlock2(out)
out = self.resBlock3(out)
out = self.resBlock4(out)
out = out.view(-1, 512 * 4 * 4)
v = self.fc_v(out)
v = self.softmax(v)
z = self.fc_z(out)
return v, z
"Generator for training 512 Dream features to frontalized face"
class _G_vzx(nn.Module):
def __init__(self):
super(_G_vzx, self).__init__()
self.fc = nn.Linear(512, 4 * 4 * 512)
self.resBlock1 = residualBlock_up(512, 512) # 4*4-->8*8
self.resBlock2 = residualBlock_up(512, 256) # 8*8-->16*16
self.resBlock3 = residualBlock_up(256, 128) # 16*16-->32*32
self.resBlock4 = residualBlock_up(128, 64) # 32*32-->64*64
self.resBlock5 = residualBlock_up(64, 64) # 64*64-->128*128
self.bn = nn.BatchNorm2d(64)
self.ReLU = nn.ReLU()
self.conv = nn.Conv2d(64, 3, 3, 1, 1)
self.tanh = nn.Tanh()
def forward(self, x):
out = self.fc(x) # out: 512*4*4
out = out.view(-1, 512, 4, 4) # (-1, 512, 4,list_test.txt" 4)
out = self.resBlock1(out)
out = self.resBlock2(out)
out = self.resBlock3(out)
out = self.resBlock4(out)
out = self.resBlock5(out)
out = self.bn(out)
out = self.ReLU(out)
out = self.conv(out)
out = self.tanh(out)
return out
class _D_xvs(nn.Module):
def __init__(self):
super(_D_xvs, self).__init__()
self.conv = nn.Conv2d(3, 64, 3, 1, 1) # 3*64*64 --> 64*64*64
# self.conv = nn.Conv2d(3, 64, 7, 2, 3) #3*128*128 --> 64*64*64
self.resBlock0 = residualBlock_down(64, 64)
self.resBlock1 = residualBlock_down(64, 128) # 64*64*64 --> 119*32*32
self.resBlock2 = residualBlock_down(128, 256) # 128*32*32 --> 256*16*16
self.resBlock3 = residualBlock_down(256, 512) # 256*16*16 --> 512*8*8
self.resBlock4 = residualBlock_down(512, 512) # 512*8*8 --> 512*4*4
# self.fc_v = nn.Linear(512*4*4, v_siz)
self.fc_s = nn.Linear(512 * 4 * 4, 1)
self.softmax = nn.Softmax()
def forward(self, x):
x = self.conv(x)
x = self.resBlock0(x)
x = self.resBlock1(x) # 119*32*32
x = self.resBlock2(x)
x = self.resBlock3(x)
x = self.resBlock4(x)
x = x.view(-1, 512 * 4 * 4)
s = self.fc_s(x)
return s
class IP(nn.Module):
def __init__(self):
super(IP, self).__init__()
self.model_recognition = LightCNN_29Layers_v2(num_classes=243)
self.submean = common.MeanShift(rgb_range=1)
for p in self.parameters():
p.requires_grad = False
def forward(self, x):
x = self.submean(x)
x = self.model_recognition(x)
return x
class mfm(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, type=1):
super(mfm, self).__init__()
self.out_channels = out_channels
if type == 1:
self.filter = nn.Conv2d(in_channels, 2 * out_channels, kernel_size=kernel_size, stride=stride,
padding=padding)
else:
self.filter = nn.Linear(in_channels, 2 * out_channels)
def forward(self, x):
x = self.filter(x)
out = torch.split(x, self.out_channels, 1)
return torch.max(out[0], out[1])
class group(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding):
super(group, self).__init__()
self.conv_a = mfm(in_channels, in_channels, 1, 1, 0)
self.conv = mfm(in_channels, out_channels, kernel_size, stride, padding)
def forward(self, x):
x = self.conv_a(x)
x = self.conv(x)
return x
class resblock(nn.Module):
def __init__(self, in_channels, out_channels):
super(resblock, self).__init__()
self.conv1 = mfm(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
self.conv2 = mfm(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
def forward(self, x):
res = x
out = self.conv1(x)
out = self.conv2(out)
out = out + res
return out
class network_29layers_v2(nn.Module):
def __init__(self, block, layers, num_classes=243):
super(network_29layers_v2, self).__init__()
self.conv1 = mfm(3, 48, 5, 1, 2)
self.block1 = self._make_layer(block, layers[0], 48, 48)
self.group1 = group(48, 96, 3, 1, 1)
self.block2 = self._make_layer(block, layers[1], 96, 96)
self.group2 = group(96, 192, 3, 1, 1)
self.block3 = self._make_layer(block, layers[2], 192, 192)
self.group3 = group(192, 128, 3, 1, 1)
self.block4 = self._make_layer(block, layers[3], 128, 128)
self.group4 = group(128, 128, 3, 1, 1)
self.fc = nn.Linear(8 * 8 * 128, 256)
self.fc2 = nn.Linear(256, num_classes, bias=False)
def _make_layer(self, block, num_blocks, in_channels, out_channels):
layers = []
for i in range(0, num_blocks):
layers.append(block(in_channels, out_channels))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = F.max_pool2d(x, 2) + F.avg_pool2d(x, 2)
x = self.block1(x)
x = self.group1(x)
x = F.max_pool2d(x, 2) + F.avg_pool2d(x, 2)
x = self.block2(x)
x = self.group2(x)
x = F.max_pool2d(x, 2) + F.avg_pool2d(x, 2)
x = self.block3(x)
x = self.group3(x)
x = self.block4(x)
x = self.group4(x)
x = F.max_pool2d(x, 2) + F.avg_pool2d(x, 2)
x = x.view(x.size(0), -1)
fc = self.fc(x)
x = F.dropout(fc, training=self.training)
# out = self.fc2_MS(fc)
out = self.fc2(x)
return out, fc