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

Commit 13e90c2

Browse files
committed
Wrap and simplify Video input
1 parent 21a1e21 commit 13e90c2

33 files changed

+872
-867
lines changed

OnnxStack.Console/Examples/FeatureExtractorExample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using OnnxStack.Core.Image;
2+
using OnnxStack.Core.Video;
23
using OnnxStack.FeatureExtractor.Pipelines;
34
using OnnxStack.StableDiffusion.Config;
45
using SixLabors.ImageSharp;
@@ -47,15 +48,14 @@ public async Task RunAsync()
4748
var timestamp = Stopwatch.GetTimestamp();
4849
OutputHelpers.WriteConsole($"Load pipeline`{pipeline.Name}`", ConsoleColor.Cyan);
4950

50-
// Run Pipeline
51+
// Run Image Pipeline
5152
var imageFeature = await pipeline.RunAsync(inputImage);
5253

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

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

58-
5959
OutputHelpers.WriteConsole($"Unload pipeline", ConsoleColor.Cyan);
6060

6161
//Unload

OnnxStack.Console/Examples/StableDiffusionBatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public async Task RunAsync()
6161
await foreach (var result in pipeline.RunBatchAsync(batchOptions, promptOptions, progressCallback: OutputHelpers.BatchProgressCallback))
6262
{
6363
// Create Image from Tensor result
64-
var image = result.ImageResult;
64+
var image = new OnnxImage(result.Result);
6565

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

OnnxStack.Console/Examples/StableDiffusionExample.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,11 @@ public async Task RunAsync()
6767
OutputHelpers.WriteConsole($"Generating '{schedulerType}' Image...", ConsoleColor.Green);
6868

6969
// Run pipeline
70-
var result = await pipeline.RunAsync(promptOptions, schedulerOptions, progressCallback: OutputHelpers.ProgressCallback);
71-
72-
// Create Image from Tensor result
73-
var image = new OnnxImage(result);
70+
var result = await pipeline.GenerateImageAsync(promptOptions, schedulerOptions, progressCallback: OutputHelpers.ProgressCallback);
7471

7572
// Save Image File
7673
var outputFilename = Path.Combine(_outputDirectory, $"{modelSet.Name}_{schedulerOptions.SchedulerType}.png");
77-
await image.SaveAsync(outputFilename);
74+
await result.SaveAsync(outputFilename);
7875

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

OnnxStack.Console/Examples/VideoToVideoExample.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using OnnxStack.Core.Services;
2-
using OnnxStack.Core.Video;
3-
using OnnxStack.StableDiffusion.Common;
1+
using OnnxStack.Core.Video;
42
using OnnxStack.StableDiffusion.Config;
53
using OnnxStack.StableDiffusion.Enums;
64
using OnnxStack.StableDiffusion.Pipelines;
@@ -11,12 +9,10 @@ public sealed class VideoToVideoExample : IExampleRunner
119
{
1210
private readonly string _outputDirectory;
1311
private readonly StableDiffusionConfig _configuration;
14-
private readonly IVideoService _videoService;
1512

16-
public VideoToVideoExample(StableDiffusionConfig configuration, IVideoService videoService)
13+
public VideoToVideoExample(StableDiffusionConfig configuration)
1714
{
1815
_configuration = configuration;
19-
_videoService = videoService;
2016
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(VideoToVideoExample));
2117
Directory.CreateDirectory(_outputDirectory);
2218
}
@@ -30,8 +26,7 @@ public VideoToVideoExample(StableDiffusionConfig configuration, IVideoService vi
3026
public async Task RunAsync()
3127
{
3228
// Load Video
33-
var targetFPS = 15;
34-
var videoInput = await VideoInput.FromFileAsync("C:\\Users\\Deven\\Pictures\\gidsgphy.gif", targetFPS);
29+
var videoInput = await OnnxVideo.FromFileAsync("C:\\Users\\Deven\\Pictures\\gidsgphy.gif");
3530

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

5550
// Run pipeline
56-
var result = await pipeline.RunAsync(promptOptions, progressCallback: OutputHelpers.FrameProgressCallback);
51+
var result = await pipeline.GenerateVideoAsync(promptOptions, progressCallback: OutputHelpers.FrameProgressCallback);
5752

5853
// Save Video File
59-
var outputFilename = Path.Combine(_outputDirectory, $"{modelSet.Name}.mp4");
60-
await VideoInput.SaveFileAsync(result, outputFilename, targetFPS);
54+
await result.SaveAsync(Path.Combine(_outputDirectory, $"Result.mp4"));
6155
}
6256
}
6357
}

