Deep Unsupervised Learning

Stable Diffusion Fine-tuning

Stable DiffusionDDPMCLIP text encoderUNet fine-tuning
Stable Diffusion Fine-tuning
Problem

Pre-trained text-to-image diffusion models generalize broadly but may not capture a specific visual style. Can fine-tuning only the UNet on a small, captioned Naruto-style dataset shift generation toward that aesthetic while keeping the frozen VAE and CLIP text encoder as fixed perceptual and semantic anchors?

Dataset

Naruto-style dataset with text captions (Alex-0402/naruto-style-dataset-with-text via HuggingFace): 1,221 training images with paired prompts such as “a man with dark hair and brown eyes, naruto style”. Custom NarutoDataset applies resize to 512×512, RandomHorizontalFlip, ColorJitter, RandomAffine, ToTensor, and normalization to [-1, 1] with mean=std=0.5.

Approach

Base checkpoint: OFA-Sys/small-stable-diffusion-v0 (VAE + UNet2DConditionModel + CLIPTextModel + CLIPTokenizer). Only the UNet is trainable (~579M params); VAE (~84M) and text encoder (~123M) stay frozen. Training follows the standard DDPM objective: sample a timestep, add noise to VAE-encoded latents, and train the UNet to predict the noise with MSE. Inference uses StableDiffusionPipeline with PNDMScheduler, 200 steps, and classifier-free guidance scale 7.5.

Training

AdamW on UNet only, learning rate 1e-5, batch size 1, 6 epochs on Colab GPU (7,326 steps; CUDA mixed precision with GradScaler). PNDM inference with 200 steps and classifier-free guidance 7.5. Evaluated on 5 dataset prompts, 5 custom Naruto-themed prompts, and 5 original-vs-generated pairs.

My Work
  • Built NarutoDataset with augmentation beyond the assignment baseline (colour jitter, affine transforms)
  • Wired VAE latent encoding/decoding with the model’s scaling_factor
  • Implemented the DDPM noise-prediction training loop with CUDA mixed precision
  • Froze VAE and CLIP text encoder; trained UNet weights only
  • Set up StableDiffusionPipeline for text-to-image inference with classifier-free guidance
  • Prepared evaluation prompts from dataset indices and custom Naruto-themed descriptions
Evaluation
  • 5 prompts from training dataset captions (indices 10, 100, 200, 300, 400; seed 1337)
  • 5 custom Naruto-themed prompts (sharingan ninja, nine-tailed fox, kunoichi, shinobi battle, hidden leaf village; seed 42)
  • 5 original training images vs. generated outputs (indices 50, 150, 250, 350, 450; seed 8888) for style and prompt adherence
Generated images from training dataset prompts
Dataset captions — 5 prompts from training set
Generated images from custom Naruto prompts
Custom Naruto-themed prompts — 5 generations
Original vs generated Naruto-style comparison
Original training images vs. generated outputs
Results
  • 6 epochs, 7,326 steps; epoch-end step loss 0.206 → 0.006 (Colab CUDA)
  • 1,221 captioned training images; 512×512 resolution
  • UNet: 579M trainable parameters; VAE + text encoder frozen
  • 200 inference steps; 10 evaluation prompts (5 dataset + 5 custom)
Stable Diffusion fine-tuning loss curve
Training MSE loss over 7,326 steps
Takeaway

Diffusion models feel qualitatively different from GANs—generation is iterative denoising in latent space, not a single forward pass. Fine-tuning only the UNet while freezing VAE and CLIP made the pipeline modular: perceptual compression and text semantics stay fixed while the denoiser adapts to Naruto-style captions. Completing all six epochs on Colab GPU with mixed precision produced stable training and the exported results shown here.

Work completed during Oxford summer coursework. This page shows only my own implementations and outputs; course materials are not redistributed.