Skip to content

Commit

Permalink
[Other] Fix some memory leak problem (PaddlePaddle#1422)
Browse files Browse the repository at this point in the history
* Fix memory leak problem for paddleseg model

* Fix bug

* Update postprocessor.cc

---------

Co-authored-by: root <[email protected]>
  • Loading branch information
jiangjiajun and root authored Feb 23, 2023
1 parent a1f9aa1 commit 0c664fd
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 43 deletions.
4 changes: 2 additions & 2 deletions fastdeploy/vision/matting/ppmatting/ppmatting_pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand Down
37 changes: 2 additions & 35 deletions fastdeploy/vision/segmentation/ppseg/postprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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_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(
const std::vector<FDTensor>& infer_results,
std::vector<SegmentationResult>* results,
Expand Down Expand Up @@ -279,13 +247,12 @@ bool PaddleSegPostprocessor::Run(
}

FDMat mat;
std::vector<uint8_t> 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;
}
Expand Down
4 changes: 0 additions & 4 deletions fastdeploy/vision/segmentation/ppseg/postprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_t>* uint8_result_buffer);

bool is_with_softmax_ = false;

bool is_with_argmax_ = true;
Expand Down
4 changes: 2 additions & 2 deletions fastdeploy/vision/tracking/pptracking/pptracking_pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0c664fd

Please sign in to comment.