Skip to content

192cm/customizing-lora-for-diffusion-models

Repository files navigation

🎨 Customizing LoRA for Diffusion Models

Stable Diffusion LoRA experiment suite β€” fine-tunes Stable Diffusion v1.5 adapters on custom image datasets and compares dataset source, rank, and alpha effects.


Python PyTorch Diffusers Hugging Face License


Features Β· Quick Start Β· Usage Β· Architecture Β· Experiment Results Β· Dependencies Β· License


✨ Features

  • Dataset-source comparison β€” trains matching LoRA settings on web-crawled images, real photos, and AI-generated images.
  • Rank and alpha ablations β€” compares LoRA capacity settings across fixed prompts and checkpoint intervals.
  • Captioned imagefolder datasets β€” stores each training split with Hugging Face imagefolder metadata in metadata.csv.
  • Before-after generation grids β€” renders base-model and LoRA outputs side by side for visual evaluation.
  • Reusable training utilities β€” centralizes loading, preprocessing, training, inference, and grid export in experiment_utils.py.
  • Report-ready artifacts β€” saves labeled figures, summary CSVs, checkpoints, and final adapter weights.

πŸš€ Quick Start

1. Environment setup

git clone https://github.com/192cm/Customizing-LoRA-for-Diffusion-Models.git
cd Customizing-LoRA-for-Diffusion-Models
conda env create -f environment.yml
conda activate genai-assignment2
python -m ipykernel install --user --name genai-assignment2 --display-name "genai-assignment2"

2. Credentials / config

python -c "from diffusers import StableDiffusionPipeline; StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5')"

Hugging Face provides a free account tier if model access or cached downloads require authentication in your environment.

3. Run

jupyter notebook 00_Customizing_LoRA.ipynb

πŸ“– Usage

Notebooks

Run the notebooks in order when reproducing the full experiment.

Step Notebook Output
1 00_Customizing_LoRA.ipynb End-to-end LoRA workflow validation
2 01_dataset_training.ipynb Dataset-wise LoRA adapters and before-after grids
3 02_ablation_rank.ipynb Rank 8 and 16 comparison artifacts
4 02_ablation_alpha.ipynb Alpha 8 and 16 comparison artifacts
5 03_test.ipynb Additional inference and checkpoint tests
6 04_monitor.ipynb Experiment-state and artifact inspection
jupyter notebook

Use the genai-assignment2 kernel before running cells that import diffusers, accelerate, or torch.

Programmatic

Import the shared helpers when running a smaller training or inference pass from a Python script.

from experiment_utils import DATASET_CONFIGS, run_lora_training_experiment

dataset_config = DATASET_CONFIGS[0]

Dataset Format

Each dataset follows the Hugging Face imagefolder layout.

data/{dataset_name}/
└── train/
    β”œβ”€β”€ metadata.csv
    β”œβ”€β”€ image_01.jpg
    └── image_02.png

metadata.csv uses one row per image.

file_name,caption
image_01.jpg,a sks building in pixel art style
Dataset Folder Images Prompt Style token
web_crawled data/web_crawled_custom_dataset 50 a building sks
real data/real_custom_dataset 10 a city street in winter sks
generated data/generated_custom_dataset 20 a house in a flower field sks

πŸ—οΈ Architecture

Customizing-LoRA-for-Diffusion-Models/
β”œβ”€β”€ 00_Customizing_LoRA.ipynb        # baseline workflow
β”œβ”€β”€ 01_dataset_training.ipynb        # dataset comparison
β”œβ”€β”€ 02_ablation_rank.ipynb           # rank experiments
β”œβ”€β”€ 02_ablation_alpha.ipynb          # alpha experiments
β”œβ”€β”€ 03_test.ipynb                    # inference checks
β”œβ”€β”€ 04_monitor.ipynb                 # artifact inspection
β”œβ”€β”€ experiment_utils.py              # training utilities
β”œβ”€β”€ environment.yml                  # conda environment
β”œβ”€β”€ data/                            # imagefolder datasets
β”œβ”€β”€ lora_experiments/                # checkpoints and grids
β”œβ”€β”€ report_assets/                   # labeled report figures
└── sd_lora/                         # single-run adapter output
Custom images
   β”‚  image files and captions
   β–Ό
data/*/train ──▢ Hugging Face imagefolder dataset
                    β”‚  tensors and tokenized captions
                    β–Ό
             experiment_utils.py ──▢ Stable Diffusion v1.5
                    β”‚  LoRA state dicts and checkpoints
                    β–Ό
          lora_experiments/* ──▢ comparison grids and CSV summaries

The repository keeps experiment orchestration in notebooks while sharing training and inference behavior through experiment_utils.py.


πŸ€– Experiment Results

Dataset Samples

Dataset samples

Before / After LoRA Fine-tuning

Web-crawled Real photos AI-generated
Web-crawled before after Real before after Generated before after

Ablation Study

LoRA rank LoRA alpha
Rank comparison Alpha comparison

Training Settings

Key Value
Base model runwayml/stable-diffusion-v1-5
VAE stabilityai/sd-vae-ft-mse
Variant fp16
Seed 2015
Resolution 512
Batch size 8
Max train steps 2000
Checkpoint interval 500
Learning rate 1e-4
Default LoRA rank 4
Default LoRA alpha 4
Target modules to_k, to_q, to_v, to_out.0

Output Paths

Path Contents
lora_experiments/dataset_training/*/rank4_alpha4/ Dataset comparison adapters and checkpoints
lora_experiments/ablation/rank/ Rank ablation adapters and checkpoints
lora_experiments/ablation/alpha/ Alpha ablation adapters and checkpoints
lora_experiments/comparisons/ Raw before-after comparison grids
lora_experiments/experiment_summary_rank.csv Rank experiment summary
lora_experiments/experiment_summary_alpha.csv Alpha experiment summary
report_assets/ Labeled figures for reports

πŸ“¦ Dependencies

Package Version Role
python 3.10 Runtime
torch 2.1.2+cu121 Training and inference backend
torchvision 0.16.2+cu121 Image transforms
diffusers 0.26.0 Stable Diffusion pipeline and LoRA loading
accelerate 0.26.1 Training orchestration
peft 0.7.1 LoRA configuration and state dict handling
datasets 2.16.1 imagefolder dataset loading
transformers 4.37.0 Tokenizer and text encoder loading
safetensors 0.4.2 Adapter weight serialization

πŸ“„ License

No license file is included in this repository.

About

Diffusion Model Style Adaptation with Customizing LoRA

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors