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

Commit cd78ba4

Browse files
authored
Merge pull request #64 from saddam213/UpscaleUI
Add Upscale Support to UI
2 parents c1bb71a + de58517 commit cd78ba4

File tree

69 files changed

+5893
-2788
lines changed

Some content is hidden

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

69 files changed

+5893
-2788
lines changed

OnnxStack.Core/Config/OnnxModelType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public enum OnnxModelType
88
TextEncoder = 20,
99
TextEncoder2 = 21,
1010
VaeEncoder = 30,
11-
VaeDecoder = 40
11+
VaeDecoder = 40,
12+
Upscaler = 1000
1213
}
1314
}

OnnxStack.ImageUpscaler/Config/UpscaleModelSet.cs

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

55
namespace OnnxStack.StableDiffusion.Config
66
{
7-
public class UpscaleModelSet : IOnnxModelSetConfig
7+
public record UpscaleModelSet : IOnnxModelSetConfig
88
{
99
public string Name { get; set; }
1010
public int Channels { get; set; }

OnnxStack.ImageUpscaler/Services/IUpscaleService.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,21 @@ public interface IUpscaleService
4646
/// <returns></returns>
4747
Task<bool> LoadModelAsync(UpscaleModelSet model);
4848

49-
5049
/// <summary>
5150
/// Unloads the model.
5251
/// </summary>
5352
/// <param name="model">The model.</param>
5453
/// <returns></returns>
5554
Task<bool> UnloadModelAsync(UpscaleModelSet model);
5655

56+
/// <summary>
57+
/// Determines whether [is model loaded] [the specified model options].
58+
/// </summary>
59+
/// <param name="modelOptions">The model options.</param>
60+
/// <returns>
61+
/// <c>true</c> if [is model loaded] [the specified model options]; otherwise, <c>false</c>.
62+
/// </returns>
63+
bool IsModelLoaded(UpscaleModelSet modelOptions);
5764

5865
/// <summary>
5966
/// Generates the upscaled image.

OnnxStack.ImageUpscaler/Services/UpscaleService.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ public async Task<bool> UnloadModelAsync(UpscaleModelSet model)
124124
}
125125

126126

127+
/// <summary>
128+
/// Determines whether [is model loaded] [the specified model options].
129+
/// </summary>
130+
/// <param name="modelOptions">The model options.</param>
131+
/// <returns>
132+
/// <c>true</c> if [is model loaded] [the specified model options]; otherwise, <c>false</c>.
133+
/// </returns>
134+
/// <exception cref="System.NotImplementedException"></exception>
135+
public bool IsModelLoaded(UpscaleModelSet modelOptions)
136+
{
137+
return _modelService.IsModelLoaded(modelOptions);
138+
}
139+
140+
127141
/// <summary>
128142
/// Generates the upscaled image.
129143
/// </summary>
@@ -191,7 +205,7 @@ private async Task<Image<Rgba32>> GenerateInternalAsync(UpscaleModelSet modelSet
191205
using (var image = inputImage.ToImage())
192206
{
193207
var upscaleInput = CreateInputParams(image, modelSet.SampleSize, modelSet.ScaleFactor);
194-
var metadata = _modelService.GetModelMetadata(modelSet, OnnxModelType.Unet);
208+
var metadata = _modelService.GetModelMetadata(modelSet, OnnxModelType.Upscaler);
195209

196210
var outputResult = new Image<Rgba32>(upscaleInput.OutputWidth, upscaleInput.OutputHeight);
197211
foreach (var tile in upscaleInput.ImageTiles)
@@ -205,7 +219,7 @@ private async Task<Image<Rgba32>> GenerateInternalAsync(UpscaleModelSet modelSet
205219
inferenceParameters.AddInputTensor(inputTensor);
206220
inferenceParameters.AddOutputBuffer(outputDimension);
207221

208-
var results = await _modelService.RunInferenceAsync(modelSet, OnnxModelType.Unet, inferenceParameters);
222+
var results = await _modelService.RunInferenceAsync(modelSet, OnnxModelType.Upscaler, inferenceParameters);
209223
using (var result = results.First())
210224
{
211225
outputResult.Mutate(x => x.DrawImage(result.ToImage(), tile.Destination.Location, 1f));
@@ -231,5 +245,6 @@ private UpscaleInput CreateInputParams(Image<Rgba32> imageSource, int maxTileSiz
231245
var height = imageSource.Height * scaleFactor;
232246
return new UpscaleInput(tiles, width, height);
233247
}
248+
234249
}
235250
}

OnnxStack.StableDiffusion/Config/StableDiffusionModelSet.cs

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

99
namespace OnnxStack.StableDiffusion.Config
1010
{
11-
public class StableDiffusionModelSet : IOnnxModelSetConfig
11+
public record StableDiffusionModelSet : IOnnxModelSetConfig
1212
{
1313
public string Name { get; set; }
1414
public bool IsEnabled { get; set; }
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
namespace OnnxStack.StableDiffusion.Enums
1+
using System.ComponentModel;
2+
3+
namespace OnnxStack.StableDiffusion.Enums
24
{
35
public enum DiffuserType
46
{
7+
[Description("Text To Image")]
58
TextToImage = 0,
9+
10+
[Description("Image To Image")]
611
ImageToImage = 1,
12+
13+
[Description("Image Inpaint")]
714
ImageInpaint = 2,
15+
16+
[Description("Image Inpaint Legacy")]
817
ImageInpaintLegacy = 3,
18+
19+
[Description("Image To Animation")]
920
ImageToAnimation = 4
1021
}
1122
}

OnnxStack.UI/App.xaml

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:system="clr-namespace:System;assembly=mscorlib"
5+
xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=System.ComponentModel.TypeConverter"
56
xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
6-
xmlns:local="clr-namespace:OnnxStack.UI"
7+
xmlns:local="clr-namespace:OnnxStack.UI"
78
xmlns:SD_Enums="clr-namespace:OnnxStack.StableDiffusion.Enums;assembly=OnnxStack.StableDiffusion"
89
xmlns:userControls="clr-namespace:OnnxStack.UI.UserControls"
910
xmlns:behaviors="clr-namespace:OnnxStack.UI.Behaviors"
@@ -25,7 +26,8 @@
2526
<FontFamily x:Key="FontAwesomeRegular">pack://application:,,,/Fonts/fa-regular-400.ttf#Font Awesome 5 Pro Regular</FontFamily>
2627
<FontFamily x:Key="FontAwesomeSolid">pack://application:,,,/Fonts/fa-solid-900.ttf#Font Awesome 5 Pro Solid</FontFamily>
2728

28-
29+
<system:Boolean x:Key="True">True</system:Boolean>
30+
<system:Boolean x:Key="False">False</system:Boolean>
2931
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
3032
<converters:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter" />
3133
<converters:InverseBoolConverter x:Key="InverseBoolConverter" />
@@ -35,13 +37,33 @@
3537
<converters:InverseNullVisibilityConverter x:Key="InverseNullVisibilityConverter" />
3638
<converters:DiffuserVisibilityConverter x:Key="DiffuserVisibilityConverter" />
3739
<converters:PipelineVisibilityConverter x:Key="PipelineVisibilityConverter" />
40+
<converters:ComboBoxAllItemConverter x:Key="ComboBoxAllItemConverter" />
41+
<converters:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
42+
43+
<ObjectDataProvider x:Key="ListSortDirection" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
44+
<ObjectDataProvider.MethodParameters>
45+
<x:Type TypeName="componentModel:ListSortDirection"/>
46+
</ObjectDataProvider.MethodParameters>
47+
</ObjectDataProvider>
3848

3949
<ObjectDataProvider x:Key="SchedulerType" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
4050
<ObjectDataProvider.MethodParameters>
4151
<x:Type TypeName="SD_Enums:SchedulerType"/>
4252
</ObjectDataProvider.MethodParameters>
4353
</ObjectDataProvider>
4454

55+
<ObjectDataProvider x:Key="ModelType" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
56+
<ObjectDataProvider.MethodParameters>
57+
<x:Type TypeName="SD_Enums:ModelType"/>
58+
</ObjectDataProvider.MethodParameters>
59+
</ObjectDataProvider>
60+
61+
<ObjectDataProvider x:Key="TokenizerType" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
62+
<ObjectDataProvider.MethodParameters>
63+
<x:Type TypeName="SD_Enums:TokenizerType"/>
64+
</ObjectDataProvider.MethodParameters>
65+
</ObjectDataProvider>
66+
4567
<ObjectDataProvider x:Key="AlphaTransformType" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
4668
<ObjectDataProvider.MethodParameters>
4769
<x:Type TypeName="SD_Enums:AlphaTransformType"/>
@@ -106,7 +128,7 @@
106128

107129

108130

109-
131+
110132

111133

112134

@@ -233,8 +255,8 @@
233255
<TextBlock Text="{Binding SchedulerOptions.Seed}" Margin="2,0,0,0" />
234256
</StackPanel>
235257
</UniformGrid>
236-
<UniformGrid Columns="6" Margin="0,2,0,2">
237-
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveImageCommand}" CommandParameter="{Binding}">
258+
<UniformGrid Columns="7" Margin="0,2,0,2">
259+
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveImageResultCommand}" CommandParameter="{Binding}">
238260
<userControls:FontAwesome Icon="&#xf0c7;" IconStyle="Light" Size="13" ToolTip="Save Image File"/>
239261
</Button>
240262
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveBlueprintCommand}" CommandParameter="{Binding}" BorderThickness="0, 1">
@@ -252,6 +274,9 @@
252274
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateImagePaintToImageCommand}" CommandParameter="{Binding}" Padding="2,2" BorderThickness="0,1,1,1">
253275
<userControls:FontAwesome Icon="&#xf1fc;" IconStyle="Light" Size="13" ToolTip="Send To Upscaler"/>
254276
</Button>
277+
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateUpscalerCommand}" CommandParameter="{Binding}" Padding="2,2" BorderThickness="0,1,1,1">
278+
<userControls:FontAwesome Icon="&#xf424;" IconStyle="Light" Size="13" ToolTip="Send To Upscaler"/>
279+
</Button>
255280
</UniformGrid>
256281
</StackPanel>
257282
<Border Margin="0,2" BorderBrush="{StaticResource ContainerBorder}" BorderThickness="1" Background="{StaticResource ContainerBackground}" >
@@ -260,7 +285,55 @@
260285
</DockPanel>
261286
</DataTemplate>
262287

