mmdeploy/csrc/preprocess/cpu/image2tensor_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

49 lines
1.4 KiB
C++

// Copyright (c) OpenMMLab. All rights reserved.
#include "core/utils/device_utils.h"
#include "opencv_utils.h"
#include "preprocess/transform/image2tensor.h"
namespace mmdeploy {
namespace cpu {
class ImageToTensorImpl : public ::mmdeploy::ImageToTensorImpl {
public:
explicit ImageToTensorImpl(const Value& args) : ::mmdeploy::ImageToTensorImpl(args) {}
protected:
Result<Tensor> HWC2CHW(const Tensor& tensor) override {
OUTCOME_TRY(auto src_tensor, MakeAvailableOnDevice(tensor, device_, stream_));
SyncOnScopeExit(stream_, src_tensor.buffer() != tensor.buffer(), src_tensor);
auto shape = src_tensor.shape();
int height = shape[1];
int width = shape[2];
int channels = shape[3];
auto dst_mat = Transpose(Tensor2CVMat(src_tensor));
auto dst_tensor = CVMat2Tensor(dst_mat);
dst_tensor.Reshape({1, channels, height, width});
return dst_tensor;
}
};
class ImageToTensorImplCreator : public Creator<::mmdeploy::ImageToTensorImpl> {
public:
const char* GetName() const override { return "cpu"; }
int GetVersion() const override { return 1; }
ReturnType Create(const Value& args) override {
return std::make_unique<ImageToTensorImpl>(args);
}
};
} // namespace cpu
} // namespace mmdeploy
using mmdeploy::ImageToTensorImpl;
using mmdeploy::cpu::ImageToTensorImplCreator;
REGISTER_MODULE(ImageToTensorImpl, ImageToTensorImplCreator);