Skip to content

Commit b77382a

Browse files
committed
Background remove pipeline, improve video processing performance
1 parent d2fd7a8 commit b77382a

24 files changed

+657
-286
lines changed

TensorStack.Common/Extensions/Extensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using System.Runtime.CompilerServices;
7+
using System.Threading;
8+
using System.Threading.Tasks;
79

810
namespace TensorStack.Common
911
{
@@ -141,5 +143,29 @@ public static float ZeroIfNan(this float value)
141143
{
142144
return float.IsNaN(value) ? 0f : value;
143145
}
146+
147+
148+
public static bool CanBeCanceled(this CancellationTokenSource cancellationTokenSource)
149+
{
150+
if (cancellationTokenSource is null)
151+
return false;
152+
153+
try
154+
{
155+
return cancellationTokenSource.Token.CanBeCanceled;
156+
}
157+
catch { }
158+
return false;
159+
}
160+
161+
162+
public static async Task SafeCancelAsync(this CancellationTokenSource cancellationTokenSource)
163+
{
164+
try
165+
{
166+
await cancellationTokenSource.CancelAsync();
167+
}
168+
catch { }
169+
}
144170
}
145171
}

TensorStack.Common/Extensions/TensorExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,16 +581,16 @@ public static Tensor<float> Join(this IEnumerable<Tensor<float>> tensors, int ax
581581
dimensions[0] *= count;
582582

583583
var newLength = (int)tensor.Length;
584-
var buffer = new float[newLength * count].AsMemory();
584+
var buffer = new Tensor<float>(dimensions);
585585

586586
var index = 0;
587587
foreach (var item in tensors)
588588
{
589589
var start = index * newLength;
590-
item.Memory.CopyTo(buffer[start..]);
590+
item.Memory.CopyTo(buffer.Memory[start..]);
591591
index++;
592592
}
593-
return new Tensor<float>(buffer, dimensions);
593+
return buffer;
594594
}
595595

596596

