Spaces:
Build error
Build error
File size: 6,932 Bytes
c9cd3be |
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 |
"""
AUTOENCODER WITH ARCHTECTURE FROM VERSION 2
"""
from typing import Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
@torch.jit.script
def swish(x):
return x * torch.sigmoid(x)
def Normalize(in_channels):
return nn.GroupNorm(
num_groups=32,
num_channels=in_channels,
eps=1e-6,
affine=True
)
class Upsample(nn.Module):
def __init__(self, in_channels):
super().__init__()
self.conv = nn.Conv3d(
in_channels,
in_channels,
kernel_size=3,
stride=1,
padding=1
)
def forward(self, x):
x = F.interpolate(x, scale_factor=2.0, mode="nearest")
x = self.conv(x)
return x
class Downsample(nn.Module):
def __init__(self, in_channels):
super().__init__()
self.conv = nn.Conv3d(
in_channels,
in_channels,
kernel_size=3,
stride=2,
padding=0
)
def forward(self, x):
pad = (0, 1, 0, 1, 0, 1)
x = nn.functional.pad(x, pad, mode="constant", value=0)
x = self.conv(x)
return x
class ResBlock(nn.Module):
def __init__(self, in_channels, out_channels=None):
super().__init__()
self.in_channels = in_channels
self.out_channels = in_channels if out_channels is None else out_channels
self.norm1 = Normalize(in_channels)
self.conv1 = nn.Conv3d(
in_channels,
out_channels,
kernel_size=3,
stride=1,
padding=1
)
self.norm2 = Normalize(out_channels)
self.conv2 = nn.Conv3d(
out_channels,
out_channels,
kernel_size=3,
stride=1,
padding=1
)
if self.in_channels != self.out_channels:
self.nin_shortcut = nn.Conv3d(
in_channels,
out_channels,
kernel_size=1,
stride=1,
padding=0
)
def forward(self, x):
h = x
h = self.norm1(h)
h = F.silu(h)
h = self.conv1(h)
h = self.norm2(h)
h = F.silu(h)
h = self.conv2(h)
if self.in_channels != self.out_channels:
x = self.nin_shortcut(x)
return x + h
class Encoder(nn.Module):
def __init__(
self,
in_channels: int,
n_channels: int,
z_channels: int,
ch_mult: Tuple[int],
num_res_blocks: int,
resolution: Tuple[int],
attn_resolutions: Tuple[int],
**ignorekwargs,
) -> None:
super().__init__()
self.in_channels = in_channels
self.n_channels = n_channels
self.num_resolutions = len(ch_mult)
self.num_res_blocks = num_res_blocks
self.resolution = resolution
self.attn_resolutions = attn_resolutions
curr_res = resolution
in_ch_mult = (1,) + tuple(ch_mult)
blocks = []
# initial convolution
blocks.append(
nn.Conv3d(
in_channels,
n_channels,
kernel_size=3,
stride=1,
padding=1
)
)
# residual and downsampling blocks, with attention on smaller res (16x16)
for i in range(self.num_resolutions):
block_in_ch = n_channels * in_ch_mult[i]
block_out_ch = n_channels * ch_mult[i]
for _ in range(self.num_res_blocks):
blocks.append(ResBlock(block_in_ch, block_out_ch))
block_in_ch = block_out_ch
if i != self.num_resolutions - 1:
blocks.append(Downsample(block_in_ch))
curr_res = tuple(ti // 2 for ti in curr_res)
# normalise and convert to latent size
blocks.append(Normalize(block_in_ch))
blocks.append(
nn.Conv3d(
block_in_ch,
z_channels,
kernel_size=3,
stride=1,
padding=1
)
)
self.blocks = nn.ModuleList(blocks)
def forward(self, x):
for block in self.blocks:
x = block(x)
return x
class Decoder(nn.Module):
def __init__(
self,
n_channels: int,
z_channels: int,
out_channels: int,
ch_mult: Tuple[int],
num_res_blocks: int,
resolution: Tuple[int],
attn_resolutions: Tuple[int],
**ignorekwargs,
) -> None:
super().__init__()
self.n_channels = n_channels
self.z_channels = z_channels
self.out_channels = out_channels
self.ch_mult = ch_mult
self.num_resolutions = len(ch_mult)
self.num_res_blocks = num_res_blocks
self.resolution = resolution
self.attn_resolutions = attn_resolutions
block_in_ch = n_channels * self.ch_mult[-1]
curr_res = tuple(ti // 2 ** (self.num_resolutions - 1) for ti in resolution)
blocks = []
# initial conv
blocks.append(
nn.Conv3d(
z_channels,
block_in_ch,
kernel_size=3,
stride=1,
padding=1
)
)
for i in reversed(range(self.num_resolutions)):
block_out_ch = n_channels * self.ch_mult[i]
for _ in range(self.num_res_blocks):
blocks.append(ResBlock(block_in_ch, block_out_ch))
block_in_ch = block_out_ch
if i != 0:
blocks.append(Upsample(block_in_ch))
curr_res = tuple(ti * 2 for ti in curr_res)
blocks.append(Normalize(block_in_ch))
blocks.append(
nn.Conv3d(
block_in_ch,
out_channels,
kernel_size=3,
stride=1,
padding=1
)
)
self.blocks = nn.ModuleList(blocks)
def forward(self, x):
for block in self.blocks:
x = block(x)
return x
class AutoencoderKL(nn.Module):
def __init__(self, embed_dim: int, hparams) -> None:
super().__init__()
self.encoder = Encoder(**hparams)
self.decoder = Decoder(**hparams)
self.quant_conv_mu = torch.nn.Conv3d(hparams["z_channels"], embed_dim, 1)
self.quant_conv_log_sigma = torch.nn.Conv3d(hparams["z_channels"], embed_dim, 1)
self.post_quant_conv = torch.nn.Conv3d(embed_dim, hparams["z_channels"], 1)
self.embed_dim = embed_dim
def decode(self, z):
z = self.post_quant_conv(z)
dec = self.decoder(z)
return dec
def reconstruct_ldm_outputs(self, z):
x_hat = self.decode(z)
return x_hat
|