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

Commit c61e6e3

Browse files
committed
Add FeatureExtractor model UI settings
1 parent e811216 commit c61e6e3

26 files changed

+912
-51
lines changed

OnnxStack.StableDiffusion/Pipelines/Base/PipelineBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected PipelineBase(PipelineOptions pipelineOptions, ILogger logger)
105105
/// <param name="progressCallback">The progress callback.</param>
106106
/// <param name="cancellationToken">The cancellation token.</param>
107107
/// <returns></returns>
108-
public abstract IAsyncEnumerable<BatchResult> RunBatchAsync(BatchOptions batchOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions = default, ControlNetModel controlNet = default, Action<DiffusionProgress> progressCallback = null, [EnumeratorCancellation] CancellationToken cancellationToken = default);
108+
public abstract IAsyncEnumerable<BatchResult> RunBatchAsync(BatchOptions batchOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions = default, ControlNetModel controlNet = default, Action<DiffusionProgress> progressCallback = null, CancellationToken cancellationToken = default);
109109

110110

111111
/// <summary>

OnnxStack.StableDiffusion/Pipelines/LatentConsistencyPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public override Task<DenseTensor<float>> RunAsync(PromptOptions promptOptions, S
7979
/// <param name="progressCallback">The progress callback.</param>
8080
/// <param name="cancellationToken">The cancellation token.</param>
8181
/// <returns></returns>
82-
public override IAsyncEnumerable<BatchResult> RunBatchAsync(BatchOptions batchOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions = default, ControlNetModel controlNet = default, Action<DiffusionProgress> progressCallback = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
82+
public override IAsyncEnumerable<BatchResult> RunBatchAsync(BatchOptions batchOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions = default, ControlNetModel controlNet = default, Action<DiffusionProgress> progressCallback = null, CancellationToken cancellationToken = default)
8383
{
8484
// LCM does not support negative prompting
8585
promptOptions.NegativePrompt = string.Empty;

OnnxStack.StableDiffusion/Pipelines/LatentConsistencyXLPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public override Task<DenseTensor<float>> RunAsync(PromptOptions promptOptions, S
8282
/// <param name="progressCallback">The progress callback.</param>
8383
/// <param name="cancellationToken">The cancellation token.</param>
8484
/// <returns></returns>
85-
public override IAsyncEnumerable<BatchResult> RunBatchAsync(BatchOptions batchOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions = default, ControlNetModel controlNet = default, Action<DiffusionProgress> progressCallback = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
85+
public override IAsyncEnumerable<BatchResult> RunBatchAsync(BatchOptions batchOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions = default, ControlNetModel controlNet = default, Action<DiffusionProgress> progressCallback = null, CancellationToken cancellationToken = default)
8686
{
8787
// LCM does not support negative prompting
8888
promptOptions.NegativePrompt = string.Empty;

OnnxStack.UI/App.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public App()
4242
builder.Services.AddTransient<UpdateUpscaleModelDialog>();
4343
builder.Services.AddTransient<AddControlNetModelDialog>();
4444
builder.Services.AddTransient<UpdateControlNetModelDialog>();
45+
builder.Services.AddTransient<AddFeatureExtractorModelDialog>();
46+
builder.Services.AddTransient<UpdateFeatureExtractorModelDialog>();
4547

4648
builder.Services.AddSingleton<IModelFactory, ModelFactory>();
4749
builder.Services.AddSingleton<IDialogService, DialogService>();

OnnxStack.UI/Dialogs/AddControlNetModelDialog.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public AddControlNetModelDialog(OnnxStackUIConfig settings, IModelFactory modelF
3838
_modelFactory = modelFactory;
3939
SaveCommand = new AsyncRelayCommand(Save, CanExecuteSave);
4040
CancelCommand = new AsyncRelayCommand(Cancel);
41-
_invalidOptions = _settings.GetModelNames();
41+
_invalidOptions = _settings.ControlNetModelSets.Select(x => x.Name).ToList();
4242
InitializeComponent();
4343
SelectedControlNetType = ControlNetType.Canny;
4444
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<Window x:Class="OnnxStack.UI.Dialogs.AddFeatureExtractorModelDialog"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:OnnxStack.UI.Dialogs"
7+
xmlns:models="clr-namespace:OnnxStack.UI.Models"
8+
xmlns:userControls="clr-namespace:OnnxStack.UI.UserControls"
9+
mc:Ignorable="d"
10+
Name="UI"
11+
Icon="/Images/Icon.png"
12+
MinWidth="500"
13+
SizeToContent="WidthAndHeight"
14+
WindowStartupLocation="CenterOwner"
15+
SnapsToDevicePixels="True"
16+
UseLayoutRounding="True"
17+
Title="Add Feature Extractor Model">
18+
<DockPanel DataContext="{Binding ElementName=UI}" Margin="15, 15, 15, 10">
19+
<StackPanel DockPanel.Dock="Top">
20+
21+
22+
23+
<StackPanel Margin="0,0,0,0">
24+
<TextBlock Text="Model File"/>
25+
<userControls:FilePickerTextBox FileName="{Binding ModelFile, Mode=TwoWay}" IsFolderPicker="False" Filter="Onnx Models (*.onnx)|*.onnx" DefaultExt="onnx" />
26+
</StackPanel>
27+
28+
<StackPanel Margin="0,10,0,0">
29+
<TextBlock Text="Model Name"/>
30+
<TextBox Text="{Binding ModelName, UpdateSourceTrigger=PropertyChanged}" />
31+
</StackPanel>
32+
33+
34+
<UniformGrid Columns="4" Margin="0,10,0,0">
35+
36+
37+
<StackPanel Margin="0,0,0,0">
38+
<TextBlock Text="Sample Size"/>
39+
<TextBox Text="{Binding SampleSize}" />
40+
</StackPanel>
41+
42+
<StackPanel Margin="0,0,0,0">
43+
<TextBlock Text="Channels"/>
44+
<TextBox Text="{Binding Channels}" />
45+
</StackPanel>
46+
47+
<StackPanel Margin="10,0,0,0">
48+
<TextBlock Text=""/>
49+
<StackPanel Orientation="Horizontal">
50+
<CheckBox IsChecked="{Binding Normalize}" />
51+
<TextBlock Text="Normalize"/>
52+
</StackPanel>
53+
</StackPanel>
54+
55+
56+
57+
<StackPanel>
58+
<TextBlock Text="ControlNet Filter"/>
59+
<ComboBox ItemsSource="{Binding ControlNetFilters}" SelectedItem="{Binding ControlNetFilter}" />
60+
</StackPanel>
61+
62+
</UniformGrid>
63+
64+
<StackPanel Margin="5">
65+
66+
67+
<ItemsControl ItemsSource="{Binding ValidationResults}">
68+
<ItemsControl.ItemTemplate>
69+
<DataTemplate DataType="{x:Type models:ValidationResult}">
70+
<StackPanel Orientation="Horizontal">
71+
<userControls:FontAwesome >
72+
<userControls:FontAwesome.Style>
73+
<Style TargetType="{x:Type userControls:FontAwesome}">
74+
<Setter Property="Color" Value="Red" />
75+
<Setter Property="Icon" Value="&#xf00d;" />
76+
<Style.Triggers>
77+
<DataTrigger Binding="{Binding IsValid}" Value="True">
78+
<Setter Property="Color" Value="LimeGreen" />
79+
<Setter Property="Icon" Value="&#xf00c;" />
80+
</DataTrigger>
81+
</Style.Triggers>
82+
</Style>
83+
</userControls:FontAwesome.Style>
84+
</userControls:FontAwesome>
85+
<TextBlock Text="{Binding Name}" Margin="5,0,0,3" />
86+
</StackPanel>
87+
</DataTemplate>
88+
</ItemsControl.ItemTemplate>
89+
</ItemsControl>
90+
91+
</StackPanel>
92+
93+
94+
</StackPanel>
95+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,20,0,0">
96+
<UniformGrid Columns="2" Height="30">
97+
<Button Content="Save" Command="{Binding SaveCommand}" IsDefault="True"/>
98+
<Button Content="Cancel" Command="{Binding CancelCommand}" Width="100"/>
99+
</UniformGrid>
100+
</StackPanel>
101+
</DockPanel>
102+
</Window>
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using Microsoft.Extensions.Logging;
2+
using OnnxStack.FeatureExtractor.Common;
3+
using OnnxStack.StableDiffusion.Enums;
4+
using OnnxStack.UI.Commands;
5+
using OnnxStack.UI.Models;
6+
using OnnxStack.UI.Services;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Collections.ObjectModel;
10+
using System.ComponentModel;
11+
using System.IO;
12+
using System.Linq;
13+
using System.Runtime.CompilerServices;
14+
using System.Threading.Tasks;
15+
using System.Windows;
16+
17+
namespace OnnxStack.UI.Dialogs
18+
{
19+
/// <summary>
20+
/// Interaction logic for AddFeatureExtractorModelDialog.xaml
21+
/// </summary>
22+
public partial class AddFeatureExtractorModelDialog : Window, INotifyPropertyChanged
23+
{
24+
private readonly ILogger<AddFeatureExtractorModelDialog> _logger;
25+
26+
private readonly List<string> _invalidOptions;
27+
private string _modelName;
28+
private string _modelFile;
29+
private bool _normalize;
30+
private int _sampleSize = 512;
31+
private int _channels = 1;
32+
private string _controlNetFilter = "N/A";
33+
private IModelFactory _modelFactory;
34+
private OnnxStackUIConfig _settings;
35+
private FeatureExtractorModelSet _modelSetResult;
36+
37+
public AddFeatureExtractorModelDialog(OnnxStackUIConfig settings, IModelFactory modelFactory, ILogger<AddFeatureExtractorModelDialog> logger)
38+
{
39+
_logger = logger;
40+
_settings = settings;
41+
_modelFactory = modelFactory;
42+
SaveCommand = new AsyncRelayCommand(Save, CanExecuteSave);
43+
CancelCommand = new AsyncRelayCommand(Cancel);
44+
_invalidOptions = _settings.FeatureExtractorModelSets.Select(x => x.Name).ToList();
45+
ControlNetFilters = new List<string> { "N/A" };
46+
ControlNetFilters.AddRange(Enum.GetNames<ControlNetType>());
47+
InitializeComponent();
48+
}
49+
50+
public AsyncRelayCommand SaveCommand { get; }
51+
public AsyncRelayCommand CancelCommand { get; }
52+
public ObservableCollection<ValidationResult> ValidationResults { get; set; } = new ObservableCollection<ValidationResult>();
53+
54+
public List<string> ControlNetFilters { get; set; }
55+
56+
public string ModelName
57+
{
58+
get { return _modelName; }
59+
set { _modelName = value; _modelName?.Trim(); NotifyPropertyChanged(); CreateModelSet(); }
60+
}
61+
62+
public string ModelFile
63+
{
64+
get { return _modelFile; }
65+
set
66+
{
67+
_modelFile = value;
68+
_modelName = string.IsNullOrEmpty(_modelFile)
69+
? string.Empty
70+
: Path.GetFileNameWithoutExtension(_modelFile);
71+
72+
NotifyPropertyChanged();
73+
NotifyPropertyChanged(nameof(ModelName));
74+
CreateModelSet();
75+
}
76+
}
77+
78+
public string ControlNetFilter
79+
{
80+
get { return _controlNetFilter; }
81+
set { _controlNetFilter = value; NotifyPropertyChanged(); }
82+
}
83+
84+
public int SampleSize
85+
{
86+
get { return _sampleSize; }
87+
set { _sampleSize = value; NotifyPropertyChanged(); CreateModelSet(); }
88+
}
89+
90+
public bool Normalize
91+
{
92+
get { return _normalize; }
93+
set { _normalize = value; NotifyPropertyChanged(); CreateModelSet(); }
94+
}
95+
96+
public int Channels
97+
{
98+
get { return _channels; }
99+
set { _channels = value; NotifyPropertyChanged(); CreateModelSet(); }
100+
}
101+
102+
public FeatureExtractorModelSet ModelSetResult
103+
{
104+
get { return _modelSetResult; }
105+
}
106+
107+
public ControlNetType? ControlNetType
108+
{
109+
get { return Enum.TryParse<ControlNetType>(_controlNetFilter, out var result) ? result : null; }
110+
}
111+
112+
113+
public new bool ShowDialog()
114+
{
115+
return base.ShowDialog() ?? false;
116+
}
117+
118+
119+
private void CreateModelSet()
120+
{
121+
_modelSetResult = null;
122+
ValidationResults.Clear();
123+
if (string.IsNullOrEmpty(_modelFile))
124+
return;
125+
126+
_modelSetResult = _modelFactory.CreateFeatureExtractorModelSet(ModelName.Trim(), _normalize, _sampleSize, _channels, _modelFile);
127+
128+
// Validate
129+
ValidationResults.Add(new ValidationResult("Name", !_invalidOptions.Contains(_modelName, StringComparer.OrdinalIgnoreCase) && _modelName.Length > 2 && _modelName.Length < 50));
130+
ValidationResults.Add(new ValidationResult("Model", File.Exists(_modelSetResult.FeatureExtractorConfig.OnnxModelPath)));
131+
}
132+
133+
134+
private Task Save()
135+
{
136+
DialogResult = true;
137+
return Task.CompletedTask;
138+
}
139+
140+
141+
private bool CanExecuteSave()
142+
{
143+
if (string.IsNullOrEmpty(_modelFile))
144+
return false;
145+
if (_modelSetResult is null)
146+
return false;
147+
148+
return ValidationResults.Count > 0 && ValidationResults.All(x => x.IsValid);
149+
}
150+
151+
152+
private Task Cancel()
153+
{
154+
_modelSetResult = null;
155+
DialogResult = false;
156+
return Task.CompletedTask;
157+
}
158+
159+
#region INotifyPropertyChanged
160+
public event PropertyChangedEventHandler PropertyChanged;
161+
public void NotifyPropertyChanged([CallerMemberName] string property = "")
162+
{
163+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
164+
}
165+
#endregion
166+
}
167+
}

OnnxStack.UI/Dialogs/AddModelDialog.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public AddModelDialog(OnnxStackUIConfig settings, IModelFactory modelFactory, IL
3737
SaveCommand = new AsyncRelayCommand(Save, CanExecuteSave);
3838
CancelCommand = new AsyncRelayCommand(Cancel);
3939
ModelTemplates = new List<StableDiffusionModelTemplate>(_modelFactory.GetStableDiffusionModelTemplates());
40-
InvalidOptions = _settings.GetModelNames();
40+
InvalidOptions = _settings.StableDiffusionModelSets.Select(x => x.Name).ToList();
4141
InitializeComponent();
4242
}
4343

OnnxStack.UI/Dialogs/AddUpscaleModelDialog.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public AddUpscaleModelDialog(OnnxStackUIConfig settings, IModelFactory modelFact
3737
SaveCommand = new AsyncRelayCommand(Save, CanExecuteSave);
3838
CancelCommand = new AsyncRelayCommand(Cancel);
3939
ModelTemplates = new List<UpscaleModelTemplate>(_modelFactory.GetUpscaleModelTemplates());
40-
InvalidOptions = _settings.GetModelNames();
40+
InvalidOptions = _settings.UpscaleModelSets.Select(x => x.Name).ToList();
4141
InitializeComponent();
4242
}
4343

OnnxStack.UI/Dialogs/UpdateControlNetModelDialog.xaml.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Collections.Generic;
55
using System.ComponentModel;
66
using System.IO;
7+
using System.Linq;
8+
using System.Runtime;
79
using System.Runtime.CompilerServices;
810
using System.Threading.Tasks;
911
using System.Windows;
@@ -16,21 +18,21 @@ namespace OnnxStack.UI.Dialogs
1618
public partial class UpdateControlNetModelDialog : Window, INotifyPropertyChanged
1719
{
1820
private List<string> _invalidOptions;
19-
private OnnxStackUIConfig _uiSettings;
21+
private OnnxStackUIConfig _settings;
2022
private ControlNetModelSet _modelSetResult;
2123
private UpdateControlNetModelSetViewModel _updateModelSet;
2224
private string _validationError;
2325

2426
public UpdateControlNetModelDialog(OnnxStackUIConfig uiSettings)
2527
{
26-
_uiSettings = uiSettings;
28+
_settings = uiSettings;
2729
SaveCommand = new AsyncRelayCommand(Save, CanExecuteSave);
2830
CancelCommand = new AsyncRelayCommand(Cancel, CanExecuteCancel);
29-
_invalidOptions = uiSettings.GetModelNames();
31+
_invalidOptions = _settings.ControlNetModelSets.Select(x => x.Name).ToList();
3032
InitializeComponent();
3133
}
3234

33-
public OnnxStackUIConfig UISettings => _uiSettings;
35+
public OnnxStackUIConfig UISettings => _settings;
3436
public AsyncRelayCommand SaveCommand { get; }
3537
public AsyncRelayCommand CancelCommand { get; }
3638

0 commit comments

Comments
 (0)