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

Commit fc9ef24

Browse files
authored
Merge pull request #88 from saddam213/ControlNet
Add ControlNet Support
2 parents 3c12247 + cd637a3 commit fc9ef24

File tree

79 files changed

+1937
-356
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1937
-356
lines changed

OnnxStack.Console/Examples/StableDebug.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private async Task<bool> GenerateImage(StableDiffusionModelSet model, PromptOpti
7777
{
7878
var timestamp = Stopwatch.GetTimestamp();
7979
var outputFilename = Path.Combine(_outputDirectory, $"{model.Name}_{options.Seed}_{options.SchedulerType}.png");
80-
var result = await _stableDiffusionService.GenerateAsImageAsync(model, prompt, options);
80+
var result = await _stableDiffusionService.GenerateAsImageAsync(new ModelOptions(model), prompt, options);
8181
if (result is not null)
8282
{
8383
await result.SaveAsPngAsync(outputFilename);

OnnxStack.Console/Examples/StableDiffusionBatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public async Task RunAsync()
6666
OutputHelpers.WriteConsole($"Image: {progress.BatchValue}/{progress.BatchMax} - Step: {progress.StepValue}/{progress.StepMax}", ConsoleColor.Cyan);
6767
};
6868

69-
await foreach (var result in _stableDiffusionService.GenerateBatchAsync(model, promptOptions, schedulerOptions, batchOptions, default))
69+
await foreach (var result in _stableDiffusionService.GenerateBatchAsync(new ModelOptions(model), promptOptions, schedulerOptions, batchOptions, default))
7070
{
7171
var outputFilename = Path.Combine(_outputDirectory, $"{batchIndex}_{result.SchedulerOptions.Seed}.png");
7272
var image = result.ImageResult.ToImage();

OnnxStack.Console/Examples/StableDiffusionExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public async Task RunAsync()
7070
private async Task<bool> GenerateImage(StableDiffusionModelSet model, PromptOptions prompt, SchedulerOptions options)
7171
{
7272
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{options.SchedulerType}.png");
73-
var result = await _stableDiffusionService.GenerateAsImageAsync(model, prompt, options);
73+
var result = await _stableDiffusionService.GenerateAsImageAsync(new ModelOptions(model), prompt, options);
7474
if (result == null)
7575
return false;
7676

OnnxStack.Console/Examples/StableDiffusionGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public async Task RunAsync()
6767
private async Task<bool> GenerateImage(StableDiffusionModelSet model, PromptOptions prompt, SchedulerOptions options, string key)
6868
{
6969
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{options.SchedulerType}_{key}.png");
70-
var result = await _stableDiffusionService.GenerateAsImageAsync(model, prompt, options);
70+
var result = await _stableDiffusionService.GenerateAsImageAsync(new ModelOptions(model), prompt, options);
7171
if (result == null)
7272
return false;
7373

OnnxStack.Console/Examples/StableDiffusionGif.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public async Task RunAsync()
102102

103103
// Set prompt Image, Run Diffusion
104104
promptOptions.InputImage = new InputImage(mergedFrame.CloneAs<Rgba32>());
105-
var result = await _stableDiffusionService.GenerateAsImageAsync(model, promptOptions, schedulerOptions);
105+
var result = await _stableDiffusionService.GenerateAsImageAsync(new ModelOptions(model), promptOptions, schedulerOptions);
106106

107107
// Save Debug Output
108108
await result.SaveAsPngAsync(Path.Combine(_outputDirectory, $"Debug-Output.png"));

OnnxStack.Core/Config/OnnxModelType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public enum OnnxModelType
99
TextEncoder2 = 21,
1010
VaeEncoder = 30,
1111
VaeDecoder = 40,
12+
ControlNet = 50,
13+
Annotation = 51,
1214
Upscaler = 1000
1315
}
1416
}

OnnxStack.Core/Extensions/OrtValueExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public static OrtValue ToOrtValue(this DenseTensor<long> tensor, OnnxNamedMetada
5959
return OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer, tensor.Dimensions.ToLong());
6060
}
6161

62+
public static OrtValue ToOrtValue(this DenseTensor<double> tensor, OnnxNamedMetadata metadata)
63+
{
64+
return OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer, tensor.Dimensions.ToLong());
65+
}
66+
6267

6368
/// <summary>
6469
/// Creates and allocates the output tensors buffer.

OnnxStack.Core/Image/Extensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ public static async Task<byte[]> ToImageBytesAsync(this DenseTensor<float> image
6363
}
6464
}
6565

66+
public static Image<Rgba32> ToImageMask(this DenseTensor<float> imageTensor)
67+
{
68+
var width = imageTensor.Dimensions[3];
69+
var height = imageTensor.Dimensions[2];
70+
using (var result = new Image<L8>(width, height))
71+
{
72+
for (var y = 0; y < height; y++)
73+
{
74+
for (var x = 0; x < width; x++)
75+
{
76+
result[x, y] = new L8((byte)(imageTensor[0, 0, y, x] * 255.0f));
77+
}
78+
}
79+
return result.CloneAs<Rgba32>();
80+
}
81+
}
82+
6683

6784
private static byte CalculateByte(Tensor<float> imageTensor, int index, int y, int x)
6885
{

OnnxStack.Core/Model/OnnxInferenceParameters.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public void AddInputTensor(DenseTensor<float> value)
4545
_inputs.Add(metaData, value.ToOrtValue(metaData));
4646
}
4747

48+
public void AddInputTensor(DenseTensor<double> value)
49+
{
50+
var metaData = GetNextInputMetadata();
51+
_inputs.Add(metaData, value.ToOrtValue(metaData));
52+
}
53+
4854

4955
/// <summary>
5056
/// Adds the input tensor.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using OnnxStack.Core.Image;
2+
using OnnxStack.StableDiffusion.Config;
3+
using System.Threading.Tasks;
4+
5+
namespace OnnxStack.StableDiffusion.Common
6+
{
7+
public interface IControlNetImageService
8+
{
9+
10+
/// <summary>
11+
/// Prepares the ContolNet input image, If the ControlNetModelSet has a configure Annotation model this will be used to process the image
12+
/// </summary>
13+
/// <param name="controlNetModel">The control net model.</param>
14+
/// <param name="inputImage">The input image.</param>
15+
/// <param name="height">The height.</param>
16+
/// <param name="width">The width.</param>
17+
/// <returns></returns>
18+
Task<InputImage> PrepareInputImage(ControlNetModelSet controlNetModel, InputImage inputImage, int height, int width);
19+
}
20+
}

0 commit comments

Comments
 (0)