-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.py
186 lines (133 loc) · 5.07 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
# -*- coding: utf-8 -*-
from torch import nn
from utils import *
import ipdb
vgg = nn.Sequential(
nn.Conv2d(3, 3, kernel_size=1, stride=1, padding=0),
nn.ReflectionPad2d(1),
nn.Conv2d(3, 64, 3, 1),
nn.ReLU(), # ReLU1_1 :4
nn.ReflectionPad2d(1),
nn.Conv2d(64, 64, 3, 1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
nn.ReflectionPad2d(1),
nn.Conv2d(64, 128, 3, 1),
nn.ReLU(), # ReLU2_1 4:11
nn.ReflectionPad2d(1),
nn.Conv2d(128, 128, 3, 1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
nn.ReflectionPad2d(1),
nn.Conv2d(128, 256, 3, 1),
nn.ReLU(), # ReLU3_1 11:18
nn.ReflectionPad2d(1),
nn.Conv2d(256, 256, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(256, 256, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(256, 256, 3, 1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
nn.ReflectionPad2d(1),
nn.Conv2d(256, 512, 3, 1),
nn.ReLU(), # ReLU4_1 this is the last layer used 18:31
nn.ReflectionPad2d(1),
nn.Conv2d(512, 512, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(512, 512, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(512, 512, 3, 1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
nn.ReflectionPad2d(1),
nn.Conv2d(512, 512, 3, 1),
nn.ReLU(), # ReLU5_1
nn.ReflectionPad2d(1),
nn.Conv2d(512, 512, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(512, 512, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(512, 512, 3, 1),
nn.ReLU(),
)
decoder = nn.Sequential(
nn.ReflectionPad2d(1),
nn.Conv2d(512, 256, 3, 1),
nn.ReLU(),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.ReflectionPad2d(1),
nn.Conv2d(256, 256, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(256, 256, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(256, 256, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(256, 128, 3, 1),
nn.ReLU(),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.ReflectionPad2d(1),
nn.Conv2d(128, 128, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(128, 64, 3, 1),
nn.ReLU(),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.ReflectionPad2d(1),
nn.Conv2d(64, 64, 3, 1),
nn.ReLU(),
nn.ReflectionPad2d(1),
nn.Conv2d(64, 3, 3, 1),
nn.ReLU(),
)
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.decoder = decoder
self.criterion = nn.MSELoss(size_average=False, reduce=True)
def encode_with_intermediate(self, input):
results = [input] # result[0] = input
for i in range(4):
func = getattr(self, 'enc_{:d}'.format(i+1))
results.append(func(results[-1]))
return results[1:] # ReLU1_1, ReLU2_1, ReLU3_1, ReLu4_1
def calc_content_loss(self, input, target): # f(g(t)) & t
assert(input.size() == target.size())
assert(target.requires_grad is False)
L_c = self.criterion(input, target)
return L_c
def calc_style_loss(self, input, target): # \phi_i(g(t)) & \phi_i(s)
assert(input.size() == target.size())
assert(target.requires_grad is False)
input_mean, input_std = calc_mean_std(input)
target_mean, target_std = calc_mean_std(target)
L_s = self.criterion(input_mean, target_mean) + self.criterion(input_std, target_std)
return L_s
def forward(self, content, style, args):
assert( 0 <= args.alpha <= 1)
content_f = self.encode_with_intermediate(content)[-1]
style_fs = self.encode_with_intermediate(style)
t = adaptive_instance_normalization(content_f, style_fs[-1])
t = (1 - args.alpha) * content_f + args.alpha * t
g_t = self.decoder(t)
g_t_fs = self.encode_with_intermediate(g_t)
# ipdb.set_trace()
L_c = self.calc_content_loss(g_t_fs[-1], t)
L_s = self.calc_style_loss(g_t_fs[0], style_fs[0])
for i in range(3):
L_s += self.calc_style_loss(g_t_fs[i+1], style_fs[i+1])
return L_c, L_s