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

Realtime Stable-Diffusion #39

Merged
merged 21 commits into from
Nov 23, 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
2 changes: 1 addition & 1 deletion OnnxStack.StableDiffusion/Enums/DiffuserType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public enum DiffuserType
ImageToImage = 1,
ImageInpaint = 2,
ImageInpaintLegacy = 3,
ImageToAnimation = 4,
ImageToAnimation = 4
}
}
9 changes: 9 additions & 0 deletions OnnxStack.StableDiffusion/Services/StableDiffusionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -217,6 +218,10 @@ private async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelOptions,
if (diffuser is null)
throw new Exception("Diffuser not found or is unsupported");

var schedulerSupported = pipeline.PipelineType.GetSchedulerTypes().Contains(schedulerOptions.SchedulerType);
if (!schedulerSupported)
throw new Exception($"Scheduler '{schedulerOptions.SchedulerType}' is not compatible with the `{pipeline.PipelineType}` pipeline.");

return await diffuser.DiffuseAsync(modelOptions, promptOptions, schedulerOptions, progress, cancellationToken);
}

Expand All @@ -230,6 +235,10 @@ private IAsyncEnumerable<BatchResult> DiffuseBatchAsync(IModelOptions modelOptio
if (diffuser is null)
throw new Exception("Diffuser not found or is unsupported");

var schedulerSupported = pipeline.PipelineType.GetSchedulerTypes().Contains(schedulerOptions.SchedulerType);
if (!schedulerSupported)
throw new Exception($"Scheduler '{schedulerOptions.SchedulerType}' is not compatible with the `{pipeline.PipelineType}` pipeline.");

return diffuser.DiffuseBatchAsync(modelOptions, promptOptions, schedulerOptions, batchOptions, progress, cancellationToken);
}
}
Expand Down
37 changes: 32 additions & 5 deletions OnnxStack.UI/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@









Expand Down Expand Up @@ -248,10 +247,10 @@
<userControls:FontAwesome Icon="&#xf302;" IconStyle="Light" Size="13" ToolTip="Send To Image To Image"/>
</Button>
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateImageInpaintCommand}" CommandParameter="{Binding}" >
<userControls:FontAwesome Icon="&#xf1fc;" IconStyle="Light" Size="13" ToolTip="Send To Image Inpaint"/>
<userControls:FontAwesome Icon="&#xf1c5;" IconStyle="Light" Size="13" ToolTip="Send To Image Inpaint"/>
</Button>
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateImageUpscaleCommand}" CommandParameter="{Binding}" Padding="2,2" BorderThickness="0,1,1,1">
<userControls:FontAwesome Icon="&#xf065;" IconStyle="Light" Size="13" ToolTip="Send To Upscaler"/>
<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>
</UniformGrid>
</StackPanel>
Expand Down Expand Up @@ -1978,7 +1977,35 @@



<Style x:Key="ExecutionProviderTypes" TargetType="{x:Type ComboBoxItem}" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="Cpu">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding}" Value="DirectML">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>

<Style x:Key="ButtonGenerateStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Content" Value="Generate" />
<Style.Triggers>
<DataTrigger Binding="{Binding BatchOptions.IsRealtimeEnabled, ElementName=UI}" Value="True">
<Setter Property="Content" Value="Start" />
</DataTrigger>
</Style.Triggers>
</Style>

<Style x:Key="ButtonGenerateCancelStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Content" Value="Cancel" />
<Style.Triggers>
<DataTrigger Binding="{Binding BatchOptions.IsRealtimeEnabled, ElementName=UI}" Value="True">
<Setter Property="Content" Value="Stop" />
</DataTrigger>
</Style.Triggers>
</Style>


</Application.Resources>
Expand Down
13 changes: 11 additions & 2 deletions OnnxStack.UI/Behaviors/SliderMouseWheelBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Xaml.Behaviors;
using Newtonsoft.Json.Linq;
using System.Windows.Controls;
using System.Windows.Input;

Expand All @@ -16,11 +17,19 @@ private void AssociatedObject_PreviewMouseWheel(object sender, MouseWheelEventAr
var slider = (Slider)sender;
if (e.Delta > 0)
{
slider.Value += slider.TickFrequency;
var newValue = slider.Value + slider.TickFrequency;
if (newValue > slider.Maximum)
return;

slider.Value = newValue;
}
else
{
slider.Value -= slider.TickFrequency;
var newValue = slider.Value - slider.TickFrequency;
if (newValue < slider.Minimum)
return;

slider.Value = newValue;
}
}

