Skip to content

Commit

Permalink
try figure out a way....
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Dec 7, 2022
1 parent 1125248 commit 13b76b5
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 96 deletions.
2 changes: 1 addition & 1 deletion build/00-common.linq
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void DotNetRun(string args) => Run("dotnet", args, Encoding.GetEncoding("
static void Run(string exe, string args, Encoding encoding) => Util.Cmd(exe, args, encoding);
static ProjectVersion[] Projects = new[]
{
new ProjectVersion("Sdcb.PaddleInference", "2.4.0-rc.2"),
new ProjectVersion("Sdcb.PaddleInference", "2.4.0-rc.3"),
new ProjectVersion("Sdcb.PaddleOCR", "2.6.0"),
new ProjectVersion("Sdcb.PaddleOCR.Models.Online", "2.5.0"),
new ProjectVersion("Sdcb.PaddleOCR.Models.LocalV3", "2.5.0"),
Expand Down
75 changes: 9 additions & 66 deletions src/Sdcb.PaddleInference/PaddleConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,9 @@ public PaddleConfig()
_ptr = PaddleNative.PD_ConfigCreate();
}

public static readonly PaddleConfigDefaults Defaults = new();

public PaddleConfig(IntPtr configPointer)
{
_ptr = configPointer;

if (!Defaults.EnableGLog)
{
GLogEnabled = Defaults.EnableGLog;
}
}

public static PaddleConfig FromModelDir(string modelDir)
Expand All @@ -52,7 +45,7 @@ public static PaddleConfig FromModelDir(string modelDir)

if (File.Exists(pdmodel) && File.Exists(pdiparams))
{
PaddleConfig c = CreateDefault();
PaddleConfig c = new();
c.SetModel(pdmodel, pdiparams);
return c;
}
Expand All @@ -63,55 +56,18 @@ public static PaddleConfig FromModelDir(string modelDir)

public static PaddleConfig FromModelFiles(string programPath, string paramsPath)
{
PaddleConfig c = CreateDefault();
PaddleConfig c = new();
c.SetModel(programPath, paramsPath);
return c;
}

public static PaddleConfig FromMemoryModel(byte[] programBuffer, byte[] paramsBuffer)
{
PaddleConfig c = CreateDefault();
PaddleConfig c = new();
c.SetMemoryModel(programBuffer, paramsBuffer);
return c;
}

public static PaddleConfig CreateDefault()
{
var c = new PaddleConfig();

if (!Defaults.EnableGLog)
{
c.GLogEnabled = Defaults.EnableGLog;
}
if (Defaults.CpuMathThreadCount != 0)
{
c.CpuMathThreadCount = Defaults.CpuMathThreadCount;
}
if (Defaults.MemoryOptimized)
{
c.MemoryOptimized = Defaults.MemoryOptimized;
}
if (Defaults.ProfileEnabled)
{
c.ProfileEnabled = Defaults.ProfileEnabled;
}
if (Defaults.UseGpu)
{
c.EnableUseGpu(Defaults.InitialGpuMemoryMb, Defaults.GpuDeviceId);
if (Defaults.EnableGpuMultiStream)
{
c.EnableGpuMultiStream = Defaults.EnableGpuMultiStream;
}
}
else if (Defaults.UseMkldnn)
{
c.MkldnnEnabled = true;
c.MkldnnCacheCapacity = Defaults.MkldnnCacheCapacity;
}

return c;
}

public static readonly Encoding PaddleEncoding;

static PaddleConfig()
Expand Down Expand Up @@ -189,7 +145,7 @@ private static void SearchPathLoad()

public IntPtr UnsafeGetHandle() => _ptr;

/// <summary>A boolean state telling whether logs in Paddle inference are muted.</summary>
/// <summary>A boolean state telling whether logs in Paddle inference are enabled.</summary>
public bool GLogEnabled
{
get => PaddleNative.PD_ConfigGlogInfoDisabled(_ptr) == 0;
Expand Down Expand Up @@ -465,25 +421,12 @@ public void Dispose()
_ptr = IntPtr.Zero;
}
}
}

public class PaddleConfigDefaults
{
public bool EnableGLog { get; set; } = false;

public bool UseMkldnn { get; set; } = true;
public int MkldnnCacheCapacity { get; set; } = 10;

public int CpuMathThreadCount { get; set; } = 0;

public bool MemoryOptimized { get; set; } = true;

public bool ProfileEnabled { get; set; } = false;

public bool UseGpu { get; set; } = false;
public int InitialGpuMemoryMb { get; set; } = 500;
public int GpuDeviceId { get; set; } = 0;
public bool EnableGpuMultiStream { get; set; } = false;
public PaddleConfig Apply(Action<PaddleConfig> configure)
{
configure(this);
return this;
}
}

