StableDiffusion.ML.OnnxRuntime 1.0.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.0.2                
NuGet\Install-Package StableDiffusion.ML.OnnxRuntime -Version 1.0.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.0.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.0.2                
#r "nuget: StableDiffusion.ML.OnnxRuntime, 1.0.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.0.2

// Install StableDiffusion.ML.OnnxRuntime as a Cake Tool
#tool nuget:?package=StableDiffusion.ML.OnnxRuntime&version=1.0.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" />
  • Install the following NuGets for GPU (Cuda)
<PackageReference Include="Microsoft.ML.OnnxRuntime.Gpu" Version="1.14.1" />
  • 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
    {
        //num of images requested
        BatchSize = 1,
        // Number of denoising steps
        NumInferenceSteps = 15,
        // Scale for classifier-free guidance
        GuidanceScale = 7.5,
        // Set your preferred Execution Provider. Currently (GPU, DirectML, CPU) are supported in this project.
        // ONNX Runtime supports many more than this. Learn more here: https://onnxruntime.ai/docs/execution-providers/
        // The config is defaulted to CUDA. You can override it here if needed.
        // To use DirectML EP intall the Microsoft.ML.OnnxRuntime.DirectML and uninstall Microsoft.ML.OnnxRuntime.GPU
        ExecutionProviderTarget = StableDiffusionConfig.ExecutionProvider.DirectML,
        // Set GPU Device ID.
        DeviceId = 1,
        // Update paths to your models
        TokenizerOnnxPath = @".\models\text_tokenizer\custom_op_cliptok.onnx",
        TextEncoderOnnxPath = @".\models\text_encoder\model.onnx",
        UnetOnnxPath = @".\models\unet\model.onnx",
        VaeDecoderOnnxPath = @".\models\vae_decoder\model.onnx",
        SafetyModelPath = @".\models\safety_checker\model.onnx",
        OutputImagePath = "sample.png"
    };

    // 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.");
    }
    else
    {
        // Output image path
        var path = Path.Combine(Directory.GetCurrentDirectory(), config.OutputImagePath);
        Console.WriteLine("Image saved to: " + path);
    }

  • 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 629 4/19/2023
1.1.2 202 4/16/2023
1.1.1 180 4/15/2023
1.0.2 195 4/14/2023
1.0.1 200 4/14/2023
1.0.0 195 4/14/2023