Updated example for 3.0.0

This commit is contained in:
Darth Affe 2024-08-25 16:42:24 +02:00
parent ae70f7e5f0
commit d8a629c8d1
3 changed files with 223 additions and 99 deletions

View File

@ -11,10 +11,13 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="HPPH.System.Drawing" Version="1.0.0" /> <PackageReference Include="HPPH.System.Drawing" Version="1.0.0" />
<PackageReference Include="StableDiffusion.NET" Version="2.0.0" /> <PackageReference Include="StableDiffusion.NET.Backend.Cpu" Version="3.0.0" />
<PackageReference Include="StableDiffusion.NET.Backend.Cpu" Version="2.0.0" /> <PackageReference Include="StableDiffusion.NET.Backend.Cuda" Version="3.0.0" />
<PackageReference Include="StableDiffusion.NET.Backend.Cuda" Version="2.0.0" /> <PackageReference Include="StableDiffusion.NET.Backend.Rocm" Version="3.0.0" />
<PackageReference Include="StableDiffusion.NET.Backend.Rocm" Version="2.0.0" /> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\StableDiffusion.NET\StableDiffusion.NET.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -8,7 +8,7 @@
xmlns:sys="clr-namespace:System;assembly=System.Runtime" xmlns:sys="clr-namespace:System;assembly=System.Runtime"
xmlns:sd="clr-namespace:StableDiffusion.NET;assembly=StableDiffusion.NET" xmlns:sd="clr-namespace:StableDiffusion.NET;assembly=StableDiffusion.NET"
mc:Ignorable="d" mc:Ignorable="d"
Title="StableDiffusion.NET" Width="1280" Height="800"> Title="StableDiffusion.NET" Width="1706" Height="960">
<Window.DataContext> <Window.DataContext>
<local:MainWindowViewModel /> <local:MainWindowViewModel />
</Window.DataContext> </Window.DataContext>
@ -42,81 +42,112 @@
<ColumnDefinition Width="400" /> <ColumnDefinition Width="400" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<StackPanel Margin="4,0" Orientation="Vertical"> <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<TextBlock Margin="8" Text="This is just an example - inputs are not validated!&#10;Make sure everything is correct before pressing 'Create Image'." /> <StackPanel Margin="4,0" Orientation="Vertical">
<TextBlock Margin="8" Text="This is just an example - inputs are not validated!&#10;Make sure everything is correct before pressing 'Create Image'." />
<Separator /> <Separator />
<Label Content="Model-Path" /> <Label Content="Model-Type" />
<DockPanel> <StackPanel Orientation="Horizontal" Margin="4,0,0,0">
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectModelCommand}" IsEnabled="{Binding IsReady}" /> <RadioButton Content="Stable Diffusion" IsChecked="{Binding IsStableDiffusionSelected}" GroupName="ModelType" />
<TextBox Text="{Binding ModelPath}" /> <RadioButton Margin="16,0,0,0" Content="Flux" IsChecked="{Binding IsFluxSelected}" GroupName="ModelType" />
</DockPanel>
<Label Content="Vae-Path (Optional)" />
<DockPanel>
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectVaeCommand}" IsEnabled="{Binding IsReady}" />
<TextBox Text="{Binding VaePath}" />
</DockPanel>
<Label Content="Schedule" />
<ComboBox ItemsSource="{Binding Source={StaticResource ScheduleDataSource}}" SelectedItem="{Binding Schedule}" />
<Button Margin="0,8" Content="Load Model" Command="{Binding LoadModelCommand}" IsEnabled="{Binding IsReady}" />
<Separator />
<Label Margin="0,8,0,0" Content="Prompt" />
<TextBox Height="80" TextWrapping="Wrap" Text="{Binding Prompt}" />
<Label Content="AntiPrompt" />
<TextBox Height="80" TextWrapping="Wrap" Text="{Binding AntiPrompt}" />
<DockPanel LastChildFill="True">
<StackPanel DockPanel.Dock="Left">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
<Label Width="50" Content="Width" />
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Width}" />
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
<Label Width="50" Content="Height" />
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Height}" />
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
<Label Width="50" Content="Cfg" />
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Cfg}" />
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
<Label Width="50" Content="Steps" />
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Steps}" />
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
<Label Width="50" Content="Seed" />
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Seed}" />
</StackPanel>
</StackPanel> </StackPanel>
<DockPanel Margin="16,0,0,0" LastChildFill="True"> <StackPanel Orientation="Vertical" IsEnabled="{Binding IsStableDiffusionSelected}">
<Label DockPanel.Dock="Top" Content="Image2Image Source" /> <Label Content="Model-Path" />
<DockPanel DockPanel.Dock="Top" > <DockPanel>
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectImage2ImageSourceCommand}" IsEnabled="{Binding IsReady}" /> <Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectModelCommand}" IsEnabled="{Binding IsReady}" />
<TextBox Text="{Binding Image2ImageSourcePath}" /> <TextBox Text="{Binding ModelPath}" />
</DockPanel>
</StackPanel>
<StackPanel Orientation="Vertical" IsEnabled="{Binding IsFluxSelected}">
<Label Content="DiffusionModel-Path" />
<DockPanel>
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectDiffusionModelCommand}" IsEnabled="{Binding IsReady}" />
<TextBox Text="{Binding DiffusionModelPath}" />
</DockPanel> </DockPanel>
<Image Source="{Binding Image2ImageSource, Converter={StaticResource ImageToImageSourceConverter}}" /> <Label Content="ClipL-Path" />
<DockPanel>
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectClipLCommand}" IsEnabled="{Binding IsReady}" />
<TextBox Text="{Binding ClipLPath}" />
</DockPanel>
<Label Content="T5xxl-Path" />
<DockPanel>
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectT5xxlCommand}" IsEnabled="{Binding IsReady}" />
<TextBox Text="{Binding T5xxlPath}" />
</DockPanel>
</StackPanel>
<Label Content="Vae-Path (Optional)" />
<DockPanel>
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectVaeCommand}" IsEnabled="{Binding IsReady}" />
<TextBox Text="{Binding VaePath}" />
</DockPanel> </DockPanel>
</DockPanel>
<Label Content="Sample-Method" /> <Label Content="Schedule" />
<ComboBox ItemsSource="{Binding Source={StaticResource SamplerDataSource}}" SelectedItem="{Binding SampleMethod}" /> <ComboBox ItemsSource="{Binding Source={StaticResource ScheduleDataSource}}" SelectedItem="{Binding Schedule}" />
<Button Margin="0,16,0,0" Content="Create Image" Command="{Binding CreateImageCommand}" IsEnabled="{Binding IsReady}" /> <Button Margin="0,8" Content="Load Model" Command="{Binding LoadModelCommand}" IsEnabled="{Binding IsReady}" />
<Button Margin="0,16,0,0" Content="Save Image" Command="{Binding SaveImageCommand}" IsEnabled="{Binding IsReady}" />
</StackPanel> <Separator />
<Label Margin="0,8,0,0" Content="Prompt" />
<TextBox Height="80" TextWrapping="Wrap" Text="{Binding Prompt}" />
<Label Content="AntiPrompt" />
<TextBox Height="60" TextWrapping="Wrap" Text="{Binding AntiPrompt}" />
<DockPanel LastChildFill="True">
<StackPanel DockPanel.Dock="Left">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
<Label Width="50" Content="Width" />
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Width}" />
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
<Label Width="50" Content="Height" />
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Height}" />
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
<Label Width="50" Content="Cfg" />
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Cfg}" />
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
<Label Width="50" Content="Steps" />
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Steps}" />
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
<Label Width="50" Content="Seed" />
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Seed}" />
</StackPanel>
</StackPanel>
<DockPanel Margin="16,0,0,0" LastChildFill="True">
<Label DockPanel.Dock="Top" Content="Image2Image Source" />
<DockPanel DockPanel.Dock="Top" >
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectImage2ImageSourceCommand}" IsEnabled="{Binding IsReady}" />
<TextBox Text="{Binding Image2ImageSourcePath}" />
</DockPanel>
<Image Source="{Binding Image2ImageSource, Converter={StaticResource ImageToImageSourceConverter}}" />
</DockPanel>
</DockPanel>
<Label Content="Sample-Method" />
<ComboBox ItemsSource="{Binding Source={StaticResource SamplerDataSource}}" SelectedItem="{Binding SampleMethod}" />
<Button Margin="0,16,0,0" Content="Create Image" Command="{Binding CreateImageCommand}" IsEnabled="{Binding IsReady}" />
<Button Margin="0,16,0,0" Content="Save Image" Command="{Binding SaveImageCommand}" IsEnabled="{Binding IsReady}" />
</StackPanel>
</ScrollViewer>
<GridSplitter Grid.Column="1" Margin="2,0" Width="2" Background="DimGray" VerticalAlignment="Stretch" HorizontalAlignment="Center" /> <GridSplitter Grid.Column="1" Margin="2,0" Width="2" Background="DimGray" VerticalAlignment="Stretch" HorizontalAlignment="Center" />

