Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions csrcs/fastdeploy/pybind/main.cc.in
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pybind11::dtype FDDataTypeToNumpyDataType(const FDDataType& fd_dtype) {
dt = pybind11::dtype::of<float>();
} else if (fd_dtype == FDDataType::FP64) {
dt = pybind11::dtype::of<double>();
} else if (fd_dtype == FDDataType::UINT8) {
dt = pybind11::dtype::of<uint8_t>();
} else {
FDASSERT(false, "The function doesn't support data type of " +
Str(fd_dtype) + ".");
Expand All @@ -46,6 +48,8 @@ FDDataType NumpyDataTypeToFDDataType(const pybind11::dtype& np_dtype) {
return FDDataType::FP32;
} else if (np_dtype.is(pybind11::dtype::of<double>())) {
return FDDataType::FP64;
} else if (np_dtype.is(pybind11::dtype::of<uint8_t>())) {
return FDDataType::UINT8;
}
FDASSERT(false,
"NumpyDataTypeToFDDataType() only support "
Expand All @@ -66,6 +70,13 @@ void PyArrayToTensor(pybind11::array& pyarray, FDTensor* tensor,
}
}

pybind11::array TensorToPyArray(const FDTensor& tensor) {
auto numpy_dtype = FDDataTypeToNumpyDataType(tensor.dtype);
auto out = pybind11::array(numpy_dtype, tensor.shape);
memcpy(out.mutable_data(), tensor.Data(), tensor.Numel() * FDDataTypeSize(tensor.dtype));
return out;
}

#ifdef ENABLE_VISION
int NumpyDataTypeToOpenCvType(const pybind11::dtype& np_dtype) {
if (np_dtype.is(pybind11::dtype::of<int32_t>())) {
Expand Down
12 changes: 7 additions & 5 deletions csrcs/fastdeploy/pybind/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ FDDataType NumpyDataTypeToFDDataType(const pybind11::dtype& np_dtype);

void PyArrayToTensor(pybind11::array& pyarray, FDTensor* tensor,
bool share_buffer = false);
pybind11::array TensorToPyArray(const FDTensor& tensor);

#ifdef ENABLE_VISION
cv::Mat PyArrayToCvMat(pybind11::array& pyarray);
#endif

template <typename T> FDDataType CTypeToFDDataType() {
template <typename T>
FDDataType CTypeToFDDataType() {
if (std::is_same<T, int32_t>::value) {
return FDDataType::INT32;
} else if (std::is_same<T, int64_t>::value) {
Expand All @@ -57,9 +59,9 @@ template <typename T> FDDataType CTypeToFDDataType() {
}

template <typename T>
std::vector<pybind11::array>
PyBackendInfer(T& self, const std::vector<std::string>& names,
std::vector<pybind11::array>& data) {
std::vector<pybind11::array> PyBackendInfer(
T& self, const std::vector<std::string>& names,
std::vector<pybind11::array>& data) {
std::vector<FDTensor> inputs(data.size());
for (size_t i = 0; i < data.size(); ++i) {
// TODO(jiangjiajun) here is considered to use user memory directly
Expand All @@ -85,4 +87,4 @@ PyBackendInfer(T& self, const std::vector<std::string>& names,
return results;
}

} // namespace fastdeploy
} // namespace fastdeploy
6 changes: 3 additions & 3 deletions csrcs/fastdeploy/vision.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include "fastdeploy/vision/deepinsight/partial_fc.h"
#include "fastdeploy/vision/deepinsight/scrfd.h"
#include "fastdeploy/vision/deepinsight/vpl.h"
#include "fastdeploy/vision/detection/contrib/scaledyolov4.h"
#include "fastdeploy/vision/detection/contrib/yolor.h"
#include "fastdeploy/vision/detection/contrib/yolov7.h"
#include "fastdeploy/vision/linzaer/ultraface.h"
#include "fastdeploy/vision/megvii/yolox.h"
#include "fastdeploy/vision/meituan/yolov6.h"
Expand All @@ -32,9 +35,6 @@
#include "fastdeploy/vision/ppseg/model.h"
#include "fastdeploy/vision/rangilyu/nanodet_plus.h"
#include "fastdeploy/vision/ultralytics/yolov5.h"
#include "fastdeploy/vision/wongkinyiu/scaledyolov4.h"
#include "fastdeploy/vision/wongkinyiu/yolor.h"
#include "fastdeploy/vision/wongkinyiu/yolov7.h"
#include "fastdeploy/vision/zhkkke/modnet.h"
#endif

Expand Down
6 changes: 3 additions & 3 deletions csrcs/fastdeploy/vision/common/processors/mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace vision {

enum Layout { HWC, CHW };

struct Mat {
struct FASTDEPLOY_DECL Mat {
explicit Mat(cv::Mat& mat) {
cpu_mat = mat;
device = Device::CPU;
Expand Down Expand Up @@ -76,5 +76,5 @@ struct Mat {
Device device = Device::CPU;
};

} // namespace vision
} // namespace fastdeploy
} // namespace vision
} // namespace fastdeploy
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "fastdeploy/vision/wongkinyiu/scaledyolov4.h"
#include "fastdeploy/vision/detection/contrib/scaledyolov4.h"
#include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h"

namespace fastdeploy {
namespace vision {
namespace wongkinyiu {
namespace detection {

void ScaledYOLOv4::LetterBox(Mat* mat, const std::vector<int>& size,
const std::vector<float>& color, bool _auto,
Expand Down Expand Up @@ -65,8 +65,8 @@ ScaledYOLOv4::ScaledYOLOv4(const std::string& model_file,
valid_cpu_backends = {Backend::ORT}; // 指定可用的CPU后端
valid_gpu_backends = {Backend::ORT, Backend::TRT}; // 指定可用的GPU后端
} else {
valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
valid_cpu_backends = {Backend::PDINFER};
valid_gpu_backends = {Backend::PDINFER};
}
runtime_option = custom_option;
runtime_option.model_format = model_format;
Expand Down Expand Up @@ -219,10 +219,6 @@ bool ScaledYOLOv4::Postprocess(

bool ScaledYOLOv4::Predict(cv::Mat* im, DetectionResult* result,
float conf_threshold, float nms_iou_threshold) {
#ifdef FASTDEPLOY_DEBUG
TIMERECORD_START(0)
#endif

Mat mat(*im);
std::vector<FDTensor> input_tensors(1);

Expand All @@ -239,34 +235,21 @@ bool ScaledYOLOv4::Predict(cv::Mat* im, DetectionResult* result,
return false;
}

#ifdef FASTDEPLOY_DEBUG
TIMERECORD_END(0, "Preprocess")
TIMERECORD_START(1)
#endif

input_tensors[0].name = InputInfoOfRuntime(0).name;
std::vector<FDTensor> output_tensors;
if (!Infer(input_tensors, &output_tensors)) {
FDERROR << "Failed to inference." << std::endl;
return false;
}
#ifdef FASTDEPLOY_DEBUG
TIMERECORD_END(1, "Inference")
TIMERECORD_START(2)
#endif

if (!Postprocess(output_tensors[0], result, im_info, conf_threshold,
nms_iou_threshold)) {
FDERROR << "Failed to post process." << std::endl;
return false;
}

#ifdef FASTDEPLOY_DEBUG
TIMERECORD_END(2, "Postprocess")
#endif
return true;
}

} // namespace wongkinyiu
} // namespace detection
} // namespace vision
} // namespace fastdeploy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace fastdeploy {
namespace vision {
namespace wongkinyiu {
namespace detection {

class FASTDEPLOY_DECL ScaledYOLOv4 : public FastDeployModel {
public:
Expand All @@ -31,7 +31,7 @@ class FASTDEPLOY_DECL ScaledYOLOv4 : public FastDeployModel {
const Frontend& model_format = Frontend::ONNX);

// 定义模型的名称
virtual std::string ModelName() const { return "WongKinYiu/ScaledYOLOv4"; }
virtual std::string ModelName() const { return "ScaledYOLOv4"; }

// 模型预测接口,即用户调用的接口
// im 为用户的输入数据,目前对于CV均定义为cv::Mat
Expand Down Expand Up @@ -98,6 +98,6 @@ class FASTDEPLOY_DECL ScaledYOLOv4 : public FastDeployModel {
// auto check by fastdeploy after the internal Runtime already initialized.
bool is_dynamic_input_;
};
} // namespace wongkinyiu
} // namespace detection
} // namespace vision
} // namespace fastdeploy
41 changes: 41 additions & 0 deletions csrcs/fastdeploy/vision/detection/contrib/scaledyolov4_pybind.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "fastdeploy/pybind/main.h"

namespace fastdeploy {
void BindScaledYOLOv4(pybind11::module& m) {
pybind11::class_<vision::detection::ScaledYOLOv4, FastDeployModel>(
m, "ScaledYOLOv4")
.def(pybind11::init<std::string, std::string, RuntimeOption, Frontend>())
.def("predict",
[](vision::detection::ScaledYOLOv4& self, pybind11::array& data,
float conf_threshold, float nms_iou_threshold) {
auto mat = PyArrayToCvMat(data);
vision::DetectionResult res;
self.Predict(&mat, &res, conf_threshold, nms_iou_threshold);
return res;
})
.def_readwrite("size", &vision::detection::ScaledYOLOv4::size)
.def_readwrite("padding_value",
&vision::detection::ScaledYOLOv4::padding_value)
.def_readwrite("is_mini_pad",
&vision::detection::ScaledYOLOv4::is_mini_pad)
.def_readwrite("is_no_pad", &vision::detection::ScaledYOLOv4::is_no_pad)
.def_readwrite("is_scale_up",
&vision::detection::ScaledYOLOv4::is_scale_up)
.def_readwrite("stride", &vision::detection::ScaledYOLOv4::stride)
.def_readwrite("max_wh", &vision::detection::ScaledYOLOv4::max_wh);
}
} // namespace fastdeploy
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "fastdeploy/vision/wongkinyiu/yolor.h"
#include "fastdeploy/vision/detection/contrib/yolor.h"
#include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h"

namespace fastdeploy {
namespace vision {
namespace wongkinyiu {
namespace detection {

void YOLOR::LetterBox(Mat* mat, const std::vector<int>& size,
const std::vector<float>& color, bool _auto,
Expand Down Expand Up @@ -63,8 +63,8 @@ YOLOR::YOLOR(const std::string& model_file, const std::string& params_file,
valid_cpu_backends = {Backend::ORT}; // 指定可用的CPU后端
valid_gpu_backends = {Backend::ORT, Backend::TRT}; // 指定可用的GPU后端
} else {
valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
valid_cpu_backends = {Backend::PDINFER};
valid_gpu_backends = {Backend::PDINFER};
}
runtime_option = custom_option;
runtime_option.model_format = model_format;
Expand Down Expand Up @@ -216,10 +216,6 @@ bool YOLOR::Postprocess(

bool YOLOR::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold,
float nms_iou_threshold) {
#ifdef FASTDEPLOY_DEBUG
TIMERECORD_START(0)
#endif

Mat mat(*im);
std::vector<FDTensor> input_tensors(1);

Expand All @@ -236,34 +232,22 @@ bool YOLOR::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold,
return false;
}

#ifdef FASTDEPLOY_DEBUG
TIMERECORD_END(0, "Preprocess")
TIMERECORD_START(1)
#endif

input_tensors[0].name = InputInfoOfRuntime(0).name;
std::vector<FDTensor> output_tensors;
if (!Infer(input_tensors, &output_tensors)) {
FDERROR << "Failed to inference." << std::endl;
return false;
}
#ifdef FASTDEPLOY_DEBUG
TIMERECORD_END(1, "Inference")
TIMERECORD_START(2)
#endif

if (!Postprocess(output_tensors[0], result, im_info, conf_threshold,
nms_iou_threshold)) {
FDERROR << "Failed to post process." << std::endl;
return false;
}

#ifdef FASTDEPLOY_DEBUG
TIMERECORD_END(2, "Postprocess")
#endif
return true;
}

} // namespace wongkinyiu
} // namespace detection
} // namespace vision
} // namespace fastdeploy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace fastdeploy {
namespace vision {
namespace wongkinyiu {
namespace detection {

class FASTDEPLOY_DECL YOLOR : public FastDeployModel {
public:
Expand All @@ -30,7 +30,7 @@ class FASTDEPLOY_DECL YOLOR : public FastDeployModel {
const Frontend& model_format = Frontend::ONNX);

// 定义模型的名称
virtual std::string ModelName() const { return "WongKinYiu/yolor"; }
virtual std::string ModelName() const { return "YOLOR"; }

// 模型预测接口,即用户调用的接口
// im 为用户的输入数据,目前对于CV均定义为cv::Mat
Expand Down Expand Up @@ -97,6 +97,6 @@ class FASTDEPLOY_DECL YOLOR : public FastDeployModel {
// auto check by fastdeploy after the internal Runtime already initialized.
bool is_dynamic_input_;
};
} // namespace wongkinyiu
} // namespace detection
} // namespace vision
} // namespace fastdeploy
37 changes: 37 additions & 0 deletions csrcs/fastdeploy/vision/detection/contrib/yolor_pybind.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "fastdeploy/pybind/main.h"

namespace fastdeploy {
void BindYOLOR(pybind11::module& m) {
pybind11::class_<vision::detection::YOLOR, FastDeployModel>(m, "YOLOR")
.def(pybind11::init<std::string, std::string, RuntimeOption, Frontend>())
.def("predict",
[](vision::detection::YOLOR& self, pybind11::array& data,
float conf_threshold, float nms_iou_threshold) {
auto mat = PyArrayToCvMat(data);
vision::DetectionResult res;
self.Predict(&mat, &res, conf_threshold, nms_iou_threshold);
return res;
})
.def_readwrite("size", &vision::detection::YOLOR::size)
.def_readwrite("padding_value", &vision::detection::YOLOR::padding_value)
.def_readwrite("is_mini_pad", &vision::detection::YOLOR::is_mini_pad)
.def_readwrite("is_no_pad", &vision::detection::YOLOR::is_no_pad)
.def_readwrite("is_scale_up", &vision::detection::YOLOR::is_scale_up)
.def_readwrite("stride", &vision::detection::YOLOR::stride)
.def_readwrite("max_wh", &vision::detection::YOLOR::max_wh);
}
} // namespace fastdeploy
Loading