Implementation:Kornia Kornia Stable Diffusion Dissolving
| Knowledge Sources | |
|---|---|
| Domains | Vision, Image_Filtering |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Applies a dissolving transformation to images using one-step reverse diffusion with Stable Diffusion models from HuggingFace Diffusers.
Description
This module is part of the Kornia library's filters subpackage. It implements the dissolving transformation described by Shi et al. (2024), which uses a single reverse diffusion step from a pretrained Stable Diffusion model to progressively dissolve image details. The StableDiffusionDissolving class (an ImageModule) wraps a HuggingFace Diffusers pipeline and supports SD 1.4, SD 1.5, and SD XL models. Internally, a _DissolvingWraper_HF helper class handles prompt embedding, VAE encoding/decoding, and the one-step DDIM reverse diffusion. SD 1.x models tend to remove more details than SD XL.
Usage
Import this class when you want to apply a learned dissolving or detail-removal transformation to images, typically for artistic effects, data augmentation, or as part of a diffusion-based image processing pipeline. Requires the diffusers library and a HuggingFace token for model access.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/filters/dissolving.py
- Lines: 1-277
Signature
class StableDiffusionDissolving(ImageModule):
def __init__(self, version: str = "1.5", **kwargs: Any): ...
def forward(self, input: torch.Tensor, step_number: int) -> torch.Tensor: ...
Import
from kornia.filters import StableDiffusionDissolving
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | torch.Tensor (B, C, H, W) | Yes | The input image tensor with values in [0, 1]. |
| step_number | int | Yes | The diffusion timestep index controlling the dissolution strength. Higher values produce more dissolving. |
Constructor Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| version | str | No (default "1.5") | Stable Diffusion model version. Options: "1.4", "1.5", "xl". |
| **kwargs | Any | No | Additional arguments passed to the HuggingFace from_pretrained method (e.g., torch_dtype, token). |
Outputs
| Name | Type | Description |
|---|---|---|
| output | torch.Tensor (B, C, H, W) | The dissolved image with values clamped to [0, 1]. |
Usage Examples
import torch
from kornia.filters import StableDiffusionDissolving
# Initialize with SD 1.5 (requires diffusers library and HF_TOKEN)
dissolve = StableDiffusionDissolving(version="1.5", torch_dtype=torch.float16)
dissolve = dissolve.to("cuda")
# Apply dissolving transformation
input_image = torch.rand(1, 3, 512, 512).to("cuda")
dissolved = dissolve(input_image, step_number=10)
print(dissolved.shape) # torch.Size([1, 3, 512, 512])
# Use SD XL for less aggressive dissolving
dissolve_xl = StableDiffusionDissolving(version="xl", torch_dtype=torch.float16)