1- using OnnxStack . StableDiffusion ;
2- using OnnxStack . StableDiffusion . Common ;
1+ using OnnxStack . Core . Image ;
32using OnnxStack . StableDiffusion . Config ;
4- using OnnxStack . StableDiffusion . Enums ;
3+ using OnnxStack . StableDiffusion . Pipelines ;
54using SixLabors . ImageSharp ;
65using System . Diagnostics ;
76
@@ -11,15 +10,15 @@ 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
20+ public int Index => 0 ;
21+
2322 public string Name => "Stable Diffusion Debug" ;
2423
2524 public string Description => "Stable Diffusion Debugger" ;
@@ -35,59 +34,58 @@ public async Task RunAsync()
3534 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," ;
3635 while ( true )
3736 {
37+ var developmentSeed = 624461087 ;
3838 var promptOptions = new PromptOptions
3939 {
4040 Prompt = prompt ,
4141 NegativePrompt = negativePrompt ,
4242 } ;
4343
44- var schedulerOptions = new SchedulerOptions
44+ // Loop though the appsettings.json model sets
45+ foreach ( var modelSet in _configuration . ModelSets )
4546 {
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 ) ;
5248
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 ) ;
5751
58- schedulerOptions . Width = model . SampleSize ;
59- schedulerOptions . Height = model . SampleSize ;
52+ // Preload Models (optional)
53+ await pipeline . LoadAsync ( ) ;
6054
61- foreach ( var schedulerType in model . PipelineType . GetSchedulerTypes ( ) )
55+ // Loop though schedulers
56+ foreach ( var scheduler in pipeline . SupportedSchedulers )
6257 {
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 ) ;
6680 }
6781
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 ( ) ;
7086 }
7187 break ;
7288 }
7389 }
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- }
9290 }
9391}
0 commit comments