Skip to content

Commit

Permalink
Fix a finalized-lazy memory leak issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcb committed Jul 16, 2023
1 parent 9d20471 commit af04f4f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build/00-common.linq
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static ProjectVersion[] Projects = new[]
new ProjectVersion("Sdcb.Mkldnn", "0.19-preview.1"), // 0.19
new ProjectVersion("Sdcb.Paddle2Onnx", "1.0.0-preview.2"), // 1.0.0-rc.2
new ProjectVersion("Sdcb.PaddleInference", "2.5.0-preview.3"),
new ProjectVersion("Sdcb.PaddleOCR", "2.6.0.6-preview.4"),
new ProjectVersion("Sdcb.PaddleOCR", "2.6.0.6-preview.5"),
new ProjectVersion("Sdcb.PaddleOCR.Models.Online", "2.6.0.6-preview.4"),
new ProjectVersion("Sdcb.PaddleOCR.Models.LocalV3", "2.6.0.6-preview.4"),
new ProjectVersion("Sdcb.PaddleDetection", "2.3.1"),
Expand Down
48 changes: 30 additions & 18 deletions src/Sdcb.PaddleOCR/PaddleOcrRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,40 @@ private PaddleOcrRecognizerResult[] RunMulti(Mat[] srcs)
return 1.0 * size.Width / size.Height * modelHeight;
}));

Mat[] normalizeds = srcs
.Select(src =>
{
using Mat channel3 = src.Channels() switch
Mat[] normalizeds = null!;
try
{
normalizeds = srcs
.Select(src =>
{
4 => src.CvtColor(ColorConversionCodes.RGBA2BGR),
1 => src.CvtColor(ColorConversionCodes.GRAY2RGB),
3 => src.Clone(),
var x => throw new Exception($"Unexpect src channel: {x}, allow: (1/3/4)")
};
using Mat resized = ResizePadding(channel3, modelHeight, maxWidth);
return Normalize(resized);
})
.ToArray();
using Mat channel3 = src.Channels() switch
{
4 => src.CvtColor(ColorConversionCodes.RGBA2BGR),
1 => src.CvtColor(ColorConversionCodes.GRAY2RGB),
3 => src.Clone(),
var x => throw new Exception($"Unexpect src channel: {x}, allow: (1/3/4)")
};
using Mat resized = ResizePadding(channel3, modelHeight, maxWidth);
return Normalize(resized);
})
.ToArray();

using (PaddleTensor input = _p.GetInputTensor(_p.InputNames[0]))
using (PaddleTensor input = _p.GetInputTensor(_p.InputNames[0]))
{
int channel = normalizeds[0].Channels();
input.Shape = new[] { normalizeds.Length, channel, modelHeight, maxWidth };
float[] data = ExtractMat(normalizeds, channel, modelHeight, maxWidth);
input.SetData(data);
}
}
finally
{
int channel = normalizeds[0].Channels();
input.Shape = new[] { normalizeds.Length, channel, modelHeight, maxWidth };
float[] data = ExtractMat(normalizeds, channel, modelHeight, maxWidth);
input.SetData(data);
foreach (Mat normalized in normalizeds)
{
normalized.Dispose();
}
}

if (!_p.Run())
{
throw new Exception($"PaddlePredictor(Recognizer) run failed.");
Expand Down

0 comments on commit af04f4f

Please sign in to comment.