This code implements a Variational Autoencoder (VAE) for generating and manipulating facial images using TensorFlow and Keras. Here's a brief description and definition of major concepts:
-
Variational Autoencoder (VAE):
- A generative model that learns to encode and decode data.
- Comprises an encoder that maps input data to a probability distribution in the latent space and a decoder that reconstructs the data from sampled points in this space.
- Includes a sampling layer that introduces stochasticity, enabling generation of diverse samples.
-
Encoder and Decoder:
- Encoder: Accepts input images and maps them to the latent space, producing mean and log-variance vectors.
- Decoder: Reconstructs images from sampled points in the latent space.
-
Sampling Layer:
- Introduced in the
Sampling
class, it samples from the learned distribution in the latent space during model training.
- Introduced in the
-
Training the VAE:
- Trains the VAE using a custom loss function composed of a reconstruction loss and a KL divergence term.
- The model is trained on a dataset of facial images to learn a representation in the latent space.
-
Data Preprocessing:
- Images are loaded and preprocessed using TensorFlow's
image_dataset_from_directory
function. - Images are normalized to the range [0, 1] and converted to a float32 format.
- Images are loaded and preprocessed using TensorFlow's
-
Callbacks:
- Utilizes various callbacks, such as model checkpointing and TensorBoard, for monitoring and saving the training progress.
-
Image Generation:
- Generates images during training and saves them as checkpoints using the
ImageGenerator
callback.
- Generates images during training and saves them as checkpoints using the
-
TensorBoard Logging:
- Logs training metrics and visualizations to TensorBoard for monitoring and analysis.
-
Testing and Visualization:
- Samples images from the trained VAE and visualizes both real and reconstructed images.
- Visualizes the latent space by sampling points and decoding them.
-
Attribute Manipulation:
- Defines functions to extract an attribute vector for a specific facial attribute (e.g., Blond Hair).
- Manipulates the latent space to emphasize or de-emphasize the chosen attribute.
- Facial Morphing:
- Illustrates facial morphing by interpolating between latent space representations of two images.
- Face Dataset and Labels:
- Uses the CelebA dataset, containing facial images of celebrities.
- Labels such as hair color are utilized to perform attribute manipulation.
The code demonstrates the training and use of a VAE for generating realistic facial images and manipulating facial attributes in the latent space.