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

Add Upscale Support to UI #64

Merged
merged 10 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion OnnxStack.Core/Config/OnnxModelType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum OnnxModelType
TextEncoder = 20,
TextEncoder2 = 21,
VaeEncoder = 30,
VaeDecoder = 40
VaeDecoder = 40,
Upscaler = 1000
}
}
2 changes: 1 addition & 1 deletion OnnxStack.ImageUpscaler/Config/UpscaleModelSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OnnxStack.StableDiffusion.Config
{
public class UpscaleModelSet : IOnnxModelSetConfig
public record UpscaleModelSet : IOnnxModelSetConfig
{
public string Name { get; set; }
public int Channels { get; set; }
Expand Down
9 changes: 8 additions & 1 deletion OnnxStack.ImageUpscaler/Services/IUpscaleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,21 @@ public interface IUpscaleService
/// <returns></returns>
Task<bool> LoadModelAsync(UpscaleModelSet model);


/// <summary>
/// Unloads the model.
/// </summary>
/// <param name="model">The model.</param>
/// <returns></returns>
Task<bool> UnloadModelAsync(UpscaleModelSet model);

/// <summary>
/// Determines whether [is model loaded] [the specified model options].
/// </summary>
/// <param name="modelOptions">The model options.</param>
/// <returns>
/// <c>true</c> if [is model loaded] [the specified model options]; otherwise, <c>false</c>.
/// </returns>
bool IsModelLoaded(UpscaleModelSet modelOptions);

/// <summary>
/// Generates the upscaled image.
Expand Down
19 changes: 17 additions & 2 deletions OnnxStack.ImageUpscaler/Services/UpscaleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ public async Task<bool> UnloadModelAsync(UpscaleModelSet model)
}


/// <summary>
/// Determines whether [is model loaded] [the specified model options].
/// </summary>
/// <param name="modelOptions">The model options.</param>
/// <returns>
/// <c>true</c> if [is model loaded] [the specified model options]; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="System.NotImplementedException"></exception>
public bool IsModelLoaded(UpscaleModelSet modelOptions)
{
return _modelService.IsModelLoaded(modelOptions);
}


/// <summary>
/// Generates the upscaled image.
/// </summary>
Expand Down Expand Up @@ -191,7 +205,7 @@ private async Task<Image<Rgba32>> GenerateInternalAsync(UpscaleModelSet modelSet
using (var image = inputImage.ToImage())
{
var upscaleInput = CreateInputParams(image, modelSet.SampleSize, modelSet.ScaleFactor);
var metadata = _modelService.GetModelMetadata(modelSet, OnnxModelType.Unet);
var metadata = _modelService.GetModelMetadata(modelSet, OnnxModelType.Upscaler);

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

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

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace OnnxStack.StableDiffusion.Config
{
public class StableDiffusionModelSet : IOnnxModelSetConfig
public record StableDiffusionModelSet : IOnnxModelSetConfig
{
public string Name { get; set; }
public bool IsEnabled { get; set; }
Expand Down
13 changes: 12 additions & 1 deletion OnnxStack.StableDiffusion/Enums/DiffuserType.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
namespace OnnxStack.StableDiffusion.Enums
using System.ComponentModel;

namespace OnnxStack.StableDiffusion.Enums
{
public enum DiffuserType
{
[Description("Text To Image")]
TextToImage = 0,

[Description("Image To Image")]
ImageToImage = 1,

[Description("Image Inpaint")]
ImageInpaint = 2,

[Description("Image Inpaint Legacy")]
ImageInpaintLegacy = 3,

[Description("Image To Animation")]
ImageToAnimation = 4
}
}
100 changes: 87 additions & 13 deletions OnnxStack.UI/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=System.ComponentModel.TypeConverter"
xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
xmlns:local="clr-namespace:OnnxStack.UI"
xmlns:local="clr-namespace:OnnxStack.UI"
xmlns:SD_Enums="clr-namespace:OnnxStack.StableDiffusion.Enums;assembly=OnnxStack.StableDiffusion"
xmlns:userControls="clr-namespace:OnnxStack.UI.UserControls"
xmlns:behaviors="clr-namespace:OnnxStack.UI.Behaviors"
Expand All @@ -25,7 +26,8 @@
<FontFamily x:Key="FontAwesomeRegular">pack://application:,,,/Fonts/fa-regular-400.ttf#Font Awesome 5 Pro Regular</FontFamily>
<FontFamily x:Key="FontAwesomeSolid">pack://application:,,,/Fonts/fa-solid-900.ttf#Font Awesome 5 Pro Solid</FontFamily>


<system:Boolean x:Key="True">True</system:Boolean>
<system:Boolean x:Key="False">False</system:Boolean>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<converters:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter" />
<converters:InverseBoolConverter x:Key="InverseBoolConverter" />
Expand All @@ -35,13 +37,33 @@
<converters:InverseNullVisibilityConverter x:Key="InverseNullVisibilityConverter" />
<converters:DiffuserVisibilityConverter x:Key="DiffuserVisibilityConverter" />
<converters:PipelineVisibilityConverter x:Key="PipelineVisibilityConverter" />
<converters:ComboBoxAllItemConverter x:Key="ComboBoxAllItemConverter" />
<converters:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />

<ObjectDataProvider x:Key="ListSortDirection" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="componentModel:ListSortDirection"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

<ObjectDataProvider x:Key="SchedulerType" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="SD_Enums:SchedulerType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

<ObjectDataProvider x:Key="ModelType" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="SD_Enums:ModelType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

<ObjectDataProvider x:Key="TokenizerType" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="SD_Enums:TokenizerType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

<ObjectDataProvider x:Key="AlphaTransformType" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="SD_Enums:AlphaTransformType"/>
Expand Down Expand Up @@ -106,7 +128,7 @@







Expand Down Expand Up @@ -233,8 +255,8 @@
<TextBlock Text="{Binding SchedulerOptions.Seed}" Margin="2,0,0,0" />
</StackPanel>
</UniformGrid>
<UniformGrid Columns="6" Margin="0,2,0,2">
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveImageCommand}" CommandParameter="{Binding}">
<UniformGrid Columns="7" Margin="0,2,0,2">
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveImageResultCommand}" CommandParameter="{Binding}">
<userControls:FontAwesome Icon="&#xf0c7;" IconStyle="Light" Size="13" ToolTip="Save Image File"/>
</Button>
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveBlueprintCommand}" CommandParameter="{Binding}" BorderThickness="0, 1">
Expand All @@ -252,6 +274,9 @@
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateImagePaintToImageCommand}" CommandParameter="{Binding}" Padding="2,2" BorderThickness="0,1,1,1">
<userControls:FontAwesome Icon="&#xf1fc;" IconStyle="Light" Size="13" ToolTip="Send To Upscaler"/>
</Button>
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateUpscalerCommand}" CommandParameter="{Binding}" Padding="2,2" BorderThickness="0,1,1,1">
<userControls:FontAwesome Icon="&#xf424;" IconStyle="Light" Size="13" ToolTip="Send To Upscaler"/>
</Button>
</UniformGrid>
</StackPanel>
<Border Margin="0,2" BorderBrush="{StaticResource ContainerBorder}" BorderThickness="1" Background="{StaticResource ContainerBackground}" >
Expand All @@ -260,7 +285,55 @@
</DockPanel>
</DataTemplate>

