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,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}
0 commit comments