OnnxStack.Core/Config/OnnxModelSetConfig.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

OnnxStack.Core/Constants.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

OnnxStack.Core/Extensions/TensorExtension.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.ML.OnnxRuntime.Tensors;
22
using System;
3+
using System.Collections.Generic;
34

45
namespace OnnxStack.Core
56
{
@@ -54,6 +55,26 @@ public static void NormalizeMinMax(this DenseTensor<float> tensor)
5455
}
5556

5657

58+
/// <summary>
59+
/// Splits the tensor across the batch dimension.
60+
/// </summary>
61+
/// <param name="tensor">The tensor.</param>
62+
/// <returns></returns>
63+
public static IEnumerable<DenseTensor<float>> SplitBatch(this DenseTensor<float> tensor)
64+
{
65+
var count = tensor.Dimensions[0];
66+
var dimensions = tensor.Dimensions.ToArray();
67+
dimensions[0] = 1;
68+
69+
var newLength = (int)tensor.Length / count;
70+
for (int i = 0; i < count; i++)
71+
{
72+
var start = i * newLength;
73+
yield return new DenseTensor<float>(tensor.Buffer.Slice(start, newLength), dimensions);
74+
}
75+
}
76+
77+
5778
/// <summary>
5879
/// Concatenates the specified tensors along the specified axis.
5980
/// </summary>

OnnxStack.Core/Image/OnnxImage.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace OnnxStack.Core.Image
1212
{
13-
public class OnnxImage : IDisposable
13+
public sealed class OnnxImage : IDisposable
1414
{
1515
private readonly Image<Rgba32> _imageData;
1616

@@ -222,6 +222,10 @@ public void Resize(int height, int width, ResizeMode resizeMode = ResizeMode.Cro
222222
});
223223
}
224224

225+
public OnnxImage Clone()
226+
{
227+
return new OnnxImage(_imageData);
228+
}
225229

226230
/// <summary>
227231
/// Saves the specified image to file.

OnnxStack.Core/Model/OnnxInferenceParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace OnnxStack.Core.Model
77
{
8-
public class OnnxInferenceParameters : IDisposable
8+
public sealed class OnnxInferenceParameters : IDisposable
99
{
1010
private readonly RunOptions _runOptions;
1111
private readonly OnnxMetadata _metadata;

OnnxStack.Core/Model/OnnxMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace OnnxStack.Core.Model
44
{
5-
public record OnnxMetadata
5+
public sealed record OnnxMetadata
66
{
77
/// <summary>
88
/// Gets or sets the inputs.

OnnxStack.Core/Model/OnnxNamedMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace OnnxStack.Core.Model
55
{
6-
public record OnnxNamedMetadata(string Name, NodeMetadata Value)
6+
public sealed record OnnxNamedMetadata(string Name, NodeMetadata Value)
77
{
88
internal static OnnxNamedMetadata Create(KeyValuePair<string, NodeMetadata> metadata)
99
{

OnnxStack.Core/Model/OnnxValueCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OnnxStack.Core.Model
66
{
7-
public class OnnxValueCollection : IDisposable
7+
public sealed class OnnxValueCollection : IDisposable
88
{
99
private readonly List<OnnxNamedMetadata> _metaData;
1010
private readonly Dictionary<string, OrtValue> _values;

OnnxStack.Core/Registration.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using OnnxStack.Common.Config;
33
using OnnxStack.Core.Config;
4-
using OnnxStack.Core.Services;
54

65
namespace OnnxStack.Core
76
{

OnnxStack.Core/Services/IVideoService.cs

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)