Skip to content

Commit 9c59d5b

Browse files
sdcbAvenSun
andauthored
Use VConcat to improve performance (#7)
* initiate benchmark project * add image stacking benchmark * improve benchmark * add memory diagnoser and multi exporter add benchmark for Net 8.0 add more test images * benchmark result * improve performance greatly (30% ~ 44%) * update benchmark * update images and benchmark result * update StackingVertically and ResizePadding * fix problems * add unit test, align code. * update packages. * Remove benchmark project since it's too specific. --------- Co-authored-by: Aven <aven@ocr.one>
1 parent 9bf945d commit 9c59d5b

9 files changed

Lines changed: 50 additions & 16 deletions

File tree

build/00-common.linq

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ static void Run(string exe, string args, Encoding encoding) => Util.Cmd(exe, arg
2222
static ProjectVersion[] Projects = new[]
2323
{
2424
new ProjectVersion("Sdcb.OpenVINO", "0.6.1"),
25-
new ProjectVersion("Sdcb.OpenVINO.Extensions.OpenCvSharp4", "0.2.0"),
26-
new ProjectVersion("Sdcb.OpenVINO.PaddleOCR", "0.5.1"),
25+
new ProjectVersion("Sdcb.OpenVINO.Extensions.OpenCvSharp4", "0.6.1"),
26+
new ProjectVersion("Sdcb.OpenVINO.PaddleOCR", "0.6.1"),
2727
new ProjectVersion("Sdcb.OpenVINO.PaddleOCR.Models.Online", "0.2.1"),
2828
};
2929

projects/PaddleOCR/Sdcb.OpenVINO.PaddleOCR.Tests/OnlineRecTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,18 @@ private void RecAsserts(PaddleOcrRecognizer rec, params Mat[] srcs)
9191
Assert.True(result.Score > 0.9);
9292
}
9393
}
94+
95+
[Theory]
96+
[InlineData(27, 294)]
97+
[InlineData(48, 1024)]
98+
[InlineData(30, 512)]
99+
public void ResizePaddingTests(int srcHeight, int srcWidth)
100+
{
101+
int modelHeight = 48;
102+
int targetWidth = 512;
103+
using Mat src = new(srcHeight, srcWidth, MatType.CV_8UC1);
104+
using Mat res = PaddleOcrRecognizer.ResizePadding(src, modelHeight, targetWidth);
105+
Assert.Equal(targetWidth, res.Width);
106+
Assert.Equal(modelHeight, res.Height);
107+
}
94108
}

projects/PaddleOCR/Sdcb.OpenVINO.PaddleOCR.Tests/Sdcb.OpenVINO.PaddleOCR.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
<ItemGroup>
3434
<ProjectReference Include="..\Sdcb.OpenVINO.PaddleOCR.Models.Online\Sdcb.OpenVINO.PaddleOCR.Models.Online.csproj" />
35+
<ProjectReference Include="..\Sdcb.OpenVINO.PaddleOCR\Sdcb.OpenVINO.PaddleOCR.csproj" />
3536
</ItemGroup>
3637

3738
</Project>

projects/PaddleOCR/Sdcb.OpenVINO.PaddleOCR/PaddleOcrClassifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private Mat PrepareAndStackImages(Mat[] srcs)
141141
return ResizePadding(channel3, Shape);
142142
})
143143
.ToArray();
144-
using Mat combined = normalizeds.StackingVertically(Shape.Height, Shape.Width);
144+
using Mat combined = normalizeds.StackingVertically();
145145
combined.ConvertTo(final, MatType.CV_32FC3, 2.0 / 255, -1.0);
146146
}
147147
finally

projects/PaddleOCR/Sdcb.OpenVINO.PaddleOCR/PaddleOcrRecognizer.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private static unsafe Mat PrepareAndStackImages(Mat[] srcs, int modelHeight, int
224224
return ResizePadding(channel3, modelHeight, maxWidth);
225225
})
226226
.ToArray();
227-
using Mat combined = normalizeds.StackingVertically(modelHeight, maxWidth);
227+
using Mat combined = normalizeds.StackingVertically();
228228
combined.ConvertTo(final, MatType.CV_32FC3, 2.0 / 255, -1.0);
229229
}
230230
finally
@@ -246,17 +246,16 @@ internal static Mat ResizePadding(Mat src, int modelHeight, int targetWidth)
246246
// Resize image
247247
Mat resized = new();
248248
Cv2.Resize(src, resized, new Size(), scale, scale);
249-
250249
// Compute padding for height and width
251-
int padTop = Math.Max(0, (modelHeight - resized.Height) / 2);
250+
int padH = modelHeight - resized.Height;
251+
int padRight = targetWidth - resized.Width;
252252

