Models¶
Variational autoencoder architectures. See Model architecture for a conceptual overview.
- class phenocoder.model.Sampling(*args, **kwargs)¶
Bases:
LayerCustom 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:
ModelConvolutional 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:
- 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
Conv2Dlayers (one per entry inself.conv_layers) followed by a dense projection, and outputsz_mean,z_log_varand the reparameterized latent samplez.- 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
Conv2DTransposelayers (upsampling) to reconstruct all input channels.- Returns:
Decoder mapping a latent vector to a reconstructed patch.
- Return type:
keras.Model
- property metrics¶
- class phenocoder.model.CondCVAE(*args, **kwargs)[source]¶
Bases:
CVAEConditional 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)
- 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_classeswide) 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].
- Encoder mapping
- 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_classeswide) with the latent vector before decoding.- Returns:
- Decoder mapping
[latent, condition]to a reconstructed patch.
- Decoder mapping
- Return type:
keras.Model
- train_step(data)¶
- test_step(data)¶