Home

Awesome

Inference Stable Diffusion with C# and ONNX Runtime

This repo 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.

For the below example sentence the CLIP model creates a text embedding that connects text to image. A random noise image is created and then denoised with the unet model and scheduler algorithm to create an image that represents the text prompt. Lastly the decoder model vae_decoder is used to create a final image that is the result of the text prompt and the latent image.

"make a picture of green tree with flowers around it and a red sky" 
Auto Generated Random Latent Seed InputResulting image output
<img src="images/latent.png" width="256" height="256" alt="Image of browser inferencing on sample images."/><img src="images/sample-output-stablediff.png" width="256" height="256" alt="Image of browser inferencing on sample images."/>

More Images Created with this Repo:

| <img src="images/cat-sunglasses.png" width="256" height="256" alt="Image of browser inferencing on sample images."/> | <img src="images/dog-beach-sample.png" width="256" height="256" alt="Image of browser inferencing on sample images."/> |

| <img src="images/cabin.png" width="256" height="256" alt="Image of browser inferencing on sample images."/> | <img src="images/shipwreck.png" width="256" height="256" alt="Image of browser inferencing on sample images."/> |

Prerequisites

git clone https://github.com/cassiebreviu/StableDiffusion.git

Use Hugging Face to download the Stable Diffusion models

Download the ONNX Stable Diffusion models from Hugging Face.

Once you have selected a model version repo, click Files and Versions, then select the ONNX branch. If there isn't an ONNX model branch available, use the main branch and convert it to ONNX. See the ONNX conversion tutorial for PyTorch for more information.

git lfs install
git clone https://huggingface.co/CompVis/stable-diffusion-v1-4 -b onnx

Follow the full Stable Diffusion C# Tutorial for this Repo here


Resources