StableDiffusion.ML.OnnxRuntime 1.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package StableDiffusion.ML.OnnxRuntime --version 1.1.2                
NuGet\Install-Package StableDiffusion.ML.OnnxRuntime -Version 1.1.2                
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="StableDiffusion.ML.OnnxRuntime" Version="1.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StableDiffusion.ML.OnnxRuntime --version 1.1.2                
#r "nuget: StableDiffusion.ML.OnnxRuntime, 1.1.2"                
#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 StableDiffusion.ML.OnnxRuntime as a Cake Addin
#addin nuget:?package=StableDiffusion.ML.OnnxRuntime&version=1.1.2

// Install StableDiffusion.ML.OnnxRuntime as a Cake Tool
#tool nuget:?package=StableDiffusion.ML.OnnxRuntime&version=1.1.2                

Inference Stable Diffusion with C# and ONNX Runtime

This package contains the logic to do inferencing for the popular Stable Diffusion deep learning model in C#. Stable Diffusion models take a text prompt and create an image that represents the text.

How to use this NuGet package

git lfs install
git clone https://huggingface.co/CompVis/stable-diffusion-v1-4 -b onnx
  • Copy the folders with the ONNX files to the C# project folder models. The folders to copy are: unet, vae_decoder, text_encoder, safety_checker.

  • Install the following NuGets for DirectML

<PackageReference Include="Microsoft.ML" Version="2.0.1" />
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.14.1" />
  • Cuda support coming soon.

  • Sample logic for implementing in your project

    //Default args
    var prompt = "a fireplace in an old cabin in the woods";
    Console.WriteLine(prompt);

    var config = new StableDiffusionConfig
    {
        // Number of denoising steps
        NumInferenceSteps = 15,
        // Scale for classifier-free guidance
        GuidanceScale = 7.5,
        // Set your preferred Execution Provider. Currently DirectML and CPU are supported.
        ExecutionProviderTarget = StableDiffusionConfig.ExecutionProvider.DirectML,
        // Set GPU Device ID.
        DeviceId = 1,
        // Update paths to your models
        TextEncoderOnnxPath = @".\models\text_encoder\model.onnx",
        UnetOnnxPath = @".\models\unet\model.onnx",
        VaeDecoderOnnxPath = @".\models\vae_decoder\model.onnx",
        SafetyModelPath = @".\models\safety_checker\model.onnx",
    };

    // Inference Stable Diff
    var image = UNet.Inference(prompt, config);

    // If image failed or was unsafe it will return null.
    if (image == null)
    {
        Console.WriteLine("Unable to create image, please try again.");
    }

  • Set Build for x64

  • Hit F5 to run the project in Visual Studio or dotnet run in the terminal to run the project in VS Code.


Resources

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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 StableDiffusion.ML.OnnxRuntime:

Package Downloads
Frank.SemanticKernel.Connectors.OnnxRuntime.StableDiffusion

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.3 632 4/19/2023
1.1.2 204 4/16/2023
1.1.1 182 4/15/2023
1.0.2 197 4/14/2023
1.0.1 202 4/14/2023
1.0.0 197 4/14/2023