[FlyCV] optimize the integration of FlyCV (#492)

* [Backend] fix lite backend save model error

* [Backend] fixed typos

* [FlyCV] optimize the integration of FlyCV

* [cmake] close some tests options

* [cmake] close some test option

* [FlyCV] remove un-need warnings

* [FlyCV] remove un-need GetMat method

* [FlyCV] optimize FlyCV codes

* [cmake] remove un-need cmake function in examples/CMakelists

* [cmake] support gflags for Android
This commit is contained in:
DefTruth
2022-11-04 09:19:03 +08:00
committed by GitHub
parent 5b1fecd668
commit a36d49a973
56 changed files with 670 additions and 487 deletions

2
.gitignore vendored
View File

@@ -38,3 +38,5 @@ coverage
*.local *.local
yalc.* yalc.*
.yalc .yalc
examples/vision/collect_quantize_cc.sh
examples/vision/tests_quantize

View File

@@ -38,36 +38,71 @@ ENDIF(WIN32)
INCLUDE_DIRECTORIES(${GFLAGS_INCLUDE_DIR}) INCLUDE_DIRECTORIES(${GFLAGS_INCLUDE_DIR})
ExternalProject_Add( if(ANDROID)
extern_gflags ExternalProject_Add(
${EXTERNAL_PROJECT_LOG_ARGS} extern_gflags
${SHALLOW_CLONE} ${EXTERNAL_PROJECT_LOG_ARGS}
GIT_REPOSITORY ${GFLAGS_REPOSITORY} ${SHALLOW_CLONE}
GIT_TAG ${GFLAGS_TAG} GIT_REPOSITORY ${GFLAGS_REPOSITORY}
PREFIX ${GFLAGS_PREFIX_DIR} GIT_TAG ${GFLAGS_TAG}
UPDATE_COMMAND "" PREFIX ${GFLAGS_PREFIX_DIR}
BUILD_COMMAND ${BUILD_COMMAND} UPDATE_COMMAND ""
INSTALL_COMMAND ${INSTALL_COMMAND} BUILD_COMMAND ${BUILD_COMMAND}
CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} INSTALL_COMMAND ${INSTALL_COMMAND}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} -DANDROID_ABI=${ANDROID_ABI}
-DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE} -DANDROID_NDK=${ANDROID_NDK}
-DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG} -DANDROID_PLATFORM=${ANDROID_PLATFORM}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} -DANDROID_STL=c++_static
-DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG} -DANDROID_TOOLCHAIN=${ANDROID_TOOLCHAIN}
-DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE} -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DBUILD_STATIC_LIBS=ON -DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
-DCMAKE_INSTALL_PREFIX=${GFLAGS_INSTALL_DIR} -DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}
-DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DBUILD_TESTING=OFF -DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}
-DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE} -DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}
${EXTERNAL_OPTIONAL_ARGS} -DBUILD_STATIC_LIBS=ON
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${GFLAGS_INSTALL_DIR} -DCMAKE_INSTALL_PREFIX=${GFLAGS_INSTALL_DIR}
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON -DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE} -DBUILD_TESTING=OFF
BUILD_BYPRODUCTS ${GFLAGS_LIBRARIES} -DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
) ${EXTERNAL_OPTIONAL_ARGS}
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${GFLAGS_INSTALL_DIR}
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
-DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE}
BUILD_BYPRODUCTS ${GFLAGS_LIBRARIES}
)
else()
ExternalProject_Add(
extern_gflags
${EXTERNAL_PROJECT_LOG_ARGS}
${SHALLOW_CLONE}
GIT_REPOSITORY ${GFLAGS_REPOSITORY}
GIT_TAG ${GFLAGS_TAG}
PREFIX ${GFLAGS_PREFIX_DIR}
UPDATE_COMMAND ""
BUILD_COMMAND ${BUILD_COMMAND}
INSTALL_COMMAND ${INSTALL_COMMAND}
CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
-DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}
-DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}
-DBUILD_STATIC_LIBS=ON
-DCMAKE_INSTALL_PREFIX=${GFLAGS_INSTALL_DIR}
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DBUILD_TESTING=OFF
-DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
${EXTERNAL_OPTIONAL_ARGS}
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${GFLAGS_INSTALL_DIR}
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
-DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE}
BUILD_BYPRODUCTS ${GFLAGS_LIBRARIES}
)
endif()
ADD_LIBRARY(gflags STATIC IMPORTED GLOBAL) ADD_LIBRARY(gflags STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET gflags PROPERTY IMPORTED_LOCATION ${GFLAGS_LIBRARIES}) SET_PROPERTY(TARGET gflags PROPERTY IMPORTED_LOCATION ${GFLAGS_LIBRARIES})
ADD_DEPENDENCIES(gflags extern_gflags) ADD_DEPENDENCIES(gflags extern_gflags)

View File

