Skip to content

SHuBhAM71205/Diffusion2

Repository files navigation

🎨 Diffusion2 - Minimal Diffusion Model Implementation

A clean, educational implementation of a diffusion-based generative model from scratch, featuring training, sampling, and a FastAPI REST API for inference.


πŸ“‹ Table of Contents


Some Samples

NO IMG NO IMG

FashionMNIST

NO IMG NO IMG

✨ Features

βœ… From-Scratch Implementation – UNet-based diffusion model without third-party frameworks
βœ… YAML Configuration – Centralized, tunable training & model hyperparameters
βœ… FastAPI Integration – RESTful API endpoint for image generation
βœ… Docker Support – Containerized deployment ready
βœ… Caltech-101 Dataset – Pre-configured for object image training
βœ… GPU/CPU Aware – Automatic device detection and fallback
βœ… Modular Architecture – Clean separation of config, model, diffusion, and utilities


πŸ“ Project Structure

Diffusion2/
β”œβ”€β”€ πŸ“„ README.md                    # This file
β”œβ”€β”€ πŸ“„ main.py                      # Entry point for CLI modes (train, sample, serve)
β”œβ”€β”€ πŸ“„ train_collab.ipynb           # notebook for experimentation
β”œβ”€β”€ πŸ“„ pyproject.toml               # Project metadata & dependencies
β”œβ”€β”€ 🐳 Dockerfile                   # Container image definition
β”œβ”€β”€ πŸ“¦ .python-version              # Python version spec (3.x)
β”œβ”€β”€ πŸ“¦ .venv/                       # Virtual environment (gitignored)
β”œβ”€β”€ πŸ“‚ build/                       # build outputs (wheel, egg-info)
β”œβ”€β”€ πŸ“‚ diffusion2.egg-info/         # package metadata (top-level)
β”‚
β”œβ”€β”€ πŸ“‚ app/
β”‚   └── app.py                      # FastAPI application & HTTP endpoints
β”‚
β”œβ”€β”€ πŸ“‚ configs/
β”‚   └── base.yaml                   # Hyperparameters & model config (YAML)
β”‚
β”œβ”€β”€ πŸ“‚ data/
β”‚   └── plane/                      # local plane images for Dataset
β”‚
β”œβ”€β”€ πŸ“‚ Dataset/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── plane.py                    # custom dataset loader
β”‚
β”œβ”€β”€ πŸ“‚ Logger/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── logger.py                   # logging helper
β”‚
β”œβ”€β”€ πŸ“‚ logs/
β”‚   β”œβ”€β”€ inference-logs/
β”‚   └── train-logs/
β”‚
β”œβ”€β”€ πŸ“‚ saves/                       # trained model checkpoints
β”œβ”€β”€ πŸ“‚ scripts/
β”‚   └── temp.ipynb                 # miscellaneous script
β”‚
β”œβ”€β”€ πŸ“‚ src/                         # installable source package
β”‚   β”œβ”€β”€ diffusion2.egg-info/        # package metadata within src
β”‚   β”œβ”€β”€ πŸ“‚ mini_diffusion/          # Core diffusion model package
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ config.py               # Config loader & Pydantic models
β”‚   β”‚   β”œβ”€β”€ diffusion.py            # Diffusion process (noise scheduling)
β”‚   β”‚   β”œβ”€β”€ model.py                # UNet architecture with time embeddings
β”‚   β”‚   β”œβ”€β”€ preprocessing.py        # image transforms
β”‚   β”‚   β”œβ”€β”€ train.py                # Training loop
β”‚   β”‚   β”œβ”€β”€ sample.py               # Sampling/inference function
β”‚   β”‚   └── __pycache__/
β”‚   β”‚
β”‚   └── πŸ“‚ argparsers/              # CLI argument parsers (extensible)
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ train_parser.py
β”‚       └── inference_parser.py
β”‚
β”œβ”€β”€ πŸ“‚ tests/                        # Unit tests (currently empty)
└── πŸ“‚ data/plane/                   # dataset images

πŸš€ Quick Start

1️⃣ Installation

# Clone the repository
git clone <repo-url>
cd Diffusion2



# Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

--OR

uv sync

# Install dependencies
pip install -e .
# or manually:
pip install torch torchvision numpy pydantic pyyaml tqdm fastapi uvicorn pillow

if using uv no need to manually install 

2️⃣ Train the Model

# use the configuration file to control hyperparameters & paths
uv run python main.py train --config ./configs/base.yaml

Trains a UNet diffusion model on the dataset specified in the config (default is Caltech-101 airplanes). A checkpoint is written to the save_path defined in the config (by default ./saves/a.pth).

