-
Notifications
You must be signed in to change notification settings - Fork 5
/
net.py
259 lines (213 loc) · 8.81 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
import torch.nn as nn
import torch
from function import normal
from function import calc_mean_std
import scipy.stats as stats
from torchvision.utils import save_image
import random
decoder = nn.Sequential(
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 256, (3, 3)),
nn.ReLU(),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 128, (3, 3)),
nn.ReLU(),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 128, (3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 64, (3, 3)),
nn.ReLU(),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 64, (3, 3)),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 3, (3, 3)),
)
vgg = nn.Sequential(
nn.Conv2d(3, 3, (1, 1)),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(3, 64, (3, 3)),
nn.ReLU(), # relu1-1
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 64, (3, 3)),
nn.ReLU(), # relu1-2
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 128, (3, 3)),
nn.ReLU(), # relu2-1
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 128, (3, 3)),
nn.ReLU(), # relu2-2
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 256, (3, 3)),
nn.ReLU(), # relu3-1
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(), # relu3-2
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(), # relu3-3
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, (3, 3)),
nn.ReLU(), # relu3-4
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 512, (3, 3)),
nn.ReLU(), # relu4-1, this is the last layer used
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu4-2
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu4-3
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu4-4
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu5-1
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu5-2
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU(), # relu5-3
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, (3, 3)),
nn.ReLU() # relu5-4
)
class CA(nn.Module):
def __init__(self, in_dim):
super(CA, self).__init__()
self.f = nn.Conv2d(in_dim , in_dim , (1,1))
self.g = nn.Conv2d(in_dim , in_dim , (1,1))
self.h = nn.Conv2d(in_dim , in_dim , (1,1))
self.softmax = nn.Softmax(dim=-1)
self.out_conv = nn.Conv2d(in_dim, in_dim, (1, 1))
def forward(self,content_feat,style_feat):
B,C,H,W = content_feat.size()
F_Fc_norm = self.f(normal(content_feat)).view(B,-1,H*W).permute(0,2,1)
B,C,H,W = style_feat.size()
G_Fs_norm = self.g(normal(style_feat)).view(B,-1,H*W)
energy = torch.bmm(F_Fc_norm,G_Fs_norm)
attention = self.softmax(energy)
H_Fs = self.h(style_feat).view(B,-1,H*W)
out = torch.bmm(H_Fs,attention.permute(0,2,1) )
B,C,H,W = content_feat.size()
out = out.view(B,C,H,W)
out = self.out_conv(out)
out += content_feat
return out
class Style_SA(nn.Module):
def __init__(self, in_dim):
super(Style_SA, self).__init__()
self.f = nn.Conv2d(in_dim , in_dim , (1,1))
self.g = nn.Conv2d(in_dim , in_dim , (1,1))
self.h = nn.Conv2d(in_dim , in_dim , (1,1))
self.softmax = nn.Softmax(dim=-1)
self.out_conv = nn.Conv2d(in_dim, in_dim, (1, 1))
def forward(self,style_feat):
B,C,H,W = style_feat.size()
F_Fc_norm = self.f(style_feat).view(B,-1,H*W)
B,C,H,W = style_feat.size()
G_Fs_norm = self.g(style_feat).view(B,-1,H*W).permute(0,2,1)
energy = torch.bmm(F_Fc_norm,G_Fs_norm)
attention = self.softmax(energy)
H_Fs = self.h(normal(style_feat)).view(B,-1,H*W)
out = torch.bmm(attention.permute(0,2,1), H_Fs)
out = out.view(B,C,H,W)
out = self.out_conv(out)
out += style_feat
return out
class Content_SA(nn.Module):
def __init__(self, in_dim):
super(Content_SA, self).__init__()
self.f = nn.Conv2d(in_dim , in_dim , (1,1))
self.g = nn.Conv2d(in_dim , in_dim , (1,1))
self.h = nn.Conv2d(in_dim , in_dim , (1,1))
self.softmax = nn.Softmax(dim=-1)
self.out_conv = nn.Conv2d(in_dim, in_dim, (1, 1))
def forward(self,content_feat):
B,C,H,W = content_feat.size()
F_Fc_norm = self.f(normal(content_feat)).view(B,-1,H*W).permute(0,2,1)
B,C,H,W = content_feat.size()
G_Fs_norm = self.g(normal(content_feat)).view(B,-1,H*W)
energy = torch.bmm(F_Fc_norm,G_Fs_norm)
attention = self.softmax(energy)
H_Fs = self.h(content_feat).view(B,-1,H*W)
out = torch.bmm(H_Fs,attention.permute(0,2,1) )
B,C,H,W = content_feat.size()
out = out.view(B,C,H,W)
out = self.out_conv(out)
out += content_feat
return out
class Multi_Adaptation_Module(nn.Module):
def __init__(self, in_dim):
super(Multi_Adaptation_Module, self).__init__()
self.CA=CA(in_dim)
self.CSA=Content_SA(in_dim)
self.SSA=Style_SA(in_dim)
def forward(self, content_feats, style_feats):
content_feat = self.CSA(content_feats[-2])
style_feat = self.SSA(style_feats[-2])
Fcsc = self.CA(content_feat, style_feat)
return Fcsc
class Net(nn.Module):
def __init__(self, encoder, decoder):
super(Net, self).__init__()
enc_layers = list(encoder.children())
self.enc_1 = nn.Sequential(*enc_layers[:4]) # input -> relu1_1
self.enc_2 = nn.Sequential(*enc_layers[4:11]) # relu1_1 -> relu2_1
self.enc_3 = nn.Sequential(*enc_layers[11:18]) # relu2_1 -> relu3_1
self.enc_4 = nn.Sequential(*enc_layers[18:31]) # relu3_1 -> relu4_1
self.enc_5 = nn.Sequential(*enc_layers[31:44]) # relu4_1 -> relu5_1
#transform
self.ma_module = Multi_Adaptation_Module(512)
self.decoder = decoder
self.mse_loss = nn.MSELoss()
# fix the encoder
for name in ['enc_1', 'enc_2', 'enc_3', 'enc_4', 'enc_5']:
for param in getattr(self, name).parameters():
param.requires_grad = False
# extract relu1_1, relu2_1, relu3_1, relu4_1, relu5_1 from input image
def encode_with_intermediate(self, input):
results = [input]
for i in range(5):
func = getattr(self, 'enc_{:d}'.format(i + 1))
results.append(func(results[-1]))
return results[1:]
def forward(self, content, content1, style, style1):
#print(content.size())
style_feats = self.encode_with_intermediate(style)
content_feats = self.encode_with_intermediate(content)
style_feats1 = self.encode_with_intermediate(style1)
content_feats1 = self.encode_with_intermediate(content1)
Ics = self.decoder(self.ma_module(content_feats, style_feats))
Ics_feats = self.encode_with_intermediate(Ics)
# Content loss
Ics1 = self.decoder(self.ma_module(content_feats, style_feats1))
Ics1_feats = self.encode_with_intermediate(Ics1)
Ic1s = self.decoder(self.ma_module(content_feats1, style_feats))
Ic1s_feats = self.encode_with_intermediate(Ic1s)
#Identity losses lambda 1
Icc = self.decoder(self.ma_module(content_feats, content_feats))
Iss = self.decoder(self.ma_module(style_feats, style_feats))
#Identity losses lambda 2
Icc_feats=self.encode_with_intermediate(Icc)
Iss_feats=self.encode_with_intermediate(Iss)
return style_feats, content_feats, style_feats1, content_feats1 ,Ics_feats,Ics1_feats,Ic1s_feats,Icc,Iss,Icc_feats,Iss_feats