file class PtrFromStringArray : IDisposable
Expand Down
19 changes: 19 additions & 0 deletions src/Sdcb.PaddleInference/PaddleConfigure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Sdcb.PaddleInference
{
public static class PaddleConfigure
{
public static void EnableMkldnn(this PaddleConfig c)
{
c.MkldnnEnabled = true;
c.MkldnnCacheCapacity = 10;
c.MemoryOptimized = true;
}

public static void EnableGpu(this PaddleConfig c, int initialMemoryMB = 500, int deviceId = 0)
{
c.EnableUseGpu(initialMemoryMB, deviceId);
c.EnableGpuMultiStream = true;
c.MemoryOptimized = true;
}
}
}
28 changes: 28 additions & 0 deletions src/Sdcb.PaddleOCR/Models/DetectionModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using Sdcb.PaddleInference;
using Sdcb.PaddleInference.TensorRt;
using Sdcb.PaddleOCR.Models.Details;
using System;
using System.Collections.Generic;
using System.IO;

namespace Sdcb.PaddleOCR.Models
{
Expand All @@ -8,5 +12,29 @@ public abstract class DetectionModel
public abstract PaddleConfig CreateConfig();

public static DetectionModel FromDirectory(string directoryPath) => new FileDetectionModel(directoryPath);

public static void EnableTensorRt(PaddleConfig cfg, string? cacheDir = null)
{
if (cacheDir != null)
{
cfg.SetOptimCacheDir(cacheDir);
}
cfg.EnableTensorRtEngine(1 << 20, 1, 20, 0, useStatic: true, useCalibMode: false);
cfg.SetTrtDynamicShapeInfo(new Dictionary<string, TrtShapeGroup>
{
["x"] = new(new(1, 3, 50, 50), new(1, 3, 1536, 1536), new(1, 3, 640, 640)),
["conv2d_92.tmp_0"] = new(new(1, 120, 20, 20), new(1, 120, 400, 400), new(1, 120, 160, 160)),
["conv2d_91.tmp_0"] = new(new(1, 24, 10, 10), new(1, 24, 200, 200), new(1, 24, 80, 80)),
["conv2d_59.tmp_0"] = new(new(1, 96, 20, 20), new(1, 96, 400, 400), new(1, 96, 160, 160)),
["nearest_interp_v2_1.tmp_0"] = new(new(1, 256, 10, 10), new(1, 256, 200, 200), new(1, 256, 80, 80)),
["nearest_interp_v2_2.tmp_0"] = new(new(1, 256, 20, 20), new(1, 256, 400, 400), new(1, 256, 160, 160)),
["conv2d_124.tmp_0"] = new(new(1, 256, 20, 20), new(1, 256, 400, 400), new(1, 256, 160, 160)),
["nearest_interp_v2_3.tmp_0"] = new(new(1, 64, 20, 20), new(1, 64, 400, 400), new(1, 64, 160, 160)),
["nearest_interp_v2_4.tmp_0"] = new(new(1, 64, 20, 20), new(1, 64, 400, 400), new(1, 64, 160, 160)),
["nearest_interp_v2_5.tmp_0"] = new(new(1, 64, 20, 20), new(1, 64, 400, 400), new(1, 64, 160, 160)),
["elementwise_add_7"] = new(new(1, 56, 2, 2), new(1, 56, 400, 400), new(1, 56, 40, 40)),
["nearest_interp_v2_0.tmp_0"] = new(new(1, 256, 2, 2), new(1, 256, 400, 400), new(1, 256, 40, 40)),
});
}
}
}
13 changes: 0 additions & 13 deletions src/Sdcb.PaddleOCR/Models/FullOcrModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,4 @@ public static FullOcrModel FromDirectory(string detectionModelDir, string classi
RecognizationModel.FromDirectory(recognitionModelDir, labelFilePath, version));
}
}
}

namespace System.Runtime.CompilerServices
{
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// This dummy class is required to compile records when targeting .NET Standard
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class IsExternalInit
{
}
}
22 changes: 14 additions & 8 deletions src/Sdcb.PaddleOCR/PaddleOcrAll.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR.Models;
using System;
using System.Linq;
Expand All @@ -14,25 +15,30 @@ public class PaddleOcrAll : IDisposable
public bool Enable180Classification { get; set; } = false;
public bool AllowRotateDetection { get; set; } = true;

public PaddleOcrAll(FullOcrModel model)
[Obsolete("use PaddleOcrAll(FullOcrModel model, Action<PaddleConfig> configure)")]
public PaddleOcrAll(FullOcrModel model) : this(model, PaddleConfigure.EnableMkldnn)
{
Detector = new PaddleOcrDetector(model.DetectionModel);
}

