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.1.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.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: diffusion_pytorch_lib-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 08821d625db04f6de7aed85716dfb9c89194753d73b7967eb9cc4880b1d26b95
MD5 4aeb267b56a03705503fe9a10a559103
BLAKE2b-256 6f800a4e32e63df6504c362ff9b8127efc2d3807413b1c478045bb9fdbce7352

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for diffusion_pytorch_lib-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 712880e5a00a58b42cac5ce36d6d0ca57a633bc81753ecc75a1192cf8e885190
MD5 bac761796d56e25ad59eeaedb273543d
BLAKE2b-256 b0f71787ca54b67520596c2d8cfaca4343741ff7da1b7db4a0855f8758990a16

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