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

Commit f2e3305

Browse files
committed
Remove StableDiffusionService, Update Examples to new API
1 parent 82826ac commit f2e3305

12 files changed

+191
-1108
lines changed
Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using OnnxStack.StableDiffusion;
2-
using OnnxStack.StableDiffusion.Common;
1+
using OnnxStack.Core.Image;
32
using OnnxStack.StableDiffusion.Config;
4-
using OnnxStack.StableDiffusion.Enums;
3+
using OnnxStack.StableDiffusion.Pipelines;
54
using SixLabors.ImageSharp;
65
using System.Diagnostics;
76

@@ -11,12 +10,10 @@ public sealed class StableDebug : IExampleRunner
1110
{
1211
private readonly string _outputDirectory;
1312
private readonly StableDiffusionConfig _configuration;
14-
private readonly IStableDiffusionService _stableDiffusionService;
1513

16-
public StableDebug(StableDiffusionConfig configuration, IStableDiffusionService stableDiffusionService)
14+
public StableDebug(StableDiffusionConfig configuration)
1715
{
1816
_configuration = configuration;
19-
_stableDiffusionService = stableDiffusionService;
2017
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDebug));
2118
}
2219

@@ -35,59 +32,58 @@ public async Task RunAsync()
3532
var negativePrompt = "painting, drawing, sketches, monochrome, grayscale, illustration, anime, cartoon, graphic, text, crayon, graphite, abstract, easynegative, low quality, normal quality, worst quality, lowres, close up, cropped, out of frame, jpeg artifacts, duplicate, morbid, mutilated, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, glitch, deformed, mutated, cross-eyed, ugly, dehydrated, bad anatomy, bad proportions, gross proportions, cloned face, disfigured, malformed limbs, missing arms, missing legs fused fingers, too many fingers,extra fingers, extra limbs,, extra arms, extra legs,disfigured,";
3633
while (true)
3734
{
35+
var developmentSeed = 624461087;
3836
var promptOptions = new PromptOptions
3937
{
4038
Prompt = prompt,
4139
NegativePrompt = negativePrompt,
4240
};
4341

44-
var schedulerOptions = new SchedulerOptions
42+
// Loop though the appsettings.json model sets
43+
foreach (var modelSet in _configuration.ModelSets)
4544
{
46-
SchedulerType = SchedulerType.LMS,
47-
Seed = 624461087,
48-
GuidanceScale = 8,
49-
InferenceSteps = 22,
50-
Strength = 0.6f
51-
};
45+
OutputHelpers.WriteConsole($"Loading Model `{modelSet.Name}`...", ConsoleColor.Cyan);
5246

53-
foreach (var model in _configuration.ModelSets)
54-
{
55-
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
56-
await _stableDiffusionService.LoadModelAsync(model);
47+
// Create Pipeline
48+
var pipeline = PipelineBase.CreatePipeline(modelSet);
5749

58-
schedulerOptions.Width = model.SampleSize;
59-
schedulerOptions.Height = model.SampleSize;
50+
// Preload Models (optional)
51+
await pipeline.LoadAsync();
6052

61-
foreach (var schedulerType in model.PipelineType.GetSchedulerTypes())
53+
// Loop though schedulers
54+
foreach (var scheduler in pipeline.SupportedSchedulers)
6255
{
63-
schedulerOptions.SchedulerType = schedulerType;
64-
OutputHelpers.WriteConsole($"Generating {schedulerType} Image...", ConsoleColor.Green);
65-
await GenerateImage(model, promptOptions, schedulerOptions);
56+
// Create SchedulerOptions based on pipeline defaults
57+
var schedulerOptions = pipeline.DefaultSchedulerOptions with
58+
{
59+
Seed = developmentSeed,
60+
SchedulerType = scheduler
61+
};
62+
63+
var timestamp = Stopwatch.GetTimestamp();
64+
OutputHelpers.WriteConsole($"Generating {scheduler} Image...", ConsoleColor.Green);
65+
66+
// Run pipeline
67+
var result = await pipeline.RunAsync(promptOptions, schedulerOptions);
68+
69+
// Create Image from Tensor result
70+
var image = result.ToImage();
71+
72+
// Save Image File
73+
var outputFilename = Path.Combine(_outputDirectory, $"{modelSet.Name}_{schedulerOptions.SchedulerType}.png");
74+
await image.SaveAsPngAsync(outputFilename);
75+
76+
OutputHelpers.WriteConsole($"{schedulerOptions.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
77+
OutputHelpers.WriteConsole($"Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Yellow);
6678
}
6779

68-
OutputHelpers.WriteConsole($"Unloading Model `{model.Name}`...", ConsoleColor.Green);
69-
await _stableDiffusionService.UnloadModelAsync(model);
80+
OutputHelpers.WriteConsole($"Unloading Model `{modelSet.Name}`...", ConsoleColor.Cyan);
81+
82+
// Unload pipeline
83+
await pipeline.UnloadAsync();
7084
}
7185
break;
7286
}
7387
}
74-
75-
76-
private async Task<bool> GenerateImage(StableDiffusionModelSet model, PromptOptions prompt, SchedulerOptions options)
77-
{
78-
var timestamp = Stopwatch.GetTimestamp();
79-
var outputFilename = Path.Combine(_outputDirectory, $"{model.Name}_{options.Seed}_{options.SchedulerType}.png");
80-
var result = await _stableDiffusionService.GenerateAsImageAsync(new ModelOptions(model), prompt, options);
81-
if (result is not null)
82-
{
83-
await result.SaveAsPngAsync(outputFilename);
84-
OutputHelpers.WriteConsole($"{options.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
85-
OutputHelpers.WriteConsole($"Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Yellow);
86-
return true;
87-
}
88-
89-
OutputHelpers.WriteConsole($"Failed to create image", ConsoleColor.Red);
90-
return false;
91-
}
9288
}
9389
}

OnnxStack.Console/Examples/StableDiffusionBatch.cs

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@
22
using OnnxStack.StableDiffusion.Common;
33
using OnnxStack.StableDiffusion.Config;
44
using OnnxStack.StableDiffusion.Enums;
5-
using OnnxStack.StableDiffusion.Helpers;
5+
using OnnxStack.StableDiffusion.Pipelines;
66
using SixLabors.ImageSharp;
7+
using System.Diagnostics;
78

89
namespace OnnxStack.Console.Runner
910
{
10-
using StableDiffusion;
11-
1211
public sealed class StableDiffusionBatch : IExampleRunner
1312
{
1413
private readonly string _outputDirectory;
1514
private readonly StableDiffusionConfig _configuration;
16-
private readonly IStableDiffusionService _stableDiffusionService;
1715

18-
public StableDiffusionBatch(StableDiffusionConfig configuration, IStableDiffusionService stableDiffusionService)
16+
public StableDiffusionBatch(StableDiffusionConfig configuration)
1917
{
2018
_configuration = configuration;
21-
_stableDiffusionService = stableDiffusionService;
2219
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionBatch));
2320
}
2421

@@ -33,63 +30,53 @@ public async Task RunAsync()
3330
{
3431
Directory.CreateDirectory(_outputDirectory);
3532

36-
while (true)
33+
34+
// Prompt
35+
var promptOptions = new PromptOptions
3736
{
37+
Prompt = "Photo of a cat"
38+
};
3839

39-
var promptOptions = new PromptOptions
40-
{
41-
Prompt = "Photo of a cat"
42-
};
40+
// Batch Of 5
41+
var batchOptions = new BatchOptions
42+
{
43+
ValueTo = 5,
44+
BatchType = BatchOptionType.Seed
45+
};
4346

44-
var schedulerOptions = new SchedulerOptions
45-
{
46-
Seed = Random.Shared.Next(),
47+
foreach (var modelSet in _configuration.ModelSets)
48+
{
49+
OutputHelpers.WriteConsole($"Loading Model `{modelSet.Name}`...", ConsoleColor.Green);
4750

48-
GuidanceScale = 8,
49-
InferenceSteps = 20,
50-
Strength = 0.6f
51-
};
51+
// Create Pipeline
52+
var pipeline = PipelineBase.CreatePipeline(modelSet);
5253

53-
var batchOptions = new BatchOptions
54-
{
55-
BatchType = BatchOptionType.Scheduler
56-
};
54+
// Preload Models (optional)
55+
await pipeline.LoadAsync();
5756

58-
foreach (var model in _configuration.ModelSets)
57+
// Progress Callback (optional)
58+
var progressCallback = (DiffusionProgress progress) => OutputHelpers.WriteConsole($"Image: {progress.BatchValue}/{progress.BatchMax} - Step: {progress.StepValue}/{progress.StepMax}", ConsoleColor.Cyan);
59+
60+
// Run Batch
61+
var timestamp = Stopwatch.GetTimestamp();
62+
await foreach (var result in pipeline.RunBatchAsync(batchOptions, promptOptions, progressCallback: progressCallback))
5963
{
60-
_setSchedulerTypeForPipeline();
61-
62-
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
63-
await _stableDiffusionService.LoadModelAsync(model);
64-
65-
schedulerOptions.Width = model.SampleSize;
66-
schedulerOptions.Height = model.SampleSize;
67-
68-
var batchIndex = 0;
69-
var callback = (DiffusionProgress progress) =>
70-
{
71-
batchIndex = progress.BatchValue;
72-
OutputHelpers.WriteConsole($"Image: {progress.BatchValue}/{progress.BatchMax} - Step: {progress.StepValue}/{progress.StepMax}", ConsoleColor.Cyan);
73-
};
74-
75-
await foreach (var result in _stableDiffusionService.GenerateBatchAsync(new ModelOptions(model), promptOptions, schedulerOptions, batchOptions, callback))
76-
{
77-
var outputFilename = Path.Combine(_outputDirectory, $"{batchIndex}_{result.SchedulerOptions.Seed}.png");
78-
var image = result.ImageResult.ToImage();
79-
await image.SaveAsPngAsync(outputFilename);
80-
OutputHelpers.WriteConsole($"Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
81-
}
82-
83-
OutputHelpers.WriteConsole($"Unloading Model `{model.Name}`...", ConsoleColor.Green);
84-
await _stableDiffusionService.UnloadModelAsync(model);
85-
continue;
86-
87-
void _setSchedulerTypeForPipeline()
88-
{
89-
SchedulerType[] scheduleTypes = model.PipelineType.GetSchedulerTypes();
90-
schedulerOptions.SchedulerType = scheduleTypes.Length == 1 ? scheduleTypes[0] : scheduleTypes[Random.Shared.Next(scheduleTypes.Length)];
91-
}
64+
// Create Image from Tensor result
65+
var image = result.ImageResult.ToImage();
66+
67+
// Save Image File
68+
var outputFilename = Path.Combine(_outputDirectory, $"{modelSet.Name}_{result.SchedulerOptions.Seed}.png");
69+
await image.SaveAsPngAsync(outputFilename);
70+
71+
OutputHelpers.WriteConsole($"Image Created: {Path.GetFileName(outputFilename)}, Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Green);
72+
timestamp = Stopwatch.GetTimestamp();
9273
}
74+
75+
OutputHelpers.WriteConsole($"Unloading Model `{modelSet.Name}`...", ConsoleColor.Green);
76+
77+
// Unload
78+
await pipeline.UnloadAsync();
79+
9380
}
9481
}
9582
}
Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
using OnnxStack.StableDiffusion;
2-
using OnnxStack.StableDiffusion.Common;
1+
using OnnxStack.Core.Image;
32
using OnnxStack.StableDiffusion.Config;
3+
using OnnxStack.StableDiffusion.Pipelines;
44
using SixLabors.ImageSharp;
5+
using System.Diagnostics;
56

67
namespace OnnxStack.Console.Runner
78
{
89
public sealed class StableDiffusionExample : IExampleRunner
910
{
1011
private readonly string _outputDirectory;
1112
private readonly StableDiffusionConfig _configuration;
12-
private readonly IStableDiffusionService _stableDiffusionService;
1313

14-
public StableDiffusionExample(StableDiffusionConfig configuration, IStableDiffusionService stableDiffusionService)
14+
public StableDiffusionExample(StableDiffusionConfig configuration)
1515
{
1616
_configuration = configuration;
17-
_stableDiffusionService = stableDiffusionService;
1817
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionExample));
1918
}
2019

@@ -41,45 +40,47 @@ public async Task RunAsync()
4140
{
4241
Prompt = prompt,
4342
NegativePrompt = negativePrompt,
44-
4543
};
4644

47-
var schedulerOptions = new SchedulerOptions
45+
foreach (var modelSet in _configuration.ModelSets)
4846
{
49-
Seed = Random.Shared.Next()
50-
};
47+
OutputHelpers.WriteConsole($"Loading Model `{modelSet.Name}`...", ConsoleColor.Green);
5148

52-
foreach (var model in _configuration.ModelSets)
53-
{
54-
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
55-
await _stableDiffusionService.LoadModelAsync(model);
49+
// Create Pipeline
50+
var pipeline = PipelineBase.CreatePipeline(modelSet);
51+
52+
// Preload Models (optional)
53+
await pipeline.LoadAsync();
5654

57-
schedulerOptions.Width = model.SampleSize;
58-
schedulerOptions.Height = model.SampleSize;
59-
60-
foreach (var schedulerType in model.PipelineType.GetSchedulerTypes())
55+
56+
// Loop through schedulers
57+
foreach (var schedulerType in pipeline.SupportedSchedulers)
6158
{
62-
schedulerOptions.SchedulerType = schedulerType;
63-
OutputHelpers.WriteConsole($"Generating {schedulerType} Image...", ConsoleColor.Green);
64-
await GenerateImage(model, promptOptions, schedulerOptions);
59+
var schedulerOptions = pipeline.DefaultSchedulerOptions with
60+
{
61+
SchedulerType = schedulerType
62+
};
63+
64+
var timestamp = Stopwatch.GetTimestamp();
65+
OutputHelpers.WriteConsole($"Generating '{schedulerType}' Image...", ConsoleColor.Green);
66+
67+
// Run pipeline
68+
var result = await pipeline.RunAsync(promptOptions, schedulerOptions);
69+
70+
// Create Image from Tensor result
71+
var image = result.ToImage();
72+
73+
// Save Image File
74+
var outputFilename = Path.Combine(_outputDirectory, $"{modelSet.Name}_{schedulerOptions.SchedulerType}.png");
75+
await image.SaveAsPngAsync(outputFilename);
76+
77+
OutputHelpers.WriteConsole($"Image Created: {Path.GetFileName(outputFilename)}, Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Green);
6578
}
6679

67-
OutputHelpers.WriteConsole($"Unloading Model `{model.Name}`...", ConsoleColor.Green);
68-
await _stableDiffusionService.UnloadModelAsync(model);
80+
OutputHelpers.WriteConsole($"Unloading Model `{modelSet.Name}`...", ConsoleColor.Green);
81+
await pipeline.UnloadAsync();
6982
}
7083
}
7184
}
72-
73-
private async Task<bool> GenerateImage(StableDiffusionModelSet model, PromptOptions prompt, SchedulerOptions options)
74-
{
75-
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{options.SchedulerType}.png");
76-
var result = await _stableDiffusionService.GenerateAsImageAsync(new ModelOptions(model), prompt, options);
77-
if (result == null)
78-
return false;
79-
80-
await result.SaveAsPngAsync(outputFilename);
81-
OutputHelpers.WriteConsole($"{options.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
82-
return true;
83-
}
8485
}
8586
}

0 commit comments

Comments
 (0)