-
Notifications
You must be signed in to change notification settings - Fork 118
/
model.py
193 lines (167 loc) · 5.63 KB
/
model.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
import torch
from torchvision.models.vgg import VGG19_Weights, vgg19
class VGG19(torch.nn.Module):
def __init__(self):
super().__init__()
self.vgg = vgg19(weights=VGG19_Weights.IMAGENET1K_V1).features[:34]
for param in self.vgg.parameters():
param.requires_grad = False
self.register_buffer(
"mean",
torch.tensor([0.485, 0.456, 0.406], requires_grad=False).view(1, 3, 1, 1),
)
self.register_buffer(
"std",
torch.tensor([0.229, 0.224, 0.225], requires_grad=False).view(1, 3, 1, 1),
)
def forward(self, x):
x = (x + 1.0) / 2.0
x = (x - self.mean) / self.std
return self.vgg(x)
class UpSamplingBlock(torch.nn.Module):
def __init__(self, config):
super().__init__()
self.conv = torch.nn.Conv2d(
in_channels=config.n_filters,
out_channels=config.n_filters * 4,
kernel_size=3,
padding=1,
)
self.phase_shift = torch.nn.PixelShuffle(upscale_factor=2)
self.relu = torch.nn.PReLU()
def forward(self, x):
return self.relu(self.phase_shift(self.conv(x)))
class ResidualBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv1 = torch.nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3,
stride=1,
padding=1,
bias=False,
)
self.bn1 = torch.nn.InstanceNorm2d(out_channels)
self.relu1 = torch.nn.PReLU()
self.conv2 = torch.nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3,
stride=1,
padding=1,
bias=False,
)
self.bn2 = torch.nn.InstanceNorm2d(out_channels)
def forward(self, x):
y = self.relu1(self.bn1(self.conv1(x)))
return self.bn2(self.conv2(y)) + x
class Generator(torch.nn.Module):
def __init__(self, config):
super().__init__()
self.neck = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=3, out_channels=config.n_filters, kernel_size=3, padding=1),
torch.nn.PReLU(),
)
self.stem = torch.nn.Sequential(
*[
ResidualBlock(in_channels=config.n_filters, out_channels=config.n_filters)
for _ in range(config.n_layers)
]
)
self.bottleneck = torch.nn.Sequential(
torch.nn.Conv2d(
in_channels=config.n_filters,
out_channels=config.n_filters,
kernel_size=3,
padding=1,
bias=False,
),
torch.nn.InstanceNorm2d(config.n_filters),
)
self.upsampling = torch.nn.Sequential(
UpSamplingBlock(config),
UpSamplingBlock(config),
)
self.head = torch.nn.Sequential(
torch.nn.Conv2d(
in_channels=config.n_filters,
out_channels=3,
kernel_size=3,
padding=1,
),
torch.nn.Tanh(),
)
def forward(self, x):
residual = self.neck(x)
x = self.stem(residual)
x = self.bottleneck(x) + residual
x = self.upsampling(x)
return self.head(x)
class SimpleBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, stride):
super().__init__()
self.conv = torch.nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3,
padding=1,
stride=stride,
bias=False,
)
self.bn = torch.nn.InstanceNorm2d(out_channels)
self.act = torch.nn.LeakyReLU()
def forward(self, x):
return self.act(self.bn(self.conv(x)))
class Discriminator(torch.nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
self.neck = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=3, out_channels=config.n_filters, kernel_size=3, padding=1),
torch.nn.LeakyReLU(negative_slope=0.2),
)
layers = [
SimpleBlock(
in_channels=config.n_filters,
out_channels=config.n_filters,
stride=2,
),
SimpleBlock(
in_channels=config.n_filters,
out_channels=config.n_filters * 2,
stride=1,
),
SimpleBlock(
in_channels=config.n_filters * 2,
out_channels=config.n_filters * 2,
stride=2,
),
SimpleBlock(
in_channels=config.n_filters * 2,
out_channels=config.n_filters * 4,
stride=1,
),
SimpleBlock(
in_channels=config.n_filters * 4,
out_channels=config.n_filters * 4,
stride=2,
),
SimpleBlock(
in_channels=config.n_filters * 4,
out_channels=config.n_filters * 8,
stride=1,
),
SimpleBlock(
in_channels=config.n_filters * 8,
out_channels=config.n_filters * 8,
stride=2,
),
torch.nn.Conv2d(
in_channels=config.n_filters * 8, out_channels=1, kernel_size=1, padding=0, stride=1
),
]
self.stem = torch.nn.Sequential(*layers)
def forward(self, x):
x = self.neck(x)
return self.stem(x)