[Other] Fix some memory leak problem (#1422)

* Fix memory leak problem for paddleseg model

* Fix bug

* Update postprocessor.cc

---------

Co-authored-by: root <root@bjyz-sys-gpu-kongming3.bjyz.baidu.com>
This commit is contained in:
Jason
2023-02-23 16:03:40 +08:00
committed by GitHub
parent a1f9aa1c5a
commit 0c664fd006
4 changed files with 6 additions and 43 deletions

View File

@@ -21,8 +21,8 @@ void BindPPMatting(pybind11::module& m) {
.def("predict", .def("predict",
[](vision::matting::PPMatting& self, pybind11::array& data) { [](vision::matting::PPMatting& self, pybind11::array& data) {
auto mat = PyArrayToCvMat(data); auto mat = PyArrayToCvMat(data);
vision::MattingResult* res = new vision::MattingResult(); vision::MattingResult res;
self.Predict(&mat, res); self.Predict(&mat, &res);
return res; return res;
}); });
} }

View File

@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/segmentation/ppseg/postprocessor.h" #include "fastdeploy/vision/segmentation/ppseg/postprocessor.h"
#include "fastdeploy/function/cast.h"
#include "yaml-cpp/yaml.h" #include "yaml-cpp/yaml.h"
namespace fastdeploy { namespace fastdeploy {
@@ -153,39 +154,6 @@ bool PaddleSegPostprocessor::ProcessWithLabelResult(const FDTensor& infer_result
return true; return true;
} }
bool PaddleSegPostprocessor::FDTensorCast2Uint8(FDTensor* infer_result,
const int64_t& offset,
std::vector<uint8_t>* uint8_result_buffer) {
FDDataType infer_result_dtype = infer_result->dtype;
if (infer_result_dtype == FDDataType::INT64) {
const int64_t* infer_result_buffer =
reinterpret_cast<const int64_t*>(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<const int32_t*>(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<void*>(uint8_result_buffer->data()));
return true;
}
bool PaddleSegPostprocessor::Run( bool PaddleSegPostprocessor::Run(
const std::vector<FDTensor>& infer_results, const std::vector<FDTensor>& infer_results,
std::vector<SegmentationResult>* results, std::vector<SegmentationResult>* results,
@@ -279,13 +247,12 @@ bool PaddleSegPostprocessor::Run(
} }
FDMat mat; FDMat mat;
std::vector<uint8_t> uint8_result_buffer;
// Resize interpration // Resize interpration
int interpolation = cv::INTER_LINEAR; int interpolation = cv::INTER_LINEAR;
if (is_resized) { if (is_resized) {
if (infer_results_dtype == FDDataType::INT64 || if (infer_results_dtype == FDDataType::INT64 ||
infer_results_dtype == FDDataType::INT32 ){ 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 // label map resize with nearest interpolation
interpolation = cv::INTER_NEAREST; interpolation = cv::INTER_NEAREST;
} }

View File

@@ -80,10 +80,6 @@ class FASTDEPLOY_DECL PaddleSegPostprocessor {
const int64_t& out_num, const int64_t& out_num,
SegmentationResult* result); SegmentationResult* result);
virtual bool FDTensorCast2Uint8(FDTensor* infer_result,
const int64_t& offset,
std::vector<uint8_t>* uint8_result_buffer);
bool is_with_softmax_ = false; bool is_with_softmax_ = false;
bool is_with_argmax_ = true; bool is_with_argmax_ = true;

View File

@@ -28,8 +28,8 @@ void BindPPTracking(pybind11::module &m) {
[](vision::tracking::PPTracking &self, [](vision::tracking::PPTracking &self,
pybind11::array &data) { pybind11::array &data) {
auto mat = PyArrayToCvMat(data); auto mat = PyArrayToCvMat(data);
vision::MOTResult *res = new vision::MOTResult(); vision::MOTResult res;
self.Predict(&mat, res); self.Predict(&mat, &res);
return res; return res;
}) })
.def("bind_recorder", &vision::tracking::PPTracking::BindRecorder) .def("bind_recorder", &vision::tracking::PPTracking::BindRecorder)