OnnxStack.StableDiffusion 0.31.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package OnnxStack.StableDiffusion --version 0.31.0
NuGet\Install-Package OnnxStack.StableDiffusion -Version 0.31.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OnnxStack.StableDiffusion" Version="0.31.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OnnxStack.StableDiffusion --version 0.31.0
#r "nuget: OnnxStack.StableDiffusion, 0.31.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OnnxStack.StableDiffusion as a Cake Addin
#addin nuget:?package=OnnxStack.StableDiffusion&version=0.31.0

// Install OnnxStack.StableDiffusion as a Cake Tool
#tool nuget:?package=OnnxStack.StableDiffusion&version=0.31.0

OnnxStack.StableDiffusion - Onnx Stable Diffusion Library for .NET

OnnxStack.StableDiffusion is a library that provides access to Stable Diffusion processes in .NET. It offers extensive support for features such as TextToImage, ImageToImage, VideoToVideo, ControlNet and more

Getting Started

OnnxStack.StableDiffusion can be found via the nuget package manager, download and install it.

PM> Install-Package OnnxStack.StableDiffusion

OnnxRuntime

Depending on the devices you have and the platform you are running on, you will want to install the Microsoft.ML.OnnxRuntime package that best suits your needs.

DirectML - CPU-GPU support for Windows (Windows)

PM> Install-Package Microsoft.ML.OnnxRuntime.DirectML

CUDA - GPU support for NVIDIA (Windows, Linux)

PM> Install-Package Microsoft.ML.OnnxRuntime.Gpu

CoreML - CPU-GPU support for Mac (Apple)

PM> Install-Package Microsoft.ML.OnnxRuntime.CoreML

Dependencies

Video processing support requires FFMpeg and FFProbe binaries, files must be present in your output folder

https://ffbinaries.com/downloads
https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v6.1/ffmpeg-6.1-win-64.zip
https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v6.1/ffprobe-6.1-win-64.zip

C# Stable Diffusion

Basic Stable Diffusion Example

Run a simple Stable Diffusion process with a basic prompt

//Model: 
//https://huggingface.co/runwayml/stable-diffusion-v1-5 (onnx branch)

// Create Pipeline
var pipeline = StableDiffusionPipeline.CreatePipeline("models\\stable-diffusion-v1-5");

// Set Prompt Options
var promptOptions = new PromptOptions { Prompt = "Photo of a cute dog." };

// Run Pipleine
var result = await pipeline.GenerateImageAsync(promptOptions);

// Save image result
await result.SaveAsync("D:\\Results\\Image.png");

// Unload Pipleine
await pipeline.UnloadAsync();

Stable Diffusion Batch Example

Run Stable Diffusion process and return a batch of results

//Model: 
//https://huggingface.co/runwayml/stable-diffusion-v1-5 (onnx branch)

// Create Pipeline
var pipeline = StableDiffusionPipeline.CreatePipeline("models\\stable-diffusion-v1-5");

// Prompt
var promptOptions = new PromptOptions{ Prompt = "Photo of a cat" };

// Batch Of 5 Images with unique seeds
var batchOptions = new BatchOptions
{
    ValueTo = 5,
    BatchType = BatchOptionType.Seed
};

// Run Pipleine
await foreach (var result in pipeline.RunBatchAsync(batchOptions, promptOptions))
{
    // Save Image result
   var image = new OnnxImage(result.ImageResult);
   await image.SaveAsync($"Output_Batch_{result.SchedulerOptions.Seed}.png");
}

// Unload Pipleine
await pipeline.UnloadAsync();

Stable Diffusion ImageToImage Example

Run Stable Diffusion process with an initial image as input

//Model: 
//https://huggingface.co/runwayml/stable-diffusion-v1-5 (onnx branch)

// Create Pipeline
var pipeline = StableDiffusionPipeline.CreatePipeline("models\\stable-diffusion-v1-5");

// Load Input Image
var inputImage = await OnnxImage.FromFileAsync("Input.png");

// Set Prompt Options
var promptOptions = new PromptOptions
{
    DiffuserType = DiffuserType.ImageToImage,
    Prompt = "Photo of a cute dog.",
    InputImage = inputImage
};

// Set Sheduler Options
var schedulerOptions = pipeline.DefaultSchedulerOptions with
{
    // How much the output should look like the input
    Strength = 0.8f 
};

// Run Pipleine
var result = await pipeline.GenerateImageAsync(promptOptions, schedulerOptions);

// Save image result
await result.SaveAsync("Output_ImageToImage.png");

// Unload Pipleine
await pipeline.UnloadAsync();
Input Output
<img src="../Assets/Samples/Input.png" width="256"/> <img src="../Assets/Samples/Output_ImageToImage.png" width="256"/>

Stable Diffusion ControlNet Example

Run Stable Diffusion process with ControlNet depth

//Models: 
//https://huggingface.co/axodoxian/controlnet_onnx
//https://huggingface.co/axodoxian/stable_diffusion_onnx

// Create Pipeline
var pipeline = StableDiffusionPipeline.CreatePipeline("models\\stable_diffusion_onnx", ModelType.ControlNet);

// Load ControlNet Model
var controlNet = ControlNetModel.Create("models\\controlnet_onnx\\controlnet\\depth.onnx");

// Load Control Image
var controlImage = await OnnxImage.FromFileAsync("Input_Depth.png");

// Set Prompt Options
var promptOptions = new PromptOptions
{
    DiffuserType = DiffuserType.ControlNet,
    Prompt = "Photo-realistic alien",
    InputContolImage = controlImage
};

// Run Pipleine
var result = await pipeline.GenerateImageAsync(promptOptions, controlNet: controlNet);

// Save image result
await result.SaveAsync("Output_ControlNet.png");

// Unload Pipleine
await pipeline.UnloadAsync();
Input Output
<img src="../Assets/Samples/Input_Depth.png" width="256"/> <img src="../Assets/Samples/Output_ControlNet.png" width="256"/>

Stable Diffusion VideoToVideo Example

Run Stable Diffusion process on a video frame by frame

//Model: 
//https://huggingface.co/runwayml/stable-diffusion-v1-5 (onnx branch)

 // Create Pipeline
var pipeline = StableDiffusionPipeline.CreatePipeline("models\\stable-diffusion-v1-5");

 // Preload Models (optional)
 await pipeline.LoadAsync();

 // Load Video
 var targetFPS = 15;
 var videoInput = await OnnxVideo.FromFileAsync("Input.gif", targetFPS);

 // Add text and video to prompt
 var promptOptions = new PromptOptions
 {
     Prompt = "Elon Musk",
     DiffuserType = DiffuserType.ImageToImage,
     InputVideo = videoInput
 };

 // Run pipeline
 var result = await pipeline.GenerateVideoAsync(promptOptions);

 // Save Video File
 await result.SaveAsync("Output_VideoToVideo.mp4");

// Unload Pipleine
await pipeline.UnloadAsync();
Input Output
<img src="../Assets/Samples/Input.gif" width="256"/> <img src="../Assets/Samples/Output_VideoToVideo.gif" width="256"/>
converted to gif for github readme converted to gif for github readme
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on OnnxStack.StableDiffusion:

Package Downloads
Frank.SemanticKernel.Connectors.OnnxStack.StableDiffusion

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on OnnxStack.StableDiffusion:

Repository Stars
saddam213/OnnxStack
C# Stable Diffusion using ONNX Runtime
Version Downloads Last updated
0.31.0 57 4/25/2024
0.27.0 126 3/31/2024
0.25.0 120 3/14/2024
0.23.1 100 3/1/2024
0.23.0 79 2/29/2024
0.22.0 99 2/23/2024
0.21.0 90 2/15/2024
0.19.0 102 2/1/2024
0.17.0 142 1/19/2024
0.16.0 103 1/11/2024
0.15.0 144 1/5/2024
0.14.0 117 12/27/2023
0.13.0 92 12/22/2023
0.12.0 121 12/15/2023
0.10.0 143 11/30/2023
0.9.0 133 11/23/2023
0.8.0 183 11/16/2023
0.7.0 125 11/9/2023
0.6.0 100 11/2/2023
0.5.0 127 10/27/2023
0.4.0 129 10/19/2023
0.3.1 128 10/9/2023
0.3.0 117 10/9/2023
0.2.0 122 10/3/2023
0.1.0 129 9/25/2023