Torchmanager Diffusion Models Plug-in
The torchmanager implementation for diffusion models.
Pre-requisites
- Python >= 3.9
- SciPy >= 1.11.4
- PyTorch >= 2.0.1
- LPIPS
- torchmanager >= 1.2
- einops >= 0.6.1
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]]:
...
Metadata
Release files for torchmanager-diffusion 1.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torchmanager_diffusion-1.2.2.tar.gz | 31.8 kB | Without provenance | Details for torchmanager_diffusion-1.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torchmanager_diffusion-1.2.2-py3-none-any.whl | Python 3 | none | any | Details for torchmanager_diffusion-1.2.2-py3-none-any.whl |
Total release size: 82.4 kB
Release files / torchmanager_diffusion-1.2.2.tar.gz
| Download URL | torchmanager_diffusion-1.2.2.tar.gz |
|---|---|
| Size | 31.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
134149f6b9200efbdb7b9d00f8b1a61768ae85320fa378a526b48dbb0e3ee675
|
|
BLAKE2b-256 checksum How to use checksums |
407d5b731f3bc491884ec31259fa4b03a62aa020f3d4fc74ba5c5a4620ecc7a0
|
| 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.2.2-py3-none-any.whl
| Download URL | torchmanager_diffusion-1.2.2-py3-none-any.whl |
|---|---|
| Size | 50.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8fa84fc70a38887143922901b0ad1b559de25bd4759ee525c9896e376c301d97
|
|
BLAKE2b-256 checksum How to use checksums |
da46ce6cc89db9f31560de649cf815f194b3da6b4de0018f3451f4f5a7308cc7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.18
|
Security for torchmanager-diffusion 1.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.