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

Commit 42b26a7

Browse files
committed
PaintDiffusers for SD and LCM
1 parent b9f99a7 commit 42b26a7

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Microsoft.Extensions.Logging;
2+
using Microsoft.ML.OnnxRuntime;
3+
using Microsoft.ML.OnnxRuntime.Tensors;
4+
using OnnxStack.Core;
5+
using OnnxStack.Core.Config;
6+
using OnnxStack.Core.Model;
7+
using OnnxStack.Core.Services;
8+
using OnnxStack.StableDiffusion.Common;
9+
using OnnxStack.StableDiffusion.Config;
10+
using OnnxStack.StableDiffusion.Enums;
11+
using OnnxStack.StableDiffusion.Helpers;
12+
using SixLabors.ImageSharp;
13+
using System;
14+
using System.Collections.Generic;
15+
using System.Linq;
16+
using System.Threading.Tasks;
17+
18+
namespace OnnxStack.StableDiffusion.Diffusers.LatentConsistency
19+
{
20+
public sealed class PaintDiffuser : LatentConsistencyDiffuser
21+
{
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="PaintDiffuser"/> class.
24+
/// </summary>
25+
/// <param name="configuration">The configuration.</param>
26+
/// <param name="onnxModelService">The onnx model service.</param>
27+
public PaintDiffuser(IOnnxModelService onnxModelService, IPromptService promptService, ILogger<LatentConsistencyDiffuser> logger)
28+
: base(onnxModelService, promptService, logger) { }
29+
30+
31+
/// <summary>
32+
/// Gets the type of the diffuser.
33+
/// </summary>
34+
public override DiffuserType DiffuserType => DiffuserType.PaintToImage;
35+
36+
37+
/// <summary>
38+
/// Gets the timesteps.
39+
/// </summary>
40+
/// <param name="prompt">The prompt.</param>
41+
/// <param name="options">The options.</param>
42+
/// <param name="scheduler">The scheduler.</param>
43+
/// <returns></returns>
44+
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
45+
{
46+
// Image2Image we narrow step the range by the Strength
47+
var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
48+
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
49+
return scheduler.Timesteps.Skip(start).ToList();
50+
}
51+
52+
53+
/// <summary>
54+
/// Prepares the latents for inference.
55+
/// </summary>
56+
/// <param name="prompt">The prompt.</param>
57+
/// <param name="options">The options.</param>
58+
/// <param name="scheduler">The scheduler.</param>
59+
/// <returns></returns>
60+
protected override async Task<DenseTensor<float>> PrepareLatentsAsync(IModelOptions model, PromptOptions prompt, SchedulerOptions options, IScheduler scheduler, IReadOnlyList<int> timesteps)
61+
{
62+
var imageTensor = prompt.InputImage.ToDenseTensor(new[] { 1, 3, options.Height, options.Width });
63+
64+
//TODO: Model Config, Channels
65+
var outputDimension = options.GetScaledDimension();
66+
var metadata = _onnxModelService.GetModelMetadata(model, OnnxModelType.VaeEncoder);
67+
using (var inferenceParameters = new OnnxInferenceParameters(metadata))
68+
{
69+
inferenceParameters.AddInputTensor(imageTensor);
70+
inferenceParameters.AddOutputBuffer(outputDimension);
71+
72+
var results = await _onnxModelService.RunInferenceAsync(model, OnnxModelType.VaeEncoder, inferenceParameters);
73+
using (var result = results.First())
74+
{
75+
var outputResult = result.ToDenseTensor();
76+
var scaledSample = outputResult
77+
.Add(scheduler.CreateRandomSample(outputDimension, options.InitialNoiseLevel))
78+
.MultiplyBy(model.ScaleFactor);
79+
80+
return scheduler.AddNoise(scaledSample, scheduler.CreateRandomSample(scaledSample.Dimensions), timesteps);
81+
}
82+
}
83+
}
84+
}
85+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Microsoft.Extensions.Logging;
2+
using Microsoft.ML.OnnxRuntime;
3+
using Microsoft.ML.OnnxRuntime.Tensors;
4+
using OnnxStack.Core;
5+
using OnnxStack.Core.Config;
6+
using OnnxStack.Core.Model;
7+
using OnnxStack.Core.Services;
8+
using OnnxStack.StableDiffusion.Common;
9+
using OnnxStack.StableDiffusion.Config;
10+
using OnnxStack.StableDiffusion.Enums;
11+
using OnnxStack.StableDiffusion.Helpers;
12+
using SixLabors.ImageSharp;
13+
using System;
14+
using System.Collections.Generic;
15+
using System.Linq;
16+
using System.Threading.Tasks;
17+
18+
namespace OnnxStack.StableDiffusion.Diffusers.StableDiffusion
19+
{
20+
public sealed class PaintDiffuser : StableDiffusionDiffuser
21+
{
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="PaintDiffuser"/> class.
24+
/// </summary>
25+
/// <param name="configuration">The configuration.</param>
26+
/// <param name="onnxModelService">The onnx model service.</param>
27+
public PaintDiffuser(IOnnxModelService onnxModelService, IPromptService promptService, ILogger<StableDiffusionDiffuser> logger)
28+
: base(onnxModelService, promptService, logger)
29+
{
30+
}
31+
32+
33+
/// <summary>
34+
/// Gets the type of the diffuser.
35+
/// </summary>
36+
public override DiffuserType DiffuserType => DiffuserType.PaintToImage;
37+
38+
39+
/// <summary>
40+
/// Gets the timesteps.
41+
/// </summary>
42+
/// <param name="prompt">The prompt.</param>
43+
/// <param name="options">The options.</param>
44+
/// <param name="scheduler">The scheduler.</param>
45+
/// <returns></returns>
46+
protected override IReadOnlyList<int> GetTimesteps(SchedulerOptions options, IScheduler scheduler)
47+
{
48+
// Image2Image we narrow step the range by the Strength
49+
var inittimestep = Math.Min((int)(options.InferenceSteps * options.Strength), options.InferenceSteps);
50+
var start = Math.Max(options.InferenceSteps - inittimestep, 0);
51+
return scheduler.Timesteps.Skip(start).ToList();
52+
}
53+
54+
55+
/// <summary>
56+
/// Prepares the latents for inference.
57+
/// </summary>
58+
/// <param name="prompt">The prompt.</param>
59+
/// <param name="options">The options.</param>
60+
/// <param name="scheduler">The scheduler.</param>
61+
/// <returns></returns>
62+
protected override async Task<DenseTensor<float>> PrepareLatentsAsync(IModelOptions model, PromptOptions prompt, SchedulerOptions options, IScheduler scheduler, IReadOnlyList<int> timesteps)
63+
{
64+
var imageTensor = prompt.InputImage.ToDenseTensor(new[] { 1, 3, options.Height, options.Width });
65+
66+
//TODO: Model Config, Channels
67+
var outputDimension = options.GetScaledDimension();
68+
var metadata = _onnxModelService.GetModelMetadata(model, OnnxModelType.VaeEncoder);
69+
using (var inferenceParameters = new OnnxInferenceParameters(metadata))
70+
{
71+
inferenceParameters.AddInputTensor(imageTensor);
72+
inferenceParameters.AddOutputBuffer(outputDimension);
73+
74+
var results = await _onnxModelService.RunInferenceAsync(model, OnnxModelType.VaeEncoder, inferenceParameters);
75+
using (var result = results.First())
76+
{
77+
var outputResult = result.ToDenseTensor();
78+
var scaledSample = outputResult
79+
.Add(scheduler.CreateRandomSample(outputDimension, options.InitialNoiseLevel))
80+
.MultiplyBy(model.ScaleFactor);
81+
82+
return scheduler.AddNoise(scaledSample, scheduler.CreateRandomSample(scaledSample.Dimensions), timesteps);
83+
}
84+
}
85+
}
86+
87+
}
88+
}

OnnxStack.StableDiffusion/Enums/DiffuserType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public enum DiffuserType
77
ImageInpaint = 2,
88
ImageInpaintLegacy = 3,
99
ImageToAnimation = 4,
10+
PaintToImage = 5
1011
}
1112
}

