chuchichaestli.models.autoencoder
Autoencoder implementations and utilities.
Modules:
| Name | Description |
|---|---|
autoencoder |
A highly-customizable autoencoder implementation. |
dcae |
A flexible deep-compression autoencoder implementation. |
decoder |
Decoder modules for autoencoders. |
encoder |
Encoder modules for autoencoders (AEs). |
litevae |
A lightweight wavelet-domain variational autoencoder. |
traits |
Traits the two components of an autoencoder implementation. |
vae |
A flexible variational autoencoder implementation. |
vqvae |
A flexible vector-quantized variational autoencoder implementation. |
Classes:
| Name | Description |
|---|---|
Autoencoder |
Flexible autoencoder implementation. |
DCAE |
Flexible deep-compression autoencoder implementation. |
DCDecoder |
Decoding component of a deep-compression autoencoder. |
DCEncoder |
Encoding component of a deep-compression autoencoder. |
Decoder |
Flexible decoder implementation for autoencoders. |
DecoderLike |
Interface an autoencoder expects from its decoding component. |
Encoder |
Flexible encoder implementation for autoencoders. |
EncoderLike |
Interface an autoencoder expects from its encoding component. |
LiteVAE |
Lightweight wavelet-domain variational autoencoder. |
LiteVAEDecoder |
Decoding component of a |
LiteVAEEncoder |
Wavelet-domain encoding component of a |
LiteVAE_B |
|
LiteVAE_L |
|
LiteVAE_M |
|
LiteVAE_S |
|
VAE |
Flexible variational autoencoder implementation. |
VAEDecoder |
Decoding component of a variational autoencoder. |
VAEEncoder |
Encoding component of a variational autoencoder. |
VQVAE |
Flexible vector-quantized variational autoencoder implementation. |
VectorQuantizer |
Vector Quantizer for VQVAE. |
Autoencoder
Autoencoder(
encoder: EncoderLike,
decoder: DecoderLike,
latent_proj: nn.Module | bool = True,
latent_deproj: nn.Module | bool = True,
)
Flexible autoencoder implementation.
The architecture consists of an encoder-decoder structure. The encoder chains several residual and downsampling blocks. Each downsampling block separates the encoder into spatially hierarchical levels. The encoder ends in bottleneck blocks (optionally including attention blocks and a convolutional layer) and projects the input into latent space. The decoder is built with residual convolutional and upsampling blocks, and expands from the latent space to the image domain.
Both components are constructed externally and passed in, which lets a
configuration framework instantiate them as separate groups. Use the build
constructor to assemble a model from architecture arguments instead.
Attributes:
| Name | Type | Description |
|---|---|---|
encoder_cls |
type
|
Encoder class that |
decoder_cls |
type
|
Decoder class that |
Assemble an autoencoder from an encoding and a decoding component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder
|
EncoderLike
|
Encoding component, mapping the input to latent space. |
required |
decoder
|
DecoderLike
|
Decoding component, expanding the latent space to the output. |
required |
latent_proj
|
nn.Module | bool
|
Projection between encoder and latent space; |
True
|
latent_deproj
|
nn.Module | bool
|
Projection between latent space and decoder, with the
same options as |
True
|
Methods:
| Name | Description |
|---|---|
build |
Build a model from architecture arguments, components included. |
compute_latent_shape |
Compute the shape of the latent space. |
decode |
Decode the input. |
encode |
Encode the input. |
forward |
Forward pass through the model, i.e. encode and decode. |
projection |
Pointwise convolution matching the component it serves. |
Source code in src/chuchichaestli/models/autoencoder/autoencoder.py
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 | |
channel_mults
property
channel_mults: int
Total channel multiplication across the encoder levels.
f_comp
property
f_comp: int
Spatial compression factor of the encoder (number of spatial downsampling layers).
f_exp
property
f_exp: int
Spatial expansion factor of the decoder (number of spatial upsampling layers).
latent_dim
property
latent_dim: int
Latent channel dimension.
levels
property
levels: tuple[int, int]
Number of stages in the encoder and decoder.
build
classmethod
build(
dimensions: int = 2,
in_channels: int = 1,
out_channels: int = 1,
latent_dim: int = 4,
decoder_n_channels: int | None = None,
res_act_fn: ActivationTypes = "silu",
res_dropout: float = 0.0,
res_norm_type: NormTypes = "group",
res_groups: int = 8,
res_kernel_size: int = 3,
attn_head_dim: int = 32,
attn_n_heads: int = 1,
attn_dropout_p: float = 0.0,
attn_norm_type: NormTypes = "group",
attn_groups: int = 32,
attn_kernel_size: int = 1,
attn_scales: Sequence[int] = (5,),
context_args: dict = {},
local_args: dict = {},
encoder_args: dict = {},
decoder_args: dict = {},
**kwargs,
) -> Autoencoder
Build a model from architecture arguments, components included.
The res_* and attn_* arguments are shared by both components; anything
specific to one component goes into encoder_args or decoder_args, whose
keys are the parameter names of Encoder and Decoder. Keys left out
fall back to the defaults of encoder_cls and decoder_cls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
int
|
Number of dimensions for the model. |
2
|
in_channels
|
int
|
Number of input channels. |
1
|
out_channels
|
int
|
Number of output channels. |
1
|
latent_dim
|
int
|
Number of channels in the latent space. |
4
|
decoder_n_channels
|
int | None
|
Number of channels the decoder starts from; the encoder's bottleneck width if omitted, which is what mirrored components want. Give it when the two halves are sized independently. |
None
|
res_act_fn
|
ActivationTypes
|
Activation function for the residual blocks
(see |
'silu'
|
res_dropout
|
float
|
Dropout rate for the residual blocks. |
0.0
|
res_norm_type
|
NormTypes
|
Normalization type for the residual block
(see |
'group'
|
res_groups
|
int
|
Number of groups for the residual block normalization (if group norm). |
8
|
res_kernel_size
|
int
|
Kernel size for the residual blocks. |
3
|
attn_head_dim
|
int
|
Dimension of the attention heads. |
32
|
attn_n_heads
|
int
|
Number of attention heads. |
1
|
attn_dropout_p
|
float
|
Dropout probability of the scaled dot product attention. |
0.0
|
attn_norm_type
|
NormTypes
|
Normalization type for the convolutional attention block
(see |
'group'
|
attn_groups
|
int
|
Number of groups for the convolutional attention block normalization
(if |
32
|
attn_kernel_size
|
int
|
Kernel size for the convolutional attention block. |
1
|
attn_scales
|
Sequence[int]
|
Scales for the multi-scale attention block. |
(5,)
|
context_args
|
dict
|
Keyword arguments for the context block in a transformer module. |
{}
|
local_args
|
dict
|
Keyword arguments for the local block in a transformer module. |
{}
|
encoder_args
|
dict
|
Arguments for the encoding component, overriding its defaults.
Its |
{}
|
decoder_args
|
dict
|
Arguments for the decoding component, with the same options. |
{}
|
kwargs
|
Keyword arguments for the constructor, such as |
{}
|
Returns:
| Type | Description |
|---|---|
Autoencoder
|
An assembled model of the class this was called on. |
Source code in src/chuchichaestli/models/autoencoder/autoencoder.py
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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
compute_latent_shape
compute_latent_shape(
input_shape: tuple[int, ...], no_batch_dim: bool = False
)
Compute the shape of the latent space.
Source code in src/chuchichaestli/models/autoencoder/autoencoder.py
351 352 353 354 355 356 357 358 359 360 361 | |
decode
decode(z: torch.Tensor) -> torch.Tensor
Decode the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
torch.Tensor
|
Input latent tensor. |
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Image reconstructed from latent code. |
Source code in src/chuchichaestli/models/autoencoder/autoencoder.py
377 378 379 380 381 382 383 384 385 386 387 | |
encode
encode(x: torch.Tensor) -> torch.Tensor
Encode the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
torch.Tensor
|
Input tensor. |
required |
eps
|
Small constant value for numerical stability. |
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
Multivariate normal posterior distribution |
Source code in src/chuchichaestli/models/autoencoder/autoencoder.py
363 364 365 366 367 368 369 370 371 372 373 374 375 | |
forward
forward(x: torch.Tensor) -> torch.Tensor
Forward pass through the model, i.e. encode and decode.
Source code in src/chuchichaestli/models/autoencoder/autoencoder.py
389 390 391 392 | |
projection
staticmethod
projection(
component: EncoderLike | DecoderLike,
in_channels: int,
out_channels: int,
) -> nn.Module
Pointwise convolution matching the component it serves.
Device and dtype are read from the component's first parameter or buffer, so a component placed before the model is assembled keeps the projection alongside it; a component holding neither leaves both to the torch defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component
|
EncoderLike | DecoderLike
|
Encoding or decoding component the projection is attached to. |
required |
in_channels
|
int
|
Number of input channels. |
required |
out_channels
|
int
|
Number of output channels. |
required |
Source code in src/chuchichaestli/models/autoencoder/autoencoder.py
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 | |
DCAE
DCAE(
encoder: EncoderLike,
decoder: DecoderLike,
latent_proj: nn.Module | bool = False,
latent_deproj: nn.Module | bool = False,
)
Bases: Autoencoder
Flexible deep-compression autoencoder implementation.
The architecture consists of an encoder-decoder structure. The encoder chains several residual and downsampling blocks. Each downsampling block separates the encoder into spatially hierarchical levels. The encoder ends in bottleneck blocks (optionally including attention blocks and a convolutional layer) and projects the input into latent space. The decoder is built with residual convolutional, efficient ViT blocks, and upsampling blocks, and expands from the latent space to the image domain.
The compression happens in the blocks themselves, so no latent projection is used by default.
Attributes:
| Name | Type | Description |
|---|---|---|
encoder_cls |
type
|
Encoder class that |
decoder_cls |
type
|
Decoder class that |
Assemble a deep-compression autoencoder from its two components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder
|
EncoderLike
|
Encoding component, mapping the input to latent space. |
required |
decoder
|
DecoderLike
|
Decoding component, expanding the latent space to the output. |
required |
latent_proj
|
nn.Module | bool
|
Projection between encoder and latent space (see |
False
|
latent_deproj
|
nn.Module | bool
|
Projection between latent space and decoder. |
False
|
Methods:
| Name | Description |
|---|---|
build |
Build a deep-compression autoencoder from architecture arguments. |
Source code in src/chuchichaestli/models/autoencoder/dcae.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
build
classmethod
build(
latent_dim: int = 32,
res_norm_type: NormTypes = "rms",
attn_norm_type: NormTypes
| Sequence[NormTypes]
| None = "rms",
context_args: dict = DC_ATTN_ARGS["context_args"],
local_args: dict = DC_ATTN_ARGS["local_args"],
**kwargs,
) -> DCAE
Build a deep-compression autoencoder from architecture arguments.
Only the arguments whose defaults differ from Autoencoder.build are
named here; the components supply every structural default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dim
|
int
|
Number of channels in the latent space. |
32
|
res_norm_type
|
NormTypes
|
Normalization type for the residual blocks. |
'rms'
|
attn_norm_type
|
NormTypes | Sequence[NormTypes] | None
|
Normalization type for the attention blocks. |
'rms'
|
context_args
|
dict
|
Keyword arguments for the context block in a transformer module. |
DC_ATTN_ARGS['context_args']
|
local_args
|
dict
|
Keyword arguments for the local block in a transformer module. |
DC_ATTN_ARGS['local_args']
|
kwargs
|
Further architecture arguments, see |
{}
|
Returns:
| Type | Description |
|---|---|
DCAE
|
An assembled model of the class this was called on. |
Source code in src/chuchichaestli/models/autoencoder/dcae.py
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 260 261 262 | |
DCDecoder
DCDecoder(
dimensions: int = 2,
in_channels: int = 32,
n_channels: int = 1024,
out_channels: int = 1,
in_block_type: DecoderInBlockTypes = "DCDecoderInBlock",
mid_block_types: Sequence[
AutoencoderMidBlockTypes
] = (),
up_block_types: Sequence[AutoencoderUpBlockTypes] = (
"EfficientViTBlock",
)
* 3
+ ("DCAutoencoderUpBlock",) * 3,
block_out_channel_mults: Sequence[int] = (
1,
2,
1,
2,
2,
),
num_layers_per_block: int | Sequence[int] = 3,
upsample_type: UpsampleTypes = "UpsampleShuffle",
act_fn: ActivationTypes = "relu",
norm_type: NormTypes = "rms",
num_groups: int = 8,
kernel_size: int = 3,
res_args: dict = {},
attn_args: dict = {},
in_shortcut: bool = True,
)
Bases: Decoder
Decoding component of a deep-compression autoencoder.
Expands with channel-shuffling upsamplers, mirroring DCEncoder; see
Decoder for what each argument does. Blocks are listed in order of data
flow, deepest level first, so attention precedes the convolutional blocks.
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
int
|
Number of dimensions. |
2
|
in_channels
|
int
|
Number of input channels (latent space). |
32
|
n_channels
|
int
|
Number of channels for the first block. |
1024
|
out_channels
|
int
|
Number of output channels. |
1
|
in_block_type
|
DecoderInBlockTypes
|
Type of block for input (latent space). |
'DCDecoderInBlock'
|
mid_block_types
|
Sequence[AutoencoderMidBlockTypes]
|
Type of blocks to use after the input. |
()
|
up_block_types
|
Sequence[AutoencoderUpBlockTypes]
|
Type of up blocks to use for each level. |
('EfficientViTBlock',) * 3 + ('DCAutoencoderUpBlock',) * 3
|
block_out_channel_mults
|
Sequence[int]
|
Divisor for output channels of each block. |
(1, 2, 1, 2, 2)
|
num_layers_per_block
|
int | Sequence[int]
|
Number of blocks per level. |
3
|
upsample_type
|
UpsampleTypes
|
Type of upsampling block, per level. |
'UpsampleShuffle'
|
act_fn
|
ActivationTypes
|
Activation function for the output layers. |
'relu'
|
norm_type
|
NormTypes
|
Normalization type for the output layer. |
'rms'
|
num_groups
|
int
|
Number of groups for normalization in the output layer. |
8
|
kernel_size
|
int
|
Kernel size for the output convolution. |
3
|
res_args
|
dict
|
Arguments for residual blocks, overriding |
{}
|
attn_args
|
dict
|
Arguments for attention blocks, overriding |
{}
|
in_shortcut
|
bool
|
Whether to use a shortcut for the input block. |
True
|
Source code in src/chuchichaestli/models/autoencoder/dcae.py
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 | |
DCEncoder
DCEncoder(
dimensions: int = 2,
in_channels: int = 1,
n_channels: int = 128,
out_channels: int = 32,
down_block_types: Sequence[
AutoencoderDownBlockTypes
] = ("DCAutoencoderDownBlock",) * 3
+ ("EfficientViTBlock",) * 3,
block_out_channel_mults: Sequence[int] = (
2,
2,
1,
2,
1,
),
num_layers_per_block: int | Sequence[int] = (
2,
2,
2,
3,
3,
3,
),
mid_block_types: Sequence[
AutoencoderMidBlockTypes
] = (),
out_block_type: EncoderOutBlockTypes = "DCEncoderOutBlock",
downsample_type: DownsampleTypes = "DownsampleUnshuffle",
act_fn: ActivationTypes = "silu",
norm_type: NormTypes = "rms",
num_groups: int = 8,
kernel_size: int = 3,
res_args: dict = {},
attn_args: dict = {},
out_shortcut: bool = True,
)
Bases: Encoder
Encoding component of a deep-compression autoencoder.
Compresses with channel-shuffling downsamplers and efficient attention at
the deeper levels; see Encoder for what each argument does.
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
int
|
Number of dimensions. |
2
|
in_channels
|
int
|
Number of input channels. |
1
|
n_channels
|
int
|
Number of channels in the hidden layer. |
128
|
out_channels
|
int
|
Number of output channels (latent space). |
32
|
down_block_types
|
Sequence[AutoencoderDownBlockTypes]
|
Type of down blocks to use for each level. |
('DCAutoencoderDownBlock',) * 3 + ('EfficientViTBlock',) * 3
|
block_out_channel_mults
|
Sequence[int]
|
Multiplier for output channels of each block. |
(2, 2, 1, 2, 1)
|
num_layers_per_block
|
int | Sequence[int]
|
Number of blocks per level. |
(2, 2, 2, 3, 3, 3)
|
mid_block_types
|
Sequence[AutoencoderMidBlockTypes]
|
Type of blocks to use before the output. |
()
|
out_block_type
|
EncoderOutBlockTypes
|
Type of block for output (latent space). |
'DCEncoderOutBlock'
|
downsample_type
|
DownsampleTypes
|
Type of downsampling block, per level. |
'DownsampleUnshuffle'
|
act_fn
|
ActivationTypes
|
Activation function for the output layers. |
'silu'
|
norm_type
|
NormTypes
|
Normalization type for the output layer. |
'rms'
|
num_groups
|
int
|
Number of groups for normalization in the output layer. |
8
|
kernel_size
|
int
|
Kernel size for the output convolution. |
3
|
res_args
|
dict
|
Arguments for residual blocks, overriding |
{}
|
attn_args
|
dict
|
Arguments for attention blocks, overriding |
{}
|
out_shortcut
|
bool
|
Whether to use a shortcut for the output block. |
True
|
Source code in src/chuchichaestli/models/autoencoder/dcae.py
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 | |
Decoder
Decoder(
dimensions: int = 2,
in_channels: int = 1,
n_channels: int = 512,
out_channels: int = 1,
in_block_type: DecoderInBlockTypes = "DecoderInBlock",
mid_block_types: Sequence[AutoencoderMidBlockTypes] = (
"AutoencoderMidBlock",
"AttnAutoencoderMidBlock",
),
up_block_types: Sequence[AutoencoderUpBlockTypes] = (
"AutoencoderUpBlock",
"AutoencoderUpBlock",
"AutoencoderUpBlock",
"AutoencoderUpBlock",
),
block_out_channel_mults: Sequence[int] = (1, 2, 2, 2),
num_layers_per_block: int | Sequence[int] = 3,
upsample_type: UpsampleTypes
| Sequence[UpsampleTypes] = "UpsampleInterpolate",
act_fn: ActivationTypes = "silu",
norm_type: NormTypes = "group",
num_groups: int = 8,
kernel_size: int = 3,
res_args: dict = {},
attn_args: dict = {},
in_shortcut: bool = False,
)
Flexible decoder implementation for autoencoders.
Decoder implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
int
|
Number of dimensions. |
2
|
in_channels
|
int
|
Number of input channels (latent space). |
1
|
n_channels
|
int
|
Number of channels for first block. |
512
|
out_channels
|
int
|
Number of output channels. |
1
|
in_block_type
|
DecoderInBlockTypes
|
Type of block for output (latent space). |
'DecoderInBlock'
|
mid_block_types
|
Sequence[AutoencoderMidBlockTypes]
|
Type of blocks to use before the output. |
('AutoencoderMidBlock', 'AttnAutoencoderMidBlock')
|
up_block_types
|
Sequence[AutoencoderUpBlockTypes]
|
Type of up blocks to use for each level. |
('AutoencoderUpBlock', 'AutoencoderUpBlock', 'AutoencoderUpBlock', 'AutoencoderUpBlock')
|
block_out_channel_mults
|
Sequence[int]
|
Multiplier for output channels of each block. |
(1, 2, 2, 2)
|
num_layers_per_block
|
int | Sequence[int]
|
Number of blocks per level (blocks are repeated if |
3
|
upsample_type
|
UpsampleTypes | Sequence[UpsampleTypes]
|
Type of upsampling block, per level (see
|
'UpsampleInterpolate'
|
act_fn
|
ActivationTypes
|
Activation function for the output layers
(see |
'silu'
|
norm_type
|
NormTypes
|
Normalization type for the output layer. |
'group'
|
num_groups
|
int
|
Number of groups for normalization in the output layer. |
8
|
kernel_size
|
int
|
Kernel size for the output convolution. |
3
|
res_args
|
dict
|
Arguments for residual blocks. Each value is either a single value or one per block position, in order of data flow. |
{}
|
attn_args
|
dict
|
Arguments for attention blocks, per block position or per
block that has attention. |
{}
|
in_shortcut
|
bool
|
Whether to use a shortcut for the input block. |
False
|
Methods:
| Name | Description |
|---|---|
forward |
Decoding forward pass. |
Attributes:
| Name | Type | Description |
|---|---|---|
f |
int
|
Expansion factor of the decoder. |
Source code in src/chuchichaestli/models/autoencoder/decoder.py
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 | |
f
property
f: int
Expansion factor of the decoder.
Raises:
| Type | Description |
|---|---|
ValueError
|
If a sampling block does not scale by a constant factor. |
forward
forward(z: torch.Tensor) -> torch.Tensor
Decoding forward pass.
Source code in src/chuchichaestli/models/autoencoder/decoder.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
DecoderLike
Bases: Protocol
Interface an autoencoder expects from its decoding component.
Any module exposing these members can be passed to Autoencoder; inheriting
from Decoder is not required. Attributes absent from a custom component simply
skip the consistency checks that read them.
Attributes:
| Name | Type | Description |
|---|---|---|
dimensions |
int
|
Number of spatial dimensions. |
in_channels |
int
|
Number of latent channels consumed. |
n_channels |
int
|
Number of channels after the input block. |
out_channels |
int
|
Number of output channels. |
channel_mults |
int
|
Total channel division across all levels. |
levels |
int
|
Number of spatially hierarchical levels. |
Methods:
| Name | Description |
|---|---|
__call__ |
Decode a latent tensor. |
buffers |
Iterate over the component's buffers. |
parameters |
Iterate over the component's parameters. |
f
property
f: int
Spatial expansion factor.
__call__
__call__(z: torch.Tensor) -> torch.Tensor
Decode a latent tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
torch.Tensor
|
Input latent tensor. |
required |
Source code in src/chuchichaestli/models/autoencoder/traits.py
97 98 99 100 101 102 103 | |
buffers
buffers() -> Iterator[torch.Tensor]
Iterate over the component's buffers.
Source code in src/chuchichaestli/models/autoencoder/traits.py
109 110 111 | |
parameters
parameters() -> Iterator[nn.Parameter]
Iterate over the component's parameters.
Source code in src/chuchichaestli/models/autoencoder/traits.py
105 106 107 | |
Encoder
Encoder(
dimensions: int = 2,
in_channels: int = 1,
n_channels: int = 64,
out_channels: int = 1,
down_block_types: Sequence[
AutoencoderDownBlockTypes
] = (
"AutoencoderDownBlock",
"AutoencoderDownBlock",
"AutoencoderDownBlock",
"AutoencoderDownBlock",
),
block_out_channel_mults: Sequence[int] = (1, 2, 2, 2),
num_layers_per_block: int | Sequence[int] = 2,
mid_block_types: Sequence[AutoencoderMidBlockTypes] = (
"AutoencoderMidBlock",
"AttnAutoencoderMidBlock",
),
out_block_type: EncoderOutBlockTypes = "EncoderOutBlock",
downsample_type: DownsampleTypes
| Sequence[DownsampleTypes] = "Downsample",
act_fn: ActivationTypes = "silu",
norm_type: NormTypes = "group",
num_groups: int = 8,
kernel_size: int = 3,
res_args: dict = {},
attn_args: dict = {},
out_shortcut: bool = False,
)
Flexible encoder implementation for autoencoders.
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
int
|
Number of dimensions. |
2
|
in_channels
|
int
|
Number of input channels. |
1
|
n_channels
|
int
|
Number of channels in the hidden layer. |
64
|
out_channels
|
int
|
Number of output channels (latent space). |
1
|
down_block_types
|
Sequence[AutoencoderDownBlockTypes]
|
Type of down blocks to use for each level. |
('AutoencoderDownBlock', 'AutoencoderDownBlock', 'AutoencoderDownBlock', 'AutoencoderDownBlock')
|
block_out_channel_mults
|
Sequence[int]
|
Multiplier for output channels of each block. |
(1, 2, 2, 2)
|
num_layers_per_block
|
int | Sequence[int]
|
Number of blocks per level (blocks are repeated if |
2
|
mid_block_types
|
Sequence[AutoencoderMidBlockTypes]
|
Type of blocks to use before the output. |
('AutoencoderMidBlock', 'AttnAutoencoderMidBlock')
|
out_block_type
|
EncoderOutBlockTypes
|
Type of block for output (latent space). |
'EncoderOutBlock'
|
downsample_type
|
DownsampleTypes | Sequence[DownsampleTypes]
|
Type of downsampling block, per level (see
|
'Downsample'
|
act_fn
|
ActivationTypes
|
Activation function for the output layers
(see |
'silu'
|
norm_type
|
NormTypes
|
Normalization type for the output layer. |
'group'
|
num_groups
|
int
|
Number of groups for normalization in the output layer. |
8
|
kernel_size
|
int
|
Kernel size for the output convolution. |
3
|
res_args
|
dict
|
Arguments for residual blocks. Each value is either a single value or one per block position, in order of data flow. |
{}
|
attn_args
|
dict
|
Arguments for attention blocks, per block position or per
block that has attention. |
{}
|
out_shortcut
|
bool
|
Whether to use a shortcut for the output block. |
False
|
Methods:
| Name | Description |
|---|---|
forward |
Forward pass. |
Attributes:
| Name | Type | Description |
|---|---|---|
f |
int
|
Compression factor of the encoder. |
Source code in src/chuchichaestli/models/autoencoder/encoder.py
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 | |
f
property
f: int
Compression factor of the encoder.
Raises:
| Type | Description |
|---|---|
ValueError
|
If a sampling block does not scale by a constant factor. |
forward
forward(x)
Forward pass.
Source code in src/chuchichaestli/models/autoencoder/encoder.py
216 217 218 219 220 221 222 223 224 225 226 227 228 | |
EncoderLike
Bases: Protocol
Interface an autoencoder expects from its encoding component.
Any module exposing these members can be passed to Autoencoder; inheriting
from Encoder is not required. Attributes absent from a custom component simply
skip the consistency checks that read them.
Attributes:
| Name | Type | Description |
|---|---|---|
dimensions |
int
|
Number of spatial dimensions. |
in_channels |
int
|
Number of input channels. |
n_channels |
int
|
Number of channels after the input convolution. |
out_channels |
int
|
Number of channels emitted. |
latent_channels |
int
|
Number of latent channels |
bottleneck_channels |
int
|
Number of channels entering the output block. |
channel_mults |
int
|
Total channel multiplication across all levels. |
levels |
int
|
Number of spatially hierarchical levels. |
Methods:
| Name | Description |
|---|---|
__call__ |
Encode an input tensor. |
buffers |
Iterate over the component's buffers. |
parameters |
Iterate over the component's parameters. |
f
property
f: int
Spatial compression factor.
__call__
__call__(x: torch.Tensor) -> torch.Tensor
buffers
buffers() -> Iterator[torch.Tensor]
Iterate over the component's buffers.
Source code in src/chuchichaestli/models/autoencoder/traits.py
63 64 65 | |
parameters
parameters() -> Iterator[nn.Parameter]
Iterate over the component's parameters.
Source code in src/chuchichaestli/models/autoencoder/traits.py
59 60 61 | |
LiteVAE
LiteVAE(
encoder: EncoderLike,
decoder: DecoderLike,
latent_proj: nn.Module | bool = False,
latent_deproj: nn.Module | bool = False,
)
Bases: VAE
Lightweight wavelet-domain variational autoencoder.
Replaces the convolutional encoder of a variational autoencoder with a multi-level wavelet transform followed by small per-scale feature extractors and an aggregation network, which reaches the same reconstruction quality with a fraction of the encoder parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
encoder_cls |
type
|
Encoder class that |
decoder_cls |
type
|
Decoder class that |
Assemble a wavelet-domain variational autoencoder from its components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder
|
EncoderLike
|
Encoding component; must double its latent channels. |
required |
decoder
|
DecoderLike
|
Decoding component, expanding the latent space to the output. |
required |
latent_proj
|
nn.Module | bool
|
Projection between encoder and latent space; omitted by default, which reconstructs better than a pointwise convolution. |
False
|
latent_deproj
|
nn.Module | bool
|
Projection between latent space and decoder. |
False
|
Methods:
| Name | Description |
|---|---|
build |
Build a model from architecture arguments, components included. |
Source code in src/chuchichaestli/models/autoencoder/litevae.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
build
classmethod
build(
dimensions: int = 2,
in_channels: int = 3,
out_channels: int = 3,
latent_dim: int = 12,
decoder_n_channels: int = 512,
**kwargs,
) -> LiteVAE
Build a model from architecture arguments, components included.
Every architecture argument is open; the published sizes are available
as the LiteVAE_S, LiteVAE_B, LiteVAE_M and LiteVAE_L subclasses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
int
|
Number of dimensions for the model. |
2
|
in_channels
|
int
|
Number of input channels. |
3
|
out_channels
|
int
|
Number of output channels. |
3
|
latent_dim
|
int
|
Number of channels in the latent space. |
12
|
decoder_n_channels
|
int
|
Number of channels the decoder starts from. |
512
|
kwargs
|
Further arguments for |
{}
|
Source code in src/chuchichaestli/models/autoencoder/litevae.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
LiteVAEDecoder
LiteVAEDecoder(
dimensions: int = 2,
in_channels: int = 12,
n_channels: int = 512,
out_channels: int = 3,
up_block_types: Sequence[str] = (
"SMConvAutoencoderUpBlock",
)
* 4,
mid_block_types: Sequence[str] = (
"SMConvAutoencoderMidBlock",
"AttnAutoencoderMidBlock",
),
block_out_channel_mults: Sequence[int] = (1, 1, 2, 2),
num_layers_per_block: int | Sequence[int] = 3,
act_fn: ActivationTypes = "silu",
num_groups: int = 8,
kernel_size: int = 3,
res_args: dict = {},
**kwargs,
)
Bases: Decoder
Decoding component of a LiteVAE.
Structurally the decoder of a standard latent diffusion autoencoder, with
self-modulated convolutions in place of the normalizations of its residual
blocks; see Decoder for the architecture arguments. The attention block of
the bottleneck keeps its own normalization.
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
int
|
Number of dimensions. |
2
|
in_channels
|
int
|
Number of latent channels consumed. |
12
|
n_channels
|
int
|
Number of channels the decoder starts from. |
512
|
out_channels
|
int
|
Number of output channels. |
3
|
up_block_types
|
Sequence[str]
|
Type of up blocks to use for each level. |
('SMConvAutoencoderUpBlock',) * 4
|
mid_block_types
|
Sequence[str]
|
Type of blocks to use after the input. |
('SMConvAutoencoderMidBlock', 'AttnAutoencoderMidBlock')
|
block_out_channel_mults
|
Sequence[int]
|
Divisor for the output channels of each block. |
(1, 1, 2, 2)
|
num_layers_per_block
|
int | Sequence[int]
|
Number of blocks per level. |
3
|
act_fn
|
ActivationTypes
|
Activation function. |
'silu'
|
num_groups
|
int
|
Number of groups for the normalization; also the default for the residual blocks, which are narrower here than usual. |
8
|
kernel_size
|
int
|
Kernel size for the output convolution. |
3
|
res_args
|
dict
|
Arguments for the residual blocks. |
{}
|
kwargs
|
Further architecture arguments for |
{}
|
Source code in src/chuchichaestli/models/autoencoder/litevae.py
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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
LiteVAEEncoder
LiteVAEEncoder(
dimensions: int = 2,
in_channels: int = 3,
n_channels: int = 32,
out_channels: int = 12,
dwt_levels: int = 3,
wavelet: str | Wavelet = "haar",
mode: ExtensionModeTypes = "periodization",
extractor_channel_mults: Sequence[int] = (3, 1),
extractor_num_blocks: int = 4,
aggregator_channels: int | None = None,
aggregator_channel_mults: Sequence[int] = (3, 1),
aggregator_num_blocks: int = 4,
act_fn: ActivationTypes = "silu",
norm_type: NormTypes = "group",
num_groups: int = 8,
res_args: dict = {},
attn_args: dict = {},
extractor_args: dict = {},
aggregator_args: dict = {},
)
Wavelet-domain encoding component of a LiteVAE.
The input is decomposed by a multi-level wavelet transform, every level is processed by its own constant-resolution U-Net, the results are pooled to a common resolution and a second U-Net aggregates them into the latent code. All spatial compression comes from the wavelet transform, so the U-Nets keep their resolution and only widen between levels.
Satisfies EncoderLike, so it can be passed to VAE and Autoencoder
without inheriting from Encoder. Like VAEEncoder it emits twice its
latent channels, a mean and a variance.
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
int
|
Number of dimensions. |
2
|
in_channels
|
int
|
Number of input channels. |
3
|
n_channels
|
int
|
Number of channels in the feature extractors. |
32
|
out_channels
|
int
|
Number of latent channels; twice as many are emitted. |
12
|
dwt_levels
|
int
|
Number of wavelet levels, and hence the compression factor. |
3
|
wavelet
|
str | Wavelet
|
Wavelet to decompose with, by name or as a |
'haar'
|
mode
|
ExtensionModeTypes
|
Signal extension mode; the default is critically sampled, and hence the only one that halves each axis exactly. |
'periodization'
|
extractor_channel_mults
|
Sequence[int]
|
Channel multiplier per level of an extractor, as the ratio to the level before it. |
(3, 1)
|
extractor_num_blocks
|
int
|
Number of blocks per level of an extractor. |
4
|
aggregator_channels
|
int | None
|
Number of channels in the aggregator; as many as the extractors if omitted. |
None
|
aggregator_channel_mults
|
Sequence[int]
|
Channel multiplier per level of the aggregator, as the ratio to the level before it. |
(3, 1)
|
aggregator_num_blocks
|
int
|
Number of blocks per level of the aggregator. |
4
|
act_fn
|
ActivationTypes
|
Activation function. |
'silu'
|
norm_type
|
NormTypes
|
Normalization type. |
'group'
|
num_groups
|
int
|
Number of groups for the normalization. |
8
|
res_args
|
dict
|
Arguments for the residual blocks; those a |
{}
|
attn_args
|
dict
|
Arguments for the attention blocks; those a |
{}
|
extractor_args
|
dict
|
Further architecture arguments for the extractors. |
{}
|
aggregator_args
|
dict
|
Further architecture arguments for the aggregator. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than one wavelet level is requested, or if the extension mode is not critically sampled. |
Methods:
| Name | Description |
|---|---|
forward |
Forward pass through the encoder. |
Attributes:
| Name | Type | Description |
|---|---|---|
f |
int
|
Compression factor of the encoder, which the wavelet levels alone set. |
Source code in src/chuchichaestli/models/autoencoder/litevae.py
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 | |
f
property
f: int
Compression factor of the encoder, which the wavelet levels alone set.
forward
forward(x: torch.Tensor) -> torch.Tensor
Forward pass through the encoder.
Source code in src/chuchichaestli/models/autoencoder/litevae.py
199 200 201 202 203 204 205 206 207 208 | |
LiteVAE_B
LiteVAE_B(
encoder: EncoderLike,
decoder: DecoderLike,
latent_proj: nn.Module | bool = False,
latent_deproj: nn.Module | bool = False,
)
Bases: LiteVAE
LiteVAE with the base published encoder, about 7M parameters (default).
Source code in src/chuchichaestli/models/autoencoder/litevae.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
LiteVAE_L
LiteVAE_L(
encoder: EncoderLike,
decoder: DecoderLike,
latent_proj: nn.Module | bool = False,
latent_deproj: nn.Module | bool = False,
)
Bases: LiteVAE
LiteVAE with the large published encoder, about 42M parameters.
Source code in src/chuchichaestli/models/autoencoder/litevae.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
LiteVAE_M
LiteVAE_M(
encoder: EncoderLike,
decoder: DecoderLike,
latent_proj: nn.Module | bool = False,
latent_deproj: nn.Module | bool = False,
)
Bases: LiteVAE
LiteVAE with the medium published encoder, about 32M parameters.
Source code in src/chuchichaestli/models/autoencoder/litevae.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
LiteVAE_S
LiteVAE_S(
encoder: EncoderLike,
decoder: DecoderLike,
latent_proj: nn.Module | bool = False,
latent_deproj: nn.Module | bool = False,
)
Bases: LiteVAE
LiteVAE with the small published encoder, about 1M parameters.
Source code in src/chuchichaestli/models/autoencoder/litevae.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
VAE
VAE(
encoder: EncoderLike,
decoder: DecoderLike,
latent_proj: nn.Module | bool = True,
latent_deproj: nn.Module | bool = True,
)
Bases: Autoencoder
Flexible variational autoencoder implementation.
The architecture consists of an encoder-decoder structure. The encoder chains several residual and downsampling blocks. Each downsampling block separates the encoder into spatially hierarchical levels. The encoder ends in bottleneck blocks (optionally including attention blocks and a convolutional layer) and projects the input into latent space. Latent mean and variance are sampled from a Gaussian and passed to the decoder. The decoder is built with residual convolutional and upsampling blocks, and expands from the latent space to the image domain.
Attributes:
| Name | Type | Description |
|---|---|---|
encoder_cls |
type
|
Encoder class that |
decoder_cls |
type
|
Decoder class that |
Assemble a variational autoencoder from its two components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder
|
EncoderLike
|
Encoding component; must double its latent channels. |
required |
decoder
|
DecoderLike
|
Decoding component, expanding the latent space to the output. |
required |
latent_proj
|
nn.Module | bool
|
Projection between encoder and latent space (see |
True
|
latent_deproj
|
nn.Module | bool
|
Projection between latent space and decoder. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the encoder does not double its latent channels. |
Methods:
| Name | Description |
|---|---|
encode |
Encode the input. |
forward |
Forward pass through the model. |
kl_divergence |
Compute the KL divergence between posterior and a multivariate Gaussian. |
Source code in src/chuchichaestli/models/autoencoder/vae.py
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 | |
encode
encode(
x: torch.Tensor, eps: float = 1e-12
) -> MultivariateNormal
Encode the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
torch.Tensor
|
Input tensor. |
required |
eps
|
float
|
Small constant value for numerical stability. |
1e-12
|
Returns:
| Type | Description |
|---|---|
MultivariateNormal
|
Multivariate normal posterior distribution |
Source code in src/chuchichaestli/models/autoencoder/vae.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
forward
forward(
x: torch.Tensor,
sample_posterior: bool = True,
eps: float = 1e-12,
) -> tuple[torch.Tensor, torch.Tensor]
Forward pass through the model.
Source code in src/chuchichaestli/models/autoencoder/vae.py
129 130 131 132 133 134 135 136 137 138 | |
kl_divergence
staticmethod
kl_divergence(
posterior: torch.distributions.MultivariateNormal,
)
Compute the KL divergence between posterior and a multivariate Gaussian.
Source code in src/chuchichaestli/models/autoencoder/vae.py
140 141 142 143 144 145 146 147 | |
VAEDecoder
VAEDecoder(
dimensions: int = 2,
in_channels: int = 1,
n_channels: int = 512,
out_channels: int = 1,
in_block_type: DecoderInBlockTypes = "DecoderInBlock",
mid_block_types: Sequence[AutoencoderMidBlockTypes] = (
"AutoencoderMidBlock",
"AttnAutoencoderMidBlock",
),
up_block_types: Sequence[AutoencoderUpBlockTypes] = (
"AutoencoderUpBlock",
"AutoencoderUpBlock",
"AutoencoderUpBlock",
"AutoencoderUpBlock",
),
block_out_channel_mults: Sequence[int] = (1, 2, 2, 2),
num_layers_per_block: int | Sequence[int] = 3,
upsample_type: UpsampleTypes
| Sequence[UpsampleTypes] = "UpsampleInterpolate",
act_fn: ActivationTypes = "silu",
norm_type: NormTypes = "group",
num_groups: int = 8,
kernel_size: int = 3,
res_args: dict = {},
attn_args: dict = {},
in_shortcut: bool = False,
)
Bases: Decoder
Decoding component of a variational autoencoder.
Consumes a latent code that sampling has already collapsed to a single set
of channels, so structurally it is a plain Decoder; see there for the
architecture arguments.
Source code in src/chuchichaestli/models/autoencoder/decoder.py
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 | |
VAEEncoder
VAEEncoder(
dimensions: int = 2,
in_channels: int = 1,
n_channels: int = 64,
out_channels: int = 1,
**kwargs,
)
Bases: Encoder
Encoding component of a variational autoencoder.
Doubles the latent channels so that a mean and a variance can be read from
them; see Encoder for the architecture arguments.
Constructor.
The leading arguments are named so that out_channels is doubled
whether it is passed positionally or by keyword.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimensions
|
int
|
Number of dimensions. |
2
|
in_channels
|
int
|
Number of input channels. |
1
|
n_channels
|
int
|
Number of channels in the hidden layer. |
64
|
out_channels
|
int
|
Number of latent channels; twice as many are emitted. |
1
|
kwargs
|
Further architecture arguments for |
{}
|
Source code in src/chuchichaestli/models/autoencoder/vae.py
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 | |
VQVAE
VQVAE(
encoder: EncoderLike,
decoder: DecoderLike,
vq_dim: int = 64,
vq_embeddings: int = 512,
)
Bases: Autoencoder
Flexible vector-quantized variational autoencoder implementation.
The architecture consists of an encoder-decoder structure with a codebook bottleneck. The encoder chains several residual and downsampling blocks. Each downsampling block separates the encoder into spatially hierarchical levels. The encoder ends in bottleneck blocks (optionally including attention blocks and a convolutional layer). The latent code is quantized with the codebook. The decoder is built with residual convolutional and upsampling blocks, and expands from the (quantized) latent space to the image domain.
Assemble a vector-quantized autoencoder from its two components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder
|
EncoderLike
|
Encoding component, mapping the input to latent space. |
required |
decoder
|
DecoderLike
|
Decoding component, expanding the latent space to the output. |
required |
vq_dim
|
int
|
Size of the quantized embedding vectors. |
64
|
vq_embeddings
|
int
|
Size of the quantization codebook. |
512
|
Methods:
| Name | Description |
|---|---|
compute_embedding_shape |
Compute the shape of the latent space. |
encode |
Encode the input. |
forward |
Forward pass through the model. |
Source code in src/chuchichaestli/models/autoencoder/vqvae.py
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 | |
compute_embedding_shape
compute_embedding_shape(
input_shape: tuple[int, ...], no_batch_dim: bool = False
)
Compute the shape of the latent space.
Source code in src/chuchichaestli/models/autoencoder/vqvae.py
156 157 158 159 160 161 162 163 164 165 166 | |
encode
encode(
x: torch.Tensor | torch.LongTensor,
codebook_usage: bool = False,
force_no_quant: bool = False,
load_from_codebook: bool = False,
shape: tuple[int, ...] | None = None,
) -> tuple[torch.Tensor, torch.Tensor, dict]
Encode the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
torch.Tensor | torch.LongTensor
|
Input tensor or codebook indices (if |
required |
codebook_usage
|
bool
|
If |
False
|
force_no_quant
|
bool
|
If |
False
|
load_from_codebook
|
bool
|
If |
False
|
shape
|
tuple[int, ...] | None
|
Full latent shape for codebook entries (with the embedding dimension in the last axis). |
None
|
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
|
torch.Tensor
|
|
dict
|
|
Source code in src/chuchichaestli/models/autoencoder/vqvae.py
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 | |
forward
forward(
x: torch.Tensor | torch.LongTensor,
codebook_usage: bool = False,
force_no_quant: bool = False,
load_from_codebook: bool = False,
shape: tuple[int, ...] | None = None,
) -> tuple[torch.Tensor, torch.Tensor, dict]
Forward pass through the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
torch.Tensor | torch.LongTensor
|
Input tensor or codebook indices (if |
required |
codebook_usage
|
bool
|
If |
False
|
force_no_quant
|
bool
|
If |
False
|
load_from_codebook
|
bool
|
If |
False
|
shape
|
tuple[int, ...] | None
|
Full latent shape for codebook entries (with the embedding dimension in the last axis). |
None
|
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
|
torch.Tensor
|
|
dict
|
|
Source code in src/chuchichaestli/models/autoencoder/vqvae.py
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 | |
VectorQuantizer
VectorQuantizer(
num_embeddings: int,
embedding_dim: int,
beta: float = 0.25,
)
Vector Quantizer for VQVAE.
Initialize VectorQuantizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_embeddings
|
int
|
Size of the codebook. |
required |
embedding_dim
|
int
|
Size of the embedding vectors. |
required |
beta
|
float
|
Commitment cost parameter for the loss. |
0.25
|
Methods:
| Name | Description |
|---|---|
forward |
Forward pass through the vector quantizer (codebook lookup). |
get_codebook_entry |
Get the codebook entry for the given indices. |
Attributes:
| Name | Type | Description |
|---|---|---|
embedding_dim |
int
|
Size of each embedding vector. |
num_embeddings |
int
|
Size of the embedding dictionary. |
Source code in src/chuchichaestli/models/autoencoder/vqvae.py
18 19 20 21 22 23 24 25 26 27 28 29 | |
embedding_dim
property
embedding_dim: int
Size of each embedding vector.
num_embeddings
property
num_embeddings: int
Size of the embedding dictionary.
forward
forward(
z: torch.Tensor, codebook_usage: bool = False
) -> tuple[torch.Tensor, torch.Tensor, dict]
Forward pass through the vector quantizer (codebook lookup).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
torch.Tensor
|
Input latent codes of shape |
required |
codebook_usage
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
|
torch.Tensor
|
|
dict
|
|
Source code in src/chuchichaestli/models/autoencoder/vqvae.py
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 | |
get_codebook_entry
get_codebook_entry(
indices: torch.LongTensor,
shape: tuple[int, ...] | None = None,
) -> torch.Tensor
Get the codebook entry for the given indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
torch.LongTensor
|
Indices from the codebook. |
required |
shape
|
tuple[int, ...] | None
|
Full latent shape (with the embedding dimension in the last axis). |
None
|
Source code in src/chuchichaestli/models/autoencoder/vqvae.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |