Skip to main content

Torchmanager Implementation for Diffusion Model (v1.2 Beta 1)

Project description

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]]:
        ...

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

torchmanager_diffusion-1.2b1.tar.gz (87.5 kB view details)

Uploaded Source

Built Distribution

torchmanager_diffusion-1.2b1-py3-none-any.whl (49.2 kB view details)

Uploaded Python 3

File details

Details for the file torchmanager_diffusion-1.2b1.tar.gz.

File metadata

File hashes

Hashes for torchmanager_diffusion-1.2b1.tar.gz
Algorithm Hash digest
SHA256 12e67882e3f79ebf77b9cefac08239e625bc41e7f6f2a6e9406bf31a7ce2306e
MD5 e1a492788b9a3200220dfb86762b050b
BLAKE2b-256 b5704259982dc8ed845c18f3e0d0f61f9173ccffa16ee9767568e4faac847a01

See more details on using hashes here.

File details

Details for the file torchmanager_diffusion-1.2b1-py3-none-any.whl.

File metadata

File hashes

Hashes for torchmanager_diffusion-1.2b1-py3-none-any.whl
Algorithm Hash digest
SHA256 fd378f46e6e5879e978db755bbd7687b27ac06e02aa4d40cf9ee135891a14a36
MD5 aef961d2fa89935015a843449c968090
BLAKE2b-256 89a05275d011afcf36f7da102e1a488613890a8f4fba2ce6a2b92f13f51b779d

See more details on using hashes here.

Supported by

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