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

Major: API Refactor #107

Merged
merged 13 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Assets/Samples/OpenPose.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions OnnxStack.Console/AppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public AppService(IServiceProvider serviceProvider, IEnumerable<Type> exampleRun
_exampleRunners = exampleRunnerTypes
.Select(serviceProvider.GetService)
.Cast<IExampleRunner>()
.OrderBy(x => x.Index)
.ToList();
}

Expand Down
68 changes: 68 additions & 0 deletions OnnxStack.Console/Examples/ControlNetExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using OnnxStack.Core.Image;
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Models;
using OnnxStack.StableDiffusion.Pipelines;
using SixLabors.ImageSharp;

namespace OnnxStack.Console.Runner
{
public sealed class ControlNetExample : IExampleRunner
{
private readonly string _outputDirectory;
private readonly StableDiffusionConfig _configuration;

public ControlNetExample(StableDiffusionConfig configuration)
{
_configuration = configuration;
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(ControlNetExample));
Directory.CreateDirectory(_outputDirectory);
}

public int Index => 11;

public string Name => "ControlNet Example";

public string Description => "ControlNet Example";

/// <summary>
/// ControlNet Example
/// </summary>
public async Task RunAsync()
{
// Load Control Image
var controlImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\OpenPose.png");

// Create ControlNet
var controlNet = ControlNetModel.Create("D:\\Repositories\\controlnet_onnx\\controlnet\\openpose.onnx");

// Create Pipeline
var pipeline = StableDiffusionPipeline.CreatePipeline("D:\\Repositories\\stable_diffusion_onnx", ModelType.ControlNet);

// Prompt
var promptOptions = new PromptOptions
{
Prompt = "Stormtrooper",
DiffuserType = DiffuserType.ControlNet,
InputContolImage = controlImage
};



// Run pipeline
var result = await pipeline.RunAsync(promptOptions, controlNet: controlNet, progressCallback: OutputHelpers.ProgressCallback);

// Create Image from Tensor result
var image = result.ToImage();

// Save Image File
var outputFilename = Path.Combine(_outputDirectory, $"Output.png");
await image.SaveAsPngAsync(outputFilename);

//Unload
await controlNet.UnloadAsync();
await pipeline.UnloadAsync();
}
}
}
76 changes: 76 additions & 0 deletions OnnxStack.Console/Examples/ControlNetFeatureExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using OnnxStack.Core.Image;
using OnnxStack.FeatureExtractor.Pipelines;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Models;
using OnnxStack.StableDiffusion.Pipelines;
using SixLabors.ImageSharp;

namespace OnnxStack.Console.Runner
{
public sealed class ControlNetFeatureExample : IExampleRunner
{
private readonly string _outputDirectory;
private readonly StableDiffusionConfig _configuration;

public ControlNetFeatureExample(StableDiffusionConfig configuration)
{
_configuration = configuration;
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(ControlNetFeatureExample));
Directory.CreateDirectory(_outputDirectory);
}

public int Index => 12;

public string Name => "ControlNet + Feature Extraction Example";

public string Description => "ControlNet StableDiffusion with input image Depth feature extraction";

/// <summary>
/// ControlNet Example
/// </summary>
public async Task RunAsync()
{
// Load Control Image
var inputImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp");

// Create Annotation pipeline
var annotationPipeline = AnnotationPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators");

// Create Depth Image
var controlImage = await annotationPipeline.DepthImage(inputImage);

// Save Depth Image (Debug Only)
await controlImage.Image.SaveAsPngAsync(Path.Combine(_outputDirectory, $"Depth.png"));

// Create ControlNet
var controlNet = ControlNetModel.Create("D:\\Repositories\\controlnet_onnx\\controlnet\\depth.onnx");

// Create Pipeline
var pipeline = StableDiffusionPipeline.CreatePipeline("D:\\Repositories\\stable_diffusion_onnx", ModelType.ControlNet);

// Prompt
var promptOptions = new PromptOptions
{
Prompt = "steampunk dog",
DiffuserType = DiffuserType.ControlNet,
InputContolImage = controlImage
};

// Run pipeline
var result = await pipeline.RunAsync(promptOptions, controlNet: controlNet, progressCallback: OutputHelpers.ProgressCallback);

// Create Image from Tensor result
var image = result.ToImage();

// Save Image File
var outputFilename = Path.Combine(_outputDirectory, $"Output.png");
await image.SaveAsPngAsync(outputFilename);

//Unload
await annotationPipeline.UnloadAsync();
await controlNet.UnloadAsync();
await pipeline.UnloadAsync();
}
}
}
84 changes: 41 additions & 43 deletions OnnxStack.Console/Examples/StableDebug.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using OnnxStack.StableDiffusion;
using OnnxStack.StableDiffusion.Common;
using OnnxStack.Core.Image;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Pipelines;
using SixLabors.ImageSharp;
using System.Diagnostics;

