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

Commit bf4315f

Browse files
authored
Merge pull request #115 from saddam213/FeatureExtractor
Feature extractor update
2 parents d83a59c + 68cde10 commit bf4315f

38 files changed

+1215
-303
lines changed

OnnxStack.Console/Examples/ControlNetFeatureExample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public async Task RunAsync()
3535
var inputImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp");
3636

3737
// Create Annotation pipeline
38-
var annotationPipeline = AnnotationPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators");
38+
var annotationPipeline = FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators\\depth.onnx", true);
3939

4040
// Create Depth Image
41-
var controlImage = await annotationPipeline.DepthImage(inputImage);
41+
var controlImage = await annotationPipeline.RunAsync(inputImage);
4242

4343
// Save Depth Image (Debug Only)
4444
await controlImage.Image.SaveAsPngAsync(Path.Combine(_outputDirectory, $"Depth.png"));
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.FeatureExtractor.Pipelines;
3+
using OnnxStack.StableDiffusion.Config;
4+
using SixLabors.ImageSharp;
5+
using System.Diagnostics;
6+
7+
namespace OnnxStack.Console.Runner
8+
{
9+
public sealed class FeatureExtractorExample : IExampleRunner
10+
{
11+
private readonly string _outputDirectory;
12+
private readonly StableDiffusionConfig _configuration;
13+
14+
public FeatureExtractorExample(StableDiffusionConfig configuration)
15+
{
16+
_configuration = configuration;
17+
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(FeatureExtractorExample));
18+
Directory.CreateDirectory(_outputDirectory);
19+
}
20+
21+
public int Index => 13;
22+
23+
public string Name => "Feature Extractor Example";
24+
25+
public string Description => "Simple exmaple using ControlNet feature extractors";
26+
27+
/// <summary>
28+
/// ControlNet Example
29+
/// </summary>
30+
public async Task RunAsync()
31+
{
32+
// Load Control Image
33+
var inputImage = await InputImage.FromFileAsync("D:\\Repositories\\OnnxStack\\Assets\\Samples\\Img2Img_Start.bmp");
34+
35+
var pipelines = new[]
36+
{
37+
FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators\\canny.onnx"),
38+
FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators\\hed.onnx"),
39+
FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\controlnet_onnx\\annotators\\depth.onnx", true),
40+
41+
// FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\depth-anything-large-hf\\onnx\\model.onnx", normalize: true, sampleSize: 504),
42+
// FeatureExtractorPipeline.CreatePipeline("D:\\Repositories\\sentis-MiDaS\\dpt_beit_large_512.onnx", normalize: true, sampleSize: 384),
43+
};
44+
45+
foreach (var pipeline in pipelines)
46+
{
47+
var timestamp = Stopwatch.GetTimestamp();
48+
OutputHelpers.WriteConsole($"Load pipeline`{pipeline.Name}`", ConsoleColor.Cyan);
49+
50+
// Run Pipeline
51+
var imageFeature = await pipeline.RunAsync(inputImage);
52+
53+
OutputHelpers.WriteConsole($"Generating image", ConsoleColor.Cyan);
54+
55+
// Save Image
56+
await imageFeature.Image.SaveAsPngAsync(Path.Combine(_outputDirectory, $"{pipeline.Name}.png"));
57+
58+
59+
OutputHelpers.WriteConsole($"Unload pipeline", ConsoleColor.Cyan);
60+
61+
//Unload
62+
await pipeline.UnloadAsync();
63+
64+
OutputHelpers.WriteConsole($"Elapsed: {Stopwatch.GetElapsedTime(timestamp)}ms", ConsoleColor.Yellow);
65+
}
66+
}
67+
}
68+
}

OnnxStack.Core/Extensions/TensorExtension.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ public static DenseTensor<float> Repeat(this DenseTensor<float> tensor1, int cou
3232
}
3333

3434

