mmdeploy/csrc/preprocess/cpu/normalize_impl.cpp
lzhangzz 73cf3b5feb
[Fix] Optimize preprocess & fix pontential use-after-free (#229)
* hold async data and wait only at the end of the pipeline

* fix use-after-free bugs

* fix wording

* bypass trivial cases for Pad to avoid ppl.cv's bug

* fix pad

* fix lint

* cleanup

* fix DefaultFormatBundle

* fix all cpu preprocess impl

* suppress log

* fix dynamic library build & add comments for SyncOnScopeExit
2022-03-28 17:29:22 +08:00

45 lines
1.3 KiB
C++

// Copyright (c) OpenMMLab. All rights reserved.
#include "core/registry.h"
#include "core/utils/device_utils.h"
#include "opencv_utils.h"
#include "preprocess/transform/normalize.h"
using namespace std;
namespace mmdeploy {
namespace cpu {
class NormalizeImpl : public ::mmdeploy::NormalizeImpl {
public:
NormalizeImpl(const Value& value) : ::mmdeploy::NormalizeImpl(value){};
~NormalizeImpl() = default;
protected:
Result<Tensor> NormalizeImage(const Tensor& tensor) override {
OUTCOME_TRY(auto src_tensor, MakeAvailableOnDevice(tensor, device_, stream_));
SyncOnScopeExit(stream_, src_tensor.buffer() != tensor.buffer(), src_tensor);
auto mat = Tensor2CVMat(src_tensor);
auto dst_mat = Normalize(mat, arg_.mean, arg_.std, arg_.to_rgb, true);
return CVMat2Tensor(dst_mat);
}
};
class NormalizeImplCreator : public Creator<::mmdeploy::NormalizeImpl> {
public:
const char* GetName() const override { return "cpu"; }
int GetVersion() const override { return 1; }
std::unique_ptr<::mmdeploy::NormalizeImpl> Create(const Value& args) override {
return make_unique<NormalizeImpl>(args);
}
};
} // namespace cpu
} // namespace mmdeploy
using mmdeploy::NormalizeImpl;
using mmdeploy::cpu::NormalizeImplCreator;
REGISTER_MODULE(NormalizeImpl, NormalizeImplCreator);