-
Notifications
You must be signed in to change notification settings - Fork 17
/
vqvae.py
269 lines (223 loc) · 10.8 KB
/
vqvae.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from math import log2
from typing import Tuple
from helper import HelperModule
class ReZero(HelperModule):
def build(self, in_channels: int, res_channels: int):
self.layers = nn.Sequential(
nn.Conv2d(in_channels, res_channels, 3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(res_channels),
nn.ReLU(inplace=True),
nn.Conv2d(res_channels, in_channels, 3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU(inplace=True),
)
self.alpha = nn.Parameter(torch.tensor(0.0))
def forward(self, x: torch.FloatTensor) -> torch.FloatTensor:
return self.layers(x) * self.alpha + x
class ResidualStack(HelperModule):
def build(self, in_channels: int, res_channels: int, nb_layers: int):
self.stack = nn.Sequential(*[ReZero(in_channels, res_channels)
for _ in range(nb_layers)
])
def forward(self, x: torch.FloatTensor) -> torch.FloatTensor:
return self.stack(x)
class Encoder(HelperModule):
def build(self,
in_channels: int, hidden_channels: int,
res_channels: int, nb_res_layers: int,
downscale_factor: int,
):
assert log2(downscale_factor) % 1 == 0, "Downscale must be a power of 2"
downscale_steps = int(log2(downscale_factor))
layers = []
c_channel, n_channel = in_channels, hidden_channels // 2
for _ in range(downscale_steps):
layers.append(nn.Sequential(
nn.Conv2d(c_channel, n_channel, 4, stride=2, padding=1),
nn.BatchNorm2d(n_channel),
nn.ReLU(inplace=True),
))
c_channel, n_channel = n_channel, hidden_channels
layers.append(nn.Conv2d(c_channel, n_channel, 3, stride=1, padding=1))
layers.append(nn.BatchNorm2d(n_channel))
layers.append(ResidualStack(n_channel, res_channels, nb_res_layers))
self.layers = nn.Sequential(*layers)
def forward(self, x: torch.FloatTensor) -> torch.FloatTensor:
return self.layers(x)
class Decoder(HelperModule):
def build(self,
in_channels: int, hidden_channels: int, out_channels: int,
res_channels: int, nb_res_layers: int,
upscale_factor: int,
):
assert log2(upscale_factor) % 1 == 0, "Downscale must be a power of 2"
upscale_steps = int(log2(upscale_factor))
layers = [nn.Conv2d(in_channels, hidden_channels, 3, stride=1, padding=1)]
layers.append(ResidualStack(hidden_channels, res_channels, nb_res_layers))
c_channel, n_channel = hidden_channels, hidden_channels // 2
for _ in range(upscale_steps):
layers.append(nn.Sequential(
nn.ConvTranspose2d(c_channel, n_channel, 4, stride=2, padding=1),
nn.BatchNorm2d(n_channel),
nn.ReLU(inplace=True),
))
c_channel, n_channel = n_channel, out_channels
layers.append(nn.Conv2d(c_channel, n_channel, 3, stride=1, padding=1))
layers.append(nn.BatchNorm2d(n_channel))
# layers.append(nn.ReLU(inplace=True))
self.layers = nn.Sequential(*layers)
def forward(self, x: torch.FloatTensor) -> torch.FloatTensor:
return self.layers(x)
"""
Almost directly taken from https://github.com/rosinality/vq-vae-2-pytorch/blob/master/vqvae.py
No reason to reinvent this rather complex mechanism.
Essentially handles the "discrete" part of the network, and training through EMA rather than
third term in loss function.
"""
class CodeLayer(HelperModule):
def build(self, in_channels: int, embed_dim: int, nb_entries: int):
self.conv_in = nn.Conv2d(in_channels, embed_dim, 1)
self.dim = embed_dim
self.n_embed = nb_entries
self.decay = 0.99
self.eps = 1e-5
embed = torch.randn(embed_dim, nb_entries, dtype=torch.float32)
self.register_buffer("embed", embed)
self.register_buffer("cluster_size", torch.zeros(nb_entries, dtype=torch.float32))
self.register_buffer("embed_avg", embed.clone())
@torch.cuda.amp.autocast(enabled=False)
def forward(self, x: torch.FloatTensor) -> Tuple[torch.FloatTensor, float, torch.LongTensor]:
x = self.conv_in(x.float()).permute(0,2,3,1)
flatten = x.reshape(-1, self.dim)
dist = (
flatten.pow(2).sum(1, keepdim=True)
- 2 * flatten @ self.embed
+ self.embed.pow(2).sum(0, keepdim=True)
)
_, embed_ind = (-dist).max(1)
embed_onehot = F.one_hot(embed_ind, self.n_embed).type(flatten.dtype)
embed_ind = embed_ind.view(*x.shape[:-1])
quantize = self.embed_code(embed_ind)
if self.training:
embed_onehot_sum = embed_onehot.sum(0)
embed_sum = flatten.transpose(0, 1) @ embed_onehot
# TODO: Replace this? Or can we simply comment out?
# dist_fn.all_reduce(embed_onehot_sum)
# dist_fn.all_reduce(embed_sum)
self.cluster_size.data.mul_(self.decay).add_(
embed_onehot_sum, alpha=1 - self.decay
)
self.embed_avg.data.mul_(self.decay).add_(embed_sum, alpha=1 - self.decay)
n = self.cluster_size.sum()
cluster_size = (
(self.cluster_size + self.eps) / (n + self.n_embed * self.eps) * n
)
embed_normalized = self.embed_avg / cluster_size.unsqueeze(0)
self.embed.data.copy_(embed_normalized)
diff = (quantize.detach() - x).pow(2).mean()
quantize = x + (quantize - x).detach()
return quantize.permute(0, 3, 1, 2), diff, embed_ind
def embed_code(self, embed_id: torch.LongTensor) -> torch.FloatTensor:
return F.embedding(embed_id, self.embed.transpose(0, 1))
class Upscaler(HelperModule):
def build(self,
embed_dim: int,
scaling_rates: list[int],
):
self.stages = nn.ModuleList()
for sr in scaling_rates:
upscale_steps = int(log2(sr))
layers = []
for _ in range(upscale_steps):
layers.append(nn.ConvTranspose2d(embed_dim, embed_dim, 4, stride=2, padding=1))
layers.append(nn.BatchNorm2d(embed_dim))
layers.append(nn.ReLU(inplace=True))
self.stages.append(nn.Sequential(*layers))
def forward(self, x: torch.FloatTensor, stage: int) -> torch.FloatTensor:
return self.stages[stage](x)
"""
Main VQ-VAE-2 Module, capable of support arbitrary number of levels
TODO: A lot of this class could do with a refactor. It works, but at what cost?
TODO: Add disrete code decoding function
"""
class VQVAE(HelperModule):
def build(self,
in_channels: int = 3,
hidden_channels: int = 128,
res_channels: int = 32,
nb_res_layers: int = 2,
nb_levels: int = 3,
embed_dim: int = 64,
nb_entries: int = 512,
scaling_rates: list[int] = [8, 4, 2]
):
self.nb_levels = nb_levels
assert len(scaling_rates) == nb_levels, "Number of scaling rates not equal to number of levels!"
self.encoders = nn.ModuleList([Encoder(in_channels, hidden_channels, res_channels, nb_res_layers, scaling_rates[0])])
for i, sr in enumerate(scaling_rates[1:]):
self.encoders.append(Encoder(hidden_channels, hidden_channels, res_channels, nb_res_layers, sr))
self.codebooks = nn.ModuleList()
for i in range(nb_levels - 1):
self.codebooks.append(CodeLayer(hidden_channels+embed_dim, embed_dim, nb_entries))
self.codebooks.append(CodeLayer(hidden_channels, embed_dim, nb_entries))
self.decoders = nn.ModuleList([Decoder(embed_dim*nb_levels, hidden_channels, in_channels, res_channels, nb_res_layers, scaling_rates[0])])
for i, sr in enumerate(scaling_rates[1:]):
self.decoders.append(Decoder(embed_dim*(nb_levels-1-i), hidden_channels, embed_dim, res_channels, nb_res_layers, sr))
self.upscalers = nn.ModuleList()
for i in range(nb_levels - 1):
self.upscalers.append(Upscaler(embed_dim, scaling_rates[1:len(scaling_rates) - i][::-1]))
def forward(self, x):
encoder_outputs = []
code_outputs = []
decoder_outputs = []
upscale_counts = []
id_outputs = []
diffs = []
for enc in self.encoders:
if len(encoder_outputs):
encoder_outputs.append(enc(encoder_outputs[-1]))
else:
encoder_outputs.append(enc(x))
for l in range(self.nb_levels-1, -1, -1):
codebook, decoder = self.codebooks[l], self.decoders[l]
if len(decoder_outputs): # if we have previous levels to condition on
code_q, code_d, emb_id = codebook(torch.cat([encoder_outputs[l], decoder_outputs[-1]], axis=1))
else:
code_q, code_d, emb_id = codebook(encoder_outputs[l])
diffs.append(code_d)
id_outputs.append(emb_id)
code_outputs = [self.upscalers[i](c, upscale_counts[i]) for i, c in enumerate(code_outputs)]
upscale_counts = [u+1 for u in upscale_counts]
decoder_outputs.append(decoder(torch.cat([code_q, *code_outputs], axis=1)))
code_outputs.append(code_q)
upscale_counts.append(0)
return decoder_outputs[-1], diffs, encoder_outputs, decoder_outputs, id_outputs
def decode_codes(self, *cs):
decoder_outputs = []
code_outputs = []
upscale_counts = []
for l in range(self.nb_levels - 1, -1, -1):
codebook, decoder = self.codebooks[l], self.decoders[l]
code_q = codebook.embed_code(cs[l]).permute(0, 3, 1, 2)
code_outputs = [self.upscalers[i](c, upscale_counts[i]) for i, c in enumerate(code_outputs)]
upscale_counts = [u+1 for u in upscale_counts]
decoder_outputs.append(decoder(torch.cat([code_q, *code_outputs], axis=1)))
code_outputs.append(code_q)
upscale_counts.append(0)
return decoder_outputs[-1]
if __name__ == '__main__':
from helper import get_parameter_count
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
nb_levels = 10
net = VQVAE(nb_levels=nb_levels, scaling_rates=[2]*nb_levels).to(device)
print(f"Number of trainable parameters: {get_parameter_count(net)}")
x = torch.randn(1, 3, 1024, 1024).to(device)
_, diffs, enc_out, dec_out = net(x)
print('\n'.join(str(y.shape) for y in enc_out))
print()
print('\n'.join(str(y.shape) for y in dec_out))
print()
print('\n'.join(str(y) for y in diffs))