-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.py
161 lines (127 loc) · 5.67 KB
/
nn.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
import torch
import torch.nn as nn
import torch.nn.functional as F
factors = [1, 1, 1, 1, 1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 32]
class WSConv2d(nn.Module):
def __init__(
self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, gain=2
):
super().__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)
self.scale = (gain / (in_channels * kernel_size ** 2)) ** 0.5
self.bias = self.conv.bias
self.conv.bias = None
nn.init.normal_(self.conv.weight)
nn.init.zeros_(self.bias)
def forward(self, x):
return self.conv(x * self.scale) + self.bias.view(1, self.bias.shape[0], 1, 1)
class PixelNorm(nn.Module):
def __init__(self):
super(PixelNorm, self).__init__()
self.eps = 1e-8
def forward(self, x):
return x / torch.sqrt(torch.mean(x ** 2, dim=1, keepdim=True) + self.eps)
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, use_pixelnorm=True):
super().__init__()
self.conv1 = WSConv2d(in_channels, out_channels)
self.conv2 = WSConv2d(out_channels, out_channels)
self.leaky = nn.LeakyReLU(0.2)
self.pn = PixelNorm()
self.use_pixelnorm = use_pixelnorm
def forward(self, x):
x = self.leaky(self.conv1(x))
x = self.pn(x) if self.use_pixelnorm else x
x = self.leaky(self.conv2(x))
x = self.pn(x) if self.use_pixelnorm else x
return x
class Generator(nn.Module):
def __init__(self, z_dim, in_channels, img_channels=3):
super().__init__()
self.first = nn.Sequential(
PixelNorm(),
nn.ConvTranspose2d(z_dim, in_channels, 4, 1, 0),
nn.LeakyReLU(0.2),
WSConv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(0.2),
PixelNorm(),
)
self.initial_rgb = WSConv2d(
in_channels, img_channels, kernel_size=1, stride=1, padding=0
)
self.prog_blocks = nn.ModuleList([])
self.rgb_layers = nn.ModuleList([self.initial_rgb])
for i in range(len(factors) - 1):
conv_in_channels = int(in_channels * factors[i])
conv_out_channels = int(in_channels * factors[i + 1])
conv_in_channels = int(in_channels * factors[i])
conv_out_channels = int(in_channels * factors[i + 1])
self.prog_blocks.append(ConvBlock(conv_in_channels, conv_out_channels))
self.rgb_layers.append(
WSConv2d(
conv_out_channels, img_channels, kernel_size=1, stride=1, padding=0
)
)
def fade_in(self, alpha, upscaled, generated):
return torch.tanh(alpha * generated + (1 - alpha) * upscaled)
def forward(self, x, alpha, steps):
out = self.first(x) # 4x4
if steps == 0:
return self.initial_rgb(out)
for step in range(steps):
upscaled = F.interpolate(out, scale_factor=2, mode="nearest")
out = self.prog_blocks[step](upscaled)
final_upscaled = self.rgb_layers[steps - 1](upscaled)
final_out = self.rgb_layers[steps](out)
return self.fade_in(alpha, final_upscaled, final_out)
class Critic(nn.Module):
def __init__(self, z_dim, in_channels, img_channels=3):
super().__init__()
self.prog_blocks = nn.ModuleList([])
self.rgb_layers = nn.ModuleList([])
self.leaky = nn.LeakyReLU(0.2)
for i in range(len(factors) - 1, 0, -1):
conv_in = int(in_channels * factors[i])
conv_out = int(in_channels * factors[i - 1])
self.prog_blocks.append(ConvBlock(conv_in, conv_out, use_pixelnorm=False))
self.rgb_layers.append(
WSConv2d(img_channels, conv_in, kernel_size=1, stride=1, padding=0)
)
# only for 4x4 resolution
self.end_rgb = WSConv2d(
img_channels, in_channels, kernel_size=1, stride=1, padding=0
)
self.rgb_layers.append(self.end_rgb)
self.avg_pool = nn.AvgPool2d(kernel_size=2, stride=2)
# block for 4x4 resolution
self.final_block = nn.Sequential(
WSConv2d(in_channels + 1, in_channels, kernel_size=3, padding=1),
nn.LeakyReLU(0.2),
WSConv2d(in_channels, in_channels, kernel_size=4, padding=0, stride=1),
nn.LeakyReLU(0.2),
WSConv2d(in_channels, 1, kernel_size=1, padding=0, stride=1),
)
def fade_in(self, alpha, downscaled, out):
return alpha * out + (1 - alpha) * downscaled
def minibatch_std(self, x):
batch_statistics = (
torch.std(x, dim=0).mean().repeat(x.shape[0], 1, x.shape[2], x.shape[3])
)
return torch.cat([x, batch_statistics], dim=1)
def forward(self, x, alpha, steps):
current_step = len(self.prog_blocks) - steps
out = self.leaky(self.rgb_layers[current_step](x))
if steps == 0: # 4x4
out = self.minibatch_std(out)
return self.final_block(out).view(out.shape[0], -1)
downscaled = self.leaky(self.rgb_layers[current_step + 1](self.avg_pool(x)))
out = self.avg_pool(self.prog_blocks[current_step](out))
out = self.fade_in(alpha, downscaled, out)
for step in range(current_step + 1, len(self.prog_blocks)):
out = self.prog_blocks[step](out)
out = self.avg_pool(out)
out = self.minibatch_std(out)
return self.final_block(out).view(out.shape[0], -1)
# Links:
# * https://youtu.be/nkQHASviYac
# * https://research.nvidia.com/sites/default/files/pubs/2017-10_Progressive-Growing-of/karras2018iclr-paper.pdf