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

Commit aa87704

Browse files
authored
Merge pull request #107 from saddam213/API
Major: API Refactor
2 parents 5d74009 + 9c1aa87 commit aa87704

File tree

139 files changed

+4412
-3820
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+4412
-3820
lines changed

Assets/Samples/OpenPose.png

25.9 KB
Loading

OnnxStack.Console/AppService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public AppService(IServiceProvider serviceProvider, IEnumerable<Type> exampleRun
1212
_exampleRunners = exampleRunnerTypes
1313
.Select(serviceProvider.GetService)
1414
.Cast<IExampleRunner>()
15+
.OrderBy(x => x.Index)
1516
.ToList();
1617
}
1718

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using OnnxStack.Core.Image;
2+
using OnnxStack.StableDiffusion.Common;
3+
using OnnxStack.StableDiffusion.Config;
4+
using OnnxStack.StableDiffusion.Enums;
5+
using OnnxStack.StableDiffusion.Models;
6+
using OnnxStack.StableDiffusion.Pipelines;
7+
using SixLabors.ImageSharp;
8+
9+
namespace OnnxStack.Console.Runner
10+
{
11+
public sealed class ControlNetExample : IExampleRunner
12+
{
13+
private readonly string _outputDirectory;
14+
private readonly StableDiffusionConfig _configuration;
15+
16+
public ControlNetExample(StableDiffusionConfig configuration)
17+
{
18+
_configuration = configuration;
19+
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(ControlNetExample));
20+
Directory.CreateDirectory(_outputDirectory);
21+
}
22+
23+
public int Index => 11;
24+
25+
public string Name => "ControlNet Example";
26+
27+
public string Description => "ControlNet Example";
28+
29+
/// <summary>
30+
/// ControlNet Example
31+
/// </summary>
32+
public async Task RunAsync()
33+
{
34+
// Load Control Image
35+
var controlImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\OpenPose.png");
36+
37+
// Create ControlNet
38+
var controlNet = ControlNetModel.Create("D:\\Repositories\\controlnet_onnx\\controlnet\\openpose.onnx");
39+
40+
// Create Pipeline
41+
var pipeline = StableDiffusionPipeline.CreatePipeline("D:\\Repositories\\stable_diffusion_onnx", ModelType.ControlNet);
42+
43+
// Prompt
44+
var promptOptions = new PromptOptions
45+
{
46+
Prompt = "Stormtrooper",
47+
DiffuserType = DiffuserType.ControlNet,
48+
InputContolImage = controlImage
49+
};
50+
51+
52+
53+
// Run pipeline
54+
var result = await pipeline.RunAsync(promptOptions, controlNet: controlNet, progressCallback: OutputHelpers.ProgressCallback);
55+
56+
// Create Image from Tensor result
57+
var image = result.ToImage();
58+
59+
// Save Image File
60+
var outputFilename = Path.Combine(_outputDirectory, $"Output.png");
61+
await image.SaveAsPngAsync(outputFilename);
62+
63+
//Unload
64+
await controlNet.UnloadAsync();
65+
await pipeline.UnloadAsync();
66+
}
67+
}
68+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using OnnxStack.Core.Image;
2+
using OnnxStack.FeatureExtractor.Pipelines;
3+
using OnnxStack.StableDiffusion.Config;
4+
using OnnxStack.StableDiffusion.Enums;
5+
using OnnxStack.StableDiffusion.Models;
6+
using OnnxStack.StableDiffusion.Pipelines;
7+
using SixLabors.ImageSharp;
8+
9+
namespace OnnxStack.Console.Runner
10+
{
11+
public sealed class ControlNetFeatureExample : IExampleRunner
12+
{
13+
private readonly string _outputDirectory;
14+
private readonly StableDiffusionConfig _configuration;
15+
16+
public ControlNetFeatureExample(StableDiffusionConfig configuration)
17+
{
18+
_configuration = configuration;
19+
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(ControlNetFeatureExample));
20+
Directory.CreateDirectory(_outputDirectory);
21+
}
22+
23+
public int Index => 12;
24+
25+
public string Name => "ControlNet + Feature Extraction Example";
26+
27+
public string Description => "ControlNet StableDiffusion with input image Depth feature extraction";
28+
29+
/// <summary>
30+
/// ControlNet Example
31+
/// </summary>
32+
public async Task RunAsync()
33+
{
34+
// Load Control Image
35+
var inputImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp");
36+
37+
// Create Annotation pipeline
38+
var annotationPipeline = AnnotationPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators");
39+
40+
// Create Depth Image
41+
var controlImage = await annotationPipeline.DepthImage(inputImage);
42+
43+
// Save Depth Image (Debug Only)
44+
await controlImage.Image.SaveAsPngAsync(Path.Combine(_outputDirectory, $"Depth.png"));
45+
46+
// Create ControlNet
47+
var controlNet = ControlNetModel.Create("D:\\Repositories\\controlnet_onnx\\controlnet\\depth.onnx");
48+
49+
// Create Pipeline
50+
var pipeline = StableDiffusionPipeline.CreatePipeline("D:\\Repositories\\stable_diffusion_onnx", ModelType.ControlNet);
51+
52+
// Prompt
53+
var promptOptions = new PromptOptions
54+
{
55+
Prompt = "steampunk dog",
56+
DiffuserType = DiffuserType.ControlNet,
57+
InputContolImage = controlImage
58+
};
59+
60+
// Run pipeline
61+
var result = await pipeline.RunAsync(promptOptions, controlNet: controlNet, progressCallback: OutputHelpers.ProgressCallback);
62+
63+
// Create Image from Tensor result
64+
var image = result.ToImage();
65+
66+
// Save Image File
67+
var outputFilename = Path.Combine(_outputDirectory, $"Output.png");
68+
await image.SaveAsPngAsync(outputFilename);
69+
70+
//Unload
71+
await annotationPipeline.UnloadAsync();
72+
await controlNet.UnloadAsync();
73+
await pipeline.UnloadAsync();
74+
}
75+
}
76+
}
Lines changed: 41 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,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

Comments
 (0)