File size: 5,678 Bytes
a04730e | 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 | from __future__ import annotations
import torch
from torch import nn
from src.models.autoencoder.encoder import Encoder
from src.models.autoencoder.decoder import Decoder
from src.models.autoencoder.distributions import DiagonalGaussianDistribution
class AutoencoderKL(nn.Module):
"""
VAE / AutoencoderKL wrapper
posterior = vae.encode(x)
z = posterior.sample()
x_recon = vae.decode(z)
Or directly:
x_recon, posterior, z = vae(x)
Input image range:
x in [-1, 1]
Output image range:
x_recon in [-1, 1]
"""
def __init__(
self,
in_channels: int = 3,
out_channels: int = 3,
latent_channels: int = 8,
base_channels: int = 128,
channel_multipliers: list[int] | tuple[int, ...] = (1, 2, 4, 4),
num_res_blocks: int = 3,
dropout: float = 0.0,
use_attention: bool = True,
attention_heads: int = 4,
scaling_factor: float = 1.0,
attention_resolutions: tuple[int, ...] = (32,),
):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.latent_channels = latent_channels
self.base_channels = base_channels
self.channel_multipliers = list(channel_multipliers)
self.num_res_blocks = num_res_blocks
self.scaling_factor = scaling_factor
self.encoder = Encoder(
in_channels=in_channels,
latent_channels=latent_channels,
base_channels=base_channels,
channel_multipliers=channel_multipliers,
num_res_blocks=num_res_blocks,
dropout=dropout,
use_attention=use_attention,
attention_heads=attention_heads,
attention_resolutions= attention_resolutions
)
self.decoder = Decoder(
out_channels=out_channels,
latent_channels=latent_channels,
base_channels=base_channels,
channel_multipliers=channel_multipliers,
num_res_blocks=num_res_blocks,
dropout=dropout,
use_attention=use_attention,
attention_heads=attention_heads,
)
def encode(
self,
x: torch.Tensor,
deterministic: bool = False,
) -> DiagonalGaussianDistribution:
"""
Encode image into posterior distribution.
deterministic:
If True, posterior.sample() will return mean only.
Returns:
DiagonalGaussianDistribution.
"""
moments = self.encoder(x)
posterior = DiagonalGaussianDistribution(
moments=moments,
deterministic=deterministic,
)
return posterior
def decode(
self,
z: torch.Tensor,
unscale: bool = True,
) -> torch.Tensor:
"""
Decode latent into image
z:
Latent tensor [B, latent_channels, H/8, W/8].
unscale:
If True, divide by scaling_factor before decoding.
Returns:
Reconstructed image in [-1, 1].
"""
if unscale:
z = z / self.scaling_factor
return self.decoder(z)
def forward(
self,
x: torch.Tensor,
sample_posterior: bool = True,
) -> tuple[torch.Tensor, DiagonalGaussianDistribution, torch.Tensor]:
"""
Full VAE forward pass.
Args:
x:
Image tensor [B, 3, H, W], normalized to [-1, 1].
sample_posterior:
If True:
z = posterior.sample()
If False:
z = posterior.mode()
Returns:
x_recon:
Reconstructed image [B, 3, H, W].
posterior:
DiagonalGaussianDistribution object.
z:
Latent tensor before scaling.
"""
posterior = self.encode(x)
if sample_posterior:
z = posterior.sample()
else:
z = posterior.mode()
x_recon = self.decode(z, unscale=False)
return x_recon, posterior, z
@torch.no_grad()
def reconstruct(
self,
x: torch.Tensor,
sample_posterior: bool = False,
) -> torch.Tensor:
"""
Convenience method for inference reconstruction.
By default, uses posterior mode for stable reconstructions.
"""
x_recon, _, _ = self.forward(
x,
sample_posterior=sample_posterior,
)
return x_recon
@torch.no_grad()
def encode_to_latent(
self,
x: torch.Tensor,
sample_posterior: bool = False,
scale: bool = True,
) -> torch.Tensor:
"""
Encode image into latent tensor for latent diffusion training.
Usually for latent caching, use:
sample_posterior=False
because the posterior mean is deterministic and stable.
If scale=True:
z_scaled = z * scaling_factor
Stable Diffusion-style LDMs often scale latents before diffusion.
"""
posterior = self.encode(x)
if sample_posterior:
z = posterior.sample()
else:
z = posterior.mode()
if scale:
z = z * self.scaling_factor
return z
@torch.no_grad()
def decode_from_latent(
self,
z: torch.Tensor,
unscale: bool = True,
) -> torch.Tensor:
"""
Decode latent tensor produced by diffusion model.
"""
return self.decode(z, unscale=unscale) |