288+
<DataTemplate DataType="{x:Type models:UpscaleResult}">
289+
<DockPanel Width="220" Height="285" HorizontalAlignment="Center">
290+
<StackPanel DockPanel.Dock="Bottom" >
291+
292+
<UniformGrid Columns="2" Margin="0,3">
263293

294+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
295+
<TextBlock Text="Scale Factor:" Margin="0,0,5,0" FontStyle="Italic" FontSize="10" Opacity=".6" VerticalAlignment="Center"/>
296+
<TextBlock Text="{Binding Info.ScaleFactor}" />
297+
</StackPanel>
298+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
299+
<TextBlock Text="Elapsed:" Margin="5,0,0,0" FontSize="10" Opacity=".6" VerticalAlignment="Center" FontStyle="Italic"/>
300+
<TextBlock Text="{Binding Elapsed, StringFormat={}{0:F1}s}" Margin="5,0,0,0"/>
301+
</StackPanel>
302+
303+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
304+
<TextBlock Text="Input:" Margin="0,0,5,0" FontStyle="Italic" FontSize="10" Opacity=".6" VerticalAlignment="Center"/>
305+
<TextBlock Text="{Binding Info.InputWidth}" />
306+
<TextBlock Text="x" Margin="2,0,2,0"/>
307+
<TextBlock Text="{Binding Info.InputHeight}" />
308+
</StackPanel>
309+
310+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
311+
<TextBlock Text="Output:" Margin="0,0,5,0" FontStyle="Italic" FontSize="10" Opacity=".6" VerticalAlignment="Center"/>
312+
<TextBlock Text="{Binding Info.OutputWidth}" />
313+
<TextBlock Text="x" Margin="2,0,2,0"/>
314+
<TextBlock Text="{Binding Info.OutputHeight}" />
315+
</StackPanel>
316+
317+
</UniformGrid>
318+
319+
320+
321+
<UniformGrid Columns="1" Height="30" IsEnabled="{Binding HasResult}">
322+
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveImageCommand}" CommandParameter="{Binding}" ToolTip="Save Image File">
323+
<StackPanel Orientation="Horizontal">
324+
<userControls:FontAwesome Icon="&#xf0c7;" IconStyle="Light" Size="13" />
325+
<TextBlock Text="Save Image" Margin="5,0,0,0"/>
326+
</StackPanel>
327+
</Button>
328+
</UniformGrid>
329+
330+
331+
</StackPanel>
332+
<Border Margin="0,2" BorderBrush="{StaticResource ContainerBorder}" BorderThickness="1" Background="{StaticResource ContainerBackground}" >
333+
<Image Source="{Binding Image}" DockPanel.Dock="Left" Stretch="Uniform" VerticalAlignment="Center" />
334+
</Border>
335+
</DockPanel>
336+
</DataTemplate>
264337

