CoolFace
Modelpublic

YashNagraj75/Latent-Diffusion-Conditional

sourceHugging Facemitupdated 3mo agoView on Hugging Face
0likes
Model Card

Latent Diffusion Model — Text-Conditional CelebA-HQ

A full Latent Diffusion Model (LDM) pipeline for text-conditional face image generation, trained on CelebA-HQ at 256x256 resolution. The model compresses images into a discrete latent space with VQ-VAE, then trains a text-conditioned U-Net diffusion model in that compressed space.

Model Description

This is a two-stage generative model:

  1. 1.Stage 1 — VQ-VAE: Compresses 256x256 RGB images into a 4-channel discrete latent representation with a codebook of size 8192. Trained with reconstruction, perceptual (LPIPS/VGG), and adversarial (discriminator) losses.
  2. 2.Stage 2 — LDM U-Net: A conditional U-Net diffusion model operating on the VQ-VAE latent space. Text conditioning is provided via frozen CLIP embeddings (512-dim) with classifier-free guidance.

VQ-VAE Architecture

ParameterValue
Latent channels (z)4
Codebook size8192
Down channels[64, 128, 256, 256]
Downsampling stages3
AttentionNone in encoder/decoder
Loss componentsMSE (reconstruction) + LPIPS (perceptual) + GAN (adversarial)

LDM U-Net Architecture

ParameterValue
Down channels[256, 384, 512, 768]
Mid channels[768, 512]
AttentionAll down levels
Attention heads16
Time embedding dim512
ConditionCLIP text embeddings (512-dim)
CFG dropout probability0.1

Diffusion Process

ParameterValue
Timesteps (T)1000
Beta scheduleLinear, start=0.00085, end=0.012
Classifier-free guidanceEnabled (cfguidancescale configurable)

Training Details

StageEpochsLRBatch size
VQ-VAE801e-54
LDM U-Net1005e-616
  • Dataset: CelebA-HQ, 256x256 RGB faces
  • Discriminator enabled after 15,000 steps (disc_start=15000)
  • Training tracked with Weights & Biases

Repository Contents

PathDescription
models/vqvae.pyVQ-VAE encoder/decoder with codebook
models/discriminator.pyPatchGAN discriminator
models/lpips.pyPerceptual loss (VGG-based LPIPS)
models/unet_cond.pyText-conditional LDM U-Net
models/blocks.pyShared building blocks
train_vqvae.pyStage 1 training script
train_ldm.pyStage 2 training script
scheduler.pyNoise scheduler
config/celebahq.yamlFull training config
dataset/CelebA-HQ parquet dataloader
celebhq/vqvaeautoencoderckpt.pthVQ-VAE checkpoint

How to Use

python
import yaml, torch
from models.vqvae import VQVAE
from models.unet_cond import UNet

with open("config/celebahq.yaml") as f:
    config = yaml.safe_load(f)

vqvae = VQVAE(**config["autoencoder_params"])
vqvae.load_state_dict(torch.load("celebhq/vqvae_autoencoder_ckpt.pth"))
vqvae.eval()

# Encode an image to latent space
with torch.no_grad():
    z, _, _ = vqvae.encode(image_tensor)
    reconstruction = vqvae.decode(z)

References

License

MIT