<DataTemplate DataType="{x:Type models:UpscaleResult}">
<DockPanel Width="220" Height="285" HorizontalAlignment="Center">
<StackPanel DockPanel.Dock="Bottom" >

<UniformGrid Columns="2" Margin="0,3">

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Text="Scale Factor:" Margin="0,0,5,0" FontStyle="Italic" FontSize="10" Opacity=".6" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Info.ScaleFactor}" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Text="Elapsed:" Margin="5,0,0,0" FontSize="10" Opacity=".6" VerticalAlignment="Center" FontStyle="Italic"/>
<TextBlock Text="{Binding Elapsed, StringFormat={}{0:F1}s}" Margin="5,0,0,0"/>
</StackPanel>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Text="Input:" Margin="0,0,5,0" FontStyle="Italic" FontSize="10" Opacity=".6" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Info.InputWidth}" />
<TextBlock Text="x" Margin="2,0,2,0"/>
<TextBlock Text="{Binding Info.InputHeight}" />
</StackPanel>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Text="Output:" Margin="0,0,5,0" FontStyle="Italic" FontSize="10" Opacity=".6" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Info.OutputWidth}" />
<TextBlock Text="x" Margin="2,0,2,0"/>
<TextBlock Text="{Binding Info.OutputHeight}" />
</StackPanel>

