-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvae_decoder.py
More file actions
138 lines (110 loc) · 4.77 KB
/
vae_decoder.py
File metadata and controls
138 lines (110 loc) · 4.77 KB
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
import torch
from torch import nn
from torch.nn import functional as F
from attention import SelfAttention
class VAE_ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.groupnorm_1 = nn.GroupNorm(32, in_channels)
self.conv_1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1)
self.groupnorm_2 = nn.GroupNorm(32, out_channels)
self.conv_2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1)
if in_channels == out_channels:
self.residual_layer = nn.Identity()
else:
self.residual_layer = nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0)
def forward(self, x:torch.Tensor):
#x.shape = (B, in_channels, H, W)
residue = x # for res connection
x = self.groupnorm_1(x)
x = F.silu(x)
x = self.conv_1(x)
x = self.groupnorm_2(x)
x = F.silu(x)
x = self.conv_2(x)
return x + self.residual_layer(residue) # skiip connection
class VAE_AttentionBlock(nn.Module):
def __init__(self, channels: int):
super().__init__()
self.groupnorm = nn.GroupNorm(32, channels) # argument: num of groups, num of channels
self.attention = SelfAttention(1, channels)
def forward(self, x:torch.Tensor):
# x.shape = (B, C, H, W)
residue = x # for skip connect
b, c, h, w = x.shape
# (B, C, H, W) --> (B, C, H*W)
x = x.view(b, c, h*w)
# (B, C, H*W) --> (B, H*W, C)
x = x.transpose(-1,-2)
# (B, H*W, C)
x = self.attention(x)
# (B, H*W, C) --> (B, C, H*W)
x = x.transpose(-1,-2)
# (B, C, H*W) --> (B, C, H, W)
x = x.view(b, c, h, w)
x += residue # skip connect
return x
class VAE_Decoder(nn.Sequential):
def __init__(self):
super().__init__(
# (B, 4, H/8, W/8) --> (B, 4, H/8, W/8)
nn.Conv2d(4, 4, kernel_size=1, padding=0),
# (B, 4, H/8, W/8) --> (B, 512, H/8, W/8)
nn.Conv2d(4, 512, kernel_size=3, padding=1),
# (B, 512, H/8, W/8) --> (B, 512, H/8, W/8)
VAE_ResidualBlock(512, 512),
# (B, 512, H/8, W/8) --> (B, 512, H/8, W/8)
VAE_AttentionBlock(512),
# (B, 512, H/8, W/8) --> (B, 512, H/8, W/8)
VAE_ResidualBlock(512, 512),
# (B, 512, H/8, W/8) --> (B, 512, H/8, W/8)
VAE_ResidualBlock(512, 512),
# (B, 512, H/8, W/8) --> (B, 512, H/8, W/8)
VAE_ResidualBlock(512, 512),
# (B, 512, H/8, W/8) --> (B, 512, H/8, W/8)
VAE_ResidualBlock(512, 512),
# Repeats the rows and columns of the data by scale_factor (like when you resize an image by doubling its size).
# (B, 512, H/8, W/8) --> (B, 512, H/4, W/4)
nn.Upsample(scale_factor=2),
# (B, 512, H/4, W/4) --> (B, 512, H/4, W/4)
nn.Conv2d(512, 512, kernel_size=3, padding=1),
# (B, 512, H/4, W/4) --> (B, 512, H/4, W/4)
VAE_ResidualBlock(512, 512),
# (B, 512, H/4, W/4) --> (B, 512, H/4, W/4)
VAE_ResidualBlock(512, 512),
# (B, 512, H/4, W/4) --> (B, 512, H/4, W/4)
VAE_ResidualBlock(512, 512),
# (B, 512, H/4, W/4) --> (B, 512, H/2, W/2)
nn.Upsample(scale_factor=2),
# (B, 512, H/2, W/2) --> (B, 512, H/2, W/2)
nn.Conv2d(512, 512, kernel_size=3, padding=1),
# (B, 512, H/2, W/2) --> (B, 256, H/2, W/2)
VAE_ResidualBlock(512, 256),
# (B, 256, H/2, W/2) --> (B, 256, H/2, W/2)
VAE_ResidualBlock(256, 256),
# (B, 256, H/2, W/2) --> (B, 256, H/2, W/2)
VAE_ResidualBlock(256, 256),
# (B, 256, H/2, W/2) --> (B, 256, H, W)
nn.Upsample(scale_factor=2),
# (B, 256, H, W) --> (B, 256, H, W)
nn.Conv2d(256, 256, kernel_size=3, padding=1),
# (B, 256, H, W) --> (B, 128, H, W)
VAE_ResidualBlock(256, 128),
# (B, 128, H, W) --> (B, 128, H, W)
VAE_ResidualBlock(128, 128),
# (B, 128, H, W) --> (B, 128, H, W)
VAE_ResidualBlock(128, 128),
# (B, 128, H, W) --> (B, 128, H, W)
nn.GroupNorm(32, 128),
# (B, 128, H, W) --> (B, 128, H, W)
nn.SiLU(),
# (B, 128, H, W) --> (B, 3, H, W)
nn.Conv2d(128, 3, kernel_size=3, padding=1),
)
def forward(self, x: torch.Tensor):
# x.shape = (B, 4, H/8, W/8)
# first remove the scaling we did in encoder (which was for normalization purposes)
x /= 0.18215
for layer in self:
x = layer(x)
return x # shape (B, 3, H, W)