forked from rosinality/vq-vae-2-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixelsnail.py
executable file
·431 lines (325 loc) · 12.2 KB
/
pixelsnail.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# Copyright (c) Xi Chen
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# Borrowed from https://github.com/neocxi/pixelsnail-public and ported it to PyTorch
from math import sqrt
from functools import partial, lru_cache
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
def wn_linear(in_dim, out_dim):
return nn.utils.weight_norm(nn.Linear(in_dim, out_dim))
class WNConv2d(nn.Module):
def __init__(
self,
in_channel,
out_channel,
kernel_size,
stride=1,
padding=0,
bias=True,
activation=None,
):
super().__init__()
self.conv = nn.utils.weight_norm(
nn.Conv2d(
in_channel,
out_channel,
kernel_size,
stride=stride,
padding=padding,
bias=bias,
)
)
self.out_channel = out_channel
if isinstance(kernel_size, int):
kernel_size = [kernel_size, kernel_size]
self.kernel_size = kernel_size
self.activation = activation
def forward(self, input):
out = self.conv(input)
if self.activation is not None:
out = self.activation(out)
return out
def shift_down(input, size=1):
return F.pad(input, [0, 0, size, 0])[:, :, : input.shape[2], :]
def shift_right(input, size=1):
return F.pad(input, [size, 0, 0, 0])[:, :, :, : input.shape[3]]
class CausalConv2d(nn.Module):
def __init__(
self,
in_channel,
out_channel,
kernel_size,
stride=1,
padding='downright',
activation=None,
):
super().__init__()
if isinstance(kernel_size, int):
kernel_size = [kernel_size] * 2
self.kernel_size = kernel_size
if padding == 'downright':
pad = [kernel_size[1] - 1, 0, kernel_size[0] - 1, 0]
elif padding == 'down' or padding == 'causal':
pad = kernel_size[1] // 2
pad = [pad, pad, kernel_size[0] - 1, 0]
self.causal = 0
if padding == 'causal':
self.causal = kernel_size[1] // 2
self.pad = nn.ZeroPad2d(pad)
self.conv = WNConv2d(
in_channel,
out_channel,
kernel_size,
stride=stride,
padding=0,
activation=activation,
)
def forward(self, input):
out = self.pad(input)
if self.causal > 0:
self.conv.conv.weight_v.data[:, :, -1, self.causal :].zero_()
out = self.conv(out)
return out
class GatedResBlock(nn.Module):
def __init__(
self,
in_channel,
channel,
kernel_size,
conv='wnconv2d',
activation=nn.ELU,
dropout=0.1,
auxiliary_channel=0,
condition_dim=0,
):
super().__init__()
if conv == 'wnconv2d':
conv_module = partial(WNConv2d, padding=kernel_size // 2)
elif conv == 'causal_downright':
conv_module = partial(CausalConv2d, padding='downright')
elif conv == 'causal':
conv_module = partial(CausalConv2d, padding='causal')
self.activation = activation(inplace=True)
self.conv1 = conv_module(in_channel, channel, kernel_size)
if auxiliary_channel > 0:
self.aux_conv = WNConv2d(auxiliary_channel, channel, 1)
self.dropout = nn.Dropout(dropout)
self.conv2 = conv_module(channel, in_channel * 2, kernel_size)
if condition_dim > 0:
# self.condition = nn.Linear(condition_dim, in_channel * 2, bias=False)
self.condition = WNConv2d(condition_dim, in_channel * 2, 1, bias=False)
self.gate = nn.GLU(1)
def forward(self, input, aux_input=None, condition=None):
out = self.conv1(self.activation(input))
if aux_input is not None:
out = out + self.aux_conv(self.activation(aux_input))
out = self.activation(out)
out = self.dropout(out)
out = self.conv2(out)
if condition is not None:
condition = self.condition(condition)
out += condition
# out = out + condition.view(condition.shape[0], 1, 1, condition.shape[1])
out = self.gate(out)
out += input
return out
@lru_cache(maxsize=64)
def causal_mask(size):
shape = [size, size]
mask = np.triu(np.ones(shape), k=1).astype(np.uint8).T
start_mask = np.ones(size).astype(np.float32)
start_mask[0] = 0
return (
torch.from_numpy(mask).unsqueeze(0),
torch.from_numpy(start_mask).unsqueeze(1),
)
class CausalAttention(nn.Module):
def __init__(self, query_channel, key_channel, channel, n_head=8, dropout=0.1):
super().__init__()
self.query = wn_linear(query_channel, channel)
self.key = wn_linear(key_channel, channel)
self.value = wn_linear(key_channel, channel)
self.dim_head = channel // n_head
self.n_head = n_head
self.dropout = nn.Dropout(dropout)
def forward(self, query, key):
batch, _, height, width = key.shape
def reshape(input):
return input.view(batch, -1, self.n_head, self.dim_head).transpose(1, 2)
query_flat = query.view(batch, query.shape[1], -1).transpose(1, 2)
key_flat = key.view(batch, key.shape[1], -1).transpose(1, 2)
query = reshape(self.query(query_flat))
key = reshape(self.key(key_flat)).transpose(2, 3)
value = reshape(self.value(key_flat))
attn = torch.matmul(query, key) / sqrt(self.dim_head)
mask, start_mask = causal_mask(height * width)
mask = mask.type_as(query)
start_mask = start_mask.type_as(query)
attn = attn.masked_fill(mask == 0, -1e4)
attn = torch.softmax(attn, 3) * start_mask
attn = self.dropout(attn)
out = attn @ value
out = out.transpose(1, 2).reshape(
batch, height, width, self.dim_head * self.n_head
)
out = out.permute(0, 3, 1, 2)
return out
class PixelBlock(nn.Module):
def __init__(
self,
in_channel,
channel,
kernel_size,
n_res_block,
attention=True,
dropout=0.1,
condition_dim=0,
):
super().__init__()
resblocks = []
for i in range(n_res_block):
resblocks.append(
GatedResBlock(
in_channel,
channel,
kernel_size,
conv='causal',
dropout=dropout,
condition_dim=condition_dim,
)
)
self.resblocks = nn.ModuleList(resblocks)
self.attention = attention
if attention:
self.key_resblock = GatedResBlock(
in_channel * 2 + 2, in_channel, 1, dropout=dropout
)
self.query_resblock = GatedResBlock(
in_channel + 2, in_channel, 1, dropout=dropout
)
self.causal_attention = CausalAttention(
in_channel + 2, in_channel * 2 + 2, in_channel // 2, dropout=dropout
)
self.out_resblock = GatedResBlock(
in_channel,
in_channel,
1,
auxiliary_channel=in_channel // 2,
dropout=dropout,
)
else:
self.out = WNConv2d(in_channel + 2, in_channel, 1)
def forward(self, input, background, condition=None):
out = input
for resblock in self.resblocks:
out = resblock(out, condition=condition)
if self.attention:
key_cat = torch.cat([input, out, background], 1)
key = self.key_resblock(key_cat)
query_cat = torch.cat([out, background], 1)
query = self.query_resblock(query_cat)
attn_out = self.causal_attention(query, key)
out = self.out_resblock(out, attn_out)
else:
bg_cat = torch.cat([out, background], 1)
out = self.out(bg_cat)
return out
class CondResNet(nn.Module):
def __init__(self, in_channel, channel, kernel_size, n_res_block):
super().__init__()
blocks = [WNConv2d(in_channel, channel, kernel_size, padding=kernel_size // 2)]
for i in range(n_res_block):
blocks.append(GatedResBlock(channel, channel, kernel_size))
self.blocks = nn.Sequential(*blocks)
def forward(self, input):
return self.blocks(input)
class PixelSNAIL(nn.Module):
def __init__(
self,
shape,
n_class,
channel,
kernel_size,
n_block,
n_res_block,
res_channel,
attention=True,
dropout=0.1,
n_cond_res_block=0,
cond_res_channel=0,
cond_res_kernel=3,
n_out_res_block=0,
):
super().__init__()
height, width = shape
self.n_class = n_class
if kernel_size % 2 == 0:
kernel = kernel_size + 1
else:
kernel = kernel_size
self.horizontal = CausalConv2d(
n_class, channel, [kernel // 2, kernel], padding='down'
)
self.vertical = CausalConv2d(
n_class, channel, [(kernel + 1) // 2, kernel // 2], padding='downright'
)
coord_x = (torch.arange(height).float() - height / 2) / height
coord_x = coord_x.view(1, 1, height, 1).expand(1, 1, height, width)
coord_y = (torch.arange(width).float() - width / 2) / width
coord_y = coord_y.view(1, 1, 1, width).expand(1, 1, height, width)
self.register_buffer('background', torch.cat([coord_x, coord_y], 1))
self.blocks = nn.ModuleList()
for i in range(n_block):
self.blocks.append(
PixelBlock(
channel,
res_channel,
kernel_size,
n_res_block,
attention=attention,
dropout=dropout,
condition_dim=cond_res_channel,
)
)
if n_cond_res_block > 0:
self.cond_resnet = CondResNet(
n_class, cond_res_channel, cond_res_kernel, n_cond_res_block
)
out = []
for i in range(n_out_res_block):
out.append(GatedResBlock(channel, res_channel, 1))
out.extend([nn.ELU(inplace=True), WNConv2d(channel, n_class, 1)])
self.out = nn.Sequential(*out)
def forward(self, input, condition=None, cache=None):
if cache is None:
cache = {}
batch, height, width = input.shape
input = (
F.one_hot(input, self.n_class).permute(0, 3, 1, 2).type_as(self.background)
)
horizontal = shift_down(self.horizontal(input))
vertical = shift_right(self.vertical(input))
out = horizontal + vertical
background = self.background[:, :, :height, :].expand(batch, 2, height, width)
if condition is not None:
if 'condition' in cache:
condition = cache['condition']
condition = condition[:, :, :height, :]
else:
condition = (
F.one_hot(condition, self.n_class)
.permute(0, 3, 1, 2)
.type_as(self.background)
)
condition = self.cond_resnet(condition)
condition = F.interpolate(condition, scale_factor=2)
cache['condition'] = condition.detach().clone()
condition = condition[:, :, :height, :]
for block in self.blocks:
out = block(out, background, condition=condition)
out = self.out(out)
return out, cache