mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-13 12:23:55 +08:00

* 10-29/14:05 * 新增cmake * 新增rknpu2 backend * 10-29/14:43 * Runtime fd_type新增RKNPU代码 * 10-29/15:02 * 新增ppseg RKNPU2推理代码 * 10-29/15:46 * 新增ppseg RKNPU2 cpp example代码 * 10-29/15:51 * 新增README文档 * 10-29/15:51 * 按照要求修改部分注释以及变量名称 * 10-29/15:51 * 修复重命名之后,cc文件中的部分代码还用旧函数名的bug * 10-29/22:32 * str(Device::NPU)将输出NPU而不是UNKOWN * 修改runtime文件中的注释格式 * 新增Building Summary ENABLE_RKNPU2_BACKEND输出 * pybind新增支持rknpu2 * 新增python编译选项 * 新增PPSeg Python代码 * 新增以及更新各种文档 * 10-30/14:11 * 尝试修复编译cuda时产生的错误 * 10-30/19:27 * 修改CpuName和CoreMask层级 * 修改ppseg rknn推理层级 * 图片将移动到网络进行下载 * 10-30/19:39 * 更新文档 * 10-30/19:39 * 更新文档 * 更新ppseg rknpu2 example中的函数命名方式 * 更新ppseg rknpu2 example为一个cc文件 * 修复disable_normalize_and_permute部分的逻辑错误 * 移除rknpu2初始化时的无用参数 * 10-30/19:39 * 尝试重置python代码 * 10-30/10:16 * rknpu2_config.h文件不再包含rknn_api头文件防止出现导入错误的问题 * 10-31/14:31 * 修改pybind,支持最新的rknpu2 backends * 再次支持ppseg python推理 * 移动cpuname 和 coremask的层级 * 10-31/15:35 * 尝试修复rknpu2导入错误 * 10-31/19:00 * 新增RKNPU2模型导出代码以及其对应的文档 * 更新大量文档错误 * 10-31/19:00 * 现在编译完fastdeploy仓库后无需重新设置RKNN2_TARGET_SOC * 10-31/19:26 * 修改部分错误文档 * 10-31/19:26 * 修复错误删除的部分 * 修复各种错误文档 * 修复FastDeploy.cmake在设置RKNN2_TARGET_SOC错误时,提示错误的信息 * 修复rknpu2_backend.cc中存在的中文注释 * 10-31/20:45 * 删除无用的注释 * 10-31/20:45 * 按照要求修改Device::NPU为Device::RKNPU,硬件将共用valid_hardware_backends * 删除无用注释以及debug代码 * 11-01/09:45 * 更新变量命名方式 * 11-01/10:16 * 修改部分文档,修改函数命名方式 Co-authored-by: Jason <jiangjiajun@baidu.com>
173 lines
5.8 KiB
C++
173 lines
5.8 KiB
C++
// 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 BindRuntime(pybind11::module&);
|
|
void BindFDModel(pybind11::module&);
|
|
void BindVision(pybind11::module&);
|
|
void BindText(pybind11::module&);
|
|
void BindPipeline(pybind11::module&);
|
|
|
|
pybind11::dtype FDDataTypeToNumpyDataType(const FDDataType& fd_dtype) {
|
|
pybind11::dtype dt;
|
|
if (fd_dtype == FDDataType::INT32) {
|
|
dt = pybind11::dtype::of<int32_t>();
|
|
} else if (fd_dtype == FDDataType::INT64) {
|
|
dt = pybind11::dtype::of<int64_t>();
|
|
} else if (fd_dtype == FDDataType::FP32) {
|
|
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 if (fd_dtype == FDDataType::FP16) {
|
|
dt = pybind11::dtype::of<float16>();
|
|
} else {
|
|
FDASSERT(false, "The function doesn't support data type of %s.",
|
|
Str(fd_dtype).c_str());
|
|
}
|
|
return dt;
|
|
}
|
|
|
|
FDDataType NumpyDataTypeToFDDataType(const pybind11::dtype& np_dtype) {
|
|
if (np_dtype.is(pybind11::dtype::of<int32_t>())) {
|
|
return FDDataType::INT32;
|
|
} else if (np_dtype.is(pybind11::dtype::of<int64_t>())) {
|
|
return FDDataType::INT64;
|
|
} else if (np_dtype.is(pybind11::dtype::of<float>())) {
|
|
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;
|
|
} else if (np_dtype.is(pybind11::dtype::of<float16>())) {
|
|
return FDDataType::FP16;
|
|
}
|
|
FDASSERT(false,
|
|
"NumpyDataTypeToFDDataType() only support "
|
|
"int32/int64/float32/float64/float16 now.");
|
|
return FDDataType::FP32;
|
|
}
|
|
|
|
void PyArrayToTensor(pybind11::array& pyarray, FDTensor* tensor,
|
|
bool share_buffer) {
|
|
auto dtype = NumpyDataTypeToFDDataType(pyarray.dtype());
|
|
std::vector<int64_t> data_shape;
|
|
data_shape.insert(data_shape.begin(), pyarray.shape(),
|
|
pyarray.shape() + pyarray.ndim());
|
|
if (share_buffer) {
|
|
tensor-> SetExternalData(data_shape, dtype,
|
|
pyarray.mutable_data());
|
|
} else {
|
|
tensor->Resize(data_shape, dtype);
|
|
memcpy(tensor->MutableData(), pyarray.mutable_data(), pyarray.nbytes());
|
|
}
|
|
}
|
|
|
|
void PyArrayToTensorList(std::vector<pybind11::array>& pyarrays, std::vector<FDTensor>* tensors,
|
|
bool share_buffer) {
|
|
for(auto i = 0; i < pyarrays.size(); ++i) {
|
|
PyArrayToTensor(pyarrays[i], &(*tensors)[i], share_buffer);
|
|
}
|
|
}
|
|
|
|
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>())) {
|
|
return CV_32S;
|
|
} else if (np_dtype.is(pybind11::dtype::of<int8_t>())) {
|
|
return CV_8S;
|
|
} else if (np_dtype.is(pybind11::dtype::of<uint8_t>())) {
|
|
return CV_8U;
|
|
} else if (np_dtype.is(pybind11::dtype::of<float>())) {
|
|
return CV_32F;
|
|
} else {
|
|
FDASSERT(
|
|
false,
|
|
"NumpyDataTypeToOpenCvType() only support int32/int8/uint8/float32 "
|
|
"now.");
|
|
}
|
|
return CV_8U;
|
|
}
|
|
|
|
int NumpyDataTypeToOpenCvTypeV2(pybind11::array& pyarray) {
|
|
if (pybind11::isinstance<pybind11::array_t<std::int32_t>>(pyarray)) {
|
|
return CV_32S;
|
|
} else if (pybind11::isinstance<pybind11::array_t<std::int8_t>>(pyarray)) {
|
|
return CV_8S;
|
|
} else if (pybind11::isinstance<pybind11::array_t<std::uint8_t>>(pyarray)) {
|
|
return CV_8U;
|
|
} else if (pybind11::isinstance<pybind11::array_t<std::float_t>>(pyarray)) {
|
|
return CV_32F;
|
|
} else {
|
|
FDASSERT(
|
|
false,
|
|
"NumpyDataTypeToOpenCvTypeV2() only support int32/int8/uint8/float32 "
|
|
"now.");
|
|
}
|
|
return CV_8U;
|
|
}
|
|
|
|
cv::Mat PyArrayToCvMat(pybind11::array& pyarray) {
|
|
// auto cv_type = NumpyDataTypeToOpenCvType(pyarray.dtype());
|
|
auto cv_type = NumpyDataTypeToOpenCvTypeV2(pyarray);
|
|
FDASSERT(
|
|
pyarray.ndim() == 3,
|
|
"Require rank of array to be 3 with HWC format while converting it to "
|
|
"cv::Mat.");
|
|
int channel = *(pyarray.shape() + 2);
|
|
int height = *(pyarray.shape());
|
|
int width = *(pyarray.shape() + 1);
|
|
return cv::Mat(height, width, CV_MAKETYPE(cv_type, channel),
|
|
pyarray.mutable_data());
|
|
}
|
|
#endif
|
|
|
|
PYBIND11_MODULE(@PY_LIBRARY_NAME@, m) {
|
|
m.doc() =
|
|
"Make programer easier to deploy deeplearning model, save time to save "
|
|
"the world!";
|
|
|
|
BindRuntime(m);
|
|
BindFDModel(m);
|
|
#ifdef ENABLE_VISION
|
|
auto vision_module =
|
|
m.def_submodule("vision", "Vision module of FastDeploy.");
|
|
BindVision(vision_module);
|
|
auto pipeline_module =
|
|
m.def_submodule("pipeline", "Pipeline module of FastDeploy.");
|
|
BindPipeline(pipeline_module);
|
|
#endif
|
|
#ifdef ENABLE_TEXT
|
|
auto text_module =
|
|
m.def_submodule("text", "Text module of FastDeploy.");
|
|
BindText(text_module);
|
|
#endif
|
|
auto rknpu2_module =
|
|
m.def_submodule("rknpu2", "RKNPU2 config module of FastDeploy.");
|
|
BindRKNPU2Config(rknpu2_module);
|
|
}
|
|
|
|
} // namespace fastdeploy
|