-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvgg_net.py
212 lines (190 loc) · 7.58 KB
/
vgg_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
import torch.nn as nn
import torch
from function import calc_mean_std
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 Net(nn.Module):
def __init__(self, encoder):
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.mse_loss = nn.MSELoss()
# fix the encoder
for name in ['enc_1', 'enc_2', 'enc_3', 'enc_4']:
for param in getattr(self, name).parameters():
param.requires_grad = False
# extract relu1_1, relu2_1, relu3_1, relu4_1 from input image
def encode_with_intermediate(self, input):
results = [input]
for i in range(4):
func = getattr(self, 'enc_{:d}'.format(i + 1))
results.append(func(results[-1]))
return results[1:]
# extract relu4_1 from input image
def encode(self, input):
for i in range(4):
input = getattr(self, 'enc_{:d}'.format(i + 1))(input)
return input
def calc_content_loss(self, input, target):
assert (input.size() == target.size())
assert (target.requires_grad is False)
return self.mse_loss(input, target)
def calc_style_loss(self, input, target):
assert (input.size() == target.size())
assert (target.requires_grad is False)
bs, ch = input.size()[:2]
input = input.view(bs, ch, -1)
target = target.view(bs, ch, -1)
# input_mean, input_std,input_p3 = feature_moments_caculation(input)
# target_mean, target_std,target_p3 = feature_moments_caculation(target)
input_mean, input_std = feature_moments_caculation(input)
target_mean, target_std = feature_moments_caculation(target)
return self.mse_loss(input_mean, target_mean) + \
self.mse_loss(input_std, target_std)#+ \
#self.mse_loss(input_p3, target_p3)
def forward(self, content_images, style_images, stylized_images):
style_feats = self.encode_with_intermediate(style_images)#style_images[2, 3, 256, 256];4
content_feat = self.encode(content_images)#content_feat[2, 512, 32, 32]
stylized_feats = self.encode_with_intermediate(stylized_images)
loss_c = self.calc_content_loss(stylized_feats[-1], content_feat)
loss_s = self.calc_style_loss(stylized_feats[0], style_feats[0])
for i in range(1, 4):
loss_s += self.calc_style_loss(stylized_feats[i], style_feats[i])
return loss_c, loss_s
def calc_feat_mean_std(self, input, eps=1e-5):
# eps is a small value added to the variance to avoid divide-by-zero.
size = input.size()
assert (len(size) == 4)
N, C = size[:2]
feat_var = input.view(N, C, -1).var(dim=2) + eps
feat_std = feat_var.sqrt().view(N, C)
feat_mean = input.view(N, C, -1).mean(dim=2).view(N, C)
return feat_mean, feat_std, torch.cat([feat_mean, feat_std], dim = 1)
def get_style_feat(self, input):
style_feats = self.encode_with_intermediate(input)
out_mean = []
out_std = []
out_mean_std = []
for style_feat in style_feats:
style_feat_mean, style_feat_std, style_feat_mean_std = self.calc_feat_mean_std(style_feat)
out_mean.append(style_feat_mean)
out_std.append(style_feat_std)
out_mean_std.append(style_feat_mean_std)
return style_feats, torch.cat(out_mean_std, dim=-1)
def get_hyper_input(self, style):
style_feats = self.encode_with_intermediate(style)
_, _, style_feat_mean_std = self.calc_feat_mean_std(style_feats[-1])
intermediate = self.fc_encoder(style_feat_mean_std)
intermediate_mean = intermediate[:, :512]
return intermediate_mean
def feature_moments_caculation(feat, eps=1e-5):
size = feat.size()
assert (len(size) == 3)
N, C = size[:2]
feat_var = feat.view(N, C, -1).var(dim=2) + eps
# feat_std = feat_var.sqrt().view(N, C, 1, 1)
# the first order
feat_mean = feat.view(N, C, -1).mean(dim=2).view(N, C, 1)
# the second order
feat_size = 2
# N, C = size[:2]
feat_p2 = torch.abs(feat-feat_mean).pow(feat_size).view(N, C, -1)
N, C,L = feat_p2.shape
feat_p2 = feat_p2.sum(dim=2)/L
feat_p2 = feat_p2.pow(1/feat_size).view(N, C, 1)
return feat_mean.view(N, C), feat_p2.view(N, C)
# # the third order
# feat_size = 3
# # N, C = size[:2]
# feat_p3 = torch.abs(feat-feat_mean).pow(feat_size).view(N, C, -1)
# # N, C,L = feat_p3.shape
# feat_p3 = feat_p3.sum(dim=2)/L
# feat_p3 = feat_p3.pow(1/feat_size).view(N, C, 1)
# return feat_mean.view(N, C), feat_p2.view(N, C), feat_p3.view(N, C)