265338
<!--RadioButton-->
266339
<SolidColorBrush x:Key="RadioButtonBackground" Color="#FF2D2D2D"/>
@@ -722,14 +795,15 @@
722795

723796

724797

725-
726-
727-
728-
729-
730-
731-
732-
798+
<Style x:Key="EnumComboBox" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
799+
<Setter Property="ItemTemplate">
800+
<Setter.Value>
801+
<DataTemplate>
802+
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"/>
803+
</DataTemplate>
804+
</Setter.Value>
805+
</Setter>
806+
</Style>
733807

734808

735809

OnnxStack.UI/App.xaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.Extensions.Logging;
44
using OnnxStack.Core;
5+
using OnnxStack.ImageUpscaler;
56
using OnnxStack.UI.Dialogs;
67
using OnnxStack.UI.Models;
78
using OnnxStack.UI.Services;
@@ -26,6 +27,7 @@ public App()
2627

2728
// Add OnnxStackStableDiffusion
2829
builder.Services.AddOnnxStackStableDiffusion();
30+
builder.Services.AddOnnxStackImageUpscaler();
2931
builder.Services.AddOnnxStackConfig<OnnxStackUIConfig>();
3032

3133
// Add Windows
@@ -34,6 +36,14 @@ public App()
3436
builder.Services.AddTransient<TextInputDialog>();
3537
builder.Services.AddTransient<CropImageDialog>();
3638
builder.Services.AddTransient<AddModelDialog>();
39+
builder.Services.AddTransient<UpdateModelDialog>();
40+
builder.Services.AddTransient<AddUpscaleModelDialog>();
41+
builder.Services.AddTransient<UpdateUpscaleModelDialog>();
42+
builder.Services.AddTransient<UpdateModelSettingsDialog>();
43+
builder.Services.AddTransient<UpdateModelMetadataDialog>();
44+
builder.Services.AddTransient<ViewModelMetadataDialog>();
45+
builder.Services.AddTransient<UpdateUpscaleModelSettingsDialog> ();
46+
builder.Services.AddSingleton<IModelFactory, ModelFactory>();
3747
builder.Services.AddSingleton<IDialogService, DialogService>();
3848
builder.Services.AddSingleton<IModelDownloadService, ModelDownloadService>();
3949

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Windows.Data;
7+
8+
namespace OnnxStack.UI.Converters
9+
{
10+
public class ComboBoxAllItemConverter : IValueConverter
11+
{
12+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
13+
{
14+
IEnumerable container = value as IEnumerable;
15+
if (container != null)
16+
{
17+
IEnumerable<object> genericContainer = container.OfType<object>();
18+
IEnumerable<object> emptyItem = new object[] { "All" };
19+
return emptyItem.Concat(genericContainer);
20+
}
21+
22+
return value;
23+
}
24+
25+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
26+
{
27+
return value is string str && str == "All" ? null : value;
28+
}
29+
}
30+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Globalization;
5+
using System.Reflection;
6+
using System.Text;
7+
using System.Windows.Data;
8+
9+
namespace OnnxStack.UI.Converters
10+
{
11+
public class EnumDescriptionConverter : IValueConverter
12+
{
13+
private string GetEnumDescription(Enum enumObj)
14+
{
15+
FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
16+
object[] attribArray = fieldInfo.GetCustomAttributes(false);
17+
18+
if (attribArray.Length == 0)
19+
return enumObj.ToString();
20+
else
21+
{
22+
DescriptionAttribute attrib = null;
23+
24+
foreach (var att in attribArray)
25+
{
26+
if (att is DescriptionAttribute)
27+
attrib = att as DescriptionAttribute;
28+
}
29+
30+
if (attrib != null)
31+
return attrib.Description;
32+
33+
return enumObj.ToString();
34+
}
35+
}
36+
37+
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
38+
{
39+
if (value is not Enum myEnum)
40+
return value;
41+
42+
string description = GetEnumDescription(myEnum);
43+
return description;
44+
}
45+
46+
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
47+
{
48+
return string.Empty;
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)