35+
/// <summary>
36+
/// Normalize the data using Min-Max scaling to ensure all values are in the range [0, 1].
37+
/// </summary>
38+
/// <param name="tensor">The tensor.</param>
39+
public static void NormalizeMinMax(this DenseTensor<float> tensor)
40+
{
41+
var values = tensor.Buffer.Span;
42+
float min = float.PositiveInfinity, max = float.NegativeInfinity;
43+
foreach (var val in values)
44+
{
45+
if (min > val) min = val;
46+
if (max < val) max = val;
47+
}
48+
49+
var range = max - min;
50+
for (var i = 0; i < values.Length; i++)
51+
{
52+
values[i] = (values[i] - min) / range;
53+
}
54+
}
55+
56+
3557
/// <summary>
3658
/// Concatenates the specified tensors along the specified axis.
3759
/// </summary>

OnnxStack.Core/Video/VideoFrame.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22

33
namespace OnnxStack.Core.Video
44
{
5-
public record VideoFrame(byte[] Frame, InputImage ControlImage = default);
5+
public record VideoFrame(byte[] Frame)
6+
{
7+
public InputImage ExtraFrame { get; set; }
8+
}
69
}

OnnxStack.FeatureExtractor/Common/FeatureExtractorModel.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,35 @@ namespace OnnxStack.FeatureExtractor.Common
77
public class FeatureExtractorModel : OnnxModelSession
88
{
99
private readonly int _sampleSize;
10+
private readonly bool _normalize;
11+
private readonly int _channels;
1012

1113
public FeatureExtractorModel(FeatureExtractorModelConfig configuration)
1214
: base(configuration)
1315
{
1416
_sampleSize = configuration.SampleSize;
17+
_normalize = configuration.Normalize;
18+
_channels = configuration.Channels;
1519
}
1620

1721
public int SampleSize => _sampleSize;
1822

23+
public bool Normalize => _normalize;
24+
25+
public int Channels => _channels;
26+
1927
public static FeatureExtractorModel Create(FeatureExtractorModelConfig configuration)
2028
{
2129
return new FeatureExtractorModel(configuration);
2230
}
2331

24-
public static FeatureExtractorModel Create(string modelFile, int deviceId = 0, ExecutionProvider executionProvider = ExecutionProvider.DirectML)
32+
public static FeatureExtractorModel Create(string modelFile, bool normalize = false, int sampleSize = 512, int channels = 3, int deviceId = 0, ExecutionProvider executionProvider = ExecutionProvider.DirectML)
2533
{
2634
var configuration = new FeatureExtractorModelConfig
2735
{
28-
SampleSize = 512,
36+
SampleSize = sampleSize,
37+
Normalize = normalize,
38+
Channels = channels,
2939
DeviceId = deviceId,
3040
ExecutionProvider = executionProvider,
3141
ExecutionMode = ExecutionMode.ORT_SEQUENTIAL,
@@ -40,5 +50,7 @@ public static FeatureExtractorModel Create(string modelFile, int deviceId = 0, E
4050
public record FeatureExtractorModelConfig : OnnxModelConfig
4151
{
4252
public int SampleSize { get; set; }
53+
public bool Normalize { get; set; }
54+
public int Channels { get; set; }
4355
}
4456
}

OnnxStack.FeatureExtractor/Common/AnnotationModelSet.cs renamed to OnnxStack.FeatureExtractor/Common/FeatureExtractorModelSet.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OnnxStack.FeatureExtractor.Common
66
{
7-
public record AnnotationModelSet : IOnnxModelSetConfig
7+
public record FeatureExtractorModelSet : IOnnxModelSetConfig
88
{
99
public string Name { get; set; }
1010
public bool IsEnabled { get; set; }
@@ -15,14 +15,6 @@ public record AnnotationModelSet : IOnnxModelSetConfig
1515
public ExecutionProvider ExecutionProvider { get; set; }
1616

1717
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
18-
public FeatureExtractorModelConfig CannyImageConfig { get; set; }
19-
20-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
21-
public FeatureExtractorModelConfig HedImageConfig { get; set; }
22-
23-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
24-
public FeatureExtractorModelConfig DepthImageConfig { get; set; }
18+
public FeatureExtractorModelConfig FeatureExtractorConfig { get; set; }
2519
}
26-
27-
2820
}

0 commit comments

Comments
 (0)