Models

Variational autoencoder architectures. See Model architecture for a conceptual overview.

class phenocoder.model.Sampling(*args, **kwargs)

Bases: Layer

Custom Keras layer implementing the reparameterization trick for VAE sampling.

This layer samples from the latent space distribution using the reparameterization trick: z = mean + exp(0.5 * log_var) * epsilon, where epsilon ~ N(0, 1). This allows backpropagation through the sampling operation during training.

seed_generator

Keras random seed generator for reproducible sampling.

class phenocoder.model.CVAE(*args, **kwargs)[source]

Bases: Model

Convolutional Variational Autoencoder (CVAE) for image data.

A VAE implementation using convolutional layers for encoding and decoding. The model learns a compressed latent representation of input images and can reconstruct them. Uses the reparameterization trick for backpropagation through the stochastic latent space.

The loss function consists of: - Reconstruction loss: Binary cross-entropy between input and reconstruction - KL divergence loss: Regularizes the latent space to approximate N(0, 1) - Total loss: reconstruction_loss + beta * kl_loss

Architecture: - Encoder: Strided Conv2D layers -> Flatten -> Dense -> Latent (z_mean, z_log_var) - Decoder: Dense -> Reshape -> Conv2DTranspose layers -> Reconstruction

Parameters:
input_shape

Shape of input images (height, width, channels).

Type:

tuple

latent_dim

Dimensionality of the latent space.

Type:

int

dense_dim

Dimensionality of dense layers.

Type:

int

conv_layers

Number of filters in each convolutional layer.

Type:

tuple

dropout

Dropout rate for regularization.

Type:

float

beta

Weight for KL divergence loss (beta-VAE parameter).

Type:

float

encoder

Encoder model.

Type:

Model

decoder

Decoder model.

Type:

Model

Example

>>> model = CVAE(
...     input_shape=(128, 128, 4),
...     latent_dim=64,
...     dense_dim=256,
...     conv_layers=(8, 16, 32, 64, 128),
...     dropout=0.25,
...     beta=1.0
... )
>>> model.compile(optimizer='adam')
>>> model.fit(train_data, epochs=100)
build_encoder()[source]

Build the convolutional encoder network.

Stacks strided Conv2D layers (one per entry in self.conv_layers) followed by a dense projection, and outputs z_mean, z_log_var and the reparameterized latent sample z.

Returns:

Encoder mapping an input patch to [z_mean, z_log_var, z].

Return type:

keras.Model

build_decoder()[source]

Build the transposed-convolutional decoder network.

Projects the latent vector back to a spatial feature map and applies stacked Conv2DTranspose layers (upsampling) to reconstruct all input channels.

Returns:

Decoder mapping a latent vector to a reconstructed patch.

Return type:

keras.Model

property metrics
train_step(data)[source]
test_step(data)[source]
class phenocoder.model.CondCVAE(*args, **kwargs)[source]

Bases: CVAE

Conditional Convolutional Variational Autoencoder (CondCVAE).

Extends CVAE to support class-conditional generation. The model conditions both the encoder and decoder on one-hot encoded class labels, allowing it to learn class-specific latent representations and generate samples conditioned on specific classes.

The conditioning is implemented by concatenating one-hot encoded labels with: - Encoder: Concatenated with flattened features before dense layers - Decoder: Concatenated with latent vector before dense layers

Inherits all functionality from CVAE with modified architecture to accept conditional inputs.

Parameters:

n_classes (int)

n_classes

Number of classes for conditional generation (one-hot dimension).

Type:

int

All other attributes inherited from CVAE.

Example

>>> model = CondCVAE(
...     n_classes=3,
...     input_shape=(128, 128, 4),
...     latent_dim=64,
...     dense_dim=256,
...     beta=1.0
... )
>>> model.compile(optimizer='adam')
>>> # Train with (images, conditions) tuples
>>> model.fit((train_images, train_conditions), epochs=100)
build_encoder()[source]

Build the conditional encoder network.

Like CVAE.build_encoder(), but concatenates the one-hot condition inputs (self.n_classes wide) with the flattened features before the dense projection, so the latent space is conditioned on the metadata.

Returns:

Encoder mapping [patch, condition] to

[z_mean, z_log_var, z].

Return type:

keras.Model

build_decoder()[source]

Build the conditional decoder network.

Like CVAE.build_decoder(), but concatenates the one-hot condition inputs (self.n_classes wide) with the latent vector before decoding.

Returns:

Decoder mapping [latent, condition] to a

reconstructed patch.

Return type:

keras.Model

train_step(data)
test_step(data)