-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
277 lines (229 loc) · 10.2 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
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
# Reference: https://github.com/SiskonEmilia/StyleGAN-PyTorch/blob/master/model.py
# "We feed a dedicated noise image to each layer of the synthesis network. The noise image is broadcasted to all feature maps using learned perfeature scaling factors and then added to the output of the corresponding convolution."
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchtyping import TensorType
# "We initialize all weights of the convolutional, fully-connected, and affine transform layers using N(0; 1).
def generate_latent_vec(
batch_size: int, latent_dim: int = 512,
) -> TensorType["batch_size", "latent_dim"]:
"""Generate a random latent vector $\mathbf{z} \in \mathcal{Z}$.
"""
return torch.randn(batch_size, latent_dim)
class MappingNetwork(nn.Module):
def __init__(self, latent_dim: int = 512, num_layers: int = 8):
"""Mapping network $f$.
"Given a latent code z in the input latent space Z, a non-linear mapping network f : Z ! W first produces w 2 W."
"We set the dimensionality of both spaces to 512, and the mapping f is implemented using an 8-layer MLP.
"The mapping network $f$ consists of 8 layers."
"""
super().__init__()
self.latent_dim = latent_dim
self.num_layers = num_layers
# Fully connected layers with leaky ReLU activations
layers = []
for _ in range(num_layers):
layers.append(nn.Linear(latent_dim, latent_dim))
layers.append(nn.LeakyReLU(negative_slope=0.2))
self.layers = nn.Sequential(*layers)
# self._initialize_weights()
# def _initialize_weights(self):
# """
# Initialize the weights of the fully connected layers.
# StyleGAN uses a scaled He initialization.
# """
# for m in self.mapping:
# if isinstance(m, nn.Linear):
# nn.init.kaiming_normal_(m.weight, a=0.2, nonlinearity='leaky_relu')
# nn.init.zeros_(m.bias)
def forward(self, x):
# Normalize the input latent vector
x = F.normalize(x, dim=1) # L2 normalization of z
return self.layers(x)
class SLinear(nn.Module):
def __init__(self, dim_in, dim_out):
super().__init__()
linear = nn.Linear(dim_in, dim_out)
linear.weight.data.normal_()
linear.bias.data.zero_()
self.linear = quick_scale(linear)
def forward(self, x):
return self.linear(x)
class LearnedAffineTransformation(nn.Module):
"""
"Learned affine transformations then specialize w to styles y = (ys, yb) that control adaptive instance normalization (AdaIN) operations after each convolution layer of the synthesis network $g$."
"""
def __init__(self, dim_latent, channels):
super().__init__()
self.transform = SLinear(dim_latent, channels * 2)
# "the biases associated with ys that we initialize to one"
self.transform.linear.bias.data[: channels] = 1
self.transform.linear.bias.data[channels:] = 0
# "The biases and noise scaling factors are initialized to zero, except for the
# biases associated with ys that we initialize to one."
def forward(self, w):
# Gain scale factor and bias with:
return self.transform(w).unsqueeze(2).unsqueeze(3)
class AdaIn(nn.Module):
"""
adaptive instance normalization
"""
def __init__(self, channels):
super().__init__()
self.norm = nn.InstanceNorm2d(channels)
def forward(self, x, style): # `style`: $y$.
scale, bias = torch.chunk(style, chunks=2, dim=1)
return scale * self.norm(x) + bias
class LearnedPerChannelScalingFactor(nn.Module):
'''
Learned per-channel scale factor, used to scale the noise
'''
def __init__(self, channels):
super().__init__()
self.weight = nn.Parameter(torch.zeros((1, channels, 1, 1))) # zeros??
def forward(self, x):
return self.weight * x
class Block1(nn.Module):
'''
This is the very first block of generator that get the constant value as input
'''
def __init__ (self, latent_dim: int, const_size: int = 4, channels: int = 512):
super().__init__()
self.const = nn.Parameter(torch.zeros(1, channels, const_size, const_size))
# "The constant input in synthesis network is initialized to one."
self.scaling_factor1 = quick_scale(LearnedPerChannelScalingFactor(channels))
self.affine_transform1 = LearnedAffineTransformation(latent_dim, channels)
self.adain = AdaIn(channels)
self.lrelu = nn.LeakyReLU(0.2)
self.conv = SConv2d(channels, channels, 3, padding=1)
self.scaling_factor2 = quick_scale(LearnedPerChannelScalingFactor(channels))
self.affine_transform2 = LearnedAffineTransformation(latent_dim, channels)
# Convolutional layer
def forward(self, w, noise):
# Gaussian Noise: Proxyed by generator
# scaling_factor1 = torch.normal(mean=0,std=torch.ones(self.constant.shape)).cuda()
# scaling_factor2 = torch.normal(mean=0,std=torch.ones(self.constant.shape)).cuda()
x = self.const.repeat(noise.size(0), 1, 1, 1)
x = x + self.scaling_factor1(noise)
x = self.adain(x, style=self.affine_transform1(w))
x = self.lrelu(x)
x = self.conv(x)
x = x + self.scaling_factor2(noise)
# "Gaussian noise is added after each convolution, before evaluating the nonlinearity."
x = self.adain(x, style=self.affine_transform2(w))
x = self.lrelu(x)
return x
class Block2(nn.Module):
'''
This is the general class of style-based convolutional blocks
'''
def __init__ (self, latent_dim: int, in_channels: int, out_channels: int):
super().__init__()
self.conv1 = SConv2d(in_channels, out_channels, 3, padding=1)
self.scaling_factor1 = quick_scale(LearnedPerChannelScalingFactor(out_channels))
self.affine_transform1 = LearnedAffineTransformation(
latent_dim, out_channels,
)
self.adain = AdaIn(out_channels)
self.lrelu = nn.LeakyReLU(0.2)
self.conv2 = SConv2d(out_channels, out_channels, 3, padding=1)
self.scaling_factor2 = quick_scale(LearnedPerChannelScalingFactor(out_channels))
self.affine_transform2 = LearnedAffineTransformation(
latent_dim, out_channels,
)
def forward(self, x, w, noise):
# Upsample: Proxyed by generator
# result = nn.functional.interpolate(x, scale_factor=2, mode='bilinear',
# align_corners=False)
# Conv 3*3
x = self.conv1(x)
# Gaussian Noise: Proxyed by generator
# scaling_factor1 = torch.normal(mean=0,std=torch.ones(result.shape)).cuda()
# scaling_factor2 = torch.normal(mean=0,std=torch.ones(result.shape)).cuda()
# Conv & Norm
x = x + self.scaling_factor1(noise)
x = self.adain(x, style=self.affine_transform1(w))
x = self.lrelu(x)
x = self.conv2(x)
x = x + self.scaling_factor2(noise)
x = self.adain(x, style=self.affine_transform2(w))
x = self.lrelu(x)
return x
class ToRGB(nn.Module):
"""
"The output of the last layer is converted to RGB using a separate 1 × 1 convolution"
"""
def __init__(self, in_channels):
super().__init__()
self.in_channels = in_channels
self.conv = nn.Conv2d(in_channels, 3, kernel_size=1)
def forward(self, x):
x = self.conv(x)
return x
class StyleGANSynthesisNetwork(nn.Module):
"""Synthesis network $g$.
"The synthesis network $g$ consists of 18 layers—two for each resolution (42 10242)."
"Our generator has a total of 26.2M trainable parameters"
"""
def __init__ (self,
latent_dim: int,
num_mapping_net_layers: int = 8,
const_size: int = 4,
channels: int = 512,
):
super().__init__()
self.mapping_net = MappingNetwork(
latent_dim=latent_dim, num_layers=num_mapping_net_layers,
)
self.res4_block = Block1(
latent_dim=latent_dim, const_size=const_size, channels=channels,
)
self.res8_block = Block2(
latent_dim=latent_dim, in_channels=channels, const_size=channels,
)
self.res16_block = Block2(
latent_dim=latent_dim, in_channels=channels, const_size=channels,
)
self.res32_block = Block2(
latent_dim=latent_dim, in_channels=channels, const_size=channels,
)
self.res64_block = Block2(
latent_dim=latent_dim, in_channels=channels, const_size=channels,
)
self.res128_block = Block2(
latent_dim=latent_dim, in_channels=channels, const_size=channels,
)
self.res256_block = Block2(
latent_dim=latent_dim, in_channels=channels, const_size=channels,
)
self.res512_block = Block2(
latent_dim=latent_dim, in_channels=channels, const_size=channels,
)
self.res1024_block = Block2(
latent_dim=latent_dim, in_channels=channels, const_size=channels,
)
self.to_rgb = ToRGB(in_channels=channels)
def forward(self, latent_vec, noise):
w = self.mapping_net(latent_vec) # $\mathbf{w}$.
x = self.res4_block(w=w, noise=noise)
x = self.res8_block(x, w=w, noise=noise)
x = self.res16_block(x, w=w, noise=noise)
x = self.res32_block(x, w=w, noise=noise)
x = self.res64_block(x, w=w, noise=noise)
x = self.res128_block(x, w=w, noise=noise)
x = self.res256_block(x, w=w, noise=noise)
x = self.res512_block(x, w=w, noise=noise)
x = self.res1024_block(x, w=w, noise=noise)
return self.to_rgb(x)
if __name__ == "__main__":
latent_dim = 512
num_layers = 8
batch_size = 16
# Instantiate the mapping network
mapping_net = MappingNetwork(latent_dim=latent_dim, num_layers=num_layers)
z = generate_latent_vec(batch_size=batch_size, latent_dim=latent_dim)
# Get the transformed latent vector w
w = mapping_net(z)
print("Input z shape:", z.shape)
print("Output w shape:", w.shape)