@@ -37,12 +37,6 @@ function(config_fastdeploy_executable_link_flags TARGET_NAME)
endif() endif()
endfunction() endfunction()
# Usage: add_fastdeploy_executable_cc_files(xxx_var vision detection)
function(add_fastdeploy_executable_cc_files CC_FILES_VAR FIELD SUB_FIELD)
file(GLOB_RECURSE _EXAMPLE_SRCS ${PROJECT_SOURCE_DIR}/examples/${FIELD}/${SUB_FIELD}/*/cpp/*.cc)
set(${CC_FILES_VAR} ${_EXAMPLE_SRCS} PARENT_SCOPE)
endfunction()
set(EXAMPLES_NUM 0) set(EXAMPLES_NUM 0)
function(add_fastdeploy_executable FIELD CC_FILE) function(add_fastdeploy_executable FIELD CC_FILE)
# temp target name/file var in function scope # temp target name/file var in function scope
@@ -55,7 +49,11 @@ function(add_fastdeploy_executable FIELD CC_FILE)
add_executable(${TEMP_TARGET_NAME} ${TEMP_TARGET_FILE}) add_executable(${TEMP_TARGET_NAME} ${TEMP_TARGET_FILE})
target_link_libraries(${TEMP_TARGET_NAME} PUBLIC fastdeploy) target_link_libraries(${TEMP_TARGET_NAME} PUBLIC fastdeploy)
if(TARGET gflags) if(TARGET gflags)
target_link_libraries(${TEMP_TARGET_NAME} PRIVATE gflags pthread) if(NOT ANDROID)
target_link_libraries(${TEMP_TARGET_NAME} PRIVATE gflags pthread)
else()
target_link_libraries(${TEMP_TARGET_NAME} PRIVATE gflags)
endif()
endif() endif()
config_fastdeploy_executable_link_flags(${TEMP_TARGET_NAME}) config_fastdeploy_executable_link_flags(${TEMP_TARGET_NAME})
math(EXPR _EXAMPLES_NUM "${EXAMPLES_NUM} + 1") math(EXPR _EXAMPLES_NUM "${EXAMPLES_NUM} + 1")
@@ -78,22 +76,13 @@ if(BUILD_EXAMPLES AND ENABLE_VISION)
if(EXISTS ${PROJECT_SOURCE_DIR}/examples/vision) if(EXISTS ${PROJECT_SOURCE_DIR}/examples/vision)
message(STATUS "") message(STATUS "")
message(STATUS "*************FastDeploy Vision Examples Summary**********") message(STATUS "*************FastDeploy Vision Examples Summary**********")
set(ALL_VISION_SUD_FIELDS classification file(GLOB_RECURSE ALL_VISION_EXAMPLE_SRCS ${PROJECT_SOURCE_DIR}/examples/vision/*/*/cpp/*.cc)
detection if(ANDROID)
facedet file(GLOB_RECURSE TRACKING_SRCS ${PROJECT_SOURCE_DIR}/examples/vision/tracking/*/cpp/*.cc)
faceid list(REMOVE_ITEM ALL_VISION_EXAMPLE_SRCS ${TRACKING_SRCS})
keypointdetection
matting
ocr
segmentation)
if(NOT ANDROID)
list(APPEND ALL_VISION_SUD_FIELDS tracking)
endif() endif()
foreach(_SUB_FIELD ${ALL_VISION_SUD_FIELDS}) foreach(_CC_FILE ${ALL_VISION_EXAMPLE_SRCS})
add_fastdeploy_executable_cc_files(_SUB_CC_FILES vision ${_SUB_FIELD}) add_fastdeploy_executable(vision ${_CC_FILE})
foreach(_CC_FILE ${_SUB_CC_FILES})
add_fastdeploy_executable(vision ${_CC_FILE})
endforeach()
endforeach() endforeach()
message(STATUS " [FastDeploy Executable Path] : ${EXECUTABLE_OUTPUT_PATH}") message(STATUS " [FastDeploy Executable Path] : ${EXECUTABLE_OUTPUT_PATH}")
endif() endif()

View File

@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/classification/ppcls/model.h" #include "fastdeploy/vision/classification/ppcls/model.h"
#include "fastdeploy/vision/utils/utils.h" #include "fastdeploy/vision/utils/utils.h"
#include "yaml-cpp/yaml.h" #include "yaml-cpp/yaml.h"
@@ -108,7 +109,7 @@ bool PaddleClasModel::Preprocess(Mat* mat, FDTensor* output) {
int height = mat->Height(); int height = mat->Height();
output->name = InputInfoOfRuntime(0).name; output->name = InputInfoOfRuntime(0).name;
output->SetExternalData({1, channel, height, width}, FDDataType::FP32, output->SetExternalData({1, channel, height, width}, FDDataType::FP32,
mat->GetOpenCVMat()->ptr()); mat->Data());
return true; return true;
} }

View File

@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/common/processors/base.h" #include "fastdeploy/vision/common/processors/base.h"
#include "fastdeploy/utils/utils.h" #include "fastdeploy/utils/utils.h"
namespace fastdeploy { namespace fastdeploy {
@@ -21,27 +22,18 @@ namespace vision {
ProcLib Processor::default_lib = ProcLib::DEFAULT; ProcLib Processor::default_lib = ProcLib::DEFAULT;
bool Processor::operator()(Mat* mat, ProcLib lib) { bool Processor::operator()(Mat* mat, ProcLib lib) {
// if default_lib is set
// then use default_lib
ProcLib target = lib; ProcLib target = lib;
if (default_lib != ProcLib::DEFAULT) { if (lib == ProcLib::DEFAULT) {
target = default_lib; target = default_lib;
} }
if (target == ProcLib::FLYCV) { if (target == ProcLib::FLYCV) {
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
if (mat->mat_type != ProcLib::FLYCV) { return ImplByFlyCV(mat);
if (mat->layout != Layout::HWC) {
FDERROR << "Cannot convert cv::Mat to fcv::Mat while layout is not HWC." << std::endl;
}
fcv::Mat fcv_mat = ConvertOpenCVMatToFalconCV(*(mat->GetOpenCVMat()));
mat->SetMat(fcv_mat);
}
return ImplByFalconCV(mat);
#else #else
FDASSERT(false, "FastDeploy didn't compile with FalconCV."); FDASSERT(false, "FastDeploy didn't compile with FlyCV.");
#endif #endif
} }
// DEFAULT & OPENCV
return ImplByOpenCV(mat); return ImplByOpenCV(mat);
} }
@@ -52,7 +44,7 @@ void EnableFlyCV() {
<< Processor::default_lib << std::endl; << Processor::default_lib << std::endl;
#else #else
FDWARNING << "FastDeploy didn't compile with FlyCV, " FDWARNING << "FastDeploy didn't compile with FlyCV, "
"will fallback to use OpenCV instead." "will fallback to use OpenCV instead."
<< std::endl; << std::endl;
#endif #endif
} }
@@ -63,5 +55,100 @@ void DisableFlyCV() {
<< Processor::default_lib << std::endl; << Processor::default_lib << std::endl;
} }
cv::Mat CreateOpenCVMatFromTensor(const FDTensor& tensor) {
FDDataType type = tensor.dtype;
FDASSERT(tensor.shape.size() == 3,
"When create FD Mat from tensor, tensor shape should be 3-Dim, HWC "
"layout");
int64_t height = tensor.shape[0];
int64_t width = tensor.shape[1];
int64_t channel = tensor.shape[2];
cv::Mat ocv_mat;
// reference to outside FDTensor, zero copy
switch (type) {
case FDDataType::UINT8:
ocv_mat = cv::Mat(height, width, CV_8UC(channel),
const_cast<void*>(tensor.Data()));
break;
case FDDataType::INT8:
ocv_mat = cv::Mat(height, width, CV_8SC(channel),
const_cast<void*>(tensor.Data()));
break;
case FDDataType::INT16:
ocv_mat = cv::Mat(height, width, CV_16SC(channel),
const_cast<void*>(tensor.Data()));
break;
case FDDataType::INT32:
ocv_mat = cv::Mat(height, width, CV_32SC(channel),
const_cast<void*>(tensor.Data()));
break;
case FDDataType::FP32:
ocv_mat = cv::Mat(height, width, CV_32FC(channel),
const_cast<void*>(tensor.Data()));
break;
case FDDataType::FP64:
ocv_mat = cv::Mat(height, width, CV_64FC(channel),
const_cast<void*>(tensor.Data()));
break;
default:
FDASSERT(false,
"Tensor type %d is not supported While calling "
"CreateFDMatFromTensor.",
type);
break;
}
return ocv_mat;
}
#ifdef ENABLE_FLYCV
fcv::Mat CreateFlyCVMatFromTensor(const FDTensor& tensor) {
FDDataType type = tensor.dtype;
FDASSERT(tensor.shape.size() == 3,
"When create FD Mat from tensor, tensor shape should be 3-Dim, HWC "
"layout");
int64_t height = tensor.shape[0];
int64_t width = tensor.shape[1];
int64_t channel = tensor.shape[2];
fcv::Mat fcv_mat;
auto fcv_type = CreateFlyCVDataType(type, static_cast<int>(channel));
switch (type) {
case FDDataType::UINT8:
fcv_mat =
fcv::Mat(width, height, fcv_type, const_cast<void*>(tensor.Data()));
break;
case FDDataType::FP32:
fcv_mat =
fcv::Mat(width, height, fcv_type, const_cast<void*>(tensor.Data()));
break;
case FDDataType::FP64:
fcv_mat =
fcv::Mat(width, height, fcv_type, const_cast<void*>(tensor.Data()));
break;
default:
FDASSERT(false,
"Tensor type %d is not supported While calling "
"CreateFDMatFromTensor.",
type);
break;
}
return fcv_mat;
}
#endif
Mat CreateFDMatFromTensor(const FDTensor& tensor) {
if (Processor::default_lib == ProcLib::FLYCV) {
#ifdef ENABLE_FLYCV
fcv::Mat fcv_mat = CreateFlyCVMatFromTensor(tensor);
Mat mat = Mat(fcv_mat);
return mat;
#else
FDASSERT(false, "FastDeploy didn't compiled with FlyCV!");
#endif
}
cv::Mat ocv_mat = CreateOpenCVMatFromTensor(tensor);
Mat mat = Mat(ocv_mat);
return mat;
}
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -22,7 +22,9 @@
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
/*! @brief Enable using FlyCV to process image while deploy vision models. Currently, FlyCV in only available on ARM(Linux aarch64/Android), so will fallback to using OpenCV in other platform /*! @brief Enable using FlyCV to process image while deploy vision models.
* Currently, FlyCV in only available on ARM(Linux aarch64/Android), so will
* fallback to using OpenCV in other platform
*/ */
FASTDEPLOY_DECL void EnableFlyCV(); FASTDEPLOY_DECL void EnableFlyCV();
@@ -41,16 +43,19 @@ class FASTDEPLOY_DECL Processor {
virtual bool ImplByOpenCV(Mat* mat) = 0; virtual bool ImplByOpenCV(Mat* mat) = 0;
virtual bool ImplByFalconCV(Mat* mat) { virtual bool ImplByFlyCV(Mat* mat) {
FDASSERT(false, return ImplByOpenCV(mat);
"%s is not implemented with FalconCV, please use OpenCV instead.",
Name().c_str());
return false;
} }
virtual bool operator()(Mat* mat, virtual bool operator()(Mat* mat, ProcLib lib = ProcLib::DEFAULT);
ProcLib lib = ProcLib::OPENCV);
}; };
// Create OpenCV/FlyCV/FD Mat from FD Tensor
cv::Mat CreateOpenCVMatFromTensor(const FDTensor& tensor);
#ifdef ENABLE_FLYCV
fcv::Mat CreateFlyCVMatFromTensor(const FDTensor& tensor);
#endif
Mat CreateFDMatFromTensor(const FDTensor& tensor);
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -36,8 +36,8 @@ bool Cast::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool Cast::ImplByFalconCV(Mat* mat) { bool Cast::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
if (dtype_ == "float" && mat->Type() == FDDataType::FP32) { if (dtype_ == "float" && mat->Type() == FDDataType::FP32) {
return true; return true;
} }
@@ -46,18 +46,18 @@ bool Cast::ImplByFalconCV(Mat* mat) {
} }
if (mat->layout != Layout::HWC) { if (mat->layout != Layout::HWC) {
FDERROR FDERROR
<< "While using Falcon to cast image, the image must be layout of HWC." << "While using FlyCV to cast image, the image must be layout of HWC."
<< std::endl; << std::endl;
return false; return false;
} }
if (dtype_ == "float") { if (dtype_ == "float") {
fcv::Mat new_im; fcv::Mat new_im;
auto fcv_type = CreateFalconCVDataType(FDDataType::FP32, im->channels()); auto fcv_type = CreateFlyCVDataType(FDDataType::FP32, im->channels());
im->convert_to(new_im, fcv_type); im->convert_to(new_im, fcv_type);
mat->SetMat(new_im); mat->SetMat(new_im);
} else if (dtype_ == "double") { } else if (dtype_ == "double") {
fcv::Mat new_im; fcv::Mat new_im;
auto fcv_type = CreateFalconCVDataType(FDDataType::FP64, im->channels()); auto fcv_type = CreateFlyCVDataType(FDDataType::FP64, im->channels());
im->convert_to(new_im, fcv_type); im->convert_to(new_im, fcv_type);
mat->SetMat(new_im); mat->SetMat(new_im);
} else { } else {

View File

@@ -24,15 +24,13 @@ class FASTDEPLOY_DECL Cast : public Processor {
explicit Cast(const std::string& dtype = "float") : dtype_(dtype) {} explicit Cast(const std::string& dtype = "float") : dtype_(dtype) {}
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "Cast"; } std::string Name() { return "Cast"; }
static bool Run(Mat* mat, const std::string& dtype, static bool Run(Mat* mat, const std::string& dtype,
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
std::string GetDtype() const { std::string GetDtype() const { return dtype_; }
return dtype_;
}
private: private:
std::string dtype_; std::string dtype_;

View File

@@ -36,8 +36,8 @@ bool CenterCrop::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool CenterCrop::ImplByFalconCV(Mat* mat) { bool CenterCrop::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
int height = static_cast<int>(im->height()); int height = static_cast<int>(im->height());
int width = static_cast<int>(im->width()); int width = static_cast<int>(im->width());
if (height < height_ || width < width_) { if (height < height_ || width < width_) {
@@ -62,5 +62,5 @@ bool CenterCrop::Run(Mat* mat, const int& width, const int& height,
return c(mat, lib); return c(mat, lib);
} }
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -24,12 +24,12 @@ class FASTDEPLOY_DECL CenterCrop : public Processor {
CenterCrop(int width, int height) : height_(height), width_(width) {} CenterCrop(int width, int height) : height_(height), width_(width) {}
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "CenterCrop"; } std::string Name() { return "CenterCrop"; }
static bool Run(Mat* mat, const int& width, const int& height, static bool Run(Mat* mat, const int& width, const int& height,
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
int height_; int height_;

View File

@@ -25,10 +25,11 @@ bool BGR2RGB::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool BGR2RGB::ImplByFalconCV(Mat* mat) { bool BGR2RGB::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
if (im->channels() != 3) { if (im->channels() != 3) {
FDERROR << "[BGR2RGB] The channel of input image must be 3, but not it's " << im->channels() << "." << std::endl; FDERROR << "[BGR2RGB] The channel of input image must be 3, but not it's "
<< im->channels() << "." << std::endl;
return false; return false;
} }
fcv::Mat new_im; fcv::Mat new_im;
@@ -47,10 +48,11 @@ bool RGB2BGR::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool RGB2BGR::ImplByFalconCV(Mat* mat) { bool RGB2BGR::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
if (im->channels() != 3) { if (im->channels() != 3) {
FDERROR << "[RGB2BGR] The channel of input image must be 3, but not it's " << im->channels() << "." << std::endl; FDERROR << "[RGB2BGR] The channel of input image must be 3, but not it's "
<< im->channels() << "." << std::endl;
return false; return false;
} }
fcv::Mat new_im; fcv::Mat new_im;

View File

@@ -23,22 +23,22 @@ class FASTDEPLOY_DECL BGR2RGB : public Processor {
public: public:
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
virtual std::string Name() { return "BGR2RGB"; } virtual std::string Name() { return "BGR2RGB"; }
static bool Run(Mat* mat, ProcLib lib = ProcLib::OPENCV); static bool Run(Mat* mat, ProcLib lib = ProcLib::DEFAULT);
}; };
class FASTDEPLOY_DECL RGB2BGR : public Processor { class FASTDEPLOY_DECL RGB2BGR : public Processor {
public: public:
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "RGB2BGR"; } std::string Name() { return "RGB2BGR"; }
static bool Run(Mat* mat, ProcLib lib = ProcLib::OPENCV); static bool Run(Mat* mat, ProcLib lib = ProcLib::DEFAULT);
}; };
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -40,17 +40,18 @@ bool Convert::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool Convert::ImplByFalconCV(Mat* mat) { bool Convert::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
FDASSERT(im->channels() == 3, "Only support 3-channels image in FalconCV."); FDASSERT(im->channels() == 3, "Only support 3-channels image in FlyCV.");
std::vector<float> mean(3, 0); std::vector<float> mean(3, 0);
std::vector<float> std(3, 0); std::vector<float> std(3, 0);
for (size_t i = 0; i < 3; ++i) { for (size_t i = 0; i < 3; ++i) {
std[i] = 1.0 / alpha_[i]; std[i] = 1.0 / alpha_[i];
mean[i] = -1 * beta_[i] * std[i]; mean[i] = -1 * beta_[i] * std[i];
} }
fcv::Mat new_im; fcv::Mat new_im;
fcv::normalize_to_submean_to_reorder(*im, mean, std, std::vector<uint32_t>(), new_im, true); fcv::normalize_to_submean_to_reorder(*im, mean, std, std::vector<uint32_t>(),
new_im, true);
mat->SetMat(new_im); mat->SetMat(new_im);
return true; return true;
} }

View File

@@ -24,7 +24,7 @@ class FASTDEPLOY_DECL Convert : public Processor {
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "Convert"; } std::string Name() { return "Convert"; }
@@ -32,7 +32,7 @@ class FASTDEPLOY_DECL Convert : public Processor {
// The default behavior is the same as OpenCV's convertTo method. // The default behavior is the same as OpenCV's convertTo method.
static bool Run(Mat* mat, const std::vector<float>& alpha, static bool Run(Mat* mat, const std::vector<float>& alpha,
const std::vector<float>& beta, const std::vector<float>& beta,
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
std::vector<float> alpha_; std::vector<float> alpha_;

View File

@@ -37,8 +37,8 @@ bool Crop::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool Crop::ImplByFalconCV(Mat* mat) { bool Crop::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
int height = static_cast<int>(im->height()); int height = static_cast<int>(im->height());
int width = static_cast<int>(im->width()); int width = static_cast<int>(im->width());
if (height < height_ + offset_h_ || width < width_ + offset_w_) { if (height < height_ + offset_h_ || width < width_ + offset_w_) {

View File

@@ -31,12 +31,12 @@ class FASTDEPLOY_DECL Crop : public Processor {
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "Crop"; } std::string Name() { return "Crop"; }
static bool Run(Mat* mat, int offset_w, int offset_h, int width, int height, static bool Run(Mat* mat, int offset_w, int offset_h, int width, int height,
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
int offset_w_; int offset_w_;

View File

@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/common/processors/hwc2chw.h" #include "fastdeploy/vision/common/processors/hwc2chw.h"
#include "fastdeploy/function/transpose.h" #include "fastdeploy/function/transpose.h"
namespace fastdeploy { namespace fastdeploy {
@@ -41,18 +42,21 @@ bool HWC2CHW::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool HWC2CHW::ImplByFalconCV(Mat* mat) { bool HWC2CHW::ImplByFlyCV(Mat* mat) {
if (mat->layout != Layout::HWC) { if (mat->layout != Layout::HWC) {
FDERROR << "HWC2CHW: The input data is not Layout::HWC format!" << std::endl; FDERROR << "HWC2CHW: The input data is not Layout::HWC format!"
<< std::endl;
return false; return false;
} }
if (mat->Type() != FDDataType::FP32) { if (mat->Type() != FDDataType::FP32) {
FDERROR << "HWC2CHW: Only support float data while use FalconCV, but now it's " << mat->Type() << "." << std::endl; FDERROR << "HWC2CHW: Only support float data while use FlyCV, but now it's "
<< mat->Type() << "." << std::endl;
return false; return false;
} }
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
fcv::Mat new_im; fcv::Mat new_im;
fcv::normalize_to_submean_to_reorder(*im, {0.0, 0.0, 0.0}, {1.0, 1.0, 1.0}, std::vector<uint32_t>(), new_im, false); fcv::normalize_to_submean_to_reorder(*im, {0.0, 0.0, 0.0}, {1.0, 1.0, 1.0},
std::vector<uint32_t>(), new_im, false);
mat->SetMat(new_im); mat->SetMat(new_im);
mat->layout = Layout::CHW; mat->layout = Layout::CHW;
return true; return true;
@@ -64,5 +68,5 @@ bool HWC2CHW::Run(Mat* mat, ProcLib lib) {
return h(mat, lib); return h(mat, lib);
} }
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -23,11 +23,11 @@ class FASTDEPLOY_DECL HWC2CHW : public Processor {
public: public:
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "HWC2CHW"; } std::string Name() { return "HWC2CHW"; }
static bool Run(Mat* mat, ProcLib lib = ProcLib::OPENCV); static bool Run(Mat* mat, ProcLib lib = ProcLib::DEFAULT);
}; };
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

6
fastdeploy/vision/common/processors/letter_box.h Executable file → Normal file
View File

@@ -23,8 +23,8 @@ class LetterBoxResize : public Processor {
public: public:
LetterBoxResize(const std::vector<int>& target_size, LetterBoxResize(const std::vector<int>& target_size,
const std::vector<float>& color) { const std::vector<float>& color) {
target_size_ = target_size; target_size_ = target_size;
color_ = color; color_ = color;
} }
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
@@ -33,7 +33,7 @@ class LetterBoxResize : public Processor {
static bool Run(Mat* mat, const std::vector<int>& target_size, static bool Run(Mat* mat, const std::vector<int>& target_size,
const std::vector<float>& color, const std::vector<float>& color,
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
std::vector<int> target_size_; std::vector<int> target_size_;

View File

@@ -38,8 +38,8 @@ bool LimitByStride::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool LimitByStride::ImplByFalconCV(Mat* mat) { bool LimitByStride::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
int origin_w = im->width(); int origin_w = im->width();
int origin_h = im->height(); int origin_h = im->height();
int rw = origin_w - origin_w % stride_; int rw = origin_w - origin_w % stride_;
@@ -59,16 +59,14 @@ bool LimitByStride::ImplByFalconCV(Mat* mat) {
} else if (interp_ == 2) { } else if (interp_ == 2) {
interp_method = fcv::InterpolationType::INTER_CUBIC; interp_method = fcv::InterpolationType::INTER_CUBIC;
} else { } else {
FDERROR << "LimitByStride: Only support interp_ be 0/1/2 with FalconCV, but " FDERROR << "LimitByStride: Only support interp_ be 0/1/2 with FlyCV, but "
"now it's " "now it's "
<< interp_ << "." << std::endl; << interp_ << "." << std::endl;
return false; return false;
} }
fcv::Mat new_im; fcv::Mat new_im;
FDERROR << "Before " << im->width() << " " << im->height() << std::endl;
fcv::resize(*im, new_im, fcv::Size(rw, rh), 0, 0, interp_method); fcv::resize(*im, new_im, fcv::Size(rw, rh), 0, 0, interp_method);
FDERROR << "After " << new_im.width() << " " << new_im.height() << std::endl;
mat->SetMat(new_im); mat->SetMat(new_im);
mat->SetWidth(new_im.width()); mat->SetWidth(new_im.width());
mat->SetHeight(new_im.height()); mat->SetHeight(new_im.height());

View File

@@ -29,12 +29,12 @@ class FASTDEPLOY_DECL LimitByStride : public Processor {
// Resize Mat* mat to make the size divisible by stride_. // Resize Mat* mat to make the size divisible by stride_.
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "LimitByStride"; } std::string Name() { return "LimitByStride"; }
static bool Run(Mat* mat, int stride = 32, int interp = 1, static bool Run(Mat* mat, int stride = 32, int interp = 1,
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
int interp_; int interp_;

View File

@@ -39,8 +39,8 @@ bool LimitLong::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool LimitLong::ImplByFalconCV(Mat* mat) { bool LimitLong::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
int origin_w = im->width(); int origin_w = im->width();
int origin_h = im->height(); int origin_h = im->height();
int im_size_max = std::max(origin_w, origin_h); int im_size_max = std::max(origin_w, origin_h);
@@ -64,16 +64,13 @@ bool LimitLong::ImplByFalconCV(Mat* mat) {
} else if (interp_ == 2) { } else if (interp_ == 2) {
interp_method = fcv::InterpolationType::INTER_CUBIC; interp_method = fcv::InterpolationType::INTER_CUBIC;
} else { } else {
FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FalconCV, but " FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FlyCV, but "
"now it's " "now it's "
<< interp_ << "." << std::endl; << interp_ << "." << std::endl;
return false; return false;
} }
fcv::Mat new_im; fcv::Mat new_im;
FDERROR << "origin " << im->width() << " " << im->height() << std::endl;
FDERROR << "scale " << scale << std::endl;
fcv::resize(*im, new_im, fcv::Size(), scale, scale, interp_method); fcv::resize(*im, new_im, fcv::Size(), scale, scale, interp_method);
FDERROR << "after " << new_im.width() << " " << new_im.height() << std::endl;
mat->SetMat(new_im); mat->SetMat(new_im);
mat->SetWidth(new_im.width()); mat->SetWidth(new_im.width());
mat->SetHeight(new_im.height()); mat->SetHeight(new_im.height());
@@ -82,7 +79,8 @@ bool LimitLong::ImplByFalconCV(Mat* mat) {
} }
#endif #endif
bool LimitLong::Run(Mat* mat, int max_long, int min_long, int interp, ProcLib lib) { bool LimitLong::Run(Mat* mat, int max_long, int min_long, int interp,
ProcLib lib) {
auto l = LimitLong(max_long, min_long, interp); auto l = LimitLong(max_long, min_long, interp);
return l(mat, lib); return l(mat, lib);
} }

View File

@@ -34,12 +34,12 @@ class FASTDEPLOY_DECL LimitLong : public Processor {
// to min_long_, while scale the short edge proportionally. // to min_long_, while scale the short edge proportionally.
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "LimitLong"; } std::string Name() { return "LimitLong"; }
static bool Run(Mat* mat, int max_long = -1, int min_long = -1, static bool Run(Mat* mat, int max_long = -1, int min_long = -1,
int interp = 1, ProcLib lib = ProcLib::OPENCV); int interp = 1, ProcLib lib = ProcLib::DEFAULT);
int GetMaxLong() const { return max_long_; } int GetMaxLong() const { return max_long_; }
private: private:

View File

@@ -41,8 +41,8 @@ bool LimitShort::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool LimitShort::ImplByFalconCV(Mat* mat) { bool LimitShort::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
int origin_w = im->width(); int origin_w = im->width();
int origin_h = im->height(); int origin_h = im->height();
int im_size_min = std::min(origin_w, origin_h); int im_size_min = std::min(origin_w, origin_h);
@@ -65,7 +65,7 @@ bool LimitShort::ImplByFalconCV(Mat* mat) {
} else if (interp_ == 2) { } else if (interp_ == 2) {
interp_method = fcv::InterpolationType::INTER_CUBIC; interp_method = fcv::InterpolationType::INTER_CUBIC;
} else { } else {
FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FalconCV, but " FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FlyCV, but "
"now it's " "now it's "
<< interp_ << "." << std::endl; << interp_ << "." << std::endl;
return false; return false;
@@ -81,7 +81,8 @@ bool LimitShort::ImplByFalconCV(Mat* mat) {
} }
#endif #endif
bool LimitShort::Run(Mat* mat, int max_short, int min_short, int interp, ProcLib lib) { bool LimitShort::Run(Mat* mat, int max_short, int min_short, int interp,
ProcLib lib) {
auto l = LimitShort(max_short, min_short, interp); auto l = LimitShort(max_short, min_short, interp);
return l(mat, lib); return l(mat, lib);
} }

View File

@@ -34,12 +34,12 @@ class LimitShort : public Processor {
// to min_short_, while scale the long edge proportionally. // to min_short_, while scale the long edge proportionally.
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "LimitShort"; } std::string Name() { return "LimitShort"; }
static bool Run(Mat* mat, int max_short = -1, int min_short = -1, static bool Run(Mat* mat, int max_short = -1, int min_short = -1,
int interp = 1, ProcLib lib = ProcLib::OPENCV); int interp = 1, ProcLib lib = ProcLib::DEFAULT);
int GetMaxShort() const { return max_short_; } int GetMaxShort() const { return max_short_; }
private: private:

View File

@@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/common/processors/mat.h" #include "fastdeploy/vision/common/processors/mat.h"
#include "fastdeploy/vision/common/processors/utils.h"
#include "fastdeploy/utils/utils.h" #include "fastdeploy/utils/utils.h"
#include "fastdeploy/vision/common/processors/utils.h"
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
@@ -23,15 +24,16 @@ void* Mat::Data() {
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
return fcv_mat.data(); return fcv_mat.data();
#else #else
FDASSERT(false, "FastDeploy didn't compile with FalconCV, but met data type with fcv::Mat."); FDASSERT(false,
"FastDeploy didn't compile with FlyCV, but met data type with "
"fcv::Mat.");
#endif #endif
} }
return cpu_mat.ptr(); return cpu_mat.ptr();
} }
void Mat::ShareWithTensor(FDTensor* tensor) { void Mat::ShareWithTensor(FDTensor* tensor) {
tensor->SetExternalData({Channels(), Height(), Width()}, Type(), tensor->SetExternalData({Channels(), Height(), Width()}, Type(), Data());
Data());
tensor->device = Device::CPU; tensor->device = Device::CPU;
if (layout == Layout::HWC) { if (layout == Layout::HWC) {
tensor->shape = {Height(), Width(), Channels()}; tensor->shape = {Height(), Width(), Channels()};
@@ -56,26 +58,28 @@ void Mat::PrintInfo(const std::string& flag) {
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
fcv::Scalar mean = fcv::mean(fcv_mat); fcv::Scalar mean = fcv::mean(fcv_mat);
std::cout << flag << ": " std::cout << flag << ": "
<< "DataType=" << Type() << ", " << "DataType=" << Type() << ", "
<< "Channel=" << Channels() << ", " << "Channel=" << Channels() << ", "
<< "Height=" << Height() << ", " << "Height=" << Height() << ", "
<< "Width=" << Width() << ", " << "Width=" << Width() << ", "
<< "Mean="; << "Mean=";
for (int i = 0; i < Channels(); ++i) { for (int i = 0; i < Channels(); ++i) {
std::cout << mean[i] << " "; std::cout << mean[i] << " ";
} }
std::cout << std::endl; std::cout << std::endl;
#else #else
FDASSERT(false, "FastDeploy didn't compile with FalconCV, but met data type with fcv::Mat."); FDASSERT(false,
"FastDeploy didn't compile with FlyCV, but met data type with "
"fcv::Mat.");
#endif #endif
} else { } else {
cv::Scalar mean = cv::mean(cpu_mat); cv::Scalar mean = cv::mean(cpu_mat);
std::cout << flag << ": " std::cout << flag << ": "
<< "DataType=" << Type() << ", " << "DataType=" << Type() << ", "
<< "Channel=" << Channels() << ", " << "Channel=" << Channels() << ", "
<< "Height=" << Height() << ", " << "Height=" << Height() << ", "
<< "Width=" << Width() << ", " << "Width=" << Width() << ", "
<< "Mean="; << "Mean=";
for (int i = 0; i < Channels(); ++i) { for (int i = 0; i < Channels(); ++i) {
std::cout << mean[i] << " "; std::cout << mean[i] << " ";
} }
@@ -84,87 +88,35 @@ void Mat::PrintInfo(const std::string& flag) {
} }
FDDataType Mat::Type() { FDDataType Mat::Type() {
int type = -1; int type = -1;
if (mat_type == ProcLib::FLYCV) { if (mat_type == ProcLib::FLYCV) {
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
return FalconCVDataTypeToFD(fcv_mat.type()); return FlyCVDataTypeToFD(fcv_mat.type());
#else #else
FDASSERT(false, "FastDeploy didn't compile with FalconCV, but met data type with fcv::Mat."); FDASSERT(false,
"FastDeploy didn't compile with FlyCV, but met data type with "
"fcv::Mat.");
#endif #endif
} }
return OpenCVDataTypeToFD(cpu_mat.type()); return OpenCVDataTypeToFD(cpu_mat.type());
} }
Mat CreateFromTensor(const FDTensor& tensor) { std::ostream& operator<<(std::ostream& out, const ProcLib& p) {
int type = tensor.dtype;
cv::Mat temp_mat;
FDASSERT(tensor.shape.size() == 3,
"When create FD Mat from tensor, tensor shape should be 3-Dim, HWC "
"layout");
int64_t height = tensor.shape[0];
int64_t width = tensor.shape[1];
int64_t channel = tensor.shape[2];
switch (type) {
case FDDataType::UINT8:
temp_mat = cv::Mat(height, width, CV_8UC(channel),
const_cast<void*>(tensor.Data()));
break;
case FDDataType::INT8:
temp_mat = cv::Mat(height, width, CV_8SC(channel),
const_cast<void*>(tensor.Data()));
break;
case FDDataType::INT16:
temp_mat = cv::Mat(height, width, CV_16SC(channel),
const_cast<void*>(tensor.Data()));
break;
case FDDataType::INT32:
temp_mat = cv::Mat(height, width, CV_32SC(channel),
const_cast<void*>(tensor.Data()));
break;
case FDDataType::FP32:
temp_mat = cv::Mat(height, width, CV_32FC(channel),
const_cast<void*>(tensor.Data()));
break;
case FDDataType::FP64:
temp_mat = cv::Mat(height, width, CV_64FC(channel),
const_cast<void*>(tensor.Data()));
break;
default:
FDASSERT(
false,
"Tensor type %d is not supported While calling CreateFromTensor.",
type);
break;
}
Mat mat = Mat(temp_mat);
return mat;
}
std::ostream& operator<<(std::ostream& out,const ProcLib& p) {
switch (p) { switch (p) {
case ProcLib::DEFAULT: case ProcLib::DEFAULT:
out << "ProcLib::DEFAULT"; out << "ProcLib::DEFAULT";
break; break;
case ProcLib::OPENCV: case ProcLib::OPENCV:
out << "ProcLib::OPENCV"; out << "ProcLib::OPENCV";
break; break;
case ProcLib::FLYCV: case ProcLib::FLYCV:
out << "ProcLib::FLYCV"; out << "ProcLib::FLYCV";
break; break;
default: default:
FDASSERT(false, "Unknow type of ProcLib."); FDASSERT(false, "Unknow type of ProcLib.");
} }
return out; return out;
} }
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -13,19 +13,19 @@
// limitations under the License. // limitations under the License.
#pragma once #pragma once
#include "fastdeploy/core/fd_tensor.h" #include "fastdeploy/core/fd_tensor.h"
#include "opencv2/core/core.hpp"
#include "fastdeploy/vision/common/processors/utils.h" #include "fastdeploy/vision/common/processors/utils.h"
#include "opencv2/core/core.hpp"
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
enum class FASTDEPLOY_DECL ProcLib { DEFAULT, OPENCV, FLYCV}; enum class FASTDEPLOY_DECL ProcLib { DEFAULT, OPENCV, FLYCV };
enum Layout { HWC, CHW }; enum Layout { HWC, CHW };
FASTDEPLOY_DECL std::ostream& operator<<(std::ostream& out, const ProcLib& p); FASTDEPLOY_DECL std::ostream& operator<<(std::ostream& out, const ProcLib& p);
struct FASTDEPLOY_DECL Mat { struct FASTDEPLOY_DECL Mat {
explicit Mat(cv::Mat& mat) { explicit Mat(const cv::Mat& mat) {
cpu_mat = mat; cpu_mat = mat;
layout = Layout::HWC; layout = Layout::HWC;
height = cpu_mat.rows; height = cpu_mat.rows;
@@ -34,7 +34,18 @@ struct FASTDEPLOY_DECL Mat {
mat_type = ProcLib::OPENCV; mat_type = ProcLib::OPENCV;
} }
// careful if you use this interface #ifdef ENABLE_FLYCV
explicit Mat(const fcv::Mat& mat) {
fcv_mat = mat;
layout = Layout::HWC;
height = fcv_mat.height();
width = fcv_mat.width();
channels = fcv_mat.channels();
mat_type = ProcLib::FLYCV;
}
#endif
// Careful if you use this interface
// this only used if you don't want to write // this only used if you don't want to write
// the original data, and write to a new cv::Mat // the original data, and write to a new cv::Mat
// then replace the old cv::Mat of this structure // then replace the old cv::Mat of this structure
@@ -43,15 +54,23 @@ struct FASTDEPLOY_DECL Mat {
mat_type = ProcLib::OPENCV; mat_type = ProcLib::OPENCV;
} }
inline cv::Mat* GetOpenCVMat() { cv::Mat* GetOpenCVMat() {
FDASSERT(mat_type == ProcLib::OPENCV, "Met non cv::Mat data structure."); if (mat_type == ProcLib::OPENCV) {
return &cpu_mat; return &cpu_mat;
} } else if (mat_type == ProcLib::FLYCV) {
#ifdef ENABLE_FLYCV
// Just a reference to fcv_mat, zero copy. After you
inline const cv::Mat* GetOpenCVMat() const { // call this method, cpu_mat and fcv_mat will point
FDASSERT(mat_type == ProcLib::OPENCV, "Met non cv::Mat data structure."); // to the same memory buffer.
return &cpu_mat; cpu_mat = ConvertFlyCVMatToOpenCV(fcv_mat);
mat_type = ProcLib::OPENCV;
return &cpu_mat;
#else
FDASSERT(false, "FastDeploy didn't compiled with FlyCV!");
#endif
} else {
FDASSERT(false, "The mat_type of custom Mat can not be ProcLib::DEFAULT");
}
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
@@ -60,9 +79,19 @@ struct FASTDEPLOY_DECL Mat {
mat_type = ProcLib::FLYCV; mat_type = ProcLib::FLYCV;
} }
inline fcv::Mat* GetFalconCVMat() { fcv::Mat* GetFlyCVMat() {
FDASSERT(mat_type == ProcLib::FLYCV, "Met non fcv::Mat data strucure."); if (mat_type == ProcLib::FLYCV) {
return &fcv_mat; return &fcv_mat;
} else if (mat_type == ProcLib::OPENCV) {
// Just a reference to cpu_mat, zero copy. After you
// call this method, fcv_mat and cpu_mat will point
// to the same memory buffer.
fcv_mat = ConvertOpenCVMatToFlyCV(cpu_mat);
mat_type = ProcLib::FLYCV;
return &fcv_mat;
} else {
FDASSERT(false, "The mat_type of custom Mat can not be ProcLib::DEFAULT");
}
} }
#endif #endif
@@ -73,17 +102,11 @@ struct FASTDEPLOY_DECL Mat {
int height; int height;
int width; int width;
cv::Mat cpu_mat; cv::Mat cpu_mat;
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
fcv::Mat fcv_mat; fcv::Mat fcv_mat;
#endif #endif
public: public:
template<typename T>
T* GetMat() {
return &cpu_mat;
}
FDDataType Type(); FDDataType Type();
int Channels() const { return channels; } int Channels() const { return channels; }
int Width() const { return width; } int Width() const { return width; }
@@ -97,18 +120,16 @@ struct FASTDEPLOY_DECL Mat {
// Only support copy to cpu tensor now // Only support copy to cpu tensor now
bool CopyToTensor(FDTensor* tensor); bool CopyToTensor(FDTensor* tensor);
// debug functions // Debug functions
// TODO(jiangjiajun) Develop a right process pipeline with c++ is not a easy // TODO(jiangjiajun) Develop a right process pipeline with c++
// things // is not a easy things, Will add more debug function here to
// Will add more debug function here to help debug processed image // help debug processed image. This function will print shape
// This function will print shape / mean of each channels of the Mat // and mean of each channels of the Mat
void PrintInfo(const std::string& flag); void PrintInfo(const std::string& flag);
ProcLib mat_type = ProcLib::OPENCV; ProcLib mat_type = ProcLib::OPENCV;
Layout layout = Layout::HWC; Layout layout = Layout::HWC;
}; };
Mat CreateFromTensor(const FDTensor& tensor);
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -65,27 +65,29 @@ bool Normalize::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool Normalize::ImplByFalconCV(Mat* mat) { bool Normalize::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
if (im->channels() != 3) { if (im->channels() != 3) {
FDERROR << "Only supports 3-channels image in FalconCV, but now it's " << im->channels() << "." << std::endl; FDERROR << "Only supports 3-channels image in FlyCV, but now it's "
<< im->channels() << "." << std::endl;
return false; return false;
} }
std::vector<float> mean(3, 0); std::vector<float> mean(3, 0);
std::vector<float> std(3, 0); std::vector<float> std(3, 0);
for (size_t i = 0; i < 3; ++i) { for (size_t i = 0; i < 3; ++i) {
std[i] = 1.0 / alpha_[i]; std[i] = 1.0 / alpha_[i];
mean[i] = -1 * beta_[i] * std[i]; mean[i] = -1 * beta_[i] * std[i];
} }
fcv::Mat new_im(im->width(), im->height(), fcv::FCVImageType::PACKAGE_BGR_F32); fcv::Mat new_im(im->width(), im->height(),
fcv::normalize_to_submean_to_reorder(*im, mean, std, std::vector<uint32_t>(), new_im, true); fcv::FCVImageType::PACKAGE_BGR_F32);
fcv::normalize_to_submean_to_reorder(*im, mean, std, std::vector<uint32_t>(),
new_im, true);
mat->SetMat(new_im); mat->SetMat(new_im);
return true; return true;
} }
#endif #endif
bool Normalize::Run(Mat* mat, const std::vector<float>& mean, bool Normalize::Run(Mat* mat, const std::vector<float>& mean,
const std::vector<float>& std, bool is_scale, const std::vector<float>& std, bool is_scale,
const std::vector<float>& min, const std::vector<float>& min,
@@ -94,5 +96,5 @@ bool Normalize::Run(Mat* mat, const std::vector<float>& mean,
return n(mat, lib); return n(mat, lib);
} }
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -26,7 +26,7 @@ class FASTDEPLOY_DECL Normalize : public Processor {
const std::vector<float>& max = std::vector<float>()); const std::vector<float>& max = std::vector<float>());
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "Normalize"; } std::string Name() { return "Normalize"; }
@@ -44,14 +44,10 @@ class FASTDEPLOY_DECL Normalize : public Processor {
const std::vector<float>& std, bool is_scale = true, const std::vector<float>& std, bool is_scale = true,
const std::vector<float>& min = std::vector<float>(), const std::vector<float>& min = std::vector<float>(),
const std::vector<float>& max = std::vector<float>(), const std::vector<float>& max = std::vector<float>(),
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
std::vector<float> GetAlpha() const { std::vector<float> GetAlpha() const { return alpha_; }
return alpha_; std::vector<float> GetBeta() const { return beta_; }
}
std::vector<float> GetBeta() const {
return beta_;
}
private: private:
std::vector<float> alpha_; std::vector<float> alpha_;

View File

@@ -17,11 +17,11 @@
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
NormalizeAndPermute::NormalizeAndPermute(const std::vector<float>& mean, NormalizeAndPermute::NormalizeAndPermute(const std::vector<float>& mean,
const std::vector<float>& std, bool is_scale, const std::vector<float>& std,
const std::vector<float>& min, bool is_scale,
const std::vector<float>& max) { const std::vector<float>& min,
const std::vector<float>& max) {
FDASSERT(mean.size() == std.size(), FDASSERT(mean.size() == std.size(),
"Normalize: requires the size of mean equal to the size of std."); "Normalize: requires the size of mean equal to the size of std.");
std::vector<double> mean_(mean.begin(), mean.end()); std::vector<double> mean_(mean.begin(), mean.end());
@@ -65,7 +65,10 @@ bool NormalizeAndPermute::ImplByOpenCV(Mat* mat) {
} }
cv::Mat res(origin_h, origin_w, CV_32FC(im->channels())); cv::Mat res(origin_h, origin_w, CV_32FC(im->channels()));
for (int i = 0; i < im->channels(); ++i) { for (int i = 0; i < im->channels(); ++i) {
cv::extractChannel(split_im[i], cv::Mat(origin_h, origin_w, CV_32FC1, res.ptr() + i * origin_h * origin_w * 4), 0); cv::extractChannel(split_im[i],
cv::Mat(origin_h, origin_w, CV_32FC1,
res.ptr() + i * origin_h * origin_w * 4),
0);
} }
mat->SetMat(res); mat->SetMat(res);
@@ -74,38 +77,39 @@ bool NormalizeAndPermute::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool NormalizeAndPermute::ImplByFalconCV(Mat* mat) { bool NormalizeAndPermute::ImplByFlyCV(Mat* mat) {
if (mat->layout != Layout::HWC) { if (mat->layout != Layout::HWC) {
FDERROR << "Only supports input with HWC layout." << std::endl; FDERROR << "Only supports input with HWC layout." << std::endl;
return false; return false;
} }
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
if (im->channels() != 3) { if (im->channels() != 3) {
FDERROR << "Only supports 3-channels image in FalconCV, but now it's " << im->channels() << "." << std::endl; FDERROR << "Only supports 3-channels image in FlyCV, but now it's "
<< im->channels() << "." << std::endl;
return false; return false;
} }
std::vector<float> mean(3, 0); std::vector<float> mean(3, 0);
std::vector<float> std(3, 0); std::vector<float> std(3, 0);
for (size_t i = 0; i < 3; ++i) { for (size_t i = 0; i < 3; ++i) {
std[i] = 1.0 / alpha_[i]; std[i] = 1.0 / alpha_[i];
mean[i] = -1 * beta_[i] * std[i]; mean[i] = -1 * beta_[i] * std[i];
} }
fcv::Mat new_im; fcv::Mat new_im;
fcv::normalize_to_submean_to_reorder(*im, mean, std, std::vector<uint32_t>(), new_im, false); fcv::normalize_to_submean_to_reorder(*im, mean, std, std::vector<uint32_t>(),
new_im, false);
mat->SetMat(new_im); mat->SetMat(new_im);
mat->layout = Layout::CHW; mat->layout = Layout::CHW;
return true; return true;
} }
#endif #endif
bool NormalizeAndPermute::Run(Mat* mat, const std::vector<float>& mean, bool NormalizeAndPermute::Run(Mat* mat, const std::vector<float>& mean,
const std::vector<float>& std, bool is_scale, const std::vector<float>& std, bool is_scale,
const std::vector<float>& min, const std::vector<float>& min,
const std::vector<float>& max, ProcLib lib) { const std::vector<float>& max, ProcLib lib) {
auto n = NormalizeAndPermute(mean, std, is_scale, min, max); auto n = NormalizeAndPermute(mean, std, is_scale, min, max);
return n(mat, lib); return n(mat, lib);
} }
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -21,13 +21,12 @@ namespace vision {
class FASTDEPLOY_DECL NormalizeAndPermute : public Processor { class FASTDEPLOY_DECL NormalizeAndPermute : public Processor {
public: public:
NormalizeAndPermute(const std::vector<float>& mean, NormalizeAndPermute(const std::vector<float>& mean,
const std::vector<float>& std, const std::vector<float>& std, bool is_scale = true,
bool is_scale = true, const std::vector<float>& min = std::vector<float>(),
const std::vector<float>& min = std::vector<float>(), const std::vector<float>& max = std::vector<float>());
const std::vector<float>& max = std::vector<float>());
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "NormalizeAndPermute"; } std::string Name() { return "NormalizeAndPermute"; }
@@ -45,7 +44,7 @@ class FASTDEPLOY_DECL NormalizeAndPermute : public Processor {
const std::vector<float>& std, bool is_scale = true, const std::vector<float>& std, bool is_scale = true,
const std::vector<float>& min = std::vector<float>(), const std::vector<float>& min = std::vector<float>(),
const std::vector<float>& max = std::vector<float>(), const std::vector<float>& max = std::vector<float>(),
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
void SetAlpha(const std::vector<float>& alpha) { void SetAlpha(const std::vector<float>& alpha) {
alpha_.clear(); alpha_.clear();

View File

@@ -53,7 +53,7 @@ bool Pad::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool Pad::ImplByFalconCV(Mat* mat) { bool Pad::ImplByFlyCV(Mat* mat) {
if (mat->layout != Layout::HWC) { if (mat->layout != Layout::HWC) {
FDERROR << "Pad: The input data must be Layout::HWC format!" << std::endl; FDERROR << "Pad: The input data must be Layout::HWC format!" << std::endl;
return false; return false;
@@ -70,7 +70,7 @@ bool Pad::ImplByFalconCV(Mat* mat) {
<< std::endl; << std::endl;
return false; return false;
} }
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
fcv::Scalar value; fcv::Scalar value;
if (value_.size() == 1) { if (value_.size() == 1) {
value = fcv::Scalar(value_[0]); value = fcv::Scalar(value_[0]);
@@ -83,7 +83,7 @@ bool Pad::ImplByFalconCV(Mat* mat) {
} }
fcv::Mat new_im; fcv::Mat new_im;
fcv::copy_make_border(*im, new_im, top_, bottom_, left_, right_, fcv::copy_make_border(*im, new_im, top_, bottom_, left_, right_,
fcv::BorderTypes::BORDER_CONSTANT, value); fcv::BorderTypes::BORDER_CONSTANT, value);
mat->SetMat(new_im); mat->SetMat(new_im);
mat->SetHeight(new_im.height()); mat->SetHeight(new_im.height());
mat->SetWidth(new_im.width()); mat->SetWidth(new_im.width());
@@ -92,11 +92,10 @@ bool Pad::ImplByFalconCV(Mat* mat) {
#endif #endif
bool Pad::Run(Mat* mat, const int& top, const int& bottom, const int& left, bool Pad::Run(Mat* mat, const int& top, const int& bottom, const int& left,
const int& right, const std::vector<float>& value, const int& right, const std::vector<float>& value, ProcLib lib) {
ProcLib lib) {
auto p = Pad(top, bottom, left, right, value); auto p = Pad(top, bottom, left, right, value);
return p(mat, lib); return p(mat, lib);
} }
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -31,13 +31,13 @@ class FASTDEPLOY_DECL Pad : public Processor {
} }
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "Pad"; } std::string Name() { return "Pad"; }
static bool Run(Mat* mat, const int& top, const int& bottom, const int& left, static bool Run(Mat* mat, const int& top, const int& bottom, const int& left,
const int& right, const std::vector<float>& value, const int& right, const std::vector<float>& value,
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
int top_; int top_;

View File

@@ -76,7 +76,7 @@ bool PadToSize::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool PadToSize::ImplByFalconCV(Mat* mat) { bool PadToSize::ImplByFlyCV(Mat* mat) {
if (width_ == -1 || height_ == -1) { if (width_ == -1 || height_ == -1) {
return true; return true;
} }
@@ -115,7 +115,7 @@ bool PadToSize::ImplByFalconCV(Mat* mat) {
return true; return true;
} }
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
fcv::Scalar value; fcv::Scalar value;
if (value_.size() == 1) { if (value_.size() == 1) {
value = fcv::Scalar(value_[0]); value = fcv::Scalar(value_[0]);
@@ -128,8 +128,9 @@ bool PadToSize::ImplByFalconCV(Mat* mat) {
} }
fcv::Mat new_im; fcv::Mat new_im;
// top, bottom, left, right // top, bottom, left, right
fcv::copy_make_border(*im, new_im, 0, height_ - origin_h, 0, width_ - origin_w, fcv::copy_make_border(*im, new_im, 0, height_ - origin_h, 0,
fcv::BorderTypes::BORDER_CONSTANT, value); width_ - origin_w, fcv::BorderTypes::BORDER_CONSTANT,
value);
mat->SetMat(new_im); mat->SetMat(new_im);
mat->SetHeight(height_); mat->SetHeight(height_);
mat->SetWidth(width_); mat->SetWidth(width_);

View File

@@ -29,13 +29,13 @@ class FASTDEPLOY_DECL PadToSize : public Processor {
} }
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "PadToSize"; } std::string Name() { return "PadToSize"; }
static bool Run(Mat* mat, int width, int height, static bool Run(Mat* mat, int width, int height,
const std::vector<float>& value, const std::vector<float>& value,
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
int width_; int width_;

View File

@@ -55,12 +55,12 @@ bool Resize::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool Resize::ImplByFalconCV(Mat* mat) { bool Resize::ImplByFlyCV(Mat* mat) {
if (mat->layout != Layout::HWC) { if (mat->layout != Layout::HWC) {
FDERROR << "Resize: The format of input is not HWC." << std::endl; FDERROR << "Resize: The format of input is not HWC." << std::endl;
return false; return false;
} }
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
int origin_w = im->width(); int origin_w = im->width();
int origin_h = im->height(); int origin_h = im->height();
@@ -79,7 +79,7 @@ bool Resize::ImplByFalconCV(Mat* mat) {
} else if (interp_ == 2) { } else if (interp_ == 2) {
interp_method = fcv::InterpolationType::INTER_CUBIC; interp_method = fcv::InterpolationType::INTER_CUBIC;
} else { } else {
FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FalconCV, but " FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FlyCV, but "
"now it's " "now it's "
<< interp_ << "." << std::endl; << interp_ << "." << std::endl;
return false; return false;
@@ -99,7 +99,8 @@ bool Resize::ImplByFalconCV(Mat* mat) {
mat->SetHeight(new_im.height()); mat->SetHeight(new_im.height());
} else if (scale_w_ > 0 && scale_h_ > 0) { } else if (scale_w_ > 0 && scale_h_ > 0) {
fcv::Mat new_im; fcv::Mat new_im;
fcv::resize(*im, new_im, fcv::Size(0, 0), scale_w_, scale_h_, interp_method); fcv::resize(*im, new_im, fcv::Size(0, 0), scale_w_, scale_h_,
interp_method);
mat->SetMat(new_im); mat->SetMat(new_im);
mat->SetWidth(new_im.width()); mat->SetWidth(new_im.width());
mat->SetHeight(new_im.height()); mat->SetHeight(new_im.height());
@@ -122,5 +123,5 @@ bool Resize::Run(Mat* mat, int width, int height, float scale_w, float scale_h,
return r(mat, lib); return r(mat, lib);
} }
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -33,13 +33,13 @@ class FASTDEPLOY_DECL Resize : public Processor {
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "Resize"; } std::string Name() { return "Resize"; }
static bool Run(Mat* mat, int width, int height, float scale_w = -1.0, static bool Run(Mat* mat, int width, int height, float scale_w = -1.0,
float scale_h = -1.0, int interp = 1, bool use_scale = false, float scale_h = -1.0, int interp = 1, bool use_scale = false,
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
bool SetWidthAndHeight(int width, int height) { bool SetWidthAndHeight(int width, int height) {
width_ = width; width_ = width;

View File

@@ -33,7 +33,7 @@ class ResizeByLong : public Processor {
static bool Run(Mat* mat, int target_size, int interp = 1, static bool Run(Mat* mat, int target_size, int interp = 1,
bool use_scale = true, int max_size = -1, bool use_scale = true, int max_size = -1,
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
double GenerateScale(const int origin_w, const int origin_h); double GenerateScale(const int origin_w, const int origin_h);

View File

@@ -37,8 +37,8 @@ bool ResizeByShort::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ResizeByShort::ImplByFalconCV(Mat* mat) { bool ResizeByShort::ImplByFlyCV(Mat* mat) {
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
int origin_w = im->width(); int origin_w = im->width();
int origin_h = im->height(); int origin_h = im->height();
double scale = GenerateScale(origin_w, origin_h); double scale = GenerateScale(origin_w, origin_h);
@@ -51,7 +51,7 @@ bool ResizeByShort::ImplByFalconCV(Mat* mat) {
} else if (interp_ == 2) { } else if (interp_ == 2) {
interp_method = fcv::InterpolationType::INTER_CUBIC; interp_method = fcv::InterpolationType::INTER_CUBIC;
} else { } else {
FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FalconCV, but " FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FlyCV, but "
"now it's " "now it's "
<< interp_ << "." << std::endl; << interp_ << "." << std::endl;
return false; return false;

View File

@@ -30,14 +30,14 @@ class FASTDEPLOY_DECL ResizeByShort : public Processor {
} }
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "ResizeByShort"; } std::string Name() { return "ResizeByShort"; }
static bool Run(Mat* mat, int target_size, int interp = 1, static bool Run(Mat* mat, int target_size, int interp = 1,
bool use_scale = true, bool use_scale = true,
const std::vector<int>& max_hw = std::vector<int>(), const std::vector<int>& max_hw = std::vector<int>(),
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
double GenerateScale(const int origin_w, const int origin_h); double GenerateScale(const int origin_w, const int origin_h);

View File

@@ -64,7 +64,7 @@ bool StridePad::ImplByOpenCV(Mat* mat) {
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool StridePad::ImplByFalconCV(Mat* mat) { bool StridePad::ImplByFlyCV(Mat* mat) {
if (mat->layout != Layout::HWC) { if (mat->layout != Layout::HWC) {
FDERROR << "StridePad: The input data must be Layout::HWC format!" FDERROR << "StridePad: The input data must be Layout::HWC format!"
<< std::endl; << std::endl;
@@ -92,7 +92,7 @@ bool StridePad::ImplByFalconCV(Mat* mat) {
if (pad_h == 0 && pad_w == 0) { if (pad_h == 0 && pad_w == 0) {
return true; return true;
} }
fcv::Mat* im = mat->GetFalconCVMat(); fcv::Mat* im = mat->GetFlyCVMat();
fcv::Scalar value; fcv::Scalar value;
if (value_.size() == 1) { if (value_.size() == 1) {
value = fcv::Scalar(value_[0]); value = fcv::Scalar(value_[0]);
@@ -105,7 +105,8 @@ bool StridePad::ImplByFalconCV(Mat* mat) {
} }
fcv::Mat new_im; fcv::Mat new_im;
// top, bottom, left, right // top, bottom, left, right
fcv::copy_make_border(*im, new_im, 0, pad_h, 0, pad_w, fcv::BorderTypes::BORDER_CONSTANT, value); fcv::copy_make_border(*im, new_im, 0, pad_h, 0, pad_w,
fcv::BorderTypes::BORDER_CONSTANT, value);
mat->SetMat(new_im); mat->SetMat(new_im);
mat->SetHeight(new_im.height()); mat->SetHeight(new_im.height());
mat->SetWidth(new_im.width()); mat->SetWidth(new_im.width());

View File

@@ -28,13 +28,13 @@ class FASTDEPLOY_DECL StridePad : public Processor {
} }
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
bool ImplByFalconCV(Mat* mat); bool ImplByFlyCV(Mat* mat);
#endif #endif
std::string Name() { return "StridePad"; } std::string Name() { return "StridePad"; }
static bool Run(Mat* mat, int stride, static bool Run(Mat* mat, int stride,
const std::vector<float>& value = std::vector<float>(), const std::vector<float>& value = std::vector<float>(),
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
int stride_ = 32; int stride_ = 32;

View File

@@ -20,59 +20,64 @@
#include "fastdeploy/vision/common/processors/convert.h" #include "fastdeploy/vision/common/processors/convert.h"
#include "fastdeploy/vision/common/processors/crop.h" #include "fastdeploy/vision/common/processors/crop.h"
#include "fastdeploy/vision/common/processors/hwc2chw.h" #include "fastdeploy/vision/common/processors/hwc2chw.h"
#include "fastdeploy/vision/common/processors/letter_box.h"
#include "fastdeploy/vision/common/processors/limit_by_stride.h" #include "fastdeploy/vision/common/processors/limit_by_stride.h"
#include "fastdeploy/vision/common/processors/limit_long.h" #include "fastdeploy/vision/common/processors/limit_long.h"
#include "fastdeploy/vision/common/processors/limit_short.h" #include "fastdeploy/vision/common/processors/limit_short.h"
#include "fastdeploy/vision/common/processors/normalize.h" #include "fastdeploy/vision/common/processors/normalize.h"
#include "fastdeploy/vision/common/processors/normalize_and_permute.h"
#include "fastdeploy/vision/common/processors/pad.h" #include "fastdeploy/vision/common/processors/pad.h"
#include "fastdeploy/vision/common/processors/pad_to_size.h" #include "fastdeploy/vision/common/processors/pad_to_size.h"
#include "fastdeploy/vision/common/processors/resize.h" #include "fastdeploy/vision/common/processors/resize.h"
#include "fastdeploy/vision/common/processors/resize_by_long.h" #include "fastdeploy/vision/common/processors/resize_by_long.h"
#include "fastdeploy/vision/common/processors/resize_by_short.h" #include "fastdeploy/vision/common/processors/resize_by_short.h"
#include "fastdeploy/vision/common/processors/stride_pad.h" #include "fastdeploy/vision/common/processors/stride_pad.h"
#include "fastdeploy/vision/common/processors/normalize_and_permute.h"
#include "fastdeploy/vision/common/processors/warp_affine.h" #include "fastdeploy/vision/common/processors/warp_affine.h"
#include "fastdeploy/vision/common/processors/letter_box.h"
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
inline void FuseNormalizeCast(std::vector<std::shared_ptr<Processor>>* processors) { inline void FuseNormalizeCast(
std::vector<std::shared_ptr<Processor>>* processors) {
// Fuse Normalize and Cast<Float> // Fuse Normalize and Cast<Float>
int cast_index = -1; int cast_index = -1;
for (size_t i = 0; i < processors->size(); ++i) { for (size_t i = 0; i < processors->size(); ++i) {
if ((*processors)[i]->Name() == "Cast") { if ((*processors)[i]->Name() == "Cast") {
if (i == 0) { if (i == 0) {
continue; continue;
} }
if ((*processors)[i - 1]->Name() != "Normalize" && (*processors)[i - 1]->Name() != "NormalizeAndPermute") { if ((*processors)[i - 1]->Name() != "Normalize" &&
continue; (*processors)[i - 1]->Name() != "NormalizeAndPermute") {
} continue;
cast_index = i; }
cast_index = i;
} }
} }
if (cast_index < 0) { if (cast_index < 0) {
return; return;
} }
std::cout << dynamic_cast<Cast*>((*processors)[cast_index].get())->GetDtype() << "-----" << std::endl; if (dynamic_cast<Cast*>((*processors)[cast_index].get())->GetDtype() !=
if (dynamic_cast<Cast*>((*processors)[cast_index].get())->GetDtype() != "float") { "float") {
return; return;
} }
processors->erase(processors->begin() + cast_index); processors->erase(processors->begin() + cast_index);
FDINFO << (*processors)[cast_index - 1]->Name() << " and Cast are fused to " << (*processors)[cast_index - 1]->Name() << " in preprocessing pipeline." << std::endl; FDINFO << (*processors)[cast_index - 1]->Name() << " and Cast are fused to "
<< (*processors)[cast_index - 1]->Name()
<< " in preprocessing pipeline." << std::endl;
} }
inline void FuseNormalizeHWC2CHW(std::vector<std::shared_ptr<Processor>>* processors) { inline void FuseNormalizeHWC2CHW(
std::vector<std::shared_ptr<Processor>>* processors) {
// Fuse Normalize and HWC2CHW to NormalizeAndPermute // Fuse Normalize and HWC2CHW to NormalizeAndPermute
int hwc2chw_index = -1; int hwc2chw_index = -1;
for (size_t i = 0; i < processors->size(); ++i) { for (size_t i = 0; i < processors->size(); ++i) {
if ((*processors)[i]->Name() == "HWC2CHW") { if ((*processors)[i]->Name() == "HWC2CHW") {
if (i == 0) { if (i == 0) {
continue; continue;
} }
if ((*processors)[i - 1]->Name() != "Normalize") { if ((*processors)[i - 1]->Name() != "Normalize") {
continue; continue;
} }
hwc2chw_index = i; hwc2chw_index = i;
} }
@@ -83,10 +88,12 @@ inline void FuseNormalizeHWC2CHW(std::vector<std::shared_ptr<Processor>>* proces
} }
// Get alpha and beta of Normalize // Get alpha and beta of Normalize
std::vector<float> alpha = dynamic_cast<Normalize*>( std::vector<float> alpha =
(*processors)[hwc2chw_index - 1].get())->GetAlpha(); dynamic_cast<Normalize*>((*processors)[hwc2chw_index - 1].get())
std::vector<float> beta = dynamic_cast<Normalize*>( ->GetAlpha();
(*processors)[hwc2chw_index - 1].get())->GetBeta(); std::vector<float> beta =
dynamic_cast<Normalize*>((*processors)[hwc2chw_index - 1].get())
->GetBeta();
// Delete Normalize and HWC2CHW // Delete Normalize and HWC2CHW
processors->erase(processors->begin() + hwc2chw_index); processors->erase(processors->begin() + hwc2chw_index);
@@ -99,12 +106,13 @@ inline void FuseNormalizeHWC2CHW(std::vector<std::shared_ptr<Processor>>* proces
// Set alpha and beta // Set alpha and beta
auto processor = dynamic_cast<NormalizeAndPermute*>( auto processor = dynamic_cast<NormalizeAndPermute*>(
(*processors)[hwc2chw_index - 1].get()); (*processors)[hwc2chw_index - 1].get());
processor->SetAlpha(alpha); processor->SetAlpha(alpha);
processor->SetBeta(beta); processor->SetBeta(beta);
FDINFO << "Normalize and HWC2CHW are fused to NormalizeAndPermute " FDINFO << "Normalize and HWC2CHW are fused to NormalizeAndPermute "
" in preprocessing pipeline." << std::endl; " in preprocessing pipeline."
<< std::endl;
} }
inline void FuseTransforms( inline void FuseTransforms(

View File

@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "fastdeploy/utils/utils.h" #include "fastdeploy/utils/utils.h"
#include "fastdeploy/vision/common/processors/utils.h" #include "fastdeploy/vision/common/processors/utils.h"
namespace fastdeploy { namespace fastdeploy {
@@ -37,12 +38,15 @@ FDDataType OpenCVDataTypeToFD(int type) {
} else if (type == 6) { } else if (type == 6) {
return FDDataType::FP64; return FDDataType::FP64;
} else { } else {
FDASSERT(false, "While calling OpenCVDataTypeToFD(), get type = %d, which is not expected.", type); FDASSERT(false,
"While calling OpenCVDataTypeToFD(), get type = %d, which is not "
"expected.",
type);
} }
} }
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
FDDataType FalconCVDataTypeToFD(fcv::FCVImageType type) { FDDataType FlyCVDataTypeToFD(fcv::FCVImageType type) {
if (type == fcv::FCVImageType::GRAY_U8) { if (type == fcv::FCVImageType::GRAY_U8) {
return FDDataType::UINT8; return FDDataType::UINT8;
} else if (type == fcv::FCVImageType::PACKAGE_BGR_U8) { } else if (type == fcv::FCVImageType::PACKAGE_BGR_U8) {
@@ -104,13 +108,14 @@ FDDataType FalconCVDataTypeToFD(fcv::FCVImageType type) {
} else if (type == fcv::FCVImageType::GRAY_F64) { } else if (type == fcv::FCVImageType::GRAY_F64) {
return FDDataType::FP64; return FDDataType::FP64;
} }
FDASSERT(false, "While calling FalconDataTypeToFD(), get unexpected type:%d.", int(type)); FDASSERT(false, "While calling FlyCVDataTypeToFD(), get unexpected type:%d.",
int(type));
return FDDataType::UNKNOWN1; return FDDataType::UNKNOWN1;
} }
fcv::FCVImageType CreateFalconCVDataType(FDDataType type, int channel) { fcv::FCVImageType CreateFlyCVDataType(FDDataType type, int channel) {
FDASSERT(channel == 1 || channel == 3 || channel == 4, FDASSERT(channel == 1 || channel == 3 || channel == 4,
"Only support channel be 1/3/4 in Falcon."); "Only support channel be 1/3/4 in FlyCV.");
if (type == FDDataType::UINT8) { if (type == FDDataType::UINT8) {
if (channel == 1) { if (channel == 1) {
return fcv::FCVImageType::GRAY_U8; return fcv::FCVImageType::GRAY_U8;
@@ -132,18 +137,54 @@ fcv::FCVImageType CreateFalconCVDataType(FDDataType type, int channel) {
return fcv::FCVImageType::PACKAGE_BGR_F32; return fcv::FCVImageType::PACKAGE_BGR_F32;
} }
fcv::Mat ConvertOpenCVMatToFalconCV(cv::Mat& im) { int CreateOpenCVDataType(FDDataType type, int channel) {
FDASSERT(channel == 1 || channel == 3 || channel == 4,
"Only support channel be 1/3/4 in OpenCV.");
if (type == FDDataType::UINT8) {
if (channel == 1) {
return CV_8UC1;
} else if (channel == 3) {
return CV_8UC3;
} else {
return CV_8UC4;
}
} else if (type == FDDataType::FP32) {
if (channel == 1) {
return CV_32FC1;
} else if (channel == 3) {
return CV_32FC3;
} else {
return CV_32FC4;
}
}
FDASSERT(false, "Data type of %s is not supported.", Str(type).c_str());
return CV_32FC3;
}
fcv::Mat ConvertOpenCVMatToFlyCV(cv::Mat& im) {
int type = im.type() % 8; int type = im.type() % 8;
// 0: uint8; 5: float32; 6: float64 // 0: uint8; 5: float32; 6: float64
if (type != 0 && type != 5 && type != 6) { if (type != 0 && type != 5 && type != 6) {
FDASSERT(false, "Only support type of uint8/float/double, but now it's %d.", im.type()); FDASSERT(false, "Only support type of uint8/float/double, but now it's %d.",
im.type());
} }
auto fcv_type = CreateFalconCVDataType(OpenCVDataTypeToFD(im.type()), im.channels()); auto fcv_type =
return fcv::Mat(im.cols, im.rows, fcv_type, im.ptr()); CreateFlyCVDataType(OpenCVDataTypeToFD(im.type()), im.channels());
return fcv::Mat(im.cols, im.rows, fcv_type, im.ptr()); // reference only
}
cv::Mat ConvertFlyCVMatToOpenCV(fcv::Mat& fim) {
auto fd_dtype = FlyCVDataTypeToFD(fim.type());
if (fd_dtype != FDDataType::UINT8 && fd_dtype != FDDataType::FP32 &&
fd_dtype != FDDataType::FP64) {
FDASSERT(false, "Only support type of uint8/float/double, but now it's %s.",
Str(fd_dtype).c_str());
}
auto ocv_type = CreateOpenCVDataType(fd_dtype, fim.channels());
return cv::Mat(fim.height(), fim.width(), ocv_type,
fim.data()); // reference only
} }
#endif #endif
} // namespace vision
} // namespace fastdeploy
} // namespace vision
} // namespace fastdeploy

View File

@@ -14,12 +14,12 @@
#pragma once #pragma once
#include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/utils/utils.h" #include "fastdeploy/utils/utils.h"
#include "opencv2/core/core.hpp" #include "opencv2/core/core.hpp"
#include "fastdeploy/core/fd_tensor.h"
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
#include "flycv.h" // NOLINT #include "flycv.h" // NOLINT
#endif #endif
namespace fastdeploy { namespace fastdeploy {
@@ -27,14 +27,18 @@ namespace vision {
// Convert data type of opencv to FDDataType // Convert data type of opencv to FDDataType
FDDataType OpenCVDataTypeToFD(int type); FDDataType OpenCVDataTypeToFD(int type);
// Create data type of opencv by FDDataType
int CreateOpenCVDataType(FDDataType type, int channel = 1);
#ifdef ENABLE_FLYCV #ifdef ENABLE_FLYCV
// Convert data type of flycv to FDDataType // Convert data type of flycv to FDDataType
FDDataType FalconCVDataTypeToFD(fcv::FCVImageType type); FDDataType FlyCVDataTypeToFD(fcv::FCVImageType type);
// Create data type of flycv by FDDataType // Create data type of flycv by FDDataType
fcv::FCVImageType CreateFalconCVDataType(FDDataType type, int channel = 1); fcv::FCVImageType CreateFlyCVDataType(FDDataType type, int channel = 1);
// Convert cv::Mat to fcv::Mat // Convert cv::Mat to fcv::Mat
fcv::Mat ConvertOpenCVMatToFalconCV(cv::Mat& im); fcv::Mat ConvertOpenCVMatToFlyCV(cv::Mat& im);
// Convert fcv::Mat to fcv::mat
cv::Mat ConvertFlyCVMatToOpenCV(fcv::Mat& fim);
#endif #endif
} // namespace vision } // namespace vision

View File

@@ -21,9 +21,7 @@ namespace vision {
class WarpAffine : public Processor { class WarpAffine : public Processor {
public: public:
WarpAffine(const cv::Mat& trans_matrix, WarpAffine(const cv::Mat& trans_matrix, int width, int height, int interp = 1,
int width, int height,
int interp = 1,
int border_mode = 0, int border_mode = 0,
const cv::Scalar& borderValue = cv::Scalar()) { const cv::Scalar& borderValue = cv::Scalar()) {
trans_matrix_ = trans_matrix; trans_matrix_ = trans_matrix;
@@ -37,7 +35,7 @@ class WarpAffine : public Processor {
bool ImplByOpenCV(Mat* mat); bool ImplByOpenCV(Mat* mat);
std::string Name() { return "WarpAffine"; } std::string Name() { return "WarpAffine"; }
bool SetTransformMatrix(const cv::Mat &trans_matrix) { bool SetTransformMatrix(const cv::Mat& trans_matrix) {
trans_matrix_ = trans_matrix; trans_matrix_ = trans_matrix;
return true; return true;
} }
@@ -46,13 +44,10 @@ class WarpAffine : public Processor {
return std::make_tuple(width_, height_); return std::make_tuple(width_, height_);
} }
static bool Run(Mat* mat, static bool Run(Mat* mat, const cv::Mat& trans_matrix, int width, int height,
const cv::Mat& trans_matrix, int interp = 1, int border_mode = 0,
int width, int height,
int interp = 1,
int border_mode = 0,
const cv::Scalar& borderValue = cv::Scalar(), const cv::Scalar& borderValue = cv::Scalar(),
ProcLib lib = ProcLib::OPENCV); ProcLib lib = ProcLib::DEFAULT);
private: private:
cv::Mat trans_matrix_; cv::Mat trans_matrix_;

View File

@@ -66,7 +66,7 @@ YOLOv5::YOLOv5(const std::string& model_file, const std::string& params_file,
valid_cpu_backends = {Backend::OPENVINO, Backend::ORT}; valid_cpu_backends = {Backend::OPENVINO, Backend::ORT};
valid_gpu_backends = {Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::ORT, Backend::TRT};
} else { } else {
valid_cpu_backends = {Backend::PDINFER, Backend::ORT}; valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::LITE};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
} }
runtime_option = custom_option; runtime_option = custom_option;
@@ -184,11 +184,14 @@ void YOLOv5::UseCudaPreprocessing(int max_image_size) {
use_cuda_preprocessing_ = true; use_cuda_preprocessing_ = true;
is_scale_up_ = true; is_scale_up_ = true;
if (input_img_cuda_buffer_host_ == nullptr) { if (input_img_cuda_buffer_host_ == nullptr) {
// prepare input data cache in GPU pinned memory // prepare input data cache in GPU pinned memory
CUDA_CHECK(cudaMallocHost((void**)&input_img_cuda_buffer_host_, max_image_size * 3)); CUDA_CHECK(cudaMallocHost((void**)&input_img_cuda_buffer_host_,
max_image_size * 3));
// prepare input data cache in GPU device memory // prepare input data cache in GPU device memory
CUDA_CHECK(cudaMalloc((void**)&input_img_cuda_buffer_device_, max_image_size * 3)); CUDA_CHECK(
CUDA_CHECK(cudaMalloc((void**)&input_tensor_cuda_buffer_device_, 3 * size_[0] * size_[1] * sizeof(float))); cudaMalloc((void**)&input_img_cuda_buffer_device_, max_image_size * 3));
CUDA_CHECK(cudaMalloc((void**)&input_tensor_cuda_buffer_device_,
3 * size_[0] * size_[1] * sizeof(float)));
} }
#else #else
FDWARNING << "The FastDeploy didn't compile with BUILD_CUDA_SRC=ON." FDWARNING << "The FastDeploy didn't compile with BUILD_CUDA_SRC=ON."
@@ -197,15 +200,17 @@ void YOLOv5::UseCudaPreprocessing(int max_image_size) {
#endif #endif
} }
bool YOLOv5::CudaPreprocess(Mat* mat, FDTensor* output, bool YOLOv5::CudaPreprocess(
std::map<std::string, std::array<float, 2>>* im_info, Mat* mat, FDTensor* output,
const std::vector<int>& size, std::map<std::string, std::array<float, 2>>* im_info,
const std::vector<float> padding_value, const std::vector<int>& size, const std::vector<float> padding_value,
bool is_mini_pad, bool is_no_pad, bool is_scale_up, bool is_mini_pad, bool is_no_pad, bool is_scale_up, int stride,
int stride, float max_wh, bool multi_label) { float max_wh, bool multi_label) {
#ifdef ENABLE_CUDA_PREPROCESS #ifdef ENABLE_CUDA_PREPROCESS
if (is_mini_pad != false || is_no_pad != false || is_scale_up != true) { if (is_mini_pad != false || is_no_pad != false || is_scale_up != true) {
FDERROR << "Preprocessing with CUDA is only available when the arguments satisfy (is_mini_pad=false, is_no_pad=false, is_scale_up=true)." << std::endl; FDERROR << "Preprocessing with CUDA is only available when the arguments "
"satisfy (is_mini_pad=false, is_no_pad=false, is_scale_up=true)."
<< std::endl;
return false; return false;
} }
@@ -219,14 +224,15 @@ bool YOLOv5::CudaPreprocess(Mat* mat, FDTensor* output,
int src_img_buf_size = mat->Height() * mat->Width() * mat->Channels(); int src_img_buf_size = mat->Height() * mat->Width() * mat->Channels();
memcpy(input_img_cuda_buffer_host_, mat->Data(), src_img_buf_size); memcpy(input_img_cuda_buffer_host_, mat->Data(), src_img_buf_size);
CUDA_CHECK(cudaMemcpyAsync(input_img_cuda_buffer_device_, CUDA_CHECK(cudaMemcpyAsync(input_img_cuda_buffer_device_,
input_img_cuda_buffer_host_, input_img_cuda_buffer_host_, src_img_buf_size,
src_img_buf_size, cudaMemcpyHostToDevice, stream)); cudaMemcpyHostToDevice, stream));
utils::CudaYoloPreprocess(input_img_cuda_buffer_device_, mat->Width(), utils::CudaYoloPreprocess(input_img_cuda_buffer_device_, mat->Width(),
mat->Height(), input_tensor_cuda_buffer_device_, mat->Height(), input_tensor_cuda_buffer_device_,
size[0], size[1], padding_value, stream); size[0], size[1], padding_value, stream);
// Record output shape of preprocessed image // Record output shape of preprocessed image
(*im_info)["output_shape"] = {static_cast<float>(size[0]), static_cast<float>(size[1])}; (*im_info)["output_shape"] = {static_cast<float>(size[0]),
static_cast<float>(size[1])};
output->SetExternalData({mat->Channels(), size[0], size[1]}, FDDataType::FP32, output->SetExternalData({mat->Channels(), size[0], size[1]}, FDDataType::FP32,
input_tensor_cuda_buffer_device_); input_tensor_cuda_buffer_device_);
@@ -339,22 +345,21 @@ bool YOLOv5::Postprocess(
bool YOLOv5::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold, bool YOLOv5::Predict(cv::Mat* im, DetectionResult* result, float conf_threshold,
float nms_iou_threshold) { float nms_iou_threshold) {
Mat mat(*im); Mat mat(*im);
std::map<std::string, std::array<float, 2>> im_info; std::map<std::string, std::array<float, 2>> im_info;
if (use_cuda_preprocessing_) { if (use_cuda_preprocessing_) {
if (!CudaPreprocess(&mat, &reused_input_tensors[0], &im_info, size_, padding_value_, if (!CudaPreprocess(&mat, &reused_input_tensors[0], &im_info, size_,
is_mini_pad_, is_no_pad_, is_scale_up_, stride_, max_wh_, padding_value_, is_mini_pad_, is_no_pad_, is_scale_up_,
multi_label_)) { stride_, max_wh_, multi_label_)) {
FDERROR << "Failed to preprocess input image." << std::endl; FDERROR << "Failed to preprocess input image." << std::endl;
return false; return false;
} }
} else { } else {
if (!Preprocess(&mat, &reused_input_tensors[0], &im_info, size_, padding_value_, if (!Preprocess(&mat, &reused_input_tensors[0], &im_info, size_,
is_mini_pad_, is_no_pad_, is_scale_up_, stride_, max_wh_, padding_value_, is_mini_pad_, is_no_pad_, is_scale_up_,
multi_label_)) { stride_, max_wh_, multi_label_)) {
FDERROR << "Failed to preprocess input image." << std::endl; FDERROR << "Failed to preprocess input image." << std::endl;
return false; return false;
} }

View File

@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/detection/contrib/yolov6.h" #include "fastdeploy/vision/detection/contrib/yolov6.h"
#include "fastdeploy/utils/perf.h" #include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h" #include "fastdeploy/vision/utils/utils.h"
#ifdef ENABLE_CUDA_PREPROCESS #ifdef ENABLE_CUDA_PREPROCESS
@@ -69,7 +70,7 @@ YOLOv6::YOLOv6(const std::string& model_file, const std::string& params_file,
valid_cpu_backends = {Backend::OPENVINO, Backend::ORT}; valid_cpu_backends = {Backend::OPENVINO, Backend::ORT};
valid_gpu_backends = {Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::ORT, Backend::TRT};
} else { } else {
valid_cpu_backends = {Backend::PDINFER, Backend::ORT}; valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::LITE};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
} }
runtime_option = custom_option; runtime_option = custom_option;
@@ -174,11 +175,14 @@ void YOLOv6::UseCudaPreprocessing(int max_image_size) {
use_cuda_preprocessing_ = true; use_cuda_preprocessing_ = true;
is_scale_up = true; is_scale_up = true;
if (input_img_cuda_buffer_host_ == nullptr) { if (input_img_cuda_buffer_host_ == nullptr) {
// prepare input data cache in GPU pinned memory // prepare input data cache in GPU pinned memory
CUDA_CHECK(cudaMallocHost((void**)&input_img_cuda_buffer_host_, max_image_size * 3)); CUDA_CHECK(cudaMallocHost((void**)&input_img_cuda_buffer_host_,
max_image_size * 3));
// prepare input data cache in GPU device memory // prepare input data cache in GPU device memory
CUDA_CHECK(cudaMalloc((void**)&input_img_cuda_buffer_device_, max_image_size * 3)); CUDA_CHECK(
CUDA_CHECK(cudaMalloc((void**)&input_tensor_cuda_buffer_device_, 3 * size[0] * size[1] * sizeof(float))); cudaMalloc((void**)&input_img_cuda_buffer_device_, max_image_size * 3));
CUDA_CHECK(cudaMalloc((void**)&input_tensor_cuda_buffer_device_,
3 * size[0] * size[1] * sizeof(float)));
} }
#else #else
FDWARNING << "The FastDeploy didn't compile with BUILD_CUDA_SRC=ON." FDWARNING << "The FastDeploy didn't compile with BUILD_CUDA_SRC=ON."
@@ -187,11 +191,14 @@ void YOLOv6::UseCudaPreprocessing(int max_image_size) {
#endif #endif
} }
bool YOLOv6::CudaPreprocess(Mat* mat, FDTensor* output, bool YOLOv6::CudaPreprocess(
std::map<std::string, std::array<float, 2>>* im_info) { Mat* mat, FDTensor* output,
std::map<std::string, std::array<float, 2>>* im_info) {
#ifdef ENABLE_CUDA_PREPROCESS #ifdef ENABLE_CUDA_PREPROCESS
if (is_mini_pad != false || is_no_pad != false || is_scale_up != true) { if (is_mini_pad != false || is_no_pad != false || is_scale_up != true) {
FDERROR << "Preprocessing with CUDA is only available when the arguments satisfy (is_mini_pad=false, is_no_pad=false, is_scale_up=true)." << std::endl; FDERROR << "Preprocessing with CUDA is only available when the arguments "
"satisfy (is_mini_pad=false, is_no_pad=false, is_scale_up=true)."
<< std::endl;
return false; return false;
} }
@@ -205,14 +212,15 @@ bool YOLOv6::CudaPreprocess(Mat* mat, FDTensor* output,
int src_img_buf_size = mat->Height() * mat->Width() * mat->Channels(); int src_img_buf_size = mat->Height() * mat->Width() * mat->Channels();
memcpy(input_img_cuda_buffer_host_, mat->Data(), src_img_buf_size); memcpy(input_img_cuda_buffer_host_, mat->Data(), src_img_buf_size);
CUDA_CHECK(cudaMemcpyAsync(input_img_cuda_buffer_device_, CUDA_CHECK(cudaMemcpyAsync(input_img_cuda_buffer_device_,
input_img_cuda_buffer_host_, input_img_cuda_buffer_host_, src_img_buf_size,
src_img_buf_size, cudaMemcpyHostToDevice, stream)); cudaMemcpyHostToDevice, stream));
utils::CudaYoloPreprocess(input_img_cuda_buffer_device_, mat->Width(), utils::CudaYoloPreprocess(input_img_cuda_buffer_device_, mat->Width(),
mat->Height(), input_tensor_cuda_buffer_device_, mat->Height(), input_tensor_cuda_buffer_device_,
size[0], size[1], padding_value, stream); size[0], size[1], padding_value, stream);
// Record output shape of preprocessed image // Record output shape of preprocessed image
(*im_info)["output_shape"] = {static_cast<float>(size[0]), static_cast<float>(size[1])}; (*im_info)["output_shape"] = {static_cast<float>(size[0]),
static_cast<float>(size[1])};
output->SetExternalData({mat->Channels(), size[0], size[1]}, FDDataType::FP32, output->SetExternalData({mat->Channels(), size[0], size[1]}, FDDataType::FP32,
input_tensor_cuda_buffer_device_); input_tensor_cuda_buffer_device_);

View File

@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/detection/contrib/yolov7.h" #include "fastdeploy/vision/detection/contrib/yolov7.h"
#include "fastdeploy/utils/perf.h" #include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h" #include "fastdeploy/vision/utils/utils.h"
#ifdef ENABLE_CUDA_PREPROCESS #ifdef ENABLE_CUDA_PREPROCESS
@@ -67,7 +68,7 @@ YOLOv7::YOLOv7(const std::string& model_file, const std::string& params_file,
valid_cpu_backends = {Backend::OPENVINO, Backend::ORT}; valid_cpu_backends = {Backend::OPENVINO, Backend::ORT};
valid_gpu_backends = {Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::ORT, Backend::TRT};
} else { } else {
valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT}; valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::LITE};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
} }
runtime_option = custom_option; runtime_option = custom_option;
@@ -172,11 +173,14 @@ void YOLOv7::UseCudaPreprocessing(int max_image_size) {
use_cuda_preprocessing_ = true; use_cuda_preprocessing_ = true;
is_scale_up = true; is_scale_up = true;
if (input_img_cuda_buffer_host_ == nullptr) { if (input_img_cuda_buffer_host_ == nullptr) {
// prepare input data cache in GPU pinned memory // prepare input data cache in GPU pinned memory
CUDA_CHECK(cudaMallocHost((void**)&input_img_cuda_buffer_host_, max_image_size * 3)); CUDA_CHECK(cudaMallocHost((void**)&input_img_cuda_buffer_host_,
max_image_size * 3));
// prepare input data cache in GPU device memory // prepare input data cache in GPU device memory
CUDA_CHECK(cudaMalloc((void**)&input_img_cuda_buffer_device_, max_image_size * 3)); CUDA_CHECK(
CUDA_CHECK(cudaMalloc((void**)&input_tensor_cuda_buffer_device_, 3 * size[0] * size[1] * sizeof(float))); cudaMalloc((void**)&input_img_cuda_buffer_device_, max_image_size * 3));
CUDA_CHECK(cudaMalloc((void**)&input_tensor_cuda_buffer_device_,
3 * size[0] * size[1] * sizeof(float)));
} }
#else #else
FDWARNING << "The FastDeploy didn't compile with BUILD_CUDA_SRC=ON." FDWARNING << "The FastDeploy didn't compile with BUILD_CUDA_SRC=ON."
@@ -185,11 +189,14 @@ void YOLOv7::UseCudaPreprocessing(int max_image_size) {
#endif #endif
} }
bool YOLOv7::CudaPreprocess(Mat* mat, FDTensor* output, bool YOLOv7::CudaPreprocess(
std::map<std::string, std::array<float, 2>>* im_info) { Mat* mat, FDTensor* output,
std::map<std::string, std::array<float, 2>>* im_info) {
#ifdef ENABLE_CUDA_PREPROCESS #ifdef ENABLE_CUDA_PREPROCESS
if (is_mini_pad != false || is_no_pad != false || is_scale_up != true) { if (is_mini_pad != false || is_no_pad != false || is_scale_up != true) {
FDERROR << "Preprocessing with CUDA is only available when the arguments satisfy (is_mini_pad=false, is_no_pad=false, is_scale_up=true)." << std::endl; FDERROR << "Preprocessing with CUDA is only available when the arguments "
"satisfy (is_mini_pad=false, is_no_pad=false, is_scale_up=true)."
<< std::endl;
return false; return false;
} }
@@ -203,14 +210,15 @@ bool YOLOv7::CudaPreprocess(Mat* mat, FDTensor* output,
int src_img_buf_size = mat->Height() * mat->Width() * mat->Channels(); int src_img_buf_size = mat->Height() * mat->Width() * mat->Channels();
memcpy(input_img_cuda_buffer_host_, mat->Data(), src_img_buf_size); memcpy(input_img_cuda_buffer_host_, mat->Data(), src_img_buf_size);
CUDA_CHECK(cudaMemcpyAsync(input_img_cuda_buffer_device_, CUDA_CHECK(cudaMemcpyAsync(input_img_cuda_buffer_device_,
input_img_cuda_buffer_host_, input_img_cuda_buffer_host_, src_img_buf_size,
src_img_buf_size, cudaMemcpyHostToDevice, stream)); cudaMemcpyHostToDevice, stream));
utils::CudaYoloPreprocess(input_img_cuda_buffer_device_, mat->Width(), utils::CudaYoloPreprocess(input_img_cuda_buffer_device_, mat->Width(),
mat->Height(), input_tensor_cuda_buffer_device_, mat->Height(), input_tensor_cuda_buffer_device_,
size[0], size[1], padding_value, stream); size[0], size[1], padding_value, stream);
// Record output shape of preprocessed image // Record output shape of preprocessed image
(*im_info)["output_shape"] = {static_cast<float>(size[0]), static_cast<float>(size[1])}; (*im_info)["output_shape"] = {static_cast<float>(size[0]),
static_cast<float>(size[1])};
output->SetExternalData({mat->Channels(), size[0], size[1]}, FDDataType::FP32, output->SetExternalData({mat->Channels(), size[0], size[1]}, FDDataType::FP32,
input_tensor_cuda_buffer_device_); input_tensor_cuda_buffer_device_);

7
fastdeploy/vision/matting/contrib/modnet.cc Executable file → Normal file
View File

@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/matting/contrib/modnet.h" #include "fastdeploy/vision/matting/contrib/modnet.h"
#include "fastdeploy/utils/perf.h" #include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h" #include "fastdeploy/vision/utils/utils.h"
@@ -26,8 +27,8 @@ MODNet::MODNet(const std::string& model_file, const std::string& params_file,
const RuntimeOption& custom_option, const RuntimeOption& custom_option,
const ModelFormat& model_format) { const ModelFormat& model_format) {
if (model_format == ModelFormat::ONNX) { if (model_format == ModelFormat::ONNX) {
valid_cpu_backends = {Backend::ORT}; valid_cpu_backends = {Backend::ORT};
valid_gpu_backends = {Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::ORT, Backend::TRT};
} else { } else {
valid_cpu_backends = {Backend::PDINFER, Backend::ORT}; valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
@@ -118,7 +119,7 @@ bool MODNet::Postprocess(
int numel = ipt_h * ipt_w; int numel = ipt_h * ipt_w;
int nbytes = numel * sizeof(float); int nbytes = numel * sizeof(float);
result->Resize(numel); result->Resize(numel);
std::memcpy(result->alpha.data(), alpha_resized.GetOpenCVMat()->data, nbytes); std::memcpy(result->alpha.data(), alpha_resized.Data(), nbytes);
return true; return true;
} }

33
fastdeploy/vision/matting/contrib/rvm.cc Executable file → Normal file
View File

@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/matting/contrib/rvm.h" #include "fastdeploy/vision/matting/contrib/rvm.h"
#include "fastdeploy/utils/perf.h" #include "fastdeploy/utils/perf.h"
#include "fastdeploy/vision/utils/utils.h" #include "fastdeploy/vision/utils/utils.h"
@@ -22,12 +23,13 @@ namespace vision {
namespace matting { namespace matting {
RobustVideoMatting::RobustVideoMatting(const std::string& model_file, const std::string& params_file, RobustVideoMatting::RobustVideoMatting(const std::string& model_file,
const RuntimeOption& custom_option, const std::string& params_file,
const ModelFormat& model_format) { const RuntimeOption& custom_option,
const ModelFormat& model_format) {
if (model_format == ModelFormat::ONNX) { if (model_format == ModelFormat::ONNX) {
valid_cpu_backends = {Backend::OPENVINO, Backend::ORT}; valid_cpu_backends = {Backend::OPENVINO, Backend::ORT};
valid_gpu_backends = {Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::ORT, Backend::TRT};
} else { } else {
valid_cpu_backends = {Backend::PDINFER, Backend::ORT}; valid_cpu_backends = {Backend::PDINFER, Backend::ORT};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
@@ -52,8 +54,9 @@ bool RobustVideoMatting::Initialize() {
return true; return true;
} }
bool RobustVideoMatting::Preprocess(Mat* mat, FDTensor* output, bool RobustVideoMatting::Preprocess(
std::map<std::string, std::array<int, 2>>* im_info) { Mat* mat, FDTensor* output,
std::map<std::string, std::array<int, 2>>* im_info) {
// Resize // Resize
int resize_w = size[0]; int resize_w = size[0];
int resize_h = size[1]; int resize_h = size[1];
@@ -61,7 +64,7 @@ bool RobustVideoMatting::Preprocess(Mat* mat, FDTensor* output,
Resize::Run(mat, resize_w, resize_h); Resize::Run(mat, resize_w, resize_h);
} }
BGR2RGB::Run(mat); BGR2RGB::Run(mat);
// Normalize // Normalize
std::vector<float> alpha = {1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f}; std::vector<float> alpha = {1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f};
std::vector<float> beta = {0.0f, 0.0f, 0.0f}; std::vector<float> beta = {0.0f, 0.0f, 0.0f};
@@ -83,7 +86,7 @@ bool RobustVideoMatting::Postprocess(
FDASSERT((infer_result.size() == 6), FDASSERT((infer_result.size() == 6),
"The default number of output tensor must be 6 according to " "The default number of output tensor must be 6 according to "
"RobustVideoMatting."); "RobustVideoMatting.");
FDTensor& fgr = infer_result.at(0); // fgr (1, 3, h, w) 0.~1. FDTensor& fgr = infer_result.at(0); // fgr (1, 3, h, w) 0.~1.
FDTensor& alpha = infer_result.at(1); // alpha (1, 1, h, w) 0.~1. FDTensor& alpha = infer_result.at(1); // alpha (1, 1, h, w) 0.~1.
FDASSERT((fgr.shape[0] == 1), "Only support batch = 1 now."); FDASSERT((fgr.shape[0] == 1), "Only support batch = 1 now.");
FDASSERT((alpha.shape[0] == 1), "Only support batch = 1 now."); FDASSERT((alpha.shape[0] == 1), "Only support batch = 1 now.");
@@ -98,11 +101,11 @@ bool RobustVideoMatting::Postprocess(
// update context // update context
if (video_mode) { if (video_mode) {
for (size_t i = 0; i < 4; ++i) { for (size_t i = 0; i < 4; ++i) {
FDTensor& rki = infer_result.at(i+2); FDTensor& rki = infer_result.at(i + 2);
dynamic_inputs_dims_[i] = rki.shape; dynamic_inputs_dims_[i] = rki.shape;
dynamic_inputs_datas_[i].resize(rki.Numel()); dynamic_inputs_datas_[i].resize(rki.Numel());
memcpy(dynamic_inputs_datas_[i].data(), rki.Data(), memcpy(dynamic_inputs_datas_[i].data(), rki.Data(),
rki.Numel() * FDDataTypeSize(rki.dtype)); rki.Numel() * FDDataTypeSize(rki.dtype));
} }
} }
@@ -139,8 +142,8 @@ bool RobustVideoMatting::Postprocess(
int numel = in_h * in_w; int numel = in_h * in_w;
int nbytes = numel * sizeof(float); int nbytes = numel * sizeof(float);
result->Resize(numel); result->Resize(numel);
memcpy(result->alpha.data(), alpha_resized.GetOpenCVMat()->data, nbytes); memcpy(result->alpha.data(), alpha_resized.Data(), nbytes);
memcpy(result->foreground.data(), fgr_resized.GetOpenCVMat()->data, nbytes); memcpy(result->foreground.data(), fgr_resized.Data(), nbytes);
return true; return true;
} }
@@ -154,7 +157,9 @@ bool RobustVideoMatting::Predict(cv::Mat* im, MattingResult* result) {
im_info["output_shape"] = {mat.Height(), mat.Width()}; im_info["output_shape"] = {mat.Height(), mat.Width()};
// convert vector to FDTensor // convert vector to FDTensor
for (size_t i = 1; i < inputs_nums; ++i) { for (size_t i = 1; i < inputs_nums; ++i) {
input_tensors[i].SetExternalData(dynamic_inputs_dims_[i-1], FDDataType::FP32, dynamic_inputs_datas_[i-1].data()); input_tensors[i].SetExternalData(dynamic_inputs_dims_[i - 1],
FDDataType::FP32,
dynamic_inputs_datas_[i - 1].data());
input_tensors[i].device = Device::CPU; input_tensors[i].device = Device::CPU;
} }
if (!Preprocess(&mat, &input_tensors[0], &im_info)) { if (!Preprocess(&mat, &input_tensors[0], &im_info)) {

14
fastdeploy/vision/matting/ppmatting/ppmatting.cc Executable file → Normal file
View File

@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/matting/ppmatting/ppmatting.h" #include "fastdeploy/vision/matting/ppmatting/ppmatting.h"
#include "fastdeploy/vision/utils/utils.h" #include "fastdeploy/vision/utils/utils.h"
#include "yaml-cpp/yaml.h" #include "yaml-cpp/yaml.h"
@@ -171,15 +172,15 @@ bool PPMatting::Postprocess(
std::vector<int64_t> dim{0, 2, 3, 1}; std::vector<int64_t> dim{0, 2, 3, 1};
Transpose(alpha_tensor, &alpha_tensor, dim); Transpose(alpha_tensor, &alpha_tensor, dim);
alpha_tensor.Squeeze(0); alpha_tensor.Squeeze(0);
Mat mat = CreateFromTensor(alpha_tensor); Mat mat = CreateFDMatFromTensor(alpha_tensor);
auto iter_ipt = im_info.find("input_shape"); auto iter_ipt = im_info.find("input_shape");
auto iter_out = im_info.find("output_shape"); auto iter_out = im_info.find("output_shape");
if (is_fixed_input_shape_){ if (is_fixed_input_shape_) {
double scale_h = static_cast<double>(iter_out->second[0]) / double scale_h = static_cast<double>(iter_out->second[0]) /
static_cast<double>(iter_ipt->second[0]); static_cast<double>(iter_ipt->second[0]);
double scale_w = static_cast<double>(iter_out->second[1]) / double scale_w = static_cast<double>(iter_out->second[1]) /
static_cast<double>(iter_ipt->second[1]); static_cast<double>(iter_ipt->second[1]);
double actual_scale = std::min(scale_h, scale_w); double actual_scale = std::min(scale_h, scale_w);
int size_before_pad_h = round(actual_scale * iter_ipt->second[0]); int size_before_pad_h = round(actual_scale * iter_ipt->second[0]);
@@ -188,7 +189,8 @@ bool PPMatting::Postprocess(
Crop::Run(&mat, 0, 0, size_before_pad_w, size_before_pad_h); Crop::Run(&mat, 0, 0, size_before_pad_w, size_before_pad_h);
} }
Resize::Run(&mat, iter_ipt->second[1], iter_ipt->second[0]); Resize::Run(&mat, iter_ipt->second[1], iter_ipt->second[0], -1.0f, -1.0f, 1,
false, ProcLib::OPENCV);
result->Clear(); result->Clear();
// note: must be setup shape before Resize // note: must be setup shape before Resize
@@ -197,7 +199,7 @@ bool PPMatting::Postprocess(
int numel = iter_ipt->second[0] * iter_ipt->second[1]; int numel = iter_ipt->second[0] * iter_ipt->second[1];
int nbytes = numel * sizeof(float); int nbytes = numel * sizeof(float);
result->Resize(numel); result->Resize(numel);
std::memcpy(result->alpha.data(), mat.GetOpenCVMat()->data, nbytes); std::memcpy(result->alpha.data(), mat.Data(), nbytes);
return true; return true;
} }

31
fastdeploy/vision/segmentation/ppseg/model.cc Executable file → Normal file
View File

@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "fastdeploy/vision/segmentation/ppseg/model.h" #include "fastdeploy/vision/segmentation/ppseg/model.h"
#include "fastdeploy/vision/utils/utils.h" #include "fastdeploy/vision/utils/utils.h"
#include "yaml-cpp/yaml.h" #include "yaml-cpp/yaml.h"
@@ -26,7 +27,8 @@ PaddleSegModel::PaddleSegModel(const std::string& model_file,
const RuntimeOption& custom_option, const RuntimeOption& custom_option,
const ModelFormat& model_format) { const ModelFormat& model_format) {
config_file_ = config_file; config_file_ = config_file;
valid_cpu_backends = {Backend::OPENVINO, Backend::PDINFER, Backend::ORT, Backend::LITE}; valid_cpu_backends = {Backend::OPENVINO, Backend::PDINFER, Backend::ORT,
Backend::LITE};
valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT}; valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
valid_rknpu_backends = {Backend::RKNPU2}; valid_rknpu_backends = {Backend::RKNPU2};
runtime_option = custom_option; runtime_option = custom_option;
@@ -68,7 +70,7 @@ bool PaddleSegModel::BuildPreprocessPipelineFromConfig() {
FDASSERT(op.IsMap(), FDASSERT(op.IsMap(),
"Require the transform information in yaml be Map type."); "Require the transform information in yaml be Map type.");
if (op["type"].as<std::string>() == "Normalize") { if (op["type"].as<std::string>() == "Normalize") {
if(!(this->disable_normalize_and_permute)){ if (!(this->disable_normalize_and_permute)) {
std::vector<float> mean = {0.5, 0.5, 0.5}; std::vector<float> mean = {0.5, 0.5, 0.5};
std::vector<float> std = {0.5, 0.5, 0.5}; std::vector<float> std = {0.5, 0.5, 0.5};
if (op["mean"]) { if (op["mean"]) {
@@ -102,12 +104,13 @@ bool PaddleSegModel::BuildPreprocessPipelineFromConfig() {
int input_width = input_shape[3].as<int>(); int input_width = input_shape[3].as<int>();
if (input_height == -1 || input_width == -1) { if (input_height == -1 || input_width == -1) {
FDWARNING << "The exported PaddleSeg model is with dynamic shape input, " FDWARNING << "The exported PaddleSeg model is with dynamic shape input, "
<< "which is not supported by ONNX Runtime and Tensorrt. " << "which is not supported by ONNX Runtime and Tensorrt. "
<< "Only OpenVINO and Paddle Inference are available now. " << "Only OpenVINO and Paddle Inference are available now. "
<< "For using ONNX Runtime or Tensorrt, " << "For using ONNX Runtime or Tensorrt, "
<< "Please refer to https://github.com/PaddlePaddle/PaddleSeg/blob/develop/docs/model_export.md" << "Please refer to "
<< " to export model with fixed input shape." "https://github.com/PaddlePaddle/PaddleSeg/blob/develop/"
<< std::endl; "docs/model_export.md"
<< " to export model with fixed input shape." << std::endl;
valid_cpu_backends = {Backend::OPENVINO, Backend::PDINFER, Backend::LITE}; valid_cpu_backends = {Backend::OPENVINO, Backend::PDINFER, Backend::LITE};
valid_gpu_backends = {Backend::PDINFER}; valid_gpu_backends = {Backend::PDINFER};
} }
@@ -132,7 +135,7 @@ bool PaddleSegModel::BuildPreprocessPipelineFromConfig() {
<< "." << std::endl; << "." << std::endl;
} }
} }
if(!(this->disable_normalize_and_permute)){ if (!(this->disable_normalize_and_permute)) {
processors_.push_back(std::make_shared<HWC2CHW>()); processors_.push_back(std::make_shared<HWC2CHW>());
} }
return true; return true;
@@ -260,7 +263,7 @@ bool PaddleSegModel::Postprocess(
infer_result->shape, FDDataType::FP32, infer_result->shape, FDDataType::FP32,
static_cast<void*>(fp32_result_buffer->data())); static_cast<void*>(fp32_result_buffer->data()));
} }
mat = new Mat(CreateFromTensor(*infer_result)); mat = new Mat(CreateFDMatFromTensor(*infer_result));
Resize::Run(mat, ipt_w, ipt_h, -1.0f, -1.0f, 1); Resize::Run(mat, ipt_w, ipt_h, -1.0f, -1.0f, 1);
mat->ShareWithTensor(&new_infer_result); mat->ShareWithTensor(&new_infer_result);
result->shape = new_infer_result.shape; result->shape = new_infer_result.shape;
@@ -361,11 +364,13 @@ bool PaddleSegModel::Predict(cv::Mat* im, SegmentationResult* result) {
return true; return true;
} }
void PaddleSegModel::DisableNormalizeAndPermute(){ void PaddleSegModel::DisableNormalizeAndPermute() {
this->disable_normalize_and_permute = true; this->disable_normalize_and_permute = true;
// the DisableNormalizeAndPermute function will be invalid if the configuration file is loaded during preprocessing // the DisableNormalizeAndPermute function will be invalid if the
// configuration file is loaded during preprocessing
if (!BuildPreprocessPipelineFromConfig()) { if (!BuildPreprocessPipelineFromConfig()) {
FDERROR << "Failed to build preprocess pipeline from configuration file." << std::endl; FDERROR << "Failed to build preprocess pipeline from configuration file."
<< std::endl;
} }
} }

View File

@@ -75,7 +75,8 @@ class FASTDEPLOY_DECL Trajectory : public TKalmanFilter {
int timestamp, int timestamp,
bool update_embedding = true); bool update_embedding = true);
virtual void activate(int &cnt, int timestamp); virtual void activate(int &cnt, int timestamp);
virtual void reactivate(Trajectory *traj, int &cnt, int timestamp, bool newid = false); virtual void reactivate(Trajectory *traj, int &cnt,
int timestamp, bool newid = false);
virtual void mark_lost(void); virtual void mark_lost(void);
virtual void mark_removed(void); virtual void mark_removed(void);
@@ -229,6 +230,6 @@ inline void Trajectory::mark_lost(void) { state = Lost; }
inline void Trajectory::mark_removed(void) { state = Removed; } inline void Trajectory::mark_removed(void) { state = Removed; }
} // namespace tracking } // namespace tracking
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -18,9 +18,9 @@ namespace fastdeploy {
namespace vision { namespace vision {
namespace utils { namespace utils {
bool CropImageByBox(const Mat& src_im, Mat* dst_im, bool CropImageByBox(Mat& src_im, Mat* dst_im,
const std::vector<float>& box, std::vector<float>* center, const std::vector<float>& box, std::vector<float>* center,
std::vector<float>* scale, const float expandratio) { std::vector<float>* scale, const float expandratio) {
const cv::Mat* img = src_im.GetOpenCVMat(); const cv::Mat* img = src_im.GetOpenCVMat();
cv::Mat* crop_img = dst_im->GetOpenCVMat(); cv::Mat* crop_img = dst_im->GetOpenCVMat();
int xmin = static_cast<int>(box[0]); int xmin = static_cast<int>(box[0]);
@@ -34,12 +34,12 @@ bool CropImageByBox(const Mat& src_im, Mat* dst_im,
// adjust h or w to keep image ratio, expand the shorter edge // adjust h or w to keep image ratio, expand the shorter edge
if (half_h * 3 > half_w * 4) { if (half_h * 3 > half_w * 4) {
half_w = half_h * 0.75; half_w = half_h * 0.75;
} }
int crop_xmin =std::max(0, static_cast<int>(centerx - half_w)); int crop_xmin = std::max(0, static_cast<int>(centerx - half_w));
int crop_ymin =std::max(0, static_cast<int>(centery - half_h)); int crop_ymin = std::max(0, static_cast<int>(centery - half_h));
int crop_xmax = std::min(img->cols - 1, static_cast<int>(centerx + half_w)); int crop_xmax = std::min(img->cols - 1, static_cast<int>(centerx + half_w));
int crop_ymax = std::min(img->rows - 1, static_cast<int>(centery + half_h)); int crop_ymax = std::min(img->rows - 1, static_cast<int>(centery + half_h));
crop_img->create(crop_ymax - crop_ymin, crop_xmax - crop_xmin, img->type()); crop_img->create(crop_ymax - crop_ymin, crop_xmax - crop_xmin, img->type());
*crop_img = *crop_img =
(*img)(cv::Range(crop_ymin, crop_ymax), cv::Range(crop_xmin, crop_xmax)); (*img)(cv::Range(crop_ymin, crop_ymax), cv::Range(crop_xmin, crop_xmax));

View File

@@ -17,6 +17,7 @@
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <set> #include <set>
#include <vector> #include <vector>
#include "fastdeploy/core/fd_tensor.h" #include "fastdeploy/core/fd_tensor.h"
#include "fastdeploy/utils/utils.h" #include "fastdeploy/utils/utils.h"
#include "fastdeploy/vision/common/result.h" #include "fastdeploy/vision/common/result.h"
@@ -43,7 +44,7 @@ std::vector<int32_t> TopKIndices(const T* array, int array_size, int topk) {
std::vector<int32_t> res(topk); std::vector<int32_t> res(topk);
std::set<int32_t> searched; std::set<int32_t> searched;
for (int32_t i = 0; i < topk; ++i) { for (int32_t i = 0; i < topk; ++i) {
T min = -99999999; T min = static_cast<T>(-99999999);
for (int32_t j = 0; j < array_size; ++j) { for (int32_t j = 0; j < array_size; ++j) {
if (searched.find(j) != searched.end()) { if (searched.find(j) != searched.end()) {
continue; continue;
@@ -75,23 +76,25 @@ FASTDEPLOY_DECL float CosineSimilarity(const std::vector<float>& a,
const std::vector<float>& b, const std::vector<float>& b,
bool normalized = true); bool normalized = true);
bool CropImageByBox(const Mat& src_im, Mat* dst_im, bool CropImageByBox(Mat& src_im, Mat* dst_im,
const std::vector<float>& box, std::vector<float>* center, const std::vector<float>& box, std::vector<float>* center,
std::vector<float>* scale, const float expandratio = 0.3); std::vector<float>* scale, const float expandratio = 0.3);
/** /**
* Function: for keypoint detection model, fine positioning of keypoints in postprocess * Function: for keypoint detection model, fine positioning of keypoints in
* Parameters: * postprocess
* heatmap: model inference results for keypoint detection models * Parameters:
* dim: shape information of the inference result * heatmap: model inference results for keypoint detection models
* coords: coordinates after refined positioning * dim: shape information of the inference result
* px: px = int(coords[ch * 2] + 0.5) , refer to API detection::GetFinalPredictions * coords: coordinates after refined positioning
* py: px = int(coords[ch * 2 + 1] + 0.5), refer to API detection::GetFinalPredictions * px: px = int(coords[ch * 2] + 0.5) , refer to API detection::GetFinalPredictions
* index: index information of heatmap pixels * py: px = int(coords[ch * 2 + 1] + 0.5), refer to API detection::GetFinalPredictions
* ch: channel * index: index information of heatmap pixels
* Paper reference: DARK postpocessing, Zhang et al. Distribution-Aware Coordinate * ch: channel
* Representation for Human Pose Estimation (CVPR 2020). * Paper reference: DARK postpocessing, Zhang et al.
*/ * Distribution-Aware Coordinate Representation for Human Pose Estimation (CVPR
* 2020).
*/
void DarkParse(const std::vector<float>& heatmap, const std::vector<int>& dim, void DarkParse(const std::vector<float>& heatmap, const std::vector<int>& dim,
std::vector<float>* coords, const int px, const int py, std::vector<float>* coords, const int px, const int py,
const int index, const int ch); const int index, const int ch);