@@ -997,7 +997,7 @@ public static Tensor<float> WithGuidance(this Tensor<float> tensor, bool applyGu
997997
/// <param name="resizeMode">The resize mode.</param>
998998
/// <param name="resizeMethod">The resize method.</param>
999999
/// <returns>ImageTensor.</returns>
1000-
public static ImageTensor ResizeImage(this ImageTensor sourceImage, int targetWidth, int targetHeight, ResizeMode resizeMode = ResizeMode.Stretch, ResizeMethod resizeMethod = ResizeMethod.Bicubic)
1000+
public static ImageTensor ResizeImage(this ImageTensor sourceImage, int targetWidth, int targetHeight, ResizeMode resizeMode = ResizeMode.Stretch, ResizeMethod resizeMethod = ResizeMethod.Bilinear)
10011001
{
10021002
return resizeMethod switch
10031003
{

TensorStack.Common/Video/VideoFrame.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public class VideoFrame
1515
/// </summary>
1616
/// <param name="frame">The frame.</param>
1717
/// <param name="sourceFrameRate">The source frame rate.</param>
18-
public VideoFrame(ImageTensor frame, float sourceFrameRate, ImageTensor auxFrame = default)
18+
public VideoFrame(int index,ImageTensor frame, float sourceFrameRate, ImageTensor auxFrame = default)
1919
{
20+
Index = index;
2021
Frame = frame;
2122
AuxFrame = auxFrame;
2223
SourceFrameRate = sourceFrameRate;
@@ -28,9 +29,13 @@ public VideoFrame(ImageTensor frame, float sourceFrameRate, ImageTensor auxFrame
2829
/// </summary>
2930
/// <param name="frame">The frame.</param>
3031
/// <param name="sourceFrameRate">The source frame rate.</param>
31-
public VideoFrame(Tensor<float> frame, float sourceFrameRate)
32-
: this(new ImageTensor(frame), sourceFrameRate) { }
32+
public VideoFrame(int index, Tensor<float> frame, float sourceFrameRate)
33+
: this(index, new ImageTensor(frame), sourceFrameRate) { }
3334

35+
/// <summary>
36+
/// Gets the index.
37+
/// </summary>
38+
public int Index { get; }
3439

3540
/// <summary>
3641
/// Gets the frame.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) TensorStack. All rights reserved.
2+
// Licensed under the Apache 2.0 License.
3+
using TensorStack.Common;
4+
using TensorStack.Common.Pipeline;
5+
using TensorStack.Common.Tensor;
6+
7+
namespace TensorStack.Extractors.Common
8+
{
9+
/// <summary>
10+
/// Default ExtractorOptions.
11+
/// </summary>
12+
public record BackgroundImageOptions : IRunOptions
13+
{
14+
/// <summary>
15+
/// Gets a value indicating whether the output is inverted.
16+
/// </summary>
17+
public BackgroundMode Mode { get; init; }
18+
19+
/// <summary>
20+
/// Gets the input.
21+
/// </summary>
22+
public ImageTensor Input { get; init; }
23+
}
24+
25+
public enum BackgroundMode
26+
{
27+
MaskBackground = 0,
28+
MaskForeground = 1,
29+
30+
RemoveBackground = 10,
31+
RemoveForeground = 11,
32+
}
33+
}

TensorStack.Extractors/Common/ExtractorConfig.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ public record ExtractorConfig : ModelConfig
2626
/// </summary>
2727
public Normalization OutputNormalization { get; init; }
2828

29-
/// <summary>
30-
/// If the result should ne inverted
31-
/// </summary>
32-
public bool IsOutputInverted { get; init; }
33-
3429
/// <summary>
3530
/// The channels the model supports 1 = Greyscale, RGB = 3, RGBA = 4.
3631
/// </summary>
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
// Copyright (c) TensorStack. All rights reserved.
22
// Licensed under the Apache 2.0 License.
3-
using TensorStack.Common;
43
using TensorStack.Common.Tensor;
54

65
namespace TensorStack.Extractors.Common
76
{
87
public record ExtractorImageOptions : ExtractorOptions
98
{
10-
public ExtractorImageOptions(ImageTensor input, bool mergeInput = false, TileMode tileMode = TileMode.None, int maxTileSize = 512, int tileOverlap = 16)
11-
: base(mergeInput, tileMode, maxTileSize, tileOverlap)
12-
{
13-
Input = input;
14-
}
15-
16-
public ImageTensor Input { get; }
9+
public ImageTensor Input { get; init; }
1710
}
1811
}

TensorStack.Extractors/Common/ExtractorOptions.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,29 @@ namespace TensorStack.Extractors.Common
1010
/// </summary>
1111
public record ExtractorOptions : IRunOptions
1212
{
13-
/// <summary>
14-
/// Initializes a new instance of the <see cref="ExtractorOptions"/> class.
15-
/// </summary>
16-
/// <param name="tileMode">Enable/Disable TileMode, splitting image into smaller tiles to save memory.</param>
17-
/// <param name="maxTileSize">The maximum size of the tile for TileMode</param>
18-
/// <param name="tileOverlap">The tile overlap in pixels to avoid visible seams.</param>
19-
public ExtractorOptions(bool mergeInput = false, TileMode tileMode = TileMode.None, int maxTileSize = 512, int tileOverlap = 16)
20-
{
21-
MergeInput = mergeInput;
22-
TileMode = tileMode;
23-
MaxTileSize = maxTileSize;
24-
TileOverlap = tileOverlap;
25-
}
26-
2713
/// <summary>
2814
/// Megre the input and output result into a new tensor.
2915
/// </summary>
30-
public bool MergeInput { get; set; }
16+
public bool MergeInput { get; init; }
3117

3218
/// <summary>
3319
/// Enable/Disable TileMode, splitting image into smaller tiles to save memory.
3420
/// </summary>
35-
public TileMode TileMode { get; }
21+
public TileMode TileMode { get; init; }
3622

3723
/// <summary>
3824
/// The maximum size of the tile.
3925
/// </summary>
40-
public int MaxTileSize { get; }
26+
public int MaxTileSize { get; init; }
4127

4228
/// <summary>
4329
/// The tile overlap in pixels to avoid visible seams.
4430
/// </summary>
45-
public int TileOverlap { get; }
31+
public int TileOverlap { get; init; }
32+
33+
/// <summary>
34+
/// Gets a value indicating whether the output is inverted.
35+
/// </summary>
36+
public bool IsInverted { get; init; }
4637
}
4738
}

TensorStack.Extractors/Common/ExtractorStreamOptions.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ namespace TensorStack.Extractors.Common
88
{
99
public record ExtractorStreamOptions : ExtractorOptions
1010
{
11-
public ExtractorStreamOptions(IAsyncEnumerable<VideoFrame> input, bool mergeInput = false, TileMode tileMode = TileMode.None, int maxTileSize = 512, int tileOverlap = 16)
12-
: base(mergeInput, tileMode, maxTileSize, tileOverlap)
13-
{
14-
Input = input;
15-
}
16-
1711
public IAsyncEnumerable<VideoFrame> Input { get; }
1812
}
1913
}

TensorStack.Extractors/Common/ExtractorVideoOptions.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ namespace TensorStack.Extractors.Common
77
{
88
public record ExtractorVideoOptions : ExtractorOptions
99
{
10-
public ExtractorVideoOptions(VideoTensor input, bool mergeInput = false, TileMode tileMode = TileMode.None, int maxTileSize = 512, int tileOverlap = 16)
11-
: base(mergeInput, tileMode, maxTileSize, tileOverlap)
12-
{
13-
Input = input;
14-
}
15-
1610
public VideoTensor Input { get; }
1711
}
1812
}

TensorStack.Extractors/Models/ExtractorModel.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ private ExtractorModel(ExtractorConfig configuration)
3434
/// </summary>
3535
public Normalization OutputNormalization => Configuration.OutputNormalization;
3636

37-
/// <summary>
38-
/// If the result should ne inverted
39-
/// </summary>
40-
public bool IsOutputInverted => Configuration.IsOutputInverted;
41-
4237
/// <summary>
4338
/// The channels the model supports 1 = Greyscale, RGB = 3, RGBA = 4.
4439
/// </summary>

0 commit comments

Comments
 (0)