Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Torchmanager Diffusion Models Plug-in

The torchmanager implementation for diffusion models.

Pre-requisites

Installation

  • PyPi: pip install torchmanager-diffusion

DDPM Manager Usage

Train DDPM

Direct compile DDPMManager with a model, a beta space, and a number of time steps. Then, use fit method to train the model.

import diffusion
from diffusion import DDPMManager
from torchmanager import callbacks, data, losses

# initialize dataset
dataset: data.Dataset = ...

# initialize model, beta_space, and time_steps
model: torch.nn.Module = ...
beta_space: diffusion.scheduling.BetaSpace = ...
time_steps: int = ...

# initialize optimizer and loss function
optimizer: torch.optim.Optimizer = ...
loss_fn: losses.Loss = ...

# compile the ddpm manager
manager = DDPMManager(model, beta_space, time_steps, optimizer=optimizer, loss_fn=loss_fn)

# initialize callbacks
callback_list: list[callbacks.Callback] = ...

# train the model
trained_model = manager.fit(dataset, epochs=..., callbacks=callback_list)

Evaluate DDPM

Add necessary metrics and use test method with sampling_images as True to evaluate the trained model.

import torch
from diffusion import DDPMManager
from torchmanager import data, metrics
from torchvision import models

# load manager from checkpoints
manager = DDPMManager.from_checkpoint(...)
assert isinstance(manager, DDPMManager), "manager is not a DDPMManager."

# initialize dataset
testing_dataset: data.Dataset = ...

# add neccessary metrics
inception = models.inception_v3(pretrained=True)
inception.fc = torch.nn.Identity()  # type: ignore
inception.eval()
fid = metrics.FID(inception)
manager.metrics.update({"FID": fid})

# evaluate the model
summary = manager.test(testing_dataset, sampling_images=True)

Customize Diffusion Algorithm

Inherit DiffusionManager and implement abstract methods forward_diffusion and sampling_step to customize the diffusion algorithm.

from diffusion import DiffusionManager

class CustomizedManager(DiffusionManager):
    def forward_diffusion(self, data: Any, condition: Optional[torch.Tensor] = None, t: Optional[torch.Tensor] = None) -> tuple[Any, torch.Tensor]:
        ...

    def sampling_step(self, data: DiffusionData, i: int, /, *, return_noise: bool = False) -> Union[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]:
        ...

Release files for torchmanager-diffusion 1.3b1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchmanager-diffusion 1.3b1
File Size Uploaded
torchmanager_diffusion-1.3b1.tar.gz 53.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torchmanager-diffusion 1.3b1
File Interpreter ABI Platform
torchmanager_diffusion-1.3b1-py3-none-any.whl Python 3 none any Details

Total release size: 132.1 kB

Release files / torchmanager_diffusion-1.3b1.tar.gz

Download URL torchmanager_diffusion-1.3b1.tar.gz
Size 53.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f35754a9b2fc8e8d28081405ced859971909ba52a4559c5d86ec54d74cd82b64
BLAKE2b-256 checksum
How to use checksums
bc8217c7b52016e39b0f99877e621f85a0943cd469f2ac8a631edbd6d354ea87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.18

Release files / torchmanager_diffusion-1.3b1-py3-none-any.whl

Download URL torchmanager_diffusion-1.3b1-py3-none-any.whl
Size 79.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a2d7b26913daeb7027723deb02d9ef3ac1f4634c13030587ab8fc168226d828
BLAKE2b-256 checksum
How to use checksums
b73be9b6532fbe76db3a59aa6a940878dfd346f298e6a129b092d0c7c6daf01a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.18

Release history Release notifications | RSS feed

This release

1.3b1 This release

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2

2 release files

1.1.1

2 release files

1.1

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page