public PaddleOcrAll(FullOcrModel model, Action<PaddleConfig> configure)
{
Detector = new PaddleOcrDetector(model.DetectionModel, configure);
if (model.ClassificationModel != null)
{
Classifier = new PaddleOcrClassifier(model.ClassificationModel);
Classifier = new PaddleOcrClassifier(model.ClassificationModel, configure);
}
Recognizer = new PaddleOcrRecognizer(model.RecognizationModel);
Recognizer = new PaddleOcrRecognizer(model.RecognizationModel, configure);
}

[Obsolete("use PaddleOcrAll(PaddleOcrDetector detector, PaddleOcrClassifier? classifier, PaddleOcrRecognizer recognizer)")]
public PaddleOcrAll(string modelPath, string labelFilePath, ModelVersion version)
: this(FullOcrModel.FromDirectory(modelPath, labelFilePath, version))
public PaddleOcrAll(string modelPath, string labelFilePath, ModelVersion version, Action<PaddleConfig> configure)
: this(FullOcrModel.FromDirectory(modelPath, labelFilePath, version), configure)
{
}

[Obsolete("use PaddleOcrAll(PaddleOcrDetector detector, PaddleOcrClassifier? classifier, PaddleOcrRecognizer recognizer)")]
public PaddleOcrAll(string detectionModelDir, string classificationModelDir, string recognitionModelDir, string labelFilePath, ModelVersion version)
: this(FullOcrModel.FromDirectory(detectionModelDir, classificationModelDir, recognitionModelDir, labelFilePath, version))
public PaddleOcrAll(string detectionModelDir, string classificationModelDir, string recognitionModelDir, string labelFilePath, ModelVersion version, Action<PaddleConfig> configure)
: this(FullOcrModel.FromDirectory(detectionModelDir, classificationModelDir, recognitionModelDir, labelFilePath, version), configure)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sdcb.PaddleOCR/PaddleOcrClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class PaddleOcrClassifier : IDisposable

public OcrShape Shape { get; init; } = ClassificationModel.DefaultShape;

public PaddleOcrClassifier(ClassificationModel model) : this(model.CreateConfig().CreatePredictor())
public PaddleOcrClassifier(ClassificationModel model, Action<PaddleConfig> configure) : this(model.CreateConfig().Apply(configure).CreatePredictor())
{
Shape = model.Shape;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sdcb.PaddleOCR/PaddleOcrDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class PaddleOcrDetector : IDisposable
{
readonly PaddlePredictor _p;

public int? MaxSize { get; set; } = 2048;
public int? MaxSize { get; set; } = 1536;
public int? DilatedSize { get; set; } = 2;
public float? BoxScoreThreahold { get; set; } = 0.7f;
public float? BoxThreshold { get; set; } = 0.3f;
public int MinSize { get; set; } = 3;
public float UnclipRatio { get; set; } = 2.0f;

public PaddleOcrDetector(DetectionModel model) : this(model.CreateConfig())
public PaddleOcrDetector(DetectionModel model, Action<PaddleConfig> configure) : this(model.CreateConfig().Apply(configure))
{
}

Expand Down
12 changes: 9 additions & 3 deletions src/Sdcb.PaddleOCR/PaddleOcrRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ public class PaddleOcrRecognizer : IDisposable

public RecognizationModel Model { get; init; }

public PaddleOcrRecognizer(RecognizationModel model)
public PaddleOcrRecognizer(RecognizationModel model, Action<PaddleConfig> configure)
{
Model = model;
_p = Model.CreateConfig().CreatePredictor();
_p = Model.CreateConfig().Apply(configure).CreatePredictor();
}

public PaddleOcrRecognizer Clone() => new PaddleOcrRecognizer(Model);
public PaddleOcrRecognizer(RecognizationModel model, PaddlePredictor predictor)
{
Model = model;
_p = predictor;
}

public PaddleOcrRecognizer Clone() => new PaddleOcrRecognizer(Model, _p.Clone());

public void Dispose() => _p.Dispose();

Expand Down
4 changes: 2 additions & 2 deletions src/Sdcb.PaddleOCR/Sdcb.PaddleOCR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<LangVersion>10</LangVersion>
<LangVersion>11</LangVersion>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/sdcb/PaddleSharp</PackageProjectUrl>
<RepositoryUrl>https://github.com/sdcb/PaddleSharp.git</RepositoryUrl>
Expand All @@ -24,7 +24,7 @@

<ItemGroup>
<PackageReference Include="OpenCvSharp4" Version="4.5.5.20211231" />
<PackageReference Include="Sdcb.PaddleInference" Version="2.3.2" />
<PackageReference Include="Sdcb.PaddleInference" Version="2.4.0-rc.3" />
</ItemGroup>

</Project>

0 comments on commit 13b76b5

Please sign in to comment.