From af04f4f97b48be9349f576906c4d249f70e72909 Mon Sep 17 00:00:00 2001 From: sdcb Date: Sun, 16 Jul 2023 15:14:40 +0800 Subject: [PATCH] Fix a finalized-lazy memory leak issue --- build/00-common.linq | 2 +- src/Sdcb.PaddleOCR/PaddleOcrRecognizer.cs | 48 ++++++++++++++--------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/build/00-common.linq b/build/00-common.linq index 6741c5c..f5b55f8 100644 --- a/build/00-common.linq +++ b/build/00-common.linq @@ -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"), diff --git a/src/Sdcb.PaddleOCR/PaddleOcrRecognizer.cs b/src/Sdcb.PaddleOCR/PaddleOcrRecognizer.cs index 721406a..1908956 100644 --- a/src/Sdcb.PaddleOCR/PaddleOcrRecognizer.cs +++ b/src/Sdcb.PaddleOCR/PaddleOcrRecognizer.cs @@ -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.");