Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit ce33e1c

Browse files
committed
Add ControlNet example
1 parent aa87704 commit ce33e1c

File tree

5 files changed

+91
-5
lines changed

5 files changed

+91
-5
lines changed

Assets/Samples/Input.png

522 KB
Loading

Assets/Samples/Input_Depth.png

64 KB
Loading

Assets/Samples/Output_ControlNet.png

388 KB
Loading
362 KB
Loading

OnnxStack.StableDiffusion/README.md

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v6.1/ffprobe
3939

4040

4141
# C# Stable Diffusion
42-
Example Model: https://huggingface.co/runwayml/stable-diffusion-v1-5 (onnx branch)
43-
4442

4543
## Basic Stable Diffusion Example
4644
Run a simple Stable Diffusion process with a basic prompt
4745
```csharp
46+
//Model:
47+
//https://huggingface.co/runwayml/stable-diffusion-v1-5 (onnx branch)
48+
4849
// Create Pipeline
49-
var pipeline = StableDiffusionPipeline.CreatePipeline("D:\\Repositories\\stable-diffusion-v1-5");
50+
var pipeline = StableDiffusionPipeline.CreatePipeline("models\\stable-diffusion-v1-5");
5051

5152
// Set Prompt Options
5253
var promptOptions = new PromptOptions { Prompt = "Photo of a cute dog." };
@@ -65,8 +66,11 @@ await pipeline.UnloadAsync();
6566
## Stable Diffusion Batch Example
6667
Run Stable Diffusion process and return a batch of results
6768
```csharp
69+
//Model:
70+
//https://huggingface.co/runwayml/stable-diffusion-v1-5 (onnx branch)
71+
6872
// Create Pipeline
69-
var pipeline = StableDiffusionPipeline.CreatePipeline("D:\\Repositories\\stable-diffusion-v1-5");
73+
var pipeline = StableDiffusionPipeline.CreatePipeline("models\\stable-diffusion-v1-5");
7074

7175
// Prompt
7276
var promptOptions = new PromptOptions{ Prompt = "Photo of a cat" };
@@ -83,10 +87,92 @@ await foreach (var result in pipeline.RunBatchAsync(batchOptions, promptOptions)
8387
{
8488
// Save Image result
8589
var image = result.ImageResult.ToImage();
86-
await image.SaveAsPngAsync($"D:\\Results\\Image_{result.SchedulerOptions.Seed}.png");
90+
await image.SaveAsPngAsync($"Output_Batch_{result.SchedulerOptions.Seed}.png");
8791
}
8892

8993
// Unload Pipleine
9094
await pipeline.UnloadAsync();
9195

9296
```
97+
98+
99+
100+
## Stable Diffusion ImageToImage Example
101+
Run Stable Diffusion process with an initial image as input
102+
```csharp
103+
//Model:
104+
//https://huggingface.co/runwayml/stable-diffusion-v1-5 (onnx branch)
105+
106+
// Create Pipeline
107+
var pipeline = StableDiffusionPipeline.CreatePipeline("models\\stable-diffusion-v1-5");
108+
109+
// Load Input Image
110+
var inputImage = await InputImage.FromFileAsync("Input.png");
111+
112+
// Set Prompt Options
113+
var promptOptions = new PromptOptions
114+
{
115+
DiffuserType = DiffuserType.ImageToImage,
116+
Prompt = "Photo of a cute dog.",
117+
InputImage = inputImage
118+
};
119+
120+
// Set Sheduler Options
121+
var schedulerOptions = pipeline.DefaultSchedulerOptions with
122+
{
123+
// How much the output should look like the input
124+
Strength = 0.8f
125+
};
126+
127+
// Run Pipleine
128+
var result = await pipeline.RunAsync(promptOptions, schedulerOptions);
129+
130+
// Save image result
131+
var image = result.ToImage();
132+
await image.SaveAsPngAsync("Output_ImageToImage.png");
133+
134+
// Unload Pipleine
135+
await pipeline.UnloadAsync();
136+
```
137+
| Input | Output |
138+
| :--- | :--- |
139+
<img src="../Assets/Samples/Input.png" width="256"/> | <img src="../Assets/Samples/Output_ImageToImage.png" width="256"/>
140+
141+
142+
## Stable Diffusion ControlNet Example
143+
Run Stable Diffusion process with ControlNet depth
144+
```csharp
145+
//Models:
146+
//https://huggingface.co/axodoxian/controlnet_onnx
147+
//https://huggingface.co/axodoxian/stable_diffusion_onnx
148+
149+
// Create Pipeline
150+
var pipeline = StableDiffusionPipeline.CreatePipeline("models\\stable_diffusion_onnx", ModelType.ControlNet);
151+
152+
// Load ControlNet Model
153+
var controlNet = ControlNetModel.Create("models\\controlnet_onnx\\controlnet\\depth.onnx");
154+
155+
// Load Control Image
156+
var controlImage = await InputImage.FromFileAsync("Input_Depth.png");
157+
158+
// Set Prompt Options
159+
var promptOptions = new PromptOptions
160+
{
161+
DiffuserType = DiffuserType.ControlNet,
162+
Prompt = "Photo-realistic alien",
163+
InputContolImage = controlImage
164+
};
165+
166+
// Run Pipleine
167+
var result = await pipeline.RunAsync(promptOptions, controlNet: controlNet) ;
168+
169+
// Save image result
170+
var image = result.ToImage();
171+
await image.SaveAsPngAsync("Output_ControlNet.png");
172+
173+
// Unload Pipleine
174+
await pipeline.UnloadAsync();
175+
```
176+
| Input | Output |
177+
| :--- | :--- |
178+
<img src="../Assets/Samples/Input_Depth.png" width="256"/> | <img src="../Assets/Samples/Input_Depth.png" width="256"/>

0 commit comments

Comments
 (0)