|
1 | | -using OnnxStack.Core; |
2 | | -using OnnxStack.StableDiffusion.Common; |
| 1 | +using OnnxStack.StableDiffusion.Common; |
3 | 2 | using OnnxStack.StableDiffusion.Config; |
4 | 3 | using OnnxStack.StableDiffusion.Enums; |
5 | | -using OnnxStack.StableDiffusion.Helpers; |
| 4 | +using OnnxStack.StableDiffusion; |
6 | 5 | using SixLabors.ImageSharp; |
| 6 | +using OnnxStack.StableDiffusion.Helpers; |
7 | 7 |
|
8 | 8 | namespace OnnxStack.Console.Runner |
9 | 9 | { |
@@ -31,68 +31,50 @@ public async Task RunAsync() |
31 | 31 |
|
32 | 32 | while (true) |
33 | 33 | { |
34 | | - OutputHelpers.WriteConsole("Please type a prompt and press ENTER", ConsoleColor.Yellow); |
35 | | - var prompt = OutputHelpers.ReadConsole(ConsoleColor.Cyan); |
36 | | - |
37 | | - OutputHelpers.WriteConsole("Please type a negative prompt and press ENTER (optional)", ConsoleColor.Yellow); |
38 | | - var negativePrompt = OutputHelpers.ReadConsole(ConsoleColor.Cyan); |
39 | | - |
40 | | - OutputHelpers.WriteConsole("Please enter a batch count and press ENTER", ConsoleColor.Yellow); |
41 | | - var batch = OutputHelpers.ReadConsole(ConsoleColor.Cyan); |
42 | | - int.TryParse(batch, out var batchCount); |
43 | | - batchCount = Math.Max(1, batchCount); |
44 | 34 |
|
45 | 35 | var promptOptions = new PromptOptions |
46 | 36 | { |
47 | | - Prompt = prompt, |
48 | | - NegativePrompt = negativePrompt, |
49 | | - BatchCount = batchCount |
| 37 | + Prompt = "Photo of a cat" |
50 | 38 | }; |
51 | 39 |
|
52 | 40 | var schedulerOptions = new SchedulerOptions |
53 | 41 | { |
54 | 42 | Seed = Random.Shared.Next(), |
55 | 43 |
|
56 | 44 | GuidanceScale = 8, |
57 | | - InferenceSteps = 22, |
| 45 | + InferenceSteps = 20, |
58 | 46 | Strength = 0.6f |
59 | 47 | }; |
60 | 48 |
|
| 49 | + var batchOptions = new BatchOptions |
| 50 | + { |
| 51 | + BatchType = BatchOptionType.Scheduler |
| 52 | + }; |
| 53 | + |
61 | 54 | foreach (var model in _stableDiffusionService.Models) |
62 | 55 | { |
63 | 56 | OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green); |
64 | 57 | await _stableDiffusionService.LoadModel(model); |
65 | 58 |
|
66 | | - foreach (var schedulerType in Helpers.GetPipelineSchedulers(model.PipelineType)) |
| 59 | + var batchIndex = 0; |
| 60 | + var callback = (int batch, int batchCount, int step, int steps) => |
| 61 | + { |
| 62 | + batchIndex = batch; |
| 63 | + OutputHelpers.WriteConsole($"Image: {batch}/{batchCount} - Step: {step}/{steps}", ConsoleColor.Cyan); |
| 64 | + }; |
| 65 | + |
| 66 | + await foreach (var result in _stableDiffusionService.GenerateBatchAsync(model, promptOptions, schedulerOptions, batchOptions, callback)) |
67 | 67 | { |
68 | | - promptOptions.SchedulerType = schedulerType; |
69 | | - OutputHelpers.WriteConsole($"Generating {schedulerType} Image...", ConsoleColor.Green); |
70 | | - await GenerateImage(model, promptOptions, schedulerOptions); |
| 68 | + var outputFilename = Path.Combine(_outputDirectory, $"{batchIndex}_{result.SchedulerOptions.Seed}.png"); |
| 69 | + var image = result.ImageResult.ToImage(); |
| 70 | + await image.SaveAsPngAsync(outputFilename); |
| 71 | + OutputHelpers.WriteConsole($"Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green); |
71 | 72 | } |
72 | 73 |
|
73 | 74 | OutputHelpers.WriteConsole($"Unloading Model `{model.Name}`...", ConsoleColor.Green); |
74 | 75 | await _stableDiffusionService.UnloadModel(model); |
75 | 76 | } |
76 | 77 | } |
77 | 78 | } |
78 | | - |
79 | | - private async Task<bool> GenerateImage(ModelOptions model, PromptOptions prompt, SchedulerOptions options) |
80 | | - { |
81 | | - |
82 | | - var result = await _stableDiffusionService.GenerateAsync(model, prompt, options); |
83 | | - if (result == null) |
84 | | - return false; |
85 | | - |
86 | | - var imageTensors = result.Split(prompt.BatchCount); |
87 | | - for (int i = 0; i < imageTensors.Length; i++) |
88 | | - { |
89 | | - var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{prompt.SchedulerType}_{i}.png"); |
90 | | - var image = imageTensors[i].ToImage(); |
91 | | - await image.SaveAsPngAsync(outputFilename); |
92 | | - OutputHelpers.WriteConsole($"{prompt.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green); |
93 | | - } |
94 | | - |
95 | | - return true; |
96 | | - } |
97 | 79 | } |
98 | 80 | } |
0 commit comments