Expand Down
26 changes: 20 additions & 6 deletions OnnxStack.UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="ClearType"
TextOptions.TextHintingMode="Fixed"
UseLayoutRounding="True"
SnapsToDevicePixels="True"
Style="{StaticResource BaseWindow}">
<Grid DataContext="{Binding ElementName=UI}">
Expand Down Expand Up @@ -51,22 +50,37 @@
<TextBlock Text="Image To Image" Margin="5,0,0,0"/>
</StackPanel>
</TabItem.Header>
<views:ImageToImage UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
<views:ImageToImageView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
</TabItem>

<!--Image Inpaint-->
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal" Margin="5">
<StackPanel Orientation="Horizontal">
<userControls:FontAwesome Icon="&#xf1fc;" IconStyle="Light"/>
<userControls:FontAwesome Icon="&#xf15b;" IconStyle="Light"/>
<userControls:FontAwesome Icon="&#xf054;" IconStyle="Regular" Size="8" Margin="3"/>
<userControls:FontAwesome Icon="&#xf1c5;" IconStyle="Light"/>
</StackPanel>
<TextBlock Text="Image Inpaint" Margin="5,0,0,0"/>
</StackPanel>
</TabItem.Header>
<views:ImageInpaint UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
<views:ImageInpaintView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
</TabItem>

<!--Paint To Image-->
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal" Margin="5">
<StackPanel Orientation="Horizontal">
<userControls:FontAwesome Icon="&#xf1fc;" IconStyle="Light"/>
<userControls:FontAwesome Icon="&#xf054;" IconStyle="Regular" Size="8" Margin="3"/>
<userControls:FontAwesome Icon="&#xf1c5;" IconStyle="Light"/>
</StackPanel>
<TextBlock Text="Paint To Image" Margin="5,0,0,0"/>
</StackPanel>
</TabItem.Header>
<views:PaintToImageView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
</TabItem>

<!--Log Window-->
Expand All @@ -78,7 +92,7 @@
</StackPanel>
</StackPanel>
</TabItem.Header>
<views:Logger UISettings="{Binding UISettings}" LogOutput="{Binding OutputLog, Mode=TwoWay}" Margin="0,6,0,0"/>
<views:LoggerView UISettings="{Binding UISettings}" LogOutput="{Binding OutputLog, Mode=TwoWay}" Margin="0,6,0,0"/>
</TabItem>

<!--Settings Window-->
Expand All @@ -90,7 +104,7 @@
</StackPanel>
</StackPanel>
</TabItem.Header>
<views:Settings UISettings="{Binding UISettings, Mode=TwoWay}" Margin="0,6,0,0"/>
<views:SettingsView UISettings="{Binding UISettings, Mode=TwoWay}" Margin="0,6,0,0"/>
</TabItem>


