1
- using OnnxStack . StableDiffusion ;
2
- using OnnxStack . StableDiffusion . Common ;
1
+ using OnnxStack . Core . Image ;
3
2
using OnnxStack . StableDiffusion . Config ;
4
- using OnnxStack . StableDiffusion . Enums ;
3
+ using OnnxStack . StableDiffusion . Pipelines ;
5
4
using SixLabors . ImageSharp ;
6
5
using System . Diagnostics ;
7
6
@@ -11,15 +10,15 @@ public sealed class StableDebug : IExampleRunner
11
10
{
12
11
private readonly string _outputDirectory ;
13
12
private readonly StableDiffusionConfig _configuration ;
14
- private readonly IStableDiffusionService _stableDiffusionService ;
15
13
16
- public StableDebug ( StableDiffusionConfig configuration , IStableDiffusionService stableDiffusionService )
14
+ public StableDebug ( StableDiffusionConfig configuration )
17
15
{
18
16
_configuration = configuration ;
19
- _stableDiffusionService = stableDiffusionService ;
20
17
_outputDirectory = Path . Combine ( Directory . GetCurrentDirectory ( ) , "Examples" , nameof ( StableDebug ) ) ;
21
18
}
22
19
20
+ public int Index => 0 ;
21
+
23
22
public string Name => "Stable Diffusion Debug" ;
24
23
25
24
public string Description => "Stable Diffusion Debugger" ;
@@ -35,59 +34,58 @@ public async Task RunAsync()
35
34
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," ;
36
35
while ( true )
37
36
{
37
+ var developmentSeed = 624461087 ;
38
38
var promptOptions = new PromptOptions
39
39
{
40
40
Prompt = prompt ,
41
41
NegativePrompt = negativePrompt ,
42
42
} ;
43
43
44
- var schedulerOptions = new SchedulerOptions
44
+ // Loop though the appsettings.json model sets
45
+ foreach ( var modelSet in _configuration . ModelSets )
45
46
{
46
- SchedulerType = SchedulerType . LMS ,
47
- Seed = 624461087 ,
48
- GuidanceScale = 8 ,
49
- InferenceSteps = 22 ,
50
- Strength = 0.6f
51
- } ;
47
+ OutputHelpers . WriteConsole ( $ "Loading Model `{ modelSet . Name } `...", ConsoleColor . Cyan ) ;
52
48
53
- foreach ( var model in _configuration . ModelSets )
54
- {
55
- OutputHelpers . WriteConsole ( $ "Loading Model `{ model . Name } `...", ConsoleColor . Green ) ;
56
- await _stableDiffusionService . LoadModelAsync ( model ) ;
49
+ // Create Pipeline
50
+ var pipeline = PipelineBase . CreatePipeline ( modelSet ) ;
57
51
58
- schedulerOptions . Width = model . SampleSize ;
59
- schedulerOptions . Height = model . SampleSize ;
52
+ // Preload Models (optional)
53
+ await pipeline . LoadAsync ( ) ;
60
54
61
- foreach ( var schedulerType in model . PipelineType . GetSchedulerTypes ( ) )
55
+ // Loop though schedulers
56
+ foreach ( var scheduler in pipeline . SupportedSchedulers )
62
57
{
63
- schedulerOptions . SchedulerType = schedulerType ;
64
- OutputHelpers . WriteConsole ( $ "Generating { schedulerType } Image...", ConsoleColor . Green ) ;
65
- await GenerateImage ( model , promptOptions , schedulerOptions ) ;
58
+ // Create SchedulerOptions based on pipeline defaults
59
+ var schedulerOptions = pipeline . DefaultSchedulerOptions with
60
+ {
61
+ Seed = developmentSeed ,
62
+ SchedulerType = scheduler
63
+ } ;
64
+
65
+ var timestamp = Stopwatch . GetTimestamp ( ) ;
66
+ OutputHelpers . WriteConsole ( $ "Generating { scheduler } Image...", ConsoleColor . Green ) ;
67
+
68
+ // Run pipeline
69
+ var result = await pipeline . RunAsync ( promptOptions , schedulerOptions , progressCallback : OutputHelpers . ProgressCallback ) ;
70
+
71
+ // Create Image from Tensor result
72
+ var image = result . ToImage ( ) ;
73
+
74
+ // Save Image File
75
+ var outputFilename = Path . Combine ( _outputDirectory , $ "{ modelSet . Name } _{ schedulerOptions . SchedulerType } .png") ;
76
+ await image . SaveAsPngAsync ( outputFilename ) ;
77
+
78
+ OutputHelpers . WriteConsole ( $ "{ schedulerOptions . SchedulerType } Image Created: { Path . GetFileName ( outputFilename ) } ", ConsoleColor . Green ) ;
79
+ OutputHelpers . WriteConsole ( $ "Elapsed: { Stopwatch . GetElapsedTime ( timestamp ) } ms", ConsoleColor . Yellow ) ;
66
80
}
67
81
68
- OutputHelpers . WriteConsole ( $ "Unloading Model `{ model . Name } `...", ConsoleColor . Green ) ;
69
- await _stableDiffusionService . UnloadModelAsync ( model ) ;
82
+ OutputHelpers . WriteConsole ( $ "Unloading Model `{ modelSet . Name } `...", ConsoleColor . Cyan ) ;
83
+
84
+ // Unload pipeline
85
+ await pipeline . UnloadAsync ( ) ;
70
86
}
71
87
break ;
72
88
}
73
89
}
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
- }
92
90
}
93
91
}
0 commit comments