-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvae.py
More file actions
185 lines (167 loc) · 6.65 KB
/
vae.py
File metadata and controls
185 lines (167 loc) · 6.65 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
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
import torch
from torch import nn
import torch.nn.functional as F
from attention import SelfAttention
class VAE_ResidualBlock(nn.Module):
def __init__(self, in_channels: int, out_channels: int, num_groups: int = 32):
super().__init__()
self.groupnorm_1 = nn.GroupNorm(num_groups, in_channels)
self.conv_1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1)
self.groupnorm_2 = nn.GroupNorm(num_groups, out_channels)
self.conv_2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1)
self.residual_layer = (
nn.Identity()
if in_channels == out_channels
else nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0)
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
residue = self.residual_layer(x)
x = self.groupnorm_1(x)
x = self.forward_conv(x, self.conv_1, self.groupnorm_1)
x = self.forward_conv(x, self.conv_2, self.groupnorm_2)
return x + residue
def forward_conv(
self, x: torch.Tensor, conv: nn.Conv2d, groupnorm: nn.GroupNorm
) -> torch.Tensor:
x = groupnorm(x)
x = F.silu(x)
x = conv(x)
return x
class VAE_AttentionBlock(nn.Module):
def __init__(
self, channels: int, *attention_args, num_groups: int = 32, **attention_kwargs
):
super().__init__()
self.groupnorm = nn.GroupNorm(num_groups, channels)
self.attention = SelfAttention(channels, *attention_args, **attention_kwargs)
def forward(self, x: torch.Tensor) -> torch.Tensor:
residue = x
B, C, H, W = x.shape
x = x.view(B, C, H * W)
x = x.transpose(-1, -2)
x = self.attention(x)
x = x.transpose(-1, -2)
x = x.view((B, C, H, W))
x += residue
return x
class VAE_Encoder(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
*attention_args,
n_layers: int = 3,
image_channels: int = 3,
num_groups: int = 32,
**attention_kwargs
):
super().__init__()
self.n_layers = n_layers
current_channels = in_channels
prev_channels = in_channels
residual_layers = []
# Output Image Dimensions = Input Image Dimensions // 2**(n_layers)
# Example (n_layers=3, w=512, h=512): Output Height = 512 // 2**3 = 64, Output Width = 512 // 2**3 = 64
for i in range(n_layers):
residual_layers = [
*residual_layers,
VAE_ResidualBlock(
in_channels=prev_channels, out_channels=current_channels
),
VAE_ResidualBlock(
in_channels=current_channels, out_channels=current_channels
),
nn.Conv2d(
current_channels,
current_channels,
kernel_size=3,
stride=2,
padding=0,
), # padding might need to be 1 here...
]
current_channels *= 2
self.ffwd = nn.Sequential(
nn.Conv2d(image_channels, in_channels, kernel_size=3, padding=1),
*residual_layers,
VAE_ResidualBlock(current_channels, current_channels),
VAE_ResidualBlock(current_channels, current_channels),
VAE_ResidualBlock(current_channels, current_channels),
VAE_AttentionBlock(current_channels, *attention_args, **attention_kwargs),
VAE_ResidualBlock(current_channels, current_channels),
nn.GroupNorm(num_groups, current_channels),
nn.SiLU(),
nn.Conv2d(current_channels, out_channels, kernel_size=3, padding=1),
nn.Conv2d(out_channels, out_channels, kernel_size=1, padding=0),
)
def forward(self, x: torch.Tensor, noise: torch.Tensor) -> torch.Tensor:
for module in self.ffwd:
if getattr(module, "stride", None) == (2, 2):
x = F.pad(x, (0, 1, 0, 1))
x = module(x)
mean, log_var = torch.chunk(x, 2, dim=1)
log_variance = torch.clamp(log_var, -30, 20)
variance = log_variance.exp()
stdev = variance.sqrt()
x = mean + stdev * noise
x *= 0.18215 # constant scale
return x
class VAE_Decoder(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
*attention_args,
n_layers: int = 3,
image_channels: int = 3,
num_groups: int = 32,
**attention_kwargs
):
super().__init__()
current_channels = out_channels
prev_channels = out_channels
residual_layers = []
for i in range(n_layers):
residual_layers = [
*residual_layers,
nn.Upsample(scale_factor=2),
nn.Conv2d(
prev_channels, prev_channels, kernel_size=3, padding=1
), # padding might need to be 1 here...
VAE_ResidualBlock(
in_channels=prev_channels, out_channels=current_channels
),
VAE_ResidualBlock(
in_channels=current_channels, out_channels=current_channels
),
VAE_ResidualBlock(
in_channels=current_channels, out_channels=current_channels
),
]
current_channels //= 2
self.ffwd = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
VAE_ResidualBlock(out_channels, out_channels),
VAE_AttentionBlock(out_channels, *attention_args, **attention_kwargs),
VAE_ResidualBlock(out_channels, out_channels),
*residual_layers,
VAE_ResidualBlock(current_channels, current_channels),
VAE_ResidualBlock(current_channels, current_channels),
VAE_ResidualBlock(
current_channels, current_channels
), # maybe one too many...
nn.GroupNorm(num_groups, current_channels),
nn.SiLU(),
nn.Conv2d(current_channels, image_channels, kernel_size=3, padding=1),
)
def forward(self, x: torch.Tensor, noise: torch.Tensor) -> torch.Tensor:
for module in self.ffwd:
if getattr(module, "stride", None) == (2, 2):
x = F.pad(x, (0, 1, 0, 1))
x = module(x)
mean, log_var = torch.chunk(x, 2, dim=1)
log_variance = torch.clamp(log_var, -30, 20)
variance = log_variance.exp()
stdev = variance.sqrt()
x = mean + stdev * noise
x *= 0.18215 # constant scale
return x