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

Commit 1e5aede

Browse files
committed
ControlNetImageService support in Diffusers
1 parent 9205547 commit 1e5aede

File tree

14 files changed

+46
-562
lines changed

14 files changed

+46
-562
lines changed

OnnxStack.StableDiffusion/Common/IImageService.cs renamed to OnnxStack.StableDiffusion/Common/IControlNetImageService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OnnxStack.StableDiffusion.Common
66
{
7-
public interface IImageService
7+
public interface IControlNetImageService
88
{
99

1010
/// <summary>

OnnxStack.StableDiffusion/Config/SchedulerOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public record SchedulerOptions
8383

8484
public float AestheticScore { get; set; } = 6f;
8585
public float AestheticNegativeScore { get; set; } = 2.5f;
86+
8687
public float ConditioningScale { get; set; } = 0.7f;
88+
public bool IsControlImageProcessingEnabled { get; set; }
8789

8890
public bool IsKarrasScheduler
8991
{

OnnxStack.StableDiffusion/Diffusers/StableDiffusion/ControlNetDiffuser.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ namespace OnnxStack.StableDiffusion.Diffusers.StableDiffusion
2121
{
2222
public class ControlNetDiffuser : DiffuserBase
2323
{
24+
private readonly IControlNetImageService _controlNetImageService;
25+
2426
/// <summary>
2527
/// Initializes a new instance of the <see cref="ControlNetDiffuser"/> class.
2628
/// </summary>
2729
/// <param name="configuration">The configuration.</param>
2830
/// <param name="onnxModelService">The onnx model service.</param>
29-
public ControlNetDiffuser(IOnnxModelService onnxModelService, IPromptService promptService, ILogger<ControlNetDiffuser> logger)
30-
: base(onnxModelService, promptService, logger) { }
31+
public ControlNetDiffuser(IOnnxModelService onnxModelService, IPromptService promptService, IControlNetImageService controlNetImageService, ILogger<ControlNetDiffuser> logger)
32+
: base(onnxModelService, promptService, logger)
33+
{
34+
_controlNetImageService = controlNetImageService;
35+
}
3136

3237

3338
/// <summary>
@@ -72,7 +77,7 @@ protected override async Task<DenseTensor<float>> SchedulerStepAsync(ModelOption
7277
var controlNetMetadata = _onnxModelService.GetModelMetadata(modelOptions.ControlNetModel, OnnxModelType.ControlNet);
7378

7479
// Control Image
75-
var controlImage = PrepareControlImage(promptOptions, schedulerOptions);
80+
var controlImage = await PrepareControlImage(modelOptions, promptOptions, schedulerOptions);
7681

7782
// Loop though the timesteps
7883
var step = 0;
@@ -191,9 +196,14 @@ protected static DenseTensor<double> CreateConditioningScaleTensor(float conditi
191196
/// <param name="promptOptions">The prompt options.</param>
192197
/// <param name="schedulerOptions">The scheduler options.</param>
193198
/// <returns></returns>
194-
protected DenseTensor<float> PrepareControlImage(PromptOptions promptOptions, SchedulerOptions schedulerOptions)
199+
protected async Task<DenseTensor<float>> PrepareControlImage(ModelOptions modelOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions)
195200
{
196-
return promptOptions.InputContolImage.ToDenseTensor(new[] { 1, 3, schedulerOptions.Height, schedulerOptions.Width }, false);
201+
var controlImage = promptOptions.InputContolImage;
202+
if (schedulerOptions.IsControlImageProcessingEnabled)
203+
{
204+
controlImage = await _controlNetImageService.PrepareInputImage(modelOptions.ControlNetModel, promptOptions.InputContolImage, schedulerOptions.Height, schedulerOptions.Width);
205+
}
206+
return controlImage.ToDenseTensor(new[] { 1, 3, schedulerOptions.Height, schedulerOptions.Width }, false);
197207
}
198208

199209

OnnxStack.StableDiffusion/Diffusers/StableDiffusion/ControlNetImageDiffuser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public sealed class ControlNetImageDiffuser : ControlNetDiffuser
2323
/// </summary>
2424
/// <param name="configuration">The configuration.</param>
2525
/// <param name="onnxModelService">The onnx model service.</param>
26-
public ControlNetImageDiffuser(IOnnxModelService onnxModelService, IPromptService promptService, ILogger<ControlNetDiffuser> logger)
27-
: base(onnxModelService, promptService, logger)
26+
public ControlNetImageDiffuser(IOnnxModelService onnxModelService, IPromptService promptService, IControlNetImageService controlNetImageService, ILogger<ControlNetDiffuser> logger)
27+
: base(onnxModelService, promptService, controlNetImageService, logger)
2828
{
2929
}
3030

OnnxStack.StableDiffusion/Registration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static void RegisterServices(this IServiceCollection serviceCollection)
4545
ConfigureLibraries();
4646

4747
// Services
48-
serviceCollection.AddSingleton<IImageService, ImageService>();
48+
serviceCollection.AddSingleton<IControlNetImageService, ControlNetImageService>();
4949
serviceCollection.AddSingleton<IVideoService, VideoService>();
5050
serviceCollection.AddSingleton<IPromptService, PromptService>();
5151
serviceCollection.AddSingleton<IStableDiffusionService, StableDiffusionService>();

OnnxStack.StableDiffusion/Services/ImageService.cs renamed to OnnxStack.StableDiffusion/Services/ControlNetImageService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ namespace OnnxStack.StableDiffusion.Services
1717
/// <summary>
1818
/// Service for handing images for input and output of the diffusion process
1919
/// </summary>
20-
/// <seealso cref="OnnxStack.StableDiffusion.Common.IImageService" />
21-
public class ImageService : IImageService
20+
/// <seealso cref="OnnxStack.StableDiffusion.Common.IControlNetImageService" />
21+
public class ControlNetImageService : IControlNetImageService
2222
{
23-
private readonly ILogger<ImageService> _logger;
23+
private readonly ILogger<ControlNetImageService> _logger;
2424
private readonly IOnnxModelService _onnxModelService;
2525

2626

2727
/// <summary>
28-
/// Initializes a new instance of the <see cref="ImageService"/> class.
28+
/// Initializes a new instance of the <see cref="ControlNetImageService"/> class.
2929
/// </summary>
3030
/// <param name="onnxModelService">The onnx model service.</param>
31-
public ImageService(IOnnxModelService onnxModelService, ILogger<ImageService> logger)
31+
public ControlNetImageService(IOnnxModelService onnxModelService, ILogger<ControlNetImageService> logger)
3232
{
3333
_logger = logger;
3434
_onnxModelService = onnxModelService;

OnnxStack.UI/MainWindow.xaml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,6 @@
7474
<views:VideoToVideoView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
7575
</TabItem>
7676

77-
<!--ControlNet-->
78-
<TabItem>
79-
<TabItem.Header>
80-
<StackPanel Orientation="Horizontal" Margin="5">
81-
<StackPanel Orientation="Horizontal">
82-
<userControls:FontAwesome Icon="&#xf6ff;" IconStyle="Light"/>
83-
</StackPanel>
84-
<TextBlock Text="ControlNet" Margin="5,0,0,0"/>
85-
</StackPanel>
86-
</TabItem.Header>
87-
<views:ControlNetView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
88-
</TabItem>
89-
9077
<!--Upscale-->
9178
<TabItem>
9279
<TabItem.Header>

OnnxStack.UI/Models/SchedulerOptionsModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class SchedulerOptionsModel : INotifyPropertyChanged
3333
private int _originalInferenceSteps = 100;
3434
private SchedulerType _schedulerType;
3535
private float _conditioningScale = 0.7f;
36-
private bool _processInputImage;
36+
private bool _isControlImageProcessingEnabled = true;
3737
private bool _hasChanged;
3838

3939
/// <summary>
@@ -209,10 +209,10 @@ public float ConditioningScale
209209
set { _conditioningScale = value; NotifyPropertyChanged(); }
210210
}
211211

212-
public bool ProcessInputImage
212+
public bool IsControlImageProcessingEnabled
213213
{
214-
get { return _processInputImage; }
215-
set { _processInputImage = value; NotifyPropertyChanged(); }
214+
get { return _isControlImageProcessingEnabled; }
215+
set { _isControlImageProcessingEnabled = value; NotifyPropertyChanged(); }
216216
}
217217

218218
public bool HasChanged

OnnxStack.UI/Models/StableDiffusionModelSetViewModel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using OnnxStack.StableDiffusion.Config;
1+
using OnnxStack.Core.Config;
2+
using OnnxStack.StableDiffusion.Config;
23
using OnnxStack.StableDiffusion.Enums;
34
using System.ComponentModel;
5+
using System.Linq;
46
using System.Runtime.CompilerServices;
57
using System.Text.Json.Serialization;
68

@@ -32,7 +34,9 @@ public bool IsLoading
3234
set { _isLoading = value; NotifyPropertyChanged(); }
3335
}
3436

37+
[JsonIgnore]
3538
public bool IsControlNet => ModelSet.Diffusers.Contains(DiffuserType.ControlNet);
39+
3640

3741
public StableDiffusionModelSet ModelSet { get; set; }
3842

OnnxStack.UI/UserControls/SchedulerControl.xaml

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,9 @@
3232
<Label>Scheduler</Label>
3333
<ComboBox ItemsSource="{Binding SchedulerTypes}" SelectedItem="{Binding SchedulerOptions.SchedulerType}"/>
3434
</StackPanel>
35-
3635
<StackPanel Margin="5,0,0,0" VerticalAlignment="Bottom" Visibility="{Binding SelectedModel.IsControlNet, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed}">
37-
<CheckBox IsChecked="{Binding SchedulerOptions.ProcessInputImage}" Content="Process Input Image" Margin="0,0,0,3"/>
38-
39-
<!--<StackPanel.Style>
40-
<Style TargetType="{x:Type StackPanel}">
41-
<Setter Property="IsEnabled" Value="False" />
42-
<Style.Triggers>
43-
<DataTrigger Binding="{Binding SchedulerOptions.Strength}" Value="1">
44-
<Setter Property="IsEnabled" Value="True" />
45-
</DataTrigger>
46-
</Style.Triggers>
47-
</Style>
48-
</StackPanel.Style>-->
36+
<CheckBox IsChecked="{Binding SchedulerOptions.IsControlImageProcessingEnabled}" Content="Process Input Image" Margin="0,0,0,3"/>
4937
</StackPanel>
50-
51-
5238
</UniformGrid>
5339

5440

0 commit comments

Comments
 (0)