Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Wrap and simplify Video input
  • Loading branch information
saddam213 committed Feb 13, 2024
commit 13e90c2429478a104d91e2d3a5c30a7bbfb2ad1d
4 changes: 2 additions & 2 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 @@ -47,15 +48,14 @@ 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.SaveAsync(Path.Combine(_outputDirectory, $"{pipeline.Name}.png"));


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

//Unload
Expand Down
2 changes: 1 addition & 1 deletion OnnxStack.Console/Examples/StableDiffusionBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ 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;
var image = new OnnxImage(result.Result);

// Save Image File
var outputFilename = Path.Combine(_outputDirectory, $"{modelSet.Name}_{result.SchedulerOptions.Seed}.png");
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 = new OnnxImage(result);
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.SaveAsync(outputFilename);
await result.SaveAsync(outputFilename);

OutputHelpers.WriteConsole($"Image Created: {Path.GetFileName(outputFilename)}, Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Green);
}
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
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
6 changes: 5 additions & 1 deletion OnnxStack.Core/Image/OnnxImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace OnnxStack.Core.Image
{
public class OnnxImage : IDisposable
public sealed class OnnxImage : IDisposable
{
private readonly Image<Rgba32> _imageData;

Expand Down Expand Up @@ -222,6 +222,10 @@ public void Resize(int height, int width, ResizeMode resizeMode = ResizeMode.Cro
});
}

public OnnxImage Clone()
{
return new OnnxImage(_imageData);
}

/// <summary>
/// Saves the specified image to file.
Expand Down
2 changes: 1 addition & 1 deletion OnnxStack.Core/Model/OnnxInferenceParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace OnnxStack.Core.Model
{
public class OnnxInferenceParameters : IDisposable
public sealed class OnnxInferenceParameters : IDisposable
{
private readonly RunOptions _runOptions;
private readonly OnnxMetadata _metadata;
Expand Down
2 changes: 1 addition & 1 deletion OnnxStack.Core/Model/OnnxMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OnnxStack.Core.Model
{
public record OnnxMetadata
public sealed record OnnxMetadata
{
/// <summary>
/// Gets or sets the inputs.
Expand Down
2 changes: 1 addition & 1 deletion OnnxStack.Core/Model/OnnxNamedMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace OnnxStack.Core.Model
{
public record OnnxNamedMetadata(string Name, NodeMetadata Value)
public sealed record OnnxNamedMetadata(string Name, NodeMetadata Value)
{
internal static OnnxNamedMetadata Create(KeyValuePair<string, NodeMetadata> metadata)
{
Expand Down
2 changes: 1 addition & 1 deletion OnnxStack.Core/Model/OnnxValueCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OnnxStack.Core.Model
{
public class OnnxValueCollection : IDisposable
public sealed class OnnxValueCollection : IDisposable
{
private readonly List<OnnxNamedMetadata> _metaData;
private readonly Dictionary<string, OrtValue> _values;
Expand Down
1 change: 0 additions & 1 deletion OnnxStack.Core/Registration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using OnnxStack.Common.Config;
using OnnxStack.Core.Config;
using OnnxStack.Core.Services;

namespace OnnxStack.Core
{
Expand Down
109 changes: 0 additions & 109 deletions OnnxStack.Core/Services/IVideoService.cs

This file was deleted.

Loading