View File

@ -13,7 +13,21 @@ public class MainWindowViewModel : INotifyPropertyChanged
{ {
#region Properties & Fields #region Properties & Fields
private StableDiffusionModel? _model; private DiffusionModel? _model;
private bool _isStableDiffusionSelected = true;
public bool IsStableDiffusionSelected
{
get => _isStableDiffusionSelected;
set => SetProperty(ref _isStableDiffusionSelected, value);
}
private bool _isFluxSelected;
public bool IsFluxSelected
{
get => _isFluxSelected;
set => SetProperty(ref _isFluxSelected, value);
}
private string _modelPath = string.Empty; private string _modelPath = string.Empty;
public string ModelPath public string ModelPath
@ -22,6 +36,27 @@ public class MainWindowViewModel : INotifyPropertyChanged
set => SetProperty(ref _modelPath, value); set => SetProperty(ref _modelPath, value);
} }
private string _clipLPath;
public string ClipLPath
{
get => _clipLPath;
set => SetProperty(ref _clipLPath, value);
}
private string _t5xxlPath;
public string T5xxlPath
{
get => _t5xxlPath;
set => SetProperty(ref _t5xxlPath, value);
}
private string _diffusionModelPath;
public string DiffusionModelPath
{
get => _diffusionModelPath;
set => SetProperty(ref _diffusionModelPath, value);
}
private string _vaePath = string.Empty; private string _vaePath = string.Empty;
public string VaePath public string VaePath
{ {
@ -78,8 +113,8 @@ public class MainWindowViewModel : INotifyPropertyChanged
set => SetProperty(ref _steps, value); set => SetProperty(ref _steps, value);
} }
private int _seed = -1; private long _seed = -1;
public int Seed public long Seed
{ {
get => _seed; get => _seed;
set => SetProperty(ref _seed, value); set => SetProperty(ref _seed, value);
@ -156,6 +191,15 @@ public class MainWindowViewModel : INotifyPropertyChanged
private ActionCommand? _selectModelCommand; private ActionCommand? _selectModelCommand;
public ActionCommand SelectModelCommand => _selectModelCommand ??= new ActionCommand(SelectModel); public ActionCommand SelectModelCommand => _selectModelCommand ??= new ActionCommand(SelectModel);
private ActionCommand? _selectDiffusionModelCommand;
public ActionCommand SelectDiffusionModelCommand => _selectDiffusionModelCommand ??= new ActionCommand(SelectDiffusionModel);
private ActionCommand? _selectClipLCommand;
public ActionCommand SelectClipLCommand => _selectClipLCommand ??= new ActionCommand(SelectClipL);
private ActionCommand? _selectT5xxlCommand;
public ActionCommand SelectT5xxlCommand => _selectT5xxlCommand ??= new ActionCommand(SelectT5xxl);
private ActionCommand? _selectVaeCommand; private ActionCommand? _selectVaeCommand;
public ActionCommand SelectVaeCommand => _selectVaeCommand ??= new ActionCommand(SelectVae); public ActionCommand SelectVaeCommand => _selectVaeCommand ??= new ActionCommand(SelectVae);
@ -170,8 +214,8 @@ public class MainWindowViewModel : INotifyPropertyChanged
{ {
try try
{ {
StableDiffusionModel.Log += (_, args) => LogLine($"LOG [{args.Level}]: {args.Text}", false); StableDiffusionCpp.Log += (_, args) => LogLine($"LOG [{args.Level}]: {args.Text}", false);
StableDiffusionModel.Progress += (_, args) => LogLine($"PROGRESS {args.Step} / {args.Steps} ({(args.Progress * 100):N2} %) {args.IterationsPerSecond:N2} it/s ({args.Time})"); StableDiffusionCpp.Progress += (_, args) => LogLine($"PROGRESS {args.Step} / {args.Steps} ({(args.Progress * 100):N2} %) {args.IterationsPerSecond:N2} it/s ({args.Time})");
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -191,8 +235,38 @@ public class MainWindowViewModel : INotifyPropertyChanged
_model?.Dispose(); _model?.Dispose();
LogLine($"Loading model '{ModelPath}'"); bool restoreDefaultParameters = false;
_model = await Task.Run(() => new StableDiffusionModel(ModelPath, new ModelParameter { VaePath = VaePath, Schedule = Schedule }));
if (IsStableDiffusionSelected)
{
restoreDefaultParameters = _model?.ModelParameter.DiffusionModelType != DiffusionModelType.StableDiffusion;
LogLine($"Loading stable diffusion-model '{ModelPath}'");
_model = await Task.Run(() => ModelBuilder.StableDiffusion(ModelPath).WithMultithreading().WithVae(VaePath).WithSchedule(Schedule).Build());
}
else if (IsFluxSelected)
{
restoreDefaultParameters = _model?.ModelParameter.DiffusionModelType != DiffusionModelType.Flux;
LogLine($"Loading flux-model '{DiffusionModelPath}'");
_model = await Task.Run(() => ModelBuilder.Flux(DiffusionModelPath, ClipLPath, T5xxlPath, VaePath).WithMultithreading().WithSchedule(Schedule).Build());
}
else
{
LogLine("No model-type selected :(");
}
if (restoreDefaultParameters && (_model != null))
{
DiffusionParameter parameter = _model.GetDefaultParameter();
AntiPrompt = parameter.NegativePrompt;
Width = parameter.Width;
Height = parameter.Height;
Steps = parameter.SampleSteps;
Seed = parameter.Seed;
SampleMethod = parameter.SampleMethod;
Cfg = _model.ModelParameter.DiffusionModelType == DiffusionModelType.Flux ? parameter.Guidance : parameter.CfgScale;
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -206,37 +280,32 @@ public class MainWindowViewModel : INotifyPropertyChanged
private async void CreateImage() private async void CreateImage()
{ {
if (_model == null) return;
try try
{ {
IsReady = false; IsReady = false;
DiffusionParameter parameter = _model.GetDefaultParameter()
.WithNegativePrompt(AntiPrompt)
.WithSize(Width, Height)
.WithSteps(Steps)
.WithSeed(Seed)
.WithSampler(SampleMethod);
if (_model.ModelParameter.DiffusionModelType == DiffusionModelType.StableDiffusion)
parameter = parameter.WithCfg(Cfg);
else if (_model.ModelParameter.DiffusionModelType == DiffusionModelType.Flux)
parameter = parameter.WithGuidance(Cfg);
if (Image2ImageSource == null) if (Image2ImageSource == null)
{ {
LogLine("Creating image ..."); LogLine("Creating image ...");
Image = await Task.Run(() => _model?.TextToImage(Prompt, new StableDiffusionParameter Image = await Task.Run(() => _model?.TextToImage(Prompt, parameter));
{
NegativePrompt = AntiPrompt,
Width = Width,
Height = Height,
CfgScale = Cfg,
SampleSteps = Steps,
Seed = Seed,
SampleMethod = SampleMethod
}));
} }
else else
{ {
LogLine("Manipulating image ..."); LogLine("Manipulating image ...");
Image = await Task.Run(() => _model?.ImageToImage(Prompt, Image2ImageSource, new StableDiffusionParameter Image = await Task.Run(() => _model?.ImageToImage(Prompt, Image2ImageSource, parameter));
{
NegativePrompt = AntiPrompt,
Width = Width,
Height = Height,
CfgScale = Cfg,
SampleSteps = Steps,
Seed = Seed,
SampleMethod = SampleMethod
}));
} }
LogLine("done!"); LogLine("done!");
@ -277,9 +346,30 @@ public class MainWindowViewModel : INotifyPropertyChanged
ModelPath = openFileDialog.FileName; ModelPath = openFileDialog.FileName;
} }
private void SelectDiffusionModel()
{
OpenFileDialog openFileDialog = new() { Filter = "Diffusion Model|*.*" };
if (openFileDialog.ShowDialog() == true)
DiffusionModelPath = openFileDialog.FileName;
}
private void SelectClipL()
{
OpenFileDialog openFileDialog = new() { Filter = "ClipL|*.*" };
if (openFileDialog.ShowDialog() == true)
ClipLPath = openFileDialog.FileName;
}
private void SelectT5xxl()
{
OpenFileDialog openFileDialog = new() { Filter = "T5xxl|*.*" };
if (openFileDialog.ShowDialog() == true)
T5xxlPath = openFileDialog.FileName;
}
private void SelectVae() private void SelectVae()
{ {
OpenFileDialog openFileDialog = new() { Filter = "Stable Diffusion VAE|*.*" }; OpenFileDialog openFileDialog = new() { Filter = "VAE|*.*" };
if (openFileDialog.ShowDialog() == true) if (openFileDialog.ShowDialog() == true)
VaePath = openFileDialog.FileName; VaePath = openFileDialog.FileName;
} }