Skip to main content

A flexible PyTorch implementation of a UNet-based Denoising Diffusion Probabilistic Model (DDPM) with customizable architecture and training options.

Project description

UNet-based Denoising Diffusion Probabilistic Model (DDPM) in PyTorch

A modular and customizable PyTorch implementation of a UNet-based Denoising Diffusion Probabilistic Model for high-quality image generation.
Supports multiple beta schedules, flexible loss functions (MSE or L1), attention layers, and residual blocks for advanced denoising performance.

🚀 Features

  • 🌀 UNet backbone for efficient denoising and image synthesis

  • 📈 Multiple beta schedules (linear, cosine, etc.) for diffusion process customization

  • 🔁 Residual and attention blocks for better feature preservation

  • ⚙️ Configurable architecture via channel multipliers and attention resolution

  • 🧮 Loss function options — Mean Squared Error (MSE) or L1 loss

  • 🧪 Clean and modular code for research and experimentation

  • 📦 Production-ready with training and sampling APIs

📦 Installation

You can access the PyPI page or install the package directly.

pip install diffusion-pytorch-lib

📁 Project Structure

diffusion-pytorch-lib/
├── diffusion_pytorch_lib/
│   ├── __init__.py
│   ├── module.py        # All architecture classes and logic
├── pyproject.toml
├── LICENSE
└── README.md

🚀 Quick Start

1. Import and create the model

import torch
from diffusion-pytorch-lib import UNet, Diffusion

# Define UNet
model = UNet(
    dim=64,
    dim_mults=(1, 2, 4, 8),
    channels=3,
)

# Define diffusion process
diffusion = Diffusion(
    model,
    image_size=256,
    timesteps=1000,
    beta_schedule="linear",  # or 'cosine'
    loss_type="l2"           # 'l2' (MSE) or 'l1'
)

2. Training step (sample loop)

optimizer = torch.optim.Adam(diffusion.parameters(), lr=1e-4)

def train_step(x):
    diffusion.train()
    optimizer.zero_grad()
    loss = diffusion(x)  # computes diffusion loss internally
    loss.backward()
    optimizer.step()
    return loss.item()

3. Sampling new images

diffusion.eval()
with torch.no_grad():
    samples = diffusion.sample(batch_size=8)  # (8, 3, 256, 256)

⚙️ Configuration Options

🧩 U-Net Architecture

Argument Type Default Description
dim int 64 Base number of feature channels in the first layer.
dim_mults tuple (1, 2, 4, 8) Multipliers for feature map dimensions at each U-Net stage.
channels int 3 Number of input/output image channels (e.g., 3 for RGB).
dropout float 0.0 Dropout rate for regularization.

🌀 Diffusion Process

Argument Type Default Description
image_size int 256 Target image resolution
timesteps int 1000 Number of diffusion steps (Higher values improve quality but increase training time).
beta_schedule str "linear" Noise schedule type ("linear", "cosine", etc.).
loss_type str "mse" Loss type ("mse" or "l1")

🙋‍♂️ Author

Developed by Mehran Bazrafkan

Built from scratch for research into diffusion models, with inspiration from modern generative modeling literature.

⭐️ Support & Contribute

If you find this project useful, consider:

  • ⭐️ Starring the repo

  • 🐛 Submitting issues

  • 📦 Suggesting improvements

🔗 Related Projects

📜 License

This project is licensed under the terms of the MIT LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diffusion_pytorch_lib-1.0.2.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

diffusion_pytorch_lib-1.0.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file diffusion_pytorch_lib-1.0.2.tar.gz.

File metadata

  • Download URL: diffusion_pytorch_lib-1.0.2.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.0

File hashes

Hashes for diffusion_pytorch_lib-1.0.2.tar.gz
Algorithm Hash digest
SHA256 fb30ad7f70fa7796da3ae2fe3d400b94462b81962f09e245b829700643f69dcb
MD5 0a283c46a3c443350b889bac36032491
BLAKE2b-256 0849867e68b743606b94cff1eff7cb841a132f4ac53e192000a2855efc8e5738

See more details on using hashes here.

File details

Details for the file diffusion_pytorch_lib-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for diffusion_pytorch_lib-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1d54e28d25a161d9b65aceb2b1cce4917c920d34605dc1c1c218362875eb5b62
MD5 a59008a3ebd00873fc2e1b7321ef0638
BLAKE2b-256 2742cbfd3184f4724e9de3860321b48d77aa70fc71a32e8b595c58cb235d2fb2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page