3️⃣ Generate Images

# pass the same config file used for training so the model path and device are picked up
uv run python main.py sample --config ./configs/base.yaml

The sample mode will load the checkpoint configured under inference.model_path and run the reverse diffusion process, writing the resulting PNG to sample.png in the current directory. You can also prefix the command with uv run if you are running inside the project's UV environment:

uv run python ./main.py sample --config ./configs/base.yaml

This command is for generation only; training should still use the train mode.

(Adjust paths and options in configs/base.yaml to point to your trained model or to change device settings.)

4️⃣ Serve via API

The serve mode still starts the FastAPI server, but you can also call uvicorn directly as before:

python main.py serve
# or directly:
uv run uvicorn app.app:app --reload --host 0.0.0.0 --port 8000

API is now live at http://localhost:8000

Endpoints:

  • GET /generate – Generate and return PNG image
# Example: Download generated image
curl -s http://localhost:8000/generate --output generated.png

βš™οΈ Configuration

Edit configs/base.yaml to tune hyperparameters:

diffusion:
  timesteps: 1000
  beta_start: 0.0001
  beta_end: 0.02

training:
  batch_size: 32
  epochs: 100
  learning_rate: 1e-4
  device: "cuda"
  data_dir: "./data"
  save_path: "./saves"
  num_workers: 4
  logs: "./logs/train-logs"

inference:
  model_path: "./saves/a.pth"
  device: "cuda"
  logs: "./logs/inference-logs"

api:
  host: "localhost"
  port: 8000
  reload: True

preprocessing:
  data_dir: "./data/plane"
  save_dir: "./data"

model:
  im_channels : 3
  im_size : 32
  down_channels : [64, 128, 256, 512]
  mid_channels : [512, 512, 256]
  down_sample : [True, True, False]
  time_emb_dim : 128
  num_down_layers : 2
  num_mid_layers : 2
  num_up_layers : 2
  num_heads : 8

πŸ—οΈ Architecture

UNet Model (model.py) This model is used from UNET of Explained AI video

  • Sinusoidal Time Embedding – Encodes timestep t as positional embeddings
  • Encoder (Down-sampling) – 2 convolutional down-blocks with max-pooling
  • Bottleneck – Central processing block
  • Decoder (Up-sampling) – 2 transposed convolutions with skip connections
  • Output – Predicts noise to subtract from noisy input

Diffusion Process (diffusion.py)

  • Forward – Adds noise to images: $x_t = \sqrt{\bar{\alpha}_t} x_0 + \sqrt{1-\bar{\alpha}_t} \epsilon$
  • Backward – Iteratively denoises: $x_{t-1} = \frac{1}{\sqrt{\alpha_t}} \left( x_t - \frac{1-\alpha_t}{\sqrt{1-\bar{\alpha}t}} \hat{\epsilon}\theta(x_t, t) \right)$

Training (train.py)

  • Loads Caltech-101 dataset with preprocessing
  • Samples random timesteps and adds noise
  • Minimizes L2 loss on noise prediction
  • Saves trained UNet to ./saves/a.pth

Sampling (sample.py)

  • Loads trained model
  • Starts from random Gaussian noise
  • Iteratively denoises over 1000 timesteps
  • Returns final generated image

πŸ“Š Dependencies

Library Purpose
torch Deep learning framework
torchvision Computer vision utilities + Caltech-101 data
numpy Numerical computing
pydantic Config validation & type hints
pyyaml YAML configuration parsing
pillow Image I/O for API responses
fastapi REST API framework
uvicorn ASGI server
tqdm Progress bars

See pyproject.toml for version specs.


🐳 Docker Deployment

Build and run in a container:

# Build image
docker build -t diffusion2 .

# Run container
docker run --gpus all -p 8000:8000 diffusion2 python main.py serve

πŸ“ Notes

  • First Run – Caltech-101 dataset (~130 MB) auto-downloads on first training
  • GPU Required – Training on CPU is slow; CUDA strongly recommended
  • Model Checkpoint – Trained model saved to ./saves/a.pth (reused by sampling & API)
  • Empty Dirs – /scripts and /tests are placeholders for future expansion

πŸ”§ Troubleshooting

Issue Solution
No module named 'mini_diffusion' Run pip install -e . from repo root
CUDA out of memory Reduce batch_size in configs/base.yaml
Dataset download fails Manual download: Caltech-101
API port already in use Change port: uvicorn app.app:app --port 8001

πŸ“š References


πŸ‘€ Author

Shubham


πŸ“„ License


Happy Diffusing! 🎨

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors