OnnxStack.StableDiffusion 0.21.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package OnnxStack.StableDiffusion --version 0.21.0                
NuGet\Install-Package OnnxStack.StableDiffusion -Version 0.21.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.21.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OnnxStack.StableDiffusion --version 0.21.0                
#r "nuget: OnnxStack.StableDiffusion, 0.21.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.21.0

// Install OnnxStack.StableDiffusion as a Cake Tool
#tool nuget:?package=OnnxStack.StableDiffusion&version=0.21.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
TensorStack-AI/OnnxStack
C# Stable Diffusion using ONNX Runtime
Version Downloads Last updated
0.39.0 406 6/12/2024
0.31.0 236 4/25/2024
0.27.0 177 3/31/2024
0.25.0 156 3/14/2024
0.23.1 140 3/1/2024
0.23.0 124 2/29/2024
0.22.0 140 2/23/2024
0.21.0 133 2/15/2024
0.19.0 140 2/1/2024
0.17.0 178 1/19/2024
0.16.0 143 1/11/2024
0.15.0 209 1/5/2024
0.14.0 178 12/27/2023
0.13.0 119 12/22/2023
0.12.0 149 12/15/2023
0.10.0 184 11/30/2023
0.9.0 159 11/23/2023
0.8.0 221 11/16/2023
0.7.0 161 11/9/2023
0.6.0 137 11/2/2023
0.5.0 167 10/27/2023
0.4.0 166 10/19/2023
0.3.1 161 10/9/2023
0.3.0 152 10/9/2023
0.2.0 163 10/3/2023
0.1.0 165 9/25/2023 0.1.0 is deprecated because it is no longer maintained.