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

Major: API Refactor Part 2 #117

Merged
merged 5 commits into from
Feb 15, 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
6 changes: 3 additions & 3 deletions OnnxStack.Console/Examples/ControlNetExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ControlNetExample(StableDiffusionConfig configuration)
public async Task RunAsync()
{
// Load Control Image
var controlImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\OpenPose.png");
var controlImage = await OnnxImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\OpenPose.png");

// Create ControlNet
var controlNet = ControlNetModel.Create("D:\\Repositories\\controlnet_onnx\\controlnet\\openpose.onnx");
Expand All @@ -54,11 +54,11 @@ public async Task RunAsync()
var result = await pipeline.RunAsync(promptOptions, controlNet: controlNet, progressCallback: OutputHelpers.ProgressCallback);

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

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

//Unload
await controlNet.UnloadAsync();
Expand Down
8 changes: 4 additions & 4 deletions OnnxStack.Console/Examples/ControlNetFeatureExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ControlNetFeatureExample(StableDiffusionConfig configuration)
public async Task RunAsync()
{
// Load Control Image
var inputImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp");
var inputImage = await OnnxImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp");

// Create Annotation pipeline
var annotationPipeline = FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators\\depth.onnx", true);
Expand All @@ -41,7 +41,7 @@ public async Task RunAsync()
var controlImage = await annotationPipeline.RunAsync(inputImage);

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

// Create ControlNet
var controlNet = ControlNetModel.Create("D:\\Repositories\\controlnet_onnx\\controlnet\\depth.onnx");
Expand All @@ -61,11 +61,11 @@ public async Task RunAsync()
var result = await pipeline.RunAsync(promptOptions, controlNet: controlNet, progressCallback: OutputHelpers.ProgressCallback);

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

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

//Unload
await annotationPipeline.UnloadAsync();
Expand Down
8 changes: 4 additions & 4 deletions OnnxStack.Console/Examples/FeatureExtractorExample.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OnnxStack.Core.Image;
using OnnxStack.Core.Video;
using OnnxStack.FeatureExtractor.Pipelines;
using OnnxStack.StableDiffusion.Config;
using SixLabors.ImageSharp;
Expand Down Expand Up @@ -30,7 +31,7 @@ public FeatureExtractorExample(StableDiffusionConfig configuration)
public async Task RunAsync()
{
// Load Control Image
var inputImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp");
var inputImage = await OnnxImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp");

var pipelines = new[]
{
Expand All @@ -47,14 +48,13 @@ public async Task RunAsync()
var timestamp = Stopwatch.GetTimestamp();
OutputHelpers.WriteConsole($"Load pipeline`{pipeline.Name}`", ConsoleColor.Cyan);

// Run Pipeline
// Run Image Pipeline
var imageFeature = await pipeline.RunAsync(inputImage);

OutputHelpers.WriteConsole($"Generating image", ConsoleColor.Cyan);

// Save Image
await imageFeature.Image.SaveAsPngAsync(Path.Combine(_outputDirectory, $"{pipeline.Name}.png"));

await imageFeature.SaveAsync(Path.Combine(_outputDirectory, $"{pipeline.Name}.png"));

OutputHelpers.WriteConsole($"Unload pipeline", ConsoleColor.Cyan);

Expand Down
4 changes: 2 additions & 2 deletions OnnxStack.Console/Examples/StableDebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public async Task RunAsync()
var result = await pipeline.RunAsync(promptOptions, schedulerOptions, progressCallback: OutputHelpers.ProgressCallback);

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

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

OutputHelpers.WriteConsole($"{schedulerOptions.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green);
OutputHelpers.WriteConsole($"Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Yellow);
Expand Down
4 changes: 2 additions & 2 deletions OnnxStack.Console/Examples/StableDiffusionBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public async Task RunAsync()
await foreach (var result in pipeline.RunBatchAsync(batchOptions, promptOptions, progressCallback: OutputHelpers.BatchProgressCallback))
{
// Create Image from Tensor result
var image = result.ImageResult.ToImage();
var image = new OnnxImage(result.Result);

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

OutputHelpers.WriteConsole($"Image Created: {Path.GetFileName(outputFilename)}, Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Green);
timestamp = Stopwatch.GetTimestamp();
Expand Down
7 changes: 2 additions & 5 deletions OnnxStack.Console/Examples/StableDiffusionExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,11 @@ public async Task RunAsync()
OutputHelpers.WriteConsole($"Generating '{schedulerType}' Image...", ConsoleColor.Green);

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

// Create Image from Tensor result
var image = result.ToImage();
var result = await pipeline.GenerateImageAsync(promptOptions, schedulerOptions, progressCallback: OutputHelpers.ProgressCallback);

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

OutputHelpers.WriteConsole($"Image Created: {Path.GetFileName(outputFilename)}, Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Green);
}
Expand Down
4 changes: 2 additions & 2 deletions OnnxStack.Console/Examples/StableDiffusionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public async Task RunAsync()
var result = await pipeline.RunAsync(promptOptions, progressCallback: OutputHelpers.ProgressCallback);

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

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

OutputHelpers.WriteConsole($"Image Created: {Path.GetFileName(outputFilename)}, Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Green);
}
Expand Down
12 changes: 4 additions & 8 deletions OnnxStack.Console/Examples/UpscaleExample.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
using OnnxStack.Core.Image;
using OnnxStack.FeatureExtractor.Pipelines;
using OnnxStack.ImageUpscaler.Config;
using SixLabors.ImageSharp;

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

public UpscaleExample(ImageUpscalerConfig configuration)
public UpscaleExample()
{
_configuration = configuration;
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(UpscaleExample));
Directory.CreateDirectory(_outputDirectory);
}
Expand All @@ -26,7 +22,7 @@ public UpscaleExample(ImageUpscalerConfig configuration)
public async Task RunAsync()
{
// Load Input Image
var inputImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp");
var inputImage = await OnnxImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp");

// Create Pipeline
var pipeline = ImageUpscalePipeline.CreatePipeline("D:\\Repositories\\upscaler\\SwinIR\\003_realSR_BSRGAN_DFO_s64w8_SwinIR-M_x4_GAN.onnx", 4);
Expand All @@ -35,11 +31,11 @@ public async Task RunAsync()
var result = await pipeline.RunAsync(inputImage);

// Create Image from Tensor result
var image = result.ToImage(ImageNormalizeType.ZeroToOne);
var image = new OnnxImage(result, ImageNormalizeType.ZeroToOne);

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

// Unload
await pipeline.UnloadAsync();
Expand Down
16 changes: 5 additions & 11 deletions OnnxStack.Console/Examples/VideoToVideoExample.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using OnnxStack.Core.Services;
using OnnxStack.Core.Video;
using OnnxStack.StableDiffusion.Common;
using OnnxStack.Core.Video;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Pipelines;
Expand All @@ -11,12 +9,10 @@ public sealed class VideoToVideoExample : IExampleRunner
{
private readonly string _outputDirectory;
private readonly StableDiffusionConfig _configuration;
private readonly IVideoService _videoService;

public VideoToVideoExample(StableDiffusionConfig configuration, IVideoService videoService)
public VideoToVideoExample(StableDiffusionConfig configuration)
{
_configuration = configuration;
_videoService = videoService;
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(VideoToVideoExample));
Directory.CreateDirectory(_outputDirectory);
}
Expand All @@ -30,8 +26,7 @@ public VideoToVideoExample(StableDiffusionConfig configuration, IVideoService vi
public async Task RunAsync()
{
// Load Video
var targetFPS = 15;
var videoInput = await VideoInput.FromFileAsync("C:\\Users\\Deven\\Pictures\\gidsgphy.gif", targetFPS);
var videoInput = await OnnxVideo.FromFileAsync("C:\\Users\\Deven\\Pictures\\gidsgphy.gif");

// Loop though the appsettings.json model sets
foreach (var modelSet in _configuration.ModelSets)
Expand All @@ -53,11 +48,10 @@ public async Task RunAsync()
};

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

// Save Video File
var outputFilename = Path.Combine(_outputDirectory, $"{modelSet.Name}.mp4");
await VideoInput.SaveFileAsync(result, outputFilename, targetFPS);
await result.SaveAsync(Path.Combine(_outputDirectory, $"Result.mp4"));
}
}
}
Expand Down
1 change: 0 additions & 1 deletion OnnxStack.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ static async Task Main(string[] _)
// Add OnnxStack
builder.Services.AddOnnxStack();
builder.Services.AddOnnxStackConfig<StableDiffusionConfig>();
builder.Services.AddOnnxStackImageUpscaler();

// Add AppService
builder.Services.AddHostedService<AppService>();
Expand Down
16 changes: 0 additions & 16 deletions OnnxStack.Core/Config/OnnxModelSetConfig.cs

This file was deleted.

18 changes: 0 additions & 18 deletions OnnxStack.Core/Constants.cs

This file was deleted.

21 changes: 21 additions & 0 deletions OnnxStack.Core/Extensions/TensorExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.ML.OnnxRuntime.Tensors;
using System;
using System.Collections.Generic;

namespace OnnxStack.Core
{
Expand Down Expand Up @@ -54,6 +55,26 @@ public static void NormalizeMinMax(this DenseTensor<float> tensor)
}


/// <summary>
/// Splits the tensor across the batch dimension.
/// </summary>
/// <param name="tensor">The tensor.</param>
/// <returns></returns>
public static IEnumerable<DenseTensor<float>> SplitBatch(this DenseTensor<float> tensor)
{
var count = tensor.Dimensions[0];
var dimensions = tensor.Dimensions.ToArray();
dimensions[0] = 1;

var newLength = (int)tensor.Length / count;
for (int i = 0; i < count; i++)
{
var start = i * newLength;
yield return new DenseTensor<float>(tensor.Buffer.Slice(start, newLength), dimensions);
}
}


/// <summary>
/// Concatenates the specified tensors along the specified axis.
/// </summary>
Expand Down
Loading