-
Notifications
You must be signed in to change notification settings - Fork 0
/
res_decoder.py
62 lines (49 loc) · 1.59 KB
/
res_decoder.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
"""
CNN Decoder for the VQ-VAE with Residual Connections.
"""
import tensorflow as tf
from tensorflow.keras import layers
class ResidualDecoder(tf.keras.Model):
def __init__(self, embedding_dim: int, num_layers: int = 5):
super().__init__()
self.embedding_dim = embedding_dim
# Residual block
self.residual_block = layers.Conv2DTranspose(
filters=self.embedding_dim,
kernel_size=3,
strides=1,
padding="same",
activation="relu",
name="residual_block"
)
self.conv_layers = [
layers.Conv2DTranspose(
filters=self.embedding_dim * 2 ** i,
kernel_size=4,
strides=2,
padding="same",
activation="relu",
name=f"conv_{i}"
)
for i in range(num_layers - 1)
]
self.conv_out = layers.Conv2DTranspose(
filters=3,
kernel_size=3,
strides=2,
padding="same",
activation="sigmoid",
name="conv_out"
)
def call(self, inputs: tf.Tensor) -> tf.Tensor:
x = inputs
x = self.residual_block(x) + x
for conv in self.conv_layers:
x = conv(x)
return self.conv_out(x)
if __name__ == '__main__':
decoder = ResidualDecoder(128, num_layers=3)
decoder.build(input_shape=(None, 16, 16, 128))
a = decoder(tf.random.normal((1, 16, 16, 128)))
decoder.summary()
print(a.shape)