Expand All @@ -11,15 +10,15 @@ public sealed class StableDebug : IExampleRunner
{
private readonly string _outputDirectory;
private readonly StableDiffusionConfig _configuration;
private readonly IStableDiffusionService _stableDiffusionService;

public StableDebug(StableDiffusionConfig configuration, IStableDiffusionService stableDiffusionService)
public StableDebug(StableDiffusionConfig configuration)
{
_configuration = configuration;
_stableDiffusionService = stableDiffusionService;
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDebug));
}

public int Index => 0;

public string Name => "Stable Diffusion Debug";

public string Description => "Stable Diffusion Debugger";
Expand All @@ -35,59 +34,58 @@ public async Task RunAsync()
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,";
while (true)
{
var developmentSeed = 624461087;
var promptOptions = new PromptOptions
{
Prompt = prompt,
NegativePrompt = negativePrompt,
};

var schedulerOptions = new SchedulerOptions
// Loop though the appsettings.json model sets
foreach (var modelSet in _configuration.ModelSets)
{
SchedulerType = SchedulerType.LMS,
Seed = 624461087,
GuidanceScale = 8,
InferenceSteps = 22,
Strength = 0.6f
};
OutputHelpers.WriteConsole($"Loading Model `{modelSet.Name}`...", ConsoleColor.Cyan);

foreach (var model in _configuration.ModelSets)
{
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
await _stableDiffusionService.LoadModelAsync(model);
// Create Pipeline
var pipeline = PipelineBase.CreatePipeline(modelSet);

schedulerOptions.Width = model.SampleSize;
schedulerOptions.Height = model.SampleSize;
// Preload Models (optional)
await pipeline.LoadAsync();

foreach (var schedulerType in model.PipelineType.GetSchedulerTypes())
// Loop though schedulers
foreach (var scheduler in pipeline.SupportedSchedulers)
{
schedulerOptions.SchedulerType = schedulerType;
OutputHelpers.WriteConsole($"Generating {schedulerType} Image...", ConsoleColor.Green);
await GenerateImage(model, promptOptions, schedulerOptions);
// Create SchedulerOptions based on pipeline defaults
var schedulerOptions = pipeline.DefaultSchedulerOptions with
{
Seed = developmentSeed,
SchedulerType = scheduler
};

var timestamp = Stopwatch.GetTimestamp();
OutputHelpers.WriteConsole($"Generating {scheduler} Image...", ConsoleColor.Green);

// Run pipeline
var result = await pipeline.RunAsync(promptOptions, schedulerOptions, progressCallback: OutputHelpers.ProgressCallback);

// Create Image from Tensor result
var image = result.ToImage();

// Save Image File
var outputFilename = Path.Combine(_outputDirectory, $"{modelSet.Name}_{schedulerOptions.SchedulerType}.png");
await image.SaveAsPngAsync(outputFilename);

OutputHelpers.WriteConsole($"{schedulerOptions.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
OutputHelpers.WriteConsole($"Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Yellow);
}

OutputHelpers.WriteConsole($"Unloading Model `{model.Name}`...", ConsoleColor.Green);
await _stableDiffusionService.UnloadModelAsync(model);
OutputHelpers.WriteConsole($"Unloading Model `{modelSet.Name}`...", ConsoleColor.Cyan);

// Unload pipeline
await pipeline.UnloadAsync();
}
break;
}
}


private async Task<bool> GenerateImage(StableDiffusionModelSet model, PromptOptions prompt, SchedulerOptions options)
{
var timestamp = Stopwatch.GetTimestamp();
var outputFilename = Path.Combine(_outputDirectory, $"{model.Name}_{options.Seed}_{options.SchedulerType}.png");
var result = await _stableDiffusionService.GenerateAsImageAsync(new ModelOptions(model), prompt, options);
if (result is not null)
{
await result.SaveAsPngAsync(outputFilename);
OutputHelpers.WriteConsole($"{options.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
OutputHelpers.WriteConsole($"Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Yellow);
return true;
}

OutputHelpers.WriteConsole($"Failed to create image", ConsoleColor.Red);
return false;
}
}
}
Loading