Expand Down
26 changes: 17 additions & 9 deletions OnnxStack.UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MainWindow(StableDiffusionConfig configuration, OnnxStackUIConfig uiSetti
NavigateTextToImageCommand = new AsyncRelayCommand<ImageResult>(NavigateTextToImage);
NavigateImageToImageCommand = new AsyncRelayCommand<ImageResult>(NavigateImageToImage);
NavigateImageInpaintCommand = new AsyncRelayCommand<ImageResult>(NavigateImageInpaint);
NavigateImageUpscaleCommand = new AsyncRelayCommand<ImageResult>(NavigateImageUpscale);
NavigateImagePaintToImageCommand = new AsyncRelayCommand<ImageResult>(NavigateImagePaintToImage);

WindowCloseCommand = new AsyncRelayCommand(WindowClose);
WindowRestoreCommand = new AsyncRelayCommand(WindowRestore);
Expand All @@ -56,7 +56,7 @@ public MainWindow(StableDiffusionConfig configuration, OnnxStackUIConfig uiSetti
public AsyncRelayCommand<ImageResult> NavigateTextToImageCommand { get; }
public AsyncRelayCommand<ImageResult> NavigateImageToImageCommand { get; }
public AsyncRelayCommand<ImageResult> NavigateImageInpaintCommand { get; }
public AsyncRelayCommand<ImageResult> NavigateImageUpscaleCommand { get; }
public AsyncRelayCommand<ImageResult> NavigateImagePaintToImageCommand { get; }

public OnnxStackUIConfig UISettings
{
Expand Down Expand Up @@ -86,31 +86,39 @@ public INavigatable SelectedTabItem

private async Task NavigateTextToImage(ImageResult result)
{
await NavigateToTab(DiffuserType.TextToImage, result);
await NavigateToTab(TabId.TextToImage, result);
}

private async Task NavigateImageToImage(ImageResult result)
{
await NavigateToTab(DiffuserType.ImageToImage, result);
await NavigateToTab(TabId.ImageToImage, result);
}

private async Task NavigateImageInpaint(ImageResult result)
{
await NavigateToTab(DiffuserType.ImageInpaint, result);
await NavigateToTab(TabId.ImageInpaint, result);
}

private Task NavigateImageUpscale(ImageResult result)
private async Task NavigateImagePaintToImage(ImageResult result)
{
return Task.CompletedTask;
await NavigateToTab(TabId.PaintToImage, result);
}


private async Task NavigateToTab(DiffuserType diffuserType, ImageResult imageResult)
private async Task NavigateToTab(TabId tab, ImageResult imageResult)
{
SelectedTabIndex = (int)diffuserType;
SelectedTabIndex = (int)tab;
await SelectedTabItem.NavigateAsync(imageResult);
}

private enum TabId
{
TextToImage = 0,
ImageToImage = 1,
ImageInpaint = 2,
PaintToImage = 3
}

private ObservableCollection<ModelOptionsModel> CreateModelOptions(List<ModelOptions> onnxModelSets)
{
var models = onnxModelSets
Expand Down
37 changes: 31 additions & 6 deletions OnnxStack.UI/Models/BatchOptionsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class BatchOptionsModel : INotifyPropertyChanged
private int _stepsValue = 1;
private int _batchValue;
private int _batchsValue = 1;
private bool _disableHistory = true;
private bool _isRealtimeEnabled;

public BatchOptionType BatchType
{
Expand All @@ -40,12 +42,6 @@ public float Increment
set { _increment = value; NotifyPropertyChanged(); }
}

public bool IsAutomationEnabled
{
get { return _isAutomationEnabled; }
set { _isAutomationEnabled = value; NotifyPropertyChanged(); }
}

public int StepValue
{
get { return _stepValue; }
Expand All @@ -70,6 +66,35 @@ public int BatchsValue
set { _batchsValue = value; NotifyPropertyChanged(); }
}

public bool DisableHistory
{
get { return _disableHistory; }
set { _disableHistory = value; NotifyPropertyChanged(); }
}

public bool IsAutomationEnabled
{
get { return _isAutomationEnabled; }
set
{
_isAutomationEnabled = value;
if (_isAutomationEnabled)
IsRealtimeEnabled = false;
NotifyPropertyChanged();
}
}

public bool IsRealtimeEnabled
{
get { return _isRealtimeEnabled; }
set
{
_isRealtimeEnabled = value;
if (_isRealtimeEnabled)
IsAutomationEnabled = false;
NotifyPropertyChanged();
}
}

#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
Expand Down
4 changes: 2 additions & 2 deletions OnnxStack.UI/Models/OnnxStackUIConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class OnnxStackUIConfig : IConfigSection
public bool ImageAutoSave { get; set; }
public bool ImageAutoSaveBlueprint { get; set; }
public string ImageAutoSaveDirectory { get; set; }


public int RealtimeRefreshRate { get; set; } = 100;
public bool RealtimeHistoryEnabled { get; set; }
public int DefaultDeviceId { get; set; }
public int DefaultInterOpNumThreads { get; set; }
public int DefaultIntraOpNumThreads { get; set; }
Expand Down
11 changes: 11 additions & 0 deletions OnnxStack.UI/Models/PromptOptionsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class PromptOptionsModel : INotifyPropertyChanged
{
private string _prompt;
private string _negativePrompt;
private bool _hasChanged;

[Required]
[StringLength(512, MinimumLength = 1)]
Expand All @@ -26,10 +27,20 @@ public string NegativePrompt
set { _prompt = value; NotifyPropertyChanged(); }
}

public bool HasChanged
{
get { return _hasChanged; }
set { _hasChanged = value; NotifyPropertyChanged(); }
}


#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string property = "")
{
if (!property.Equals(nameof(HasChanged)) && !HasChanged)
HasChanged = true;

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
#endregion
Expand Down
11 changes: 11 additions & 0 deletions OnnxStack.UI/Models/SchedulerOptionsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SchedulerOptionsModel : INotifyPropertyChanged
private float _maximumBeta = 0.999f;
private int _originalInferenceSteps = 100;
private SchedulerType _schedulerType;
private bool _hasChanged;

/// <summary>
/// Gets or sets the height.
Expand Down Expand Up @@ -210,11 +211,21 @@ public SchedulerType SchedulerType
set { _schedulerType = value; NotifyPropertyChanged(); }
}

public bool HasChanged
{
get { return _hasChanged; }
set { _hasChanged = value; NotifyPropertyChanged(); }
}



#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string property = "")
{
if (!property.Equals(nameof(HasChanged)) && !HasChanged)
HasChanged = true;

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
#endregion
Expand Down
1 change: 1 addition & 0 deletions OnnxStack.UI/OnnxStack.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.5.1" />
<PackageReference Include="OnnxStack.StableDiffusion" Version="0.8.0" Condition=" '$(Configuration)' == 'Release' " />
<ProjectReference Include="..\OnnxStack.StableDiffusion\OnnxStack.StableDiffusion.csproj" Condition=" '$(Configuration)' == 'Debug' " />
</ItemGroup>
Expand Down
Loading