From 0c664fd006b6e70fff81c4500a1c405a9b42ef0c Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 23 Feb 2023 16:03:40 +0800 Subject: [PATCH] [Other] Fix some memory leak problem (#1422) * Fix memory leak problem for paddleseg model * Fix bug * Update postprocessor.cc --------- Co-authored-by: root --- .../matting/ppmatting/ppmatting_pybind.cc | 4 +- .../segmentation/ppseg/postprocessor.cc | 37 +------------------ .../vision/segmentation/ppseg/postprocessor.h | 4 -- .../tracking/pptracking/pptracking_pybind.cc | 4 +- 4 files changed, 6 insertions(+), 43 deletions(-) diff --git a/fastdeploy/vision/matting/ppmatting/ppmatting_pybind.cc b/fastdeploy/vision/matting/ppmatting/ppmatting_pybind.cc index 97837fa6f1..a16d36f72c 100644 --- a/fastdeploy/vision/matting/ppmatting/ppmatting_pybind.cc +++ b/fastdeploy/vision/matting/ppmatting/ppmatting_pybind.cc @@ -21,8 +21,8 @@ void BindPPMatting(pybind11::module& m) { .def("predict", [](vision::matting::PPMatting& self, pybind11::array& data) { auto mat = PyArrayToCvMat(data); - vision::MattingResult* res = new vision::MattingResult(); - self.Predict(&mat, res); + vision::MattingResult res; + self.Predict(&mat, &res); return res; }); } diff --git a/fastdeploy/vision/segmentation/ppseg/postprocessor.cc b/fastdeploy/vision/segmentation/ppseg/postprocessor.cc index 364fe286d4..df8dcdba64 100644 --- a/fastdeploy/vision/segmentation/ppseg/postprocessor.cc +++ b/fastdeploy/vision/segmentation/ppseg/postprocessor.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. #include "fastdeploy/vision/segmentation/ppseg/postprocessor.h" +#include "fastdeploy/function/cast.h" #include "yaml-cpp/yaml.h" namespace fastdeploy { @@ -153,39 +154,6 @@ bool PaddleSegPostprocessor::ProcessWithLabelResult(const FDTensor& infer_result return true; } -bool PaddleSegPostprocessor::FDTensorCast2Uint8(FDTensor* infer_result, - const int64_t& offset, - std::vector* uint8_result_buffer) { - FDDataType infer_result_dtype = infer_result->dtype; - if (infer_result_dtype == FDDataType::INT64) { - const int64_t* infer_result_buffer = - reinterpret_cast(infer_result->CpuData()); - // cv::resize don't support `CV_8S` or `CV_32S` - // refer to https://github.com/opencv/opencv/issues/20991 - // https://github.com/opencv/opencv/issues/7862 - uint8_result_buffer->resize(offset * sizeof(int64_t)); - memcpy(uint8_result_buffer->data(), infer_result_buffer, offset * sizeof(int64_t)); - } else if (infer_result_dtype == FDDataType::INT32) { - const int32_t* infer_result_buffer = - reinterpret_cast(infer_result->CpuData()); - // cv::resize don't support `CV_8S` or `CV_32S` - // refer to https://github.com/opencv/opencv/issues/20991 - // https://github.com/opencv/opencv/issues/7862 - uint8_result_buffer->resize(offset * sizeof(int32_t)); - memcpy(uint8_result_buffer->data(), infer_result_buffer, offset * sizeof(int32_t)); - } else { - FDASSERT(false, - "Require the data type for casting uint8 is int64, int32, but now " - "it's %s.", - Str(infer_result_dtype).c_str()); - return false; - } - infer_result->SetExternalData( - infer_result->shape, FDDataType::UINT8, - reinterpret_cast(uint8_result_buffer->data())); - return true; -} - bool PaddleSegPostprocessor::Run( const std::vector& infer_results, std::vector* results, @@ -279,13 +247,12 @@ bool PaddleSegPostprocessor::Run( } FDMat mat; - std::vector uint8_result_buffer; // Resize interpration int interpolation = cv::INTER_LINEAR; if (is_resized) { if (infer_results_dtype == FDDataType::INT64 || infer_results_dtype == FDDataType::INT32 ){ - FDTensorCast2Uint8(&infer_result, infer_chw, &uint8_result_buffer); + function::Cast(infer_result, &infer_result, FDDataType::UINT8); // label map resize with nearest interpolation interpolation = cv::INTER_NEAREST; } diff --git a/fastdeploy/vision/segmentation/ppseg/postprocessor.h b/fastdeploy/vision/segmentation/ppseg/postprocessor.h index 89c8371ee7..966b116450 100644 --- a/fastdeploy/vision/segmentation/ppseg/postprocessor.h +++ b/fastdeploy/vision/segmentation/ppseg/postprocessor.h @@ -80,10 +80,6 @@ class FASTDEPLOY_DECL PaddleSegPostprocessor { const int64_t& out_num, SegmentationResult* result); - virtual bool FDTensorCast2Uint8(FDTensor* infer_result, - const int64_t& offset, - std::vector* uint8_result_buffer); - bool is_with_softmax_ = false; bool is_with_argmax_ = true; diff --git a/fastdeploy/vision/tracking/pptracking/pptracking_pybind.cc b/fastdeploy/vision/tracking/pptracking/pptracking_pybind.cc index a5638628ed..72273e838d 100644 --- a/fastdeploy/vision/tracking/pptracking/pptracking_pybind.cc +++ b/fastdeploy/vision/tracking/pptracking/pptracking_pybind.cc @@ -28,8 +28,8 @@ void BindPPTracking(pybind11::module &m) { [](vision::tracking::PPTracking &self, pybind11::array &data) { auto mat = PyArrayToCvMat(data); - vision::MOTResult *res = new vision::MOTResult(); - self.Predict(&mat, res); + vision::MOTResult res; + self.Predict(&mat, &res); return res; }) .def("bind_recorder", &vision::tracking::PPTracking::BindRecorder)