</UniformGrid>



<UniformGrid Columns="1" Height="30" IsEnabled="{Binding HasResult}">
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveImageCommand}" CommandParameter="{Binding}" ToolTip="Save Image File">
<StackPanel Orientation="Horizontal">
<userControls:FontAwesome Icon="&#xf0c7;" IconStyle="Light" Size="13" />
<TextBlock Text="Save Image" Margin="5,0,0,0"/>
</StackPanel>
</Button>
</UniformGrid>


</StackPanel>
<Border Margin="0,2" BorderBrush="{StaticResource ContainerBorder}" BorderThickness="1" Background="{StaticResource ContainerBackground}" >
<Image Source="{Binding Image}" DockPanel.Dock="Left" Stretch="Uniform" VerticalAlignment="Center" />
</Border>
</DockPanel>
</DataTemplate>

<!--RadioButton-->
<SolidColorBrush x:Key="RadioButtonBackground" Color="#FF2D2D2D"/>
Expand Down Expand Up @@ -722,14 +795,15 @@











<Style x:Key="EnumComboBox" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>



Expand Down
10 changes: 10 additions & 0 deletions OnnxStack.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OnnxStack.Core;
using OnnxStack.ImageUpscaler;
using OnnxStack.UI.Dialogs;
using OnnxStack.UI.Models;
using OnnxStack.UI.Services;
Expand All @@ -26,6 +27,7 @@ public App()

// Add OnnxStackStableDiffusion
builder.Services.AddOnnxStackStableDiffusion();
builder.Services.AddOnnxStackImageUpscaler();
builder.Services.AddOnnxStackConfig<OnnxStackUIConfig>();

// Add Windows
Expand All @@ -34,6 +36,14 @@ public App()
builder.Services.AddTransient<TextInputDialog>();
builder.Services.AddTransient<CropImageDialog>();
builder.Services.AddTransient<AddModelDialog>();
builder.Services.AddTransient<UpdateModelDialog>();
builder.Services.AddTransient<AddUpscaleModelDialog>();
builder.Services.AddTransient<UpdateUpscaleModelDialog>();
builder.Services.AddTransient<UpdateModelSettingsDialog>();
builder.Services.AddTransient<UpdateModelMetadataDialog>();
builder.Services.AddTransient<ViewModelMetadataDialog>();
builder.Services.AddTransient<UpdateUpscaleModelSettingsDialog> ();
builder.Services.AddSingleton<IModelFactory, ModelFactory>();
builder.Services.AddSingleton<IDialogService, DialogService>();
builder.Services.AddSingleton<IModelDownloadService, ModelDownloadService>();

Expand Down
30 changes: 30 additions & 0 deletions OnnxStack.UI/Converters/ComboBoxAllItemConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;

namespace OnnxStack.UI.Converters
{
public class ComboBoxAllItemConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
IEnumerable container = value as IEnumerable;
if (container != null)
{
IEnumerable<object> genericContainer = container.OfType<object>();
IEnumerable<object> emptyItem = new object[] { "All" };
return emptyItem.Concat(genericContainer);
}

return value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is string str && str == "All" ? null : value;
}
}
}
51 changes: 51 additions & 0 deletions OnnxStack.UI/Converters/EnumDescriptionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Windows.Data;

namespace OnnxStack.UI.Converters
{
public class EnumDescriptionConverter : IValueConverter
{
private string GetEnumDescription(Enum enumObj)
{
FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
object[] attribArray = fieldInfo.GetCustomAttributes(false);

if (attribArray.Length == 0)
return enumObj.ToString();
else
{
DescriptionAttribute attrib = null;

foreach (var att in attribArray)
{
if (att is DescriptionAttribute)
attrib = att as DescriptionAttribute;
}

if (attrib != null)
return attrib.Description;

return enumObj.ToString();
}
}

object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is not Enum myEnum)
return value;

string description = GetEnumDescription(myEnum);
return description;
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Empty;
}
}
}
Loading