OnnxStack.StableDiffusion/Registration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ public static void AddOnnxStackStableDiffusion(this IServiceCollection serviceCo
3838
serviceCollection.AddSingleton<IDiffuser, StableDiffusion.Diffusers.StableDiffusion.ImageDiffuser>();
3939
serviceCollection.AddSingleton<IDiffuser, StableDiffusion.Diffusers.StableDiffusion.InpaintDiffuser>();
4040
serviceCollection.AddSingleton<IDiffuser, StableDiffusion.Diffusers.StableDiffusion.InpaintLegacyDiffuser>();
41+
serviceCollection.AddSingleton<IDiffuser, StableDiffusion.Diffusers.StableDiffusion.PaintDiffuser>();
4142

4243
//LatentConsistency
4344
serviceCollection.AddSingleton<IDiffuser, StableDiffusion.Diffusers.LatentConsistency.TextDiffuser>();
4445
serviceCollection.AddSingleton<IDiffuser, StableDiffusion.Diffusers.LatentConsistency.ImageDiffuser>();
4546
serviceCollection.AddSingleton<IDiffuser, StableDiffusion.Diffusers.LatentConsistency.InpaintLegacyDiffuser>();
47+
serviceCollection.AddSingleton<IDiffuser, StableDiffusion.Diffusers.LatentConsistency.PaintDiffuser>();
4648
}
4749

4850

0 commit comments

Comments
 (0)