253-
if (padTop > 0)
253+
if (padH > 0 || padRight > 0)
254254
{
255-
// Add padding. If padding needs to be added to top and bottom we divide it equally,
256-
// but if there is an odd number we add the extra pixel to the bottom.
257255
Mat result = new();
258-
int remainder = (modelHeight - resized.Height) % 2;
259-
Cv2.CopyMakeBorder(resized, result, padTop, padTop + remainder, 0, 0, BorderTypes.Constant, Scalar.Black);
256+
int padTop = padH / 2;
257+
int padBottom = padH - padTop;
258+
Cv2.CopyMakeBorder(resized, result, padTop, padBottom, 0, padRight, BorderTypes.Constant, Scalar.Black);
260259
resized.Dispose();
261260
return result;
262261
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Sdcb.OpenVINO.PaddleOCR.Tests")]

projects/PaddleOCR/Sdcb.OpenVINO.PaddleOCR/Sdcb.OpenVINO.PaddleOCR.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3737
</PackageReference>
3838

39-
<PackageReference Include="Sdcb.OpenVINO" Version="0.5.1" Condition="'$(Configuration)' != 'Debug'" />
40-
<PackageReference Include="Sdcb.OpenVINO.Extensions.OpenCvSharp4" Version="0.2.0" Condition="'$(Configuration)' != 'Debug'"/>
39+
<PackageReference Include="Sdcb.OpenVINO" Version="0.6.1" Condition="'$(Configuration)' != 'Debug'" />
40+
<PackageReference Include="Sdcb.OpenVINO.Extensions.OpenCvSharp4" Version="0.6.1" Condition="'$(Configuration)' != 'Debug'" />
4141

4242
<ProjectReference Include="..\..\..\src\Sdcb.OpenVINO\Sdcb.OpenVINO.csproj" Condition="'$(Configuration)' == 'Debug'" />
4343
<ProjectReference Include="..\..\..\src\Sdcb.OpenVINO.Extensions.OpenCvSharp4\Sdcb.OpenVINO.Extensions.OpenCvSharp4.csproj" Condition="'$(Configuration)' == 'Debug'" />

src/Sdcb.OpenVINO.Extensions.OpenCvSharp4/MatExtensions.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using OpenCvSharp;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Text;
53

64
namespace Sdcb.OpenVINO.Extensions.OpenCvSharp4;
75

@@ -86,6 +84,7 @@ public static Mat Padding(Mat src, int padSize = 32, Scalar? color = default)
8684
/// <remarks>
8785
/// This method utilizes OpenCvSharp4 for operations on Mats. Pay attention to match correctly the type of Mats in the 'srcs' array.
8886
/// </remarks>
87+
[Obsolete("You can use another overloading method for better performance.")]
8988
public static Mat StackingVertically(this Mat[] srcs, int height, int width)
9089
{
9190
MatType matType = srcs[0].Type();
@@ -98,4 +97,22 @@ public static Mat StackingVertically(this Mat[] srcs, int height, int width)
9897
}
9998
return combinedMat;
10099
}
100+
101+
/// <summary>
102+
/// Stacks an array of Mats into a single Mat by placing them vertically.
103+
/// This version of StackingVertically is optimized for speed and requires the Mats to have the same width.
104+
/// </summary>
105+
/// <param name="srcs">An array of Mats that are to be stacked. All Mats must have the same width and type.</param>
106+
/// <returns>Returns a new Mat that is a vertical stack of all input Mats.</returns>
107+
/// <exception cref="OpenCVException">Thrown when the Mats within srcs have differing widths.</exception>
108+
/// <remarks>
109+
/// This method is an optimized version of vertical stacking where it is assumed that all Mats have the same width.
110+
/// It straightly stacks the Mats in the given order, therefore the widths must be consistent to avoid issues.
111+
/// </remarks>
112+
public static Mat StackingVertically(this Mat[] srcs)
113+
{
114+
Mat combinedMat = new();
115+
Cv2.VConcat(srcs, combinedMat);
116+
return combinedMat;
117+
}
101118
}

src/Sdcb.OpenVINO.Extensions.OpenCvSharp4/Sdcb.OpenVINO.Extensions.OpenCvSharp4.csproj

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

3535
<ItemGroup>
3636
<ProjectReference Include="..\Sdcb.OpenVINO\Sdcb.OpenVINO.csproj" Condition="'$(Configuration)' == 'Debug'"/>
37-
<PackageReference Include="Sdcb.OpenVINO" Version="0.5.1" Condition="'$(Configuration)' != 'Debug'" />
37+
<PackageReference Include="Sdcb.OpenVINO" Version="0.6.1" Condition="'$(Configuration)' != 'Debug'" />
3838
</ItemGroup>
3939

4040
</Project>

0 commit comments

Comments
 (0)