From bb00e0757e30c4e77b9dc406d99179e6a891fab1 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 1 Nov 2022 10:42:52 +0800 Subject: [PATCH] [Model] Integrate FlyCV image processing library (#375) * Add falcon cmake * Update CMakeLists.txt * Add implementation for more preprocessors * Add more implementation * Add more implementations by flycv * Fix some name * fix some bug * ready to test * add fuse * fix fuse problem * add debug code * add debug * [Android] integrate flycv to Android (#459) * [Android] integrate flycv into Andorid * Falconcv (#27) * [Bug Fix ]Update ppseg visualize python api && demo (#454) Update ppseg visualize python api && demo * [Model] update pptracking letterboxresize and add some comment (#438) * add override mark * delete some * recovery * recovery * add tracking * add tracking py_bind and example * add pptracking * add pptracking * iomanip head file * add opencv_video lib * add python libs package Signed-off-by: ChaoII <849453582@qq.com> * complete comments Signed-off-by: ChaoII <849453582@qq.com> * add jdeTracker_ member variable Signed-off-by: ChaoII <849453582@qq.com> * add 'FASTDEPLOY_DECL' macro Signed-off-by: ChaoII <849453582@qq.com> * remove kwargs params Signed-off-by: ChaoII <849453582@qq.com> * [Doc]update pptracking docs * delete 'ENABLE_PADDLE_FRONTEND' switch * add pptracking unit test * update pptracking unit test Signed-off-by: ChaoII <849453582@qq.com> * modify test video file path and remove trt test * update unit test model url * remove 'FASTDEPLOY_DECL' macro Signed-off-by: ChaoII <849453582@qq.com> * fix build python packages about pptracking on win32 Signed-off-by: ChaoII <849453582@qq.com> * update comment Signed-off-by: ChaoII <849453582@qq.com> * add pptracking model explain Signed-off-by: ChaoII <849453582@qq.com> Signed-off-by: ChaoII <849453582@qq.com> Co-authored-by: Jason Signed-off-by: ChaoII <849453582@qq.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: ChaoII <849453582@qq.com> Co-authored-by: Jason * [cmake] optimize examples CMakeLists.txt Signed-off-by: ChaoII <849453582@qq.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: ChaoII <849453582@qq.com> Co-authored-by: Jason * remove debug log * remove function * remove function Signed-off-by: ChaoII <849453582@qq.com> Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com> Co-authored-by: huangjianhui <852142024@qq.com> Co-authored-by: ChaoII <849453582@qq.com> --- CMakeLists.txt | 25 +--- FastDeploy.cmake.in | 15 ++ cmake/flycv.cmake | 130 +++++++++++++++++ cmake/opencv.cmake | 2 +- cmake/paddlelite.cmake | 12 +- examples/CMakeLists.txt | 28 +++- fastdeploy/core/config.h.in | 8 +- fastdeploy/vision/common/processors/base.cc | 35 ++++- fastdeploy/vision/common/processors/base.h | 9 +- fastdeploy/vision/common/processors/cast.cc | 33 +++++ fastdeploy/vision/common/processors/cast.h | 13 +- .../vision/common/processors/center_crop.cc | 24 +++- .../vision/common/processors/center_crop.h | 9 +- .../common/processors/color_space_convert.cc | 28 ++++ .../common/processors/color_space_convert.h | 14 +- .../vision/common/processors/convert.cc | 17 +++ fastdeploy/vision/common/processors/convert.h | 5 +- fastdeploy/vision/common/processors/crop.cc | 25 +++- fastdeploy/vision/common/processors/crop.h | 6 +- .../vision/common/processors/hwc2chw.cc | 24 +++- fastdeploy/vision/common/processors/hwc2chw.h | 9 +- .../common/processors/limit_by_stride.cc | 46 ++++++ .../common/processors/limit_by_stride.h | 6 +- .../vision/common/processors/limit_long.cc | 44 ++++++ .../vision/common/processors/limit_long.h | 5 +- .../vision/common/processors/limit_short.cc | 43 +++++- .../vision/common/processors/limit_short.h | 3 + fastdeploy/vision/common/processors/mat.cc | 31 +++- fastdeploy/vision/common/processors/mat.h | 25 ++-- .../vision/common/processors/normalize.cc | 23 +++ .../vision/common/processors/normalize.h | 17 ++- .../processors/normalize_and_permute.cc | 111 +++++++++++++++ .../common/processors/normalize_and_permute.h | 67 +++++++++ fastdeploy/vision/common/processors/pad.cc | 39 +++++ fastdeploy/vision/common/processors/pad.h | 9 +- .../vision/common/processors/pad_to_size.cc | 62 ++++++++ .../vision/common/processors/pad_to_size.h | 5 +- fastdeploy/vision/common/processors/resize.cc | 67 +++++++++ fastdeploy/vision/common/processors/resize.h | 5 +- .../common/processors/resize_by_short.cc | 42 ++++++ .../common/processors/resize_by_short.h | 5 +- .../vision/common/processors/stride_pad.cc | 50 +++++++ .../vision/common/processors/stride_pad.h | 5 +- .../vision/common/processors/transform.h | 83 +++++++++++ fastdeploy/vision/common/processors/utils.cc | 21 ++- fastdeploy/vision/common/processors/utils.h | 17 ++- fastdeploy/vision/detection/ppdet/ppyoloe.cc | 4 + .../vision/tracking/pptracking/model.cc | 1 - fastdeploy/vision/vision_pybind.cc | 3 + python/fastdeploy/vision/__init__.py | 9 ++ python/setup.py | 1 + scripts/fastdeploy_init.sh | 11 ++ tests/gtest_utils.h | 34 ++++- tests/vision_preprocess/test_vision_cast.cc | 51 +++++++ .../test_vision_center_crop.cc | 79 +++++++++++ .../test_vision_colorspace_convert.cc | 76 ++++++++++ .../vision_preprocess/test_vision_convert.cc | 85 +++++++++++ tests/vision_preprocess/test_vision_crop.cc | 79 +++++++++++ .../vision_preprocess/test_vision_hwc2chw.cc | 78 ++++++++++ .../test_vision_limit_long.cc | 108 ++++++++++++++ .../test_vision_limit_stride.cc | 133 ++++++++++++++++++ .../test_vision_normalize.cc | 116 +++++++++++++++ .../test_vision_normalize_and_permute.cc | 65 +++++++++ tests/vision_preprocess/test_vision_pad.cc | 78 ++++++++++ .../test_vision_pad_to_size.cc | 78 ++++++++++ tests/vision_preprocess/test_vision_resize.cc | 104 ++++++++++++++ .../test_vision_resize_by_short.cc | 104 ++++++++++++++ .../test_vision_stride_pad.cc | 78 ++++++++++ 68 files changed, 2571 insertions(+), 106 deletions(-) create mode 100644 cmake/flycv.cmake create mode 100644 fastdeploy/vision/common/processors/normalize_and_permute.cc create mode 100644 fastdeploy/vision/common/processors/normalize_and_permute.h create mode 100644 tests/vision_preprocess/test_vision_cast.cc create mode 100644 tests/vision_preprocess/test_vision_center_crop.cc create mode 100644 tests/vision_preprocess/test_vision_colorspace_convert.cc create mode 100644 tests/vision_preprocess/test_vision_convert.cc create mode 100644 tests/vision_preprocess/test_vision_crop.cc create mode 100644 tests/vision_preprocess/test_vision_hwc2chw.cc create mode 100644 tests/vision_preprocess/test_vision_limit_long.cc create mode 100644 tests/vision_preprocess/test_vision_limit_stride.cc create mode 100644 tests/vision_preprocess/test_vision_normalize.cc create mode 100644 tests/vision_preprocess/test_vision_normalize_and_permute.cc create mode 100644 tests/vision_preprocess/test_vision_pad.cc create mode 100644 tests/vision_preprocess/test_vision_pad_to_size.cc create mode 100644 tests/vision_preprocess/test_vision_resize.cc create mode 100644 tests/vision_preprocess/test_vision_resize_by_short.cc create mode 100644 tests/vision_preprocess/test_vision_stride_pad.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ef40b69c..ded88c54e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,8 +60,8 @@ option(ENABLE_POROS_BACKEND "Whether to enable poros backend." OFF) option(ENABLE_OPENVINO_BACKEND "Whether to enable openvino backend." OFF) option(ENABLE_LITE_BACKEND "Whether to enable paddle lite backend." OFF) option(ENABLE_VISION "Whether to enable vision models usage." OFF) -option(ENABLE_VISION_VISUALIZE "Whether to enable visualize vision model result toolbox." ON) option(ENABLE_TEXT "Whether to enable text models usage." OFF) +option(ENABLE_FLYCV "Whether to enable flycv to boost image preprocess." OFF) option(WITH_TESTING "Whether to compile with unittest." OFF) ############################# Options for Android cross compiling ######################### option(WITH_OPENCV_STATIC "Use OpenCV static lib for Android." OFF) @@ -136,16 +136,6 @@ set(HEAD_DIR "${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}") include_directories(${HEAD_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR}) -if(BUILD_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples) - if(ENABLE_VISION) - # ENABLE_VISION_VISUALIZE must be ON if enable vision examples. - message(STATUS "Found BUILD_EXAMPLES and ENABLE_VISION ON, so, force ENABLE_VISION_VISUALIZE ON") - set(ENABLE_VISION_VISUALIZE ON CACHE BOOL "force to enable visualize vision model result toolbox" FORCE) - else() - message(WARNING "BUILD_EXAMPLES is ON, but found ENABLE_VISION OFF, will skip vision examples.") - endif() -endif() - if(ANDROID OR IOS) if(ENABLE_ORT_BACKEND) message(FATAL_ERROR "Not support ONNXRuntime backend for Andorid/IOS now. Please set ENABLE_ORT_BACKEND=OFF.") @@ -382,6 +372,8 @@ endif() if(ENABLE_VISION) add_definitions(-DENABLE_VISION) +# set(ENABLE_VISION_VISUALIZE ON) + add_definitions(-DENABLE_VISION_VISUALIZE) if(ENABLE_OPENCV_CUDA) if(NOT WITH_GPU) message(FATAL_ERROR "ENABLE_OPENCV_CUDA is available on Linux and WITH_GPU=ON, but now WITH_GPU=OFF.") @@ -402,13 +394,10 @@ if(ENABLE_VISION) include_directories(${PROJECT_SOURCE_DIR}/third_party/yaml-cpp/include) include(${PROJECT_SOURCE_DIR}/cmake/opencv.cmake) - if(ENABLE_VISION_VISUALIZE) - add_definitions(-DENABLE_VISION_VISUALIZE) - endif() -else() - if(ENABLE_VISION_VISUALIZE) - message("While ENABLE_VISION=OFF, will force ENABLE_VISION_VISUALIZE=OFF.") - set(ENABLE_VISION_VISUALIZE OFF) + if(ENABLE_FLYCV) + add_definitions(-DENABLE_FLYCV) + include(${PROJECT_SOURCE_DIR}/cmake/flycv.cmake) + list(APPEND DEPEND_LIBS external_flycv) endif() endif() diff --git a/FastDeploy.cmake.in b/FastDeploy.cmake.in index 524b1e25c..e1a1cf2f4 100755 --- a/FastDeploy.cmake.in +++ b/FastDeploy.cmake.in @@ -10,6 +10,7 @@ set(POROS_VERSION @POROS_VERSION@) set(ENABLE_TRT_BACKEND @ENABLE_TRT_BACKEND@) set(ENABLE_PADDLE_FRONTEND @ENABLE_PADDLE_FRONTEND@) set(ENABLE_VISION @ENABLE_VISION@) +set(ENABLE_FLYCV @ENABLE_FLYCV@) set(ENABLE_TEXT @ENABLE_TEXT@) set(BUILD_ON_JETSON @BUILD_ON_JETSON@) set(PADDLEINFERENCE_VERSION @PADDLEINFERENCE_VERSION@) @@ -185,6 +186,20 @@ if(ENABLE_VISION) list(APPEND FASTDEPLOY_INCS ${OpenCV_INCLUDE_DIRS}) list(APPEND FASTDEPLOY_LIBS ${OpenCV_LIBS}) endif() + + if(ENABLE_FLYCV) + include_directories(${CMAKE_CURRENT_LIST_DIR}/third_libs/install/flycv/include) + set(FLYCV_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/flycv/lib) + if(ANDROID) + add_library(flycv_shared STATIC IMPORTED GLOBAL) + set_property(TARGET flycv_shared PROPERTY IMPORTED_LOCATION ${FLYCV_LIB_DIR}/${ANDROID_ABI}/libflycv_shared.so) + list(APPEND FASTDEPLOY_LIBS flycv_shared) + else() + find_library(FLYCV_LIB flycv_shared ${FLYCV_LIB_DIR} NO_DEFAULT_PATH) + list(APPEND FASTDEPLOY_LIBS ${FLYCV_LIB}) + endif() + endif() + endif() if (ENABLE_TEXT) diff --git a/cmake/flycv.cmake b/cmake/flycv.cmake new file mode 100644 index 000000000..52a77fb80 --- /dev/null +++ b/cmake/flycv.cmake @@ -0,0 +1,130 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +include(ExternalProject) + +set(FLYCV_PROJECT "extern_flycv") +set(FLYCV_PREFIX_DIR ${THIRD_PARTY_PATH}/flycv) +set(FLYCV_SOURCE_DIR + ${THIRD_PARTY_PATH}/flycv/src/${FLYCV_PROJECT}) +set(FLYCV_INSTALL_DIR ${THIRD_PARTY_PATH}/install/flycv) +set(FLYCV_INC_DIR + "${FLYCV_INSTALL_DIR}/include" + CACHE PATH "flycv include directory." FORCE) +if(ANDROID) + set(FLYCV_LIB_DIR + "${FLYCV_INSTALL_DIR}/lib/${ANDROID_ABI}" + CACHE PATH "flycv lib directory." FORCE) +else() + set(FLYCV_LIB_DIR + "${FLYCV_INSTALL_DIR}/lib/" + CACHE PATH "flycv lib directory." FORCE) +endif() +set(CMAKE_BUILD_RPATH "${CMAKE_BUILD_RPATH}" + "${FLYCV_LIB_DIR}") + +include_directories(${FLYCV_INC_DIR}) + +# ABI check +if(ANDROID) + if((NOT ANDROID_ABI MATCHES "armeabi-v7a") AND (NOT ANDROID_ABI MATCHES "arm64-v8a")) + message(FATAL_ERROR "FastDeploy with FlyCV only support armeabi-v7a, arm64-v8a now.") + endif() + if(NOT ANDROID_TOOLCHAIN MATCHES "clang") + message(FATAL_ERROR "Currently, only support clang toolchain while cross compiling FastDeploy for Android with FlyCV, but found ${ANDROID_TOOLCHAIN}.") + endif() +endif() + +if(WIN32) + set(FLYCV_COMPILE_LIB + "${FLYCV_INSTALL_DIR}/lib/flycv.lib" + CACHE FILEPATH "flycv compile library." FORCE) +elseif(APPLE) + set(FLYCV_COMPILE_LIB + "${FLYCV_INSTALL_DIR}/lib/libflycv.dylib" + CACHE FILEPATH "flycv compile library." FORCE) +elseif(ANDROID) + set(FLYCV_COMPILE_LIB + "${FLYCV_INSTALL_DIR}/lib/${ANDROID_ABI}/libflycv_shared.so" + CACHE FILEPATH "flycv compile library." FORCE) +else() + set(FLYCV_COMPILE_LIB + "${FLYCV_INSTALL_DIR}/lib/libflycv_shared.so" + CACHE FILEPATH "flycv compile library." FORCE) +endif(WIN32) + +set(FLYCV_URL_BASE "https://bj.bcebos.com/fastdeploy/third_libs/") +set(FLYCV_VERSION "1.0.1025") +if(WIN32) + message(FATAL_ERROR "FlyCV is not supported on Windows now.") + set(FLYCV_FILE "flycv-win-x64-${FLYCV_VERSION}.zip") +elseif(APPLE) + message(FATAL_ERROR "FlyCV is not supported on Mac OSX now.") + if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "arm64") + set(FLYCV_FILE "flycv-osx-arm64-${FLYCV_VERSION}.tgz") + else() + set(FLYCV_FILE "flycv-osx-x86_64-${FLYCV_VERSION}.tgz") + endif() +elseif(ANDROID) + set(FLYCV_FILE "flycv-android-${FLYCV_VERSION}.tgz") +else() + if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64") + set(FLYCV_FILE "flycv-linux-aarch64-${FLYCV_VERSION}.tgz") + else() + message(FATAL_ERROR "FlyCV is not supported on Linux x64 now.") + set(FLYCV_FILE "flycv-linux-x64-${FLYCV_VERSION}.tgz") + endif() +endif() +set(FLYCV_URL "${FLYCV_URL_BASE}${FLYCV_FILE}") + +if(ANDROID) + ExternalProject_Add( + ${FLYCV_PROJECT} + ${EXTERNAL_PROJECT_LOG_ARGS} + URL ${FLYCV_URL} + PREFIX ${FLYCV_PREFIX_DIR} + DOWNLOAD_NO_PROGRESS 1 + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + UPDATE_COMMAND "" + INSTALL_COMMAND + ${CMAKE_COMMAND} -E remove_directory ${FLYCV_INSTALL_DIR} && + ${CMAKE_COMMAND} -E make_directory ${FLYCV_INSTALL_DIR} && + ${CMAKE_COMMAND} -E make_directory ${FLYCV_INSTALL_DIR}/lib && + ${CMAKE_COMMAND} -E rename ${FLYCV_SOURCE_DIR}/lib/${ANDROID_ABI} + ${FLYCV_LIB_DIR} && ${CMAKE_COMMAND} -E copy_directory + ${FLYCV_SOURCE_DIR}/include ${FLYCV_INC_DIR} + BUILD_BYPRODUCTS ${FLYCV_COMPILE_LIB}) +else() + ExternalProject_Add( + ${FLYCV_PROJECT} + ${EXTERNAL_PROJECT_LOG_ARGS} + URL ${FLYCV_URL} + PREFIX ${FLYCV_PREFIX_DIR} + DOWNLOAD_NO_PROGRESS 1 + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + UPDATE_COMMAND "" + INSTALL_COMMAND + ${CMAKE_COMMAND} -E remove_directory ${FLYCV_INSTALL_DIR} && + ${CMAKE_COMMAND} -E make_directory ${FLYCV_INSTALL_DIR} && + ${CMAKE_COMMAND} -E rename ${FLYCV_SOURCE_DIR}/lib/ + ${FLYCV_LIB_DIR} && ${CMAKE_COMMAND} -E copy_directory + ${FLYCV_SOURCE_DIR}/include ${FLYCV_INC_DIR} + BUILD_BYPRODUCTS ${FLYCV_COMPILE_LIB}) +endif() + +add_library(external_flycv STATIC IMPORTED GLOBAL) +set_property(TARGET external_flycv PROPERTY IMPORTED_LOCATION + ${FLYCV_COMPILE_LIB}) +add_dependencies(external_flycv ${FLYCV_PROJECT}) diff --git a/cmake/opencv.cmake b/cmake/opencv.cmake index 4b2f09293..90ecfa095 100644 --- a/cmake/opencv.cmake +++ b/cmake/opencv.cmake @@ -184,7 +184,7 @@ else() if (WIN32) set(OpenCV_DIR ${OpenCV_DIR}/build) endif() - find_package(OpenCV REQUIRED PATHS ${OpenCV_DIR}) + find_package(OpenCV REQUIRED PATHS ${OpenCV_DIR} NO_DEFAULT_PATH) include_directories(${OpenCV_INCLUDE_DIRS}) list(APPEND DEPEND_LIBS opencv_core opencv_video opencv_highgui opencv_imgproc opencv_imgcodecs) endif() diff --git a/cmake/paddlelite.cmake b/cmake/paddlelite.cmake index a2489c34b..1f807bfce 100644 --- a/cmake/paddlelite.cmake +++ b/cmake/paddlelite.cmake @@ -23,9 +23,15 @@ set(PADDLELITE_INSTALL_DIR ${THIRD_PARTY_PATH}/install/${PADDLELITE_FILENAME}) set(PADDLELITE_INC_DIR "${PADDLELITE_INSTALL_DIR}/include" CACHE PATH "paddlelite include directory." FORCE) -set(PADDLELITE_LIB_DIR - "${PADDLELITE_INSTALL_DIR}/lib/${ANDROID_ABI}" - CACHE PATH "paddlelite lib directory." FORCE) +if(ANDROID) + set(PADDLELITE_LIB_DIR + "${PADDLELITE_INSTALL_DIR}/lib/${ANDROID_ABI}" + CACHE PATH "paddlelite lib directory." FORCE) +else() + set(PADDLELITE_LIB_DIR + "${PADDLELITE_INSTALL_DIR}/lib/" + CACHE PATH "paddlelite lib directory." FORCE) +endif() set(CMAKE_BUILD_RPATH "${CMAKE_BUILD_RPATH}" "${PADDLELITE_LIB_DIR}") set(PADDLELITE_URL_PREFIX "https://bj.bcebos.com/fastdeploy/third_libs") diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 70498f593..6f3c65988 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -37,6 +37,12 @@ function(config_fastdeploy_executable_link_flags TARGET_NAME) endif() 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) function(add_fastdeploy_executable FIELD CC_FILE) # temp target name/file var in function scope @@ -66,15 +72,25 @@ endfunction() # vision examples if(BUILD_EXAMPLES AND ENABLE_VISION) - if(NOT ENABLE_VISION_VISUALIZE) - message(FATAL_ERROR "ENABLE_VISION_VISUALIZE must be ON while BUILD_EXAMPLES and ENABLE_VISION both ON.") - endif() if(EXISTS ${PROJECT_SOURCE_DIR}/examples/vision) message(STATUS "") message(STATUS "*************FastDeploy Vision Examples Summary**********") - file(GLOB_RECURSE ALL_VISION_EXAMPLE_SRCS ${PROJECT_SOURCE_DIR}/examples/vision/*/*/cpp/*.cc) - foreach(_CC_FILE ${ALL_VISION_EXAMPLE_SRCS}) - add_fastdeploy_executable(vision ${_CC_FILE}) + set(ALL_VISION_SUD_FIELDS classification + detection + facedet + faceid + keypointdetection + matting + ocr + segmentation) + if(NOT ANDROID) + list(APPEND ALL_VISION_SUD_FIELDS tracking) + endif() + foreach(_SUB_FIELD ${ALL_VISION_SUD_FIELDS}) + add_fastdeploy_executable_cc_files(_SUB_CC_FILES vision ${_SUB_FIELD}) + foreach(_CC_FILE ${_SUB_CC_FILES}) + add_fastdeploy_executable(vision ${_CC_FILE}) + endforeach() endforeach() message(STATUS " [FastDeploy Executable Path] : ${EXECUTABLE_OUTPUT_PATH}") endif() diff --git a/fastdeploy/core/config.h.in b/fastdeploy/core/config.h.in index 8d6a07bb6..21c398af0 100755 --- a/fastdeploy/core/config.h.in +++ b/fastdeploy/core/config.h.in @@ -49,8 +49,8 @@ #cmakedefine ENABLE_VISION #endif -#ifndef ENABLE_FALCONCV -#cmakedefine ENABLE_FALCONCV +#ifndef ENABLE_FLYCV +#cmakedefine ENABLE_FLYCV #endif #ifndef ENABLE_TEXT @@ -61,8 +61,10 @@ #cmakedefine ENABLE_OPENCV_CUDA #endif +#ifdef ENABLE_VISION #ifndef ENABLE_VISION_VISUALIZE -#cmakedefine ENABLE_VISION_VISUALIZE +#define ENABLE_VISION_VISUALIZE +#endif #endif #ifndef ENABLE_FDTENSOR_FUNC diff --git a/fastdeploy/vision/common/processors/base.cc b/fastdeploy/vision/common/processors/base.cc index e6a945dac..4e2629702 100644 --- a/fastdeploy/vision/common/processors/base.cc +++ b/fastdeploy/vision/common/processors/base.cc @@ -28,8 +28,39 @@ bool Processor::operator()(Mat* mat, ProcLib lib) { target = default_lib; } - bool ret = ImplByOpenCV(mat); - return ret; + if (target == ProcLib::FLYCV) { +#ifdef ENABLE_FLYCV + if (mat->mat_type != ProcLib::FLYCV) { + 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 + FDASSERT(false, "FastDeploy didn't compile with FalconCV."); +#endif + } + return ImplByOpenCV(mat); +} + +void EnableFlyCV() { +#ifdef ENABLE_FLYCV + Processor::default_lib = ProcLib::FLYCV; + FDINFO << "Will change to use image processing library " + << Processor::default_lib << std::endl; +#else + FDWARNING << "FastDeploy didn't compile with FlyCV, " + "will fallback to use OpenCV instead." + << std::endl; +#endif +} + +void DisableFlyCV() { + Processor::default_lib = ProcLib::OPENCV; + FDINFO << "Will change to use image processing library " + << Processor::default_lib << std::endl; } } // namespace vision diff --git a/fastdeploy/vision/common/processors/base.h b/fastdeploy/vision/common/processors/base.h index b67205636..d985f348f 100644 --- a/fastdeploy/vision/common/processors/base.h +++ b/fastdeploy/vision/common/processors/base.h @@ -22,7 +22,14 @@ namespace fastdeploy { namespace vision { -class Processor { +/*! @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(); + +/// Disable using FlyCV to process image while deploy vision models. +FASTDEPLOY_DECL void DisableFlyCV(); + +class FASTDEPLOY_DECL Processor { public: // default_lib has the highest priority // all the function in `processor` will force to use diff --git a/fastdeploy/vision/common/processors/cast.cc b/fastdeploy/vision/common/processors/cast.cc index 44933345d..62aad3778 100644 --- a/fastdeploy/vision/common/processors/cast.cc +++ b/fastdeploy/vision/common/processors/cast.cc @@ -35,6 +35,39 @@ bool Cast::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool Cast::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + if (dtype_ == "float" && mat->Type() == FDDataType::FP32) { + return true; + } + if (dtype_ == "double" && mat->Type() == FDDataType::FP64) { + return true; + } + if (mat->layout != Layout::HWC) { + FDERROR + << "While using Falcon to cast image, the image must be layout of HWC." + << std::endl; + return false; + } + if (dtype_ == "float") { + fcv::Mat new_im; + auto fcv_type = CreateFalconCVDataType(FDDataType::FP32, im->channels()); + im->convert_to(new_im, fcv_type); + mat->SetMat(new_im); + } else if (dtype_ == "double") { + fcv::Mat new_im; + auto fcv_type = CreateFalconCVDataType(FDDataType::FP64, im->channels()); + im->convert_to(new_im, fcv_type); + mat->SetMat(new_im); + } else { + FDWARNING << "Cast not support for " << dtype_ + << " now! will skip this operation." << std::endl; + } + return true; +} +#endif + bool Cast::Run(Mat* mat, const std::string& dtype, ProcLib lib) { auto c = Cast(dtype); return c(mat, lib); diff --git a/fastdeploy/vision/common/processors/cast.h b/fastdeploy/vision/common/processors/cast.h index f6859caba..a438fb974 100644 --- a/fastdeploy/vision/common/processors/cast.h +++ b/fastdeploy/vision/common/processors/cast.h @@ -19,16 +19,23 @@ namespace fastdeploy { namespace vision { -class Cast : public Processor { +class FASTDEPLOY_DECL Cast : public Processor { public: explicit Cast(const std::string& dtype = "float") : dtype_(dtype) {} bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "Cast"; } static bool Run(Mat* mat, const std::string& dtype, ProcLib lib = ProcLib::OPENCV); + std::string GetDtype() const { + return dtype_; + } + private: std::string dtype_; }; -} // namespace vision -} // namespace fastdeploy +} // namespace vision +} // namespace fastdeploy diff --git a/fastdeploy/vision/common/processors/center_crop.cc b/fastdeploy/vision/common/processors/center_crop.cc index d229347c4..6aca3acd0 100644 --- a/fastdeploy/vision/common/processors/center_crop.cc +++ b/fastdeploy/vision/common/processors/center_crop.cc @@ -28,12 +28,34 @@ bool CenterCrop::ImplByOpenCV(Mat* mat) { int offset_x = static_cast((width - width_) / 2); int offset_y = static_cast((height - height_) / 2); cv::Rect crop_roi(offset_x, offset_y, width_, height_); - *im = (*im)(crop_roi); + cv::Mat new_im = (*im)(crop_roi).clone(); + mat->SetMat(new_im); mat->SetWidth(width_); mat->SetHeight(height_); return true; } +#ifdef ENABLE_FLYCV +bool CenterCrop::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + int height = static_cast(im->height()); + int width = static_cast(im->width()); + if (height < height_ || width < width_) { + FDERROR << "[CenterCrop] Image size less than crop size" << std::endl; + return false; + } + int offset_x = static_cast((width - width_) / 2); + int offset_y = static_cast((height - height_) / 2); + fcv::Rect crop_roi(offset_x, offset_y, width_, height_); + fcv::Mat new_im; + fcv::crop(*im, new_im, crop_roi); + mat->SetMat(new_im); + mat->SetWidth(width_); + mat->SetHeight(height_); + return true; +} +#endif + bool CenterCrop::Run(Mat* mat, const int& width, const int& height, ProcLib lib) { auto c = CenterCrop(width, height); diff --git a/fastdeploy/vision/common/processors/center_crop.h b/fastdeploy/vision/common/processors/center_crop.h index 9079973ce..befeeb5a3 100644 --- a/fastdeploy/vision/common/processors/center_crop.h +++ b/fastdeploy/vision/common/processors/center_crop.h @@ -19,10 +19,13 @@ namespace fastdeploy { namespace vision { -class CenterCrop : public Processor { +class FASTDEPLOY_DECL CenterCrop : public Processor { public: CenterCrop(int width, int height) : height_(height), width_(width) {} bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "CenterCrop"; } static bool Run(Mat* mat, const int& width, const int& height, @@ -33,5 +36,5 @@ class CenterCrop : public Processor { int width_; }; -} // namespace vision -} // namespace fastdeploy +} // namespace vision +} // namespace fastdeploy diff --git a/fastdeploy/vision/common/processors/color_space_convert.cc b/fastdeploy/vision/common/processors/color_space_convert.cc index 7c614a048..15e8aa426 100644 --- a/fastdeploy/vision/common/processors/color_space_convert.cc +++ b/fastdeploy/vision/common/processors/color_space_convert.cc @@ -24,6 +24,20 @@ bool BGR2RGB::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool BGR2RGB::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + if (im->channels() != 3) { + FDERROR << "[BGR2RGB] The channel of input image must be 3, but not it's " << im->channels() << "." << std::endl; + return false; + } + fcv::Mat new_im; + fcv::cvt_color(*im, new_im, fcv::ColorConvertType::CVT_PA_BGR2PA_RGB); + mat->SetMat(new_im); + return true; +} +#endif + bool RGB2BGR::ImplByOpenCV(Mat* mat) { cv::Mat* im = mat->GetOpenCVMat(); cv::Mat new_im; @@ -32,6 +46,20 @@ bool RGB2BGR::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool RGB2BGR::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + if (im->channels() != 3) { + FDERROR << "[RGB2BGR] The channel of input image must be 3, but not it's " << im->channels() << "." << std::endl; + return false; + } + fcv::Mat new_im; + fcv::cvt_color(*im, new_im, fcv::ColorConvertType::CVT_PA_RGB2PA_BGR); + mat->SetMat(new_im); + return true; +} +#endif + bool BGR2RGB::Run(Mat* mat, ProcLib lib) { auto b = BGR2RGB(); return b(mat, lib); diff --git a/fastdeploy/vision/common/processors/color_space_convert.h b/fastdeploy/vision/common/processors/color_space_convert.h index 8b813a10e..ad66acd4f 100644 --- a/fastdeploy/vision/common/processors/color_space_convert.h +++ b/fastdeploy/vision/common/processors/color_space_convert.h @@ -19,20 +19,26 @@ namespace fastdeploy { namespace vision { -class BGR2RGB : public Processor { +class FASTDEPLOY_DECL BGR2RGB : public Processor { public: bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif virtual std::string Name() { return "BGR2RGB"; } static bool Run(Mat* mat, ProcLib lib = ProcLib::OPENCV); }; -class RGB2BGR : public Processor { +class FASTDEPLOY_DECL RGB2BGR : public Processor { public: bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "RGB2BGR"; } static bool Run(Mat* mat, ProcLib lib = ProcLib::OPENCV); }; -} // namespace vision -} // namespace fastdeploy +} // namespace vision +} // namespace fastdeploy diff --git a/fastdeploy/vision/common/processors/convert.cc b/fastdeploy/vision/common/processors/convert.cc index 73ae5a0ef..01c067370 100644 --- a/fastdeploy/vision/common/processors/convert.cc +++ b/fastdeploy/vision/common/processors/convert.cc @@ -39,6 +39,23 @@ bool Convert::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool Convert::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + FDASSERT(im->channels() == 3, "Only support 3-channels image in FalconCV."); + std::vector mean(3, 0); + std::vector std(3, 0); + for (size_t i = 0; i < 3; ++i) { + std[i] = 1.0 / alpha_[i]; + mean[i] = -1 * beta_[i] * std[i]; + } + fcv::Mat new_im; + fcv::normalize_to_submean_to_reorder(*im, mean, std, std::vector(), new_im, true); + mat->SetMat(new_im); + return true; +} +#endif + bool Convert::Run(Mat* mat, const std::vector& alpha, const std::vector& beta, ProcLib lib) { auto c = Convert(alpha, beta); diff --git a/fastdeploy/vision/common/processors/convert.h b/fastdeploy/vision/common/processors/convert.h index ae5db2631..197316a52 100644 --- a/fastdeploy/vision/common/processors/convert.h +++ b/fastdeploy/vision/common/processors/convert.h @@ -18,11 +18,14 @@ namespace fastdeploy { namespace vision { -class Convert : public Processor { +class FASTDEPLOY_DECL Convert : public Processor { public: Convert(const std::vector& alpha, const std::vector& beta); bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "Convert"; } // Compute `result = mat * alpha + beta` directly by channel. diff --git a/fastdeploy/vision/common/processors/crop.cc b/fastdeploy/vision/common/processors/crop.cc index 1b5c8bcb8..fc3a41ecb 100644 --- a/fastdeploy/vision/common/processors/crop.cc +++ b/fastdeploy/vision/common/processors/crop.cc @@ -29,12 +29,35 @@ bool Crop::ImplByOpenCV(Mat* mat) { return false; } cv::Rect crop_roi(offset_w_, offset_h_, width_, height_); - *im = (*im)(crop_roi); + cv::Mat new_im = (*im)(crop_roi).clone(); + mat->SetMat(new_im); mat->SetWidth(width_); mat->SetHeight(height_); return true; } +#ifdef ENABLE_FLYCV +bool Crop::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + int height = static_cast(im->height()); + int width = static_cast(im->width()); + if (height < height_ + offset_h_ || width < width_ + offset_w_) { + FDERROR << "[Crop] Cannot crop [" << height_ << ", " << width_ + << "] from the input image [" << height << ", " << width + << "], with offset [" << offset_h_ << ", " << offset_w_ << "]." + << std::endl; + return false; + } + fcv::Rect crop_roi(offset_w_, offset_h_, width_, height_); + fcv::Mat new_im; + fcv::crop(*im, new_im, crop_roi); + mat->SetMat(new_im); + mat->SetWidth(width_); + mat->SetHeight(height_); + return true; +} +#endif + bool Crop::Run(Mat* mat, int offset_w, int offset_h, int width, int height, ProcLib lib) { auto c = Crop(offset_w, offset_h, width, height); diff --git a/fastdeploy/vision/common/processors/crop.h b/fastdeploy/vision/common/processors/crop.h index 681e1fca2..276d7d6cf 100644 --- a/fastdeploy/vision/common/processors/crop.h +++ b/fastdeploy/vision/common/processors/crop.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { -class Crop : public Processor { +class FASTDEPLOY_DECL Crop : public Processor { public: Crop(int offset_w, int offset_h, int width, int height) { offset_w_ = offset_w; @@ -29,6 +29,10 @@ class Crop : public Processor { } bool ImplByOpenCV(Mat* mat); + +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "Crop"; } static bool Run(Mat* mat, int offset_w, int offset_h, int width, int height, diff --git a/fastdeploy/vision/common/processors/hwc2chw.cc b/fastdeploy/vision/common/processors/hwc2chw.cc index aeb073a74..3af9b559d 100644 --- a/fastdeploy/vision/common/processors/hwc2chw.cc +++ b/fastdeploy/vision/common/processors/hwc2chw.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "fastdeploy/vision/common/processors/hwc2chw.h" +#include "fastdeploy/function/transpose.h" namespace fastdeploy { namespace vision { @@ -28,11 +29,7 @@ bool HWC2CHW::ImplByOpenCV(Mat* mat) { int rw = im->cols; int rc = im->channels(); - // float* data = reinterpret_cast(im->data); for (int i = 0; i < rc; ++i) { - // cv::extractChannel(im_clone, cv::Mat(rh, rw, im->type() % 8, data + i - // * rh * rw), - // i); cv::extractChannel( im_clone, cv::Mat(rh, rw, im->type() % 8, @@ -43,6 +40,25 @@ bool HWC2CHW::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool HWC2CHW::ImplByFalconCV(Mat* mat) { + if (mat->layout != Layout::HWC) { + FDERROR << "HWC2CHW: The input data is not Layout::HWC format!" << std::endl; + return false; + } + if (mat->Type() != FDDataType::FP32) { + FDERROR << "HWC2CHW: Only support float data while use FalconCV, but now it's " << mat->Type() << "." << std::endl; + return false; + } + fcv::Mat* im = mat->GetFalconCVMat(); + 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(), new_im, false); + mat->SetMat(new_im); + mat->layout = Layout::CHW; + return true; +} +#endif + bool HWC2CHW::Run(Mat* mat, ProcLib lib) { auto h = HWC2CHW(); return h(mat, lib); diff --git a/fastdeploy/vision/common/processors/hwc2chw.h b/fastdeploy/vision/common/processors/hwc2chw.h index 13b4dcb62..a21de7b4c 100644 --- a/fastdeploy/vision/common/processors/hwc2chw.h +++ b/fastdeploy/vision/common/processors/hwc2chw.h @@ -19,12 +19,15 @@ namespace fastdeploy { namespace vision { -class HWC2CHW : public Processor { +class FASTDEPLOY_DECL HWC2CHW : public Processor { public: bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "HWC2CHW"; } static bool Run(Mat* mat, ProcLib lib = ProcLib::OPENCV); }; -} // namespace vision -} // namespace fastdeploy +} // namespace vision +} // namespace fastdeploy diff --git a/fastdeploy/vision/common/processors/limit_by_stride.cc b/fastdeploy/vision/common/processors/limit_by_stride.cc index 3f9db27bc..85f3088b8 100644 --- a/fastdeploy/vision/common/processors/limit_by_stride.cc +++ b/fastdeploy/vision/common/processors/limit_by_stride.cc @@ -23,6 +23,12 @@ bool LimitByStride::ImplByOpenCV(Mat* mat) { int origin_h = im->rows; int rw = origin_w - origin_w % stride_; int rh = origin_h - origin_h % stride_; + if (rw == 0) { + rw = stride_; + } + if (rh == 0) { + rh = stride_; + } if (rw != origin_w || rh != origin_w) { cv::resize(*im, *im, cv::Size(rw, rh), 0, 0, interp_); mat->SetWidth(im->cols); @@ -31,6 +37,46 @@ bool LimitByStride::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool LimitByStride::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + int origin_w = im->width(); + int origin_h = im->height(); + int rw = origin_w - origin_w % stride_; + int rh = origin_h - origin_h % stride_; + if (rw == 0) { + rw = stride_; + } + if (rh == 0) { + rh = stride_; + } + if (rw != origin_w || rh != origin_h) { + auto interp_method = fcv::InterpolationType::INTER_LINEAR; + if (interp_ == 0) { + interp_method = fcv::InterpolationType::INTER_NEAREST; + } else if (interp_ == 1) { + interp_method = fcv::InterpolationType::INTER_LINEAR; + } else if (interp_ == 2) { + interp_method = fcv::InterpolationType::INTER_CUBIC; + } else { + FDERROR << "LimitByStride: Only support interp_ be 0/1/2 with FalconCV, but " + "now it's " + << interp_ << "." << std::endl; + return false; + } + + 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); + FDERROR << "After " << new_im.width() << " " << new_im.height() << std::endl; + mat->SetMat(new_im); + mat->SetWidth(new_im.width()); + mat->SetHeight(new_im.height()); + } + return true; +} +#endif + bool LimitByStride::Run(Mat* mat, int stride, int interp, ProcLib lib) { auto r = LimitByStride(stride, interp); return r(mat, lib); diff --git a/fastdeploy/vision/common/processors/limit_by_stride.h b/fastdeploy/vision/common/processors/limit_by_stride.h index 92e4595ea..465bc065d 100644 --- a/fastdeploy/vision/common/processors/limit_by_stride.h +++ b/fastdeploy/vision/common/processors/limit_by_stride.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { -class LimitByStride : public Processor { +class FASTDEPLOY_DECL LimitByStride : public Processor { public: explicit LimitByStride(int stride = 32, int interp = 1) { stride_ = stride; @@ -27,8 +27,10 @@ class LimitByStride : public Processor { } // Resize Mat* mat to make the size divisible by stride_. - bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "LimitByStride"; } static bool Run(Mat* mat, int stride = 32, int interp = 1, diff --git a/fastdeploy/vision/common/processors/limit_long.cc b/fastdeploy/vision/common/processors/limit_long.cc index 0229f63bd..32eaad192 100644 --- a/fastdeploy/vision/common/processors/limit_long.cc +++ b/fastdeploy/vision/common/processors/limit_long.cc @@ -38,6 +38,50 @@ bool LimitLong::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool LimitLong::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + int origin_w = im->width(); + int origin_h = im->height(); + int im_size_max = std::max(origin_w, origin_h); + int target = im_size_max; + if (max_long_ > 0 && im_size_max > max_long_) { + target = max_long_; + } else if (min_long_ > 0 && im_size_max < min_long_) { + target = min_long_; + } + if (target != im_size_max) { + double scale = + static_cast(target) / static_cast(im_size_max); + if (fabs(scale - 1.0) < 1e-06) { + return true; + } + auto interp_method = fcv::InterpolationType::INTER_LINEAR; + if (interp_ == 0) { + interp_method = fcv::InterpolationType::INTER_NEAREST; + } else if (interp_ == 1) { + interp_method = fcv::InterpolationType::INTER_LINEAR; + } else if (interp_ == 2) { + interp_method = fcv::InterpolationType::INTER_CUBIC; + } else { + FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FalconCV, but " + "now it's " + << interp_ << "." << std::endl; + return false; + } + 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); + FDERROR << "after " << new_im.width() << " " << new_im.height() << std::endl; + mat->SetMat(new_im); + mat->SetWidth(new_im.width()); + mat->SetHeight(new_im.height()); + } + return true; +} +#endif + bool LimitLong::Run(Mat* mat, int max_long, int min_long, int interp, ProcLib lib) { auto l = LimitLong(max_long, min_long, interp); return l(mat, lib); diff --git a/fastdeploy/vision/common/processors/limit_long.h b/fastdeploy/vision/common/processors/limit_long.h index 81a92f802..e21ddbf2f 100644 --- a/fastdeploy/vision/common/processors/limit_long.h +++ b/fastdeploy/vision/common/processors/limit_long.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { -class LimitLong : public Processor { +class FASTDEPLOY_DECL LimitLong : public Processor { public: explicit LimitLong(int max_long = -1, int min_long = -1, int interp = 1) { max_long_ = max_long; @@ -33,6 +33,9 @@ class LimitLong : public Processor { // If the long edge is smaller than min_long_, resize the long edge // to min_long_, while scale the short edge proportionally. bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "LimitLong"; } static bool Run(Mat* mat, int max_long = -1, int min_long = -1, diff --git a/fastdeploy/vision/common/processors/limit_short.cc b/fastdeploy/vision/common/processors/limit_short.cc index ad0aaaeb6..56eb234f8 100644 --- a/fastdeploy/vision/common/processors/limit_short.cc +++ b/fastdeploy/vision/common/processors/limit_short.cc @@ -32,7 +32,7 @@ bool LimitShort::ImplByOpenCV(Mat* mat) { if (target != im_size_min) { scale = static_cast(target) / static_cast(im_size_min); } - if (scale > 0) { + if (fabs(scale - 1.0) > 1e-06) { cv::resize(*im, *im, cv::Size(), scale, scale, interp_); mat->SetWidth(im->cols); mat->SetHeight(im->rows); @@ -40,6 +40,47 @@ bool LimitShort::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool LimitShort::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + int origin_w = im->width(); + int origin_h = im->height(); + int im_size_min = std::min(origin_w, origin_h); + int target = im_size_min; + if (max_short_ > 0 && im_size_min > max_short_) { + target = max_short_; + } else if (min_short_ > 0 && im_size_min < min_short_) { + target = min_short_; + } + double scale = -1.f; + if (target != im_size_min) { + scale = static_cast(target) / static_cast(im_size_min); + } + if (fabs(scale - 1.0) > 1e-06) { + auto interp_method = fcv::InterpolationType::INTER_LINEAR; + if (interp_ == 0) { + interp_method = fcv::InterpolationType::INTER_NEAREST; + } else if (interp_ == 1) { + interp_method = fcv::InterpolationType::INTER_LINEAR; + } else if (interp_ == 2) { + interp_method = fcv::InterpolationType::INTER_CUBIC; + } else { + FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FalconCV, but " + "now it's " + << interp_ << "." << std::endl; + return false; + } + + fcv::Mat new_im; + fcv::resize(*im, new_im, fcv::Size(), scale, scale, interp_method); + mat->SetMat(new_im); + mat->SetWidth(new_im.width()); + mat->SetHeight(new_im.height()); + } + return true; +} +#endif + bool LimitShort::Run(Mat* mat, int max_short, int min_short, int interp, ProcLib lib) { auto l = LimitShort(max_short, min_short, interp); return l(mat, lib); diff --git a/fastdeploy/vision/common/processors/limit_short.h b/fastdeploy/vision/common/processors/limit_short.h index 854385274..8f6af3467 100644 --- a/fastdeploy/vision/common/processors/limit_short.h +++ b/fastdeploy/vision/common/processors/limit_short.h @@ -33,6 +33,9 @@ class LimitShort : public Processor { // If the short edge is smaller than min_short_, resize the short edge // to min_short_, while scale the long edge proportionally. bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "LimitShort"; } static bool Run(Mat* mat, int max_short = -1, int min_short = -1, diff --git a/fastdeploy/vision/common/processors/mat.cc b/fastdeploy/vision/common/processors/mat.cc index efc109f0b..a906ff760 100644 --- a/fastdeploy/vision/common/processors/mat.cc +++ b/fastdeploy/vision/common/processors/mat.cc @@ -19,8 +19,8 @@ namespace fastdeploy { namespace vision { void* Mat::Data() { - if (mat_type == ProcLib::FALCONCV) { -#ifdef ENABLE_FALCONCV + if (mat_type == ProcLib::FLYCV) { +#ifdef ENABLE_FLYCV return fcv_mat.data(); #else FDASSERT(false, "FastDeploy didn't compile with FalconCV, but met data type with fcv::Mat."); @@ -52,8 +52,8 @@ bool Mat::CopyToTensor(FDTensor* tensor) { } void Mat::PrintInfo(const std::string& flag) { - if (mat_type == ProcLib::FALCONCV) { -#ifdef ENABLE_FALCONCV + if (mat_type == ProcLib::FLYCV) { +#ifdef ENABLE_FLYCV fcv::Scalar mean = fcv::mean(fcv_mat); std::cout << flag << ": " << "DataType=" << Type() << ", " @@ -85,8 +85,8 @@ void Mat::PrintInfo(const std::string& flag) { FDDataType Mat::Type() { int type = -1; - if (mat_type == ProcLib::FALCONCV) { -#ifdef ENABLE_FALCONCV + if (mat_type == ProcLib::FLYCV) { +#ifdef ENABLE_FLYCV return FalconCVDataTypeToFD(fcv_mat.type()); #else FDASSERT(false, "FastDeploy didn't compile with FalconCV, but met data type with fcv::Mat."); @@ -146,6 +146,25 @@ Mat CreateFromTensor(const FDTensor& tensor) { return mat; } +std::ostream& operator<<(std::ostream& out,const ProcLib& p) { + switch (p) { + case ProcLib::DEFAULT: + out << "ProcLib::DEFAULT"; + break; + case ProcLib::OPENCV: + out << "ProcLib::OPENCV"; + break; + case ProcLib::FLYCV: + out << "ProcLib::FLYCV"; + break; + default: + FDASSERT(false, "Unknow type of ProcLib."); + } + return out; +} + + + } // namespace vision } // namespace fastdeploy diff --git a/fastdeploy/vision/common/processors/mat.h b/fastdeploy/vision/common/processors/mat.h index 9cd1a4164..20e56211d 100644 --- a/fastdeploy/vision/common/processors/mat.h +++ b/fastdeploy/vision/common/processors/mat.h @@ -14,24 +14,16 @@ #pragma once #include "fastdeploy/core/fd_tensor.h" #include "opencv2/core/core.hpp" - -#ifdef ENABLE_OPENCV_CUDA -#include "opencv2/core/cuda.hpp" -#include "opencv2/cudaarithm.hpp" -#include "opencv2/cudaimgproc.hpp" -#include "opencv2/cudawarping.hpp" -#endif - -namespace fcv { -class Mat; -} // namespace fcv +#include "fastdeploy/vision/common/processors/utils.h" namespace fastdeploy { namespace vision { -enum ProcLib { DEFAULT, OPENCV, FALCONCV}; +enum class FASTDEPLOY_DECL ProcLib { DEFAULT, OPENCV, FLYCV}; enum Layout { HWC, CHW }; +FASTDEPLOY_DECL std::ostream& operator<<(std::ostream& out, const ProcLib& p); + struct FASTDEPLOY_DECL Mat { explicit Mat(cv::Mat& mat) { cpu_mat = mat; @@ -56,19 +48,20 @@ struct FASTDEPLOY_DECL Mat { return &cpu_mat; } + inline const cv::Mat* GetOpenCVMat() const { FDASSERT(mat_type == ProcLib::OPENCV, "Met non cv::Mat data structure."); return &cpu_mat; } -#ifdef ENABLE_FALCONCV +#ifdef ENABLE_FLYCV void SetMat(const fcv::Mat& mat) { fcv_mat = mat; - mat_type = Proclib::FALCONCV; + mat_type = ProcLib::FLYCV; } inline fcv::Mat* GetFalconCVMat() { - FDASSERT(mat_type == ProcLib::FALCONCV, "Met non fcv::Mat data strucure."); + FDASSERT(mat_type == ProcLib::FLYCV, "Met non fcv::Mat data strucure."); return &fcv_mat; } #endif @@ -81,7 +74,7 @@ struct FASTDEPLOY_DECL Mat { int width; cv::Mat cpu_mat; -#ifdef ENABLE_FALCONCV +#ifdef ENABLE_FLYCV fcv::Mat fcv_mat; #endif diff --git a/fastdeploy/vision/common/processors/normalize.cc b/fastdeploy/vision/common/processors/normalize.cc index e3f3d894a..88d68a65e 100644 --- a/fastdeploy/vision/common/processors/normalize.cc +++ b/fastdeploy/vision/common/processors/normalize.cc @@ -54,6 +54,7 @@ Normalize::Normalize(const std::vector& mean, bool Normalize::ImplByOpenCV(Mat* mat) { cv::Mat* im = mat->GetOpenCVMat(); + std::vector split_im; cv::split(*im, split_im); for (int c = 0; c < im->channels(); c++) { @@ -63,6 +64,28 @@ bool Normalize::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool Normalize::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + if (im->channels() != 3) { + FDERROR << "Only supports 3-channels image in FalconCV, but now it's " << im->channels() << "." << std::endl; + return false; + } + + std::vector mean(3, 0); + std::vector std(3, 0); + for (size_t i = 0; i < 3; ++i) { + std[i] = 1.0 / alpha_[i]; + mean[i] = -1 * beta_[i] * std[i]; + } + fcv::Mat new_im(im->width(), im->height(), fcv::FCVImageType::PACKAGE_BGR_F32); + fcv::normalize_to_submean_to_reorder(*im, mean, std, std::vector(), new_im, true); + mat->SetMat(new_im); + return true; +} +#endif + + bool Normalize::Run(Mat* mat, const std::vector& mean, const std::vector& std, bool is_scale, const std::vector& min, diff --git a/fastdeploy/vision/common/processors/normalize.h b/fastdeploy/vision/common/processors/normalize.h index ffb53f48b..964c19fb2 100644 --- a/fastdeploy/vision/common/processors/normalize.h +++ b/fastdeploy/vision/common/processors/normalize.h @@ -18,13 +18,16 @@ namespace fastdeploy { namespace vision { -class Normalize : public Processor { +class FASTDEPLOY_DECL Normalize : public Processor { public: Normalize(const std::vector& mean, const std::vector& std, bool is_scale = true, const std::vector& min = std::vector(), const std::vector& max = std::vector()); bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "Normalize"; } // While use normalize, it is more recommend not use this function @@ -42,9 +45,17 @@ class Normalize : public Processor { const std::vector& min = std::vector(), const std::vector& max = std::vector(), ProcLib lib = ProcLib::OPENCV); + + std::vector GetAlpha() const { + return alpha_; + } + std::vector GetBeta() const { + return beta_; + } + private: std::vector alpha_; std::vector beta_; }; -} // namespace vision -} // namespace fastdeploy +} // namespace vision +} // namespace fastdeploy diff --git a/fastdeploy/vision/common/processors/normalize_and_permute.cc b/fastdeploy/vision/common/processors/normalize_and_permute.cc new file mode 100644 index 000000000..64daf0783 --- /dev/null +++ b/fastdeploy/vision/common/processors/normalize_and_permute.cc @@ -0,0 +1,111 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy/vision/common/processors/normalize_and_permute.h" + +namespace fastdeploy { +namespace vision { + + +NormalizeAndPermute::NormalizeAndPermute(const std::vector& mean, + const std::vector& std, bool is_scale, + const std::vector& min, + const std::vector& max) { + FDASSERT(mean.size() == std.size(), + "Normalize: requires the size of mean equal to the size of std."); + std::vector mean_(mean.begin(), mean.end()); + std::vector std_(std.begin(), std.end()); + std::vector min_(mean.size(), 0.0); + std::vector max_(mean.size(), 255.0); + if (min.size() != 0) { + FDASSERT( + min.size() == mean.size(), + "Normalize: while min is defined, requires the size of min equal to " + "the size of mean."); + min_.assign(min.begin(), min.end()); + } + if (max.size() != 0) { + FDASSERT( + min.size() == mean.size(), + "Normalize: while max is defined, requires the size of max equal to " + "the size of mean."); + max_.assign(max.begin(), max.end()); + } + for (auto c = 0; c < mean_.size(); ++c) { + double alpha = 1.0; + if (is_scale) { + alpha /= (max_[c] - min_[c]); + } + double beta = -1.0 * (mean_[c] + min_[c] * alpha) / std_[c]; + alpha /= std_[c]; + alpha_.push_back(alpha); + beta_.push_back(beta); + } +} + +bool NormalizeAndPermute::ImplByOpenCV(Mat* mat) { + cv::Mat* im = mat->GetOpenCVMat(); + int origin_w = im->cols; + int origin_h = im->rows; + std::vector split_im; + cv::split(*im, split_im); + for (int c = 0; c < im->channels(); c++) { + split_im[c].convertTo(split_im[c], CV_32FC1, alpha_[c], beta_[c]); + } + cv::Mat res(origin_h, origin_w, CV_32FC(im->channels())); + 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); + } + + mat->SetMat(res); + mat->layout = Layout::CHW; + return true; +} + +#ifdef ENABLE_FLYCV +bool NormalizeAndPermute::ImplByFalconCV(Mat* mat) { + if (mat->layout != Layout::HWC) { + FDERROR << "Only supports input with HWC layout." << std::endl; + return false; + } + fcv::Mat* im = mat->GetFalconCVMat(); + if (im->channels() != 3) { + FDERROR << "Only supports 3-channels image in FalconCV, but now it's " << im->channels() << "." << std::endl; + return false; + } + std::vector mean(3, 0); + std::vector std(3, 0); + for (size_t i = 0; i < 3; ++i) { + std[i] = 1.0 / alpha_[i]; + mean[i] = -1 * beta_[i] * std[i]; + } + fcv::Mat new_im; + fcv::normalize_to_submean_to_reorder(*im, mean, std, std::vector(), new_im, false); + mat->SetMat(new_im); + mat->layout = Layout::CHW; + return true; +} +#endif + + +bool NormalizeAndPermute::Run(Mat* mat, const std::vector& mean, + const std::vector& std, bool is_scale, + const std::vector& min, + const std::vector& max, ProcLib lib) { + auto n = NormalizeAndPermute(mean, std, is_scale, min, max); + return n(mat, lib); +} + +} // namespace vision +} // namespace fastdeploy diff --git a/fastdeploy/vision/common/processors/normalize_and_permute.h b/fastdeploy/vision/common/processors/normalize_and_permute.h new file mode 100644 index 000000000..4a154f71f --- /dev/null +++ b/fastdeploy/vision/common/processors/normalize_and_permute.h @@ -0,0 +1,67 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "fastdeploy/vision/common/processors/base.h" + +namespace fastdeploy { +namespace vision { +class FASTDEPLOY_DECL NormalizeAndPermute : public Processor { + public: + NormalizeAndPermute(const std::vector& mean, + const std::vector& std, + bool is_scale = true, + const std::vector& min = std::vector(), + const std::vector& max = std::vector()); + bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif + std::string Name() { return "NormalizeAndPermute"; } + + // While use normalize, it is more recommend not use this function + // this function will need to compute result = ((mat / 255) - mean) / std + // if we use the following method + // ``` + // auto norm = Normalize(...) + // norm(mat) + // ``` + // There will be some precomputation in contruct function + // and the `norm(mat)` only need to compute result = mat * alpha + beta + // which will reduce lots of time + static bool Run(Mat* mat, const std::vector& mean, + const std::vector& std, bool is_scale = true, + const std::vector& min = std::vector(), + const std::vector& max = std::vector(), + ProcLib lib = ProcLib::OPENCV); + + void SetAlpha(const std::vector& alpha) { + alpha_.clear(); + std::vector().swap(alpha_); + alpha_.assign(alpha.begin(), alpha.end()); + } + + void SetBeta(const std::vector& beta) { + beta_.clear(); + std::vector().swap(beta_); + beta_.assign(beta.begin(), beta.end()); + } + + private: + std::vector alpha_; + std::vector beta_; +}; +} // namespace vision +} // namespace fastdeploy diff --git a/fastdeploy/vision/common/processors/pad.cc b/fastdeploy/vision/common/processors/pad.cc index ab6e6df1e..7352356d0 100644 --- a/fastdeploy/vision/common/processors/pad.cc +++ b/fastdeploy/vision/common/processors/pad.cc @@ -52,6 +52,45 @@ bool Pad::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool Pad::ImplByFalconCV(Mat* mat) { + if (mat->layout != Layout::HWC) { + FDERROR << "Pad: The input data must be Layout::HWC format!" << std::endl; + return false; + } + if (mat->Channels() > 4) { + FDERROR << "Pad: Only support channels <= 4." << std::endl; + return false; + } + if (mat->Channels() != value_.size()) { + FDERROR << "Pad: Require input channels equals to size of padding value, " + "but now channels = " + << mat->Channels() + << ", the size of padding values = " << value_.size() << "." + << std::endl; + return false; + } + fcv::Mat* im = mat->GetFalconCVMat(); + fcv::Scalar value; + if (value_.size() == 1) { + value = fcv::Scalar(value_[0]); + } else if (value_.size() == 2) { + value = fcv::Scalar(value_[0], value_[1]); + } else if (value_.size() == 3) { + value = fcv::Scalar(value_[0], value_[1], value_[2]); + } else { + value = fcv::Scalar(value_[0], value_[1], value_[2], value_[3]); + } + fcv::Mat new_im; + fcv::copy_make_border(*im, new_im, top_, bottom_, left_, right_, + fcv::BorderTypes::BORDER_CONSTANT, value); + mat->SetMat(new_im); + mat->SetHeight(new_im.height()); + mat->SetWidth(new_im.width()); + return true; +} +#endif + bool Pad::Run(Mat* mat, const int& top, const int& bottom, const int& left, const int& right, const std::vector& value, ProcLib lib) { diff --git a/fastdeploy/vision/common/processors/pad.h b/fastdeploy/vision/common/processors/pad.h index 4d92a11d1..5405564ec 100644 --- a/fastdeploy/vision/common/processors/pad.h +++ b/fastdeploy/vision/common/processors/pad.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { -class Pad : public Processor { +class FASTDEPLOY_DECL Pad : public Processor { public: Pad(int top, int bottom, int left, int right, const std::vector& value) { @@ -30,6 +30,9 @@ class Pad : public Processor { value_ = value; } bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "Pad"; } static bool Run(Mat* mat, const int& top, const int& bottom, const int& left, @@ -43,5 +46,5 @@ class Pad : public Processor { int right_; std::vector value_; }; -} // namespace vision -} // namespace fastdeploy +} // namespace vision +} // namespace fastdeploy diff --git a/fastdeploy/vision/common/processors/pad_to_size.cc b/fastdeploy/vision/common/processors/pad_to_size.cc index 290505df1..5956953e0 100644 --- a/fastdeploy/vision/common/processors/pad_to_size.cc +++ b/fastdeploy/vision/common/processors/pad_to_size.cc @@ -75,6 +75,68 @@ bool PadToSize::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool PadToSize::ImplByFalconCV(Mat* mat) { + if (width_ == -1 || height_ == -1) { + return true; + } + if (mat->layout != Layout::HWC) { + FDERROR << "PadToSize: The input data must be Layout::HWC format!" + << std::endl; + return false; + } + if (mat->Channels() > 4) { + FDERROR << "PadToSize: Only support channels <= 4." << std::endl; + return false; + } + if (mat->Channels() != value_.size()) { + FDERROR + << "PadToSize: Require input channels equals to size of padding value, " + "but now channels = " + << mat->Channels() << ", the size of padding values = " << value_.size() + << "." << std::endl; + return false; + } + int origin_w = mat->Width(); + int origin_h = mat->Height(); + if (origin_w > width_) { + FDERROR << "PadToSize: the input width:" << origin_w + << " is greater than the target width: " << width_ << "." + << std::endl; + return false; + } + if (origin_h > height_) { + FDERROR << "PadToSize: the input height:" << origin_h + << " is greater than the target height: " << height_ << "." + << std::endl; + return false; + } + if (origin_w == width_ && origin_h == height_) { + return true; + } + + fcv::Mat* im = mat->GetFalconCVMat(); + fcv::Scalar value; + if (value_.size() == 1) { + value = fcv::Scalar(value_[0]); + } else if (value_.size() == 2) { + value = fcv::Scalar(value_[0], value_[1]); + } else if (value_.size() == 3) { + value = fcv::Scalar(value_[0], value_[1], value_[2]); + } else { + value = fcv::Scalar(value_[0], value_[1], value_[2], value_[3]); + } + fcv::Mat new_im; + // top, bottom, left, right + fcv::copy_make_border(*im, new_im, 0, height_ - origin_h, 0, width_ - origin_w, + fcv::BorderTypes::BORDER_CONSTANT, value); + mat->SetMat(new_im); + mat->SetHeight(height_); + mat->SetWidth(width_); + return true; +} +#endif + bool PadToSize::Run(Mat* mat, int width, int height, const std::vector& value, ProcLib lib) { auto p = PadToSize(width, height, value); diff --git a/fastdeploy/vision/common/processors/pad_to_size.h b/fastdeploy/vision/common/processors/pad_to_size.h index 83933ddcc..dff811089 100644 --- a/fastdeploy/vision/common/processors/pad_to_size.h +++ b/fastdeploy/vision/common/processors/pad_to_size.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { -class PadToSize : public Processor { +class FASTDEPLOY_DECL PadToSize : public Processor { public: // only support pad with right-bottom padding mode PadToSize(int width, int height, const std::vector& value) { @@ -28,6 +28,9 @@ class PadToSize : public Processor { value_ = value; } bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "PadToSize"; } static bool Run(Mat* mat, int width, int height, diff --git a/fastdeploy/vision/common/processors/resize.cc b/fastdeploy/vision/common/processors/resize.cc index 44e27a22a..eb1932abc 100644 --- a/fastdeploy/vision/common/processors/resize.cc +++ b/fastdeploy/vision/common/processors/resize.cc @@ -25,6 +25,14 @@ bool Resize::ImplByOpenCV(Mat* mat) { cv::Mat* im = mat->GetOpenCVMat(); int origin_w = im->cols; int origin_h = im->rows; + + if (width_ == origin_w && height_ == origin_h) { + return true; + } + if (fabs(scale_w_ - 1.0) < 1e-06 && fabs(scale_h_ - 1.0) < 1e-06) { + return true; + } + if (width_ > 0 && height_ > 0) { if (use_scale_) { float scale_w = width_ * 1.0 / origin_w; @@ -46,6 +54,65 @@ bool Resize::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool Resize::ImplByFalconCV(Mat* mat) { + if (mat->layout != Layout::HWC) { + FDERROR << "Resize: The format of input is not HWC." << std::endl; + return false; + } + fcv::Mat* im = mat->GetFalconCVMat(); + int origin_w = im->width(); + int origin_h = im->height(); + + if (width_ == origin_w && height_ == origin_h) { + return true; + } + if (fabs(scale_w_ - 1.0) < 1e-06 && fabs(scale_h_ - 1.0) < 1e-06) { + return true; + } + + auto interp_method = fcv::InterpolationType::INTER_LINEAR; + if (interp_ == 0) { + interp_method = fcv::InterpolationType::INTER_NEAREST; + } else if (interp_ == 1) { + interp_method = fcv::InterpolationType::INTER_LINEAR; + } else if (interp_ == 2) { + interp_method = fcv::InterpolationType::INTER_CUBIC; + } else { + FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FalconCV, but " + "now it's " + << interp_ << "." << std::endl; + return false; + } + + if (width_ > 0 && height_ > 0) { + fcv::Mat new_im; + if (use_scale_) { + float scale_w = width_ * 1.0 / origin_w; + float scale_h = height_ * 1.0 / origin_h; + fcv::resize(*im, new_im, fcv::Size(), scale_w, scale_h, interp_method); + } else { + fcv::resize(*im, new_im, fcv::Size(width_, height_), 0, 0, interp_method); + } + mat->SetMat(new_im); + mat->SetWidth(new_im.width()); + mat->SetHeight(new_im.height()); + } else if (scale_w_ > 0 && scale_h_ > 0) { + fcv::Mat new_im; + fcv::resize(*im, new_im, fcv::Size(0, 0), scale_w_, scale_h_, interp_method); + mat->SetMat(new_im); + mat->SetWidth(new_im.width()); + mat->SetHeight(new_im.height()); + } else { + FDERROR << "Resize: the parameters must satisfy (width > 0 && height > 0) " + "or (scale_w > 0 && scale_h > 0)." + << std::endl; + return false; + } + return true; +} +#endif + bool Resize::Run(Mat* mat, int width, int height, float scale_w, float scale_h, int interp, bool use_scale, ProcLib lib) { if (mat->Height() == height && mat->Width() == width) { diff --git a/fastdeploy/vision/common/processors/resize.h b/fastdeploy/vision/common/processors/resize.h index c62bcef93..b20f0e468 100644 --- a/fastdeploy/vision/common/processors/resize.h +++ b/fastdeploy/vision/common/processors/resize.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { -class Resize : public Processor { +class FASTDEPLOY_DECL Resize : public Processor { public: Resize(int width, int height, float scale_w = -1.0, float scale_h = -1.0, int interp = 1, bool use_scale = false) { @@ -32,6 +32,9 @@ class Resize : public Processor { } bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "Resize"; } static bool Run(Mat* mat, int width, int height, float scale_w = -1.0, diff --git a/fastdeploy/vision/common/processors/resize_by_short.cc b/fastdeploy/vision/common/processors/resize_by_short.cc index 0c48a7e18..2fe60b9ee 100644 --- a/fastdeploy/vision/common/processors/resize_by_short.cc +++ b/fastdeploy/vision/common/processors/resize_by_short.cc @@ -36,6 +36,48 @@ bool ResizeByShort::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool ResizeByShort::ImplByFalconCV(Mat* mat) { + fcv::Mat* im = mat->GetFalconCVMat(); + int origin_w = im->width(); + int origin_h = im->height(); + double scale = GenerateScale(origin_w, origin_h); + + auto interp_method = fcv::InterpolationType::INTER_LINEAR; + if (interp_ == 0) { + interp_method = fcv::InterpolationType::INTER_NEAREST; + } else if (interp_ == 1) { + interp_method = fcv::InterpolationType::INTER_LINEAR; + } else if (interp_ == 2) { + interp_method = fcv::InterpolationType::INTER_CUBIC; + } else { + FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FalconCV, but " + "now it's " + << interp_ << "." << std::endl; + return false; + } + + if (use_scale_ && fabs(scale - 1.0) >= 1e-06) { + fcv::Mat new_im; + fcv::resize(*im, new_im, fcv::Size(), scale, scale, interp_method); + mat->SetMat(new_im); + mat->SetHeight(new_im.height()); + mat->SetWidth(new_im.width()); + } else { + int width = static_cast(round(scale * im->width())); + int height = static_cast(round(scale * im->height())); + if (width != origin_w || height != origin_h) { + fcv::Mat new_im; + fcv::resize(*im, new_im, fcv::Size(width, height), 0, 0, interp_method); + mat->SetMat(new_im); + mat->SetHeight(new_im.height()); + mat->SetWidth(new_im.width()); + } + } + return true; +} +#endif + double ResizeByShort::GenerateScale(const int origin_w, const int origin_h) { int im_size_max = std::max(origin_w, origin_h); int im_size_min = std::min(origin_w, origin_h); diff --git a/fastdeploy/vision/common/processors/resize_by_short.h b/fastdeploy/vision/common/processors/resize_by_short.h index 8a43fdc8b..2ea901ee3 100644 --- a/fastdeploy/vision/common/processors/resize_by_short.h +++ b/fastdeploy/vision/common/processors/resize_by_short.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { -class ResizeByShort : public Processor { +class FASTDEPLOY_DECL ResizeByShort : public Processor { public: ResizeByShort(int target_size, int interp = 1, bool use_scale = true, const std::vector& max_hw = std::vector()) { @@ -29,6 +29,9 @@ class ResizeByShort : public Processor { use_scale_ = use_scale; } bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "ResizeByShort"; } static bool Run(Mat* mat, int target_size, int interp = 1, diff --git a/fastdeploy/vision/common/processors/stride_pad.cc b/fastdeploy/vision/common/processors/stride_pad.cc index 50e3fd660..4b71e34fb 100644 --- a/fastdeploy/vision/common/processors/stride_pad.cc +++ b/fastdeploy/vision/common/processors/stride_pad.cc @@ -63,6 +63,56 @@ bool StridePad::ImplByOpenCV(Mat* mat) { return true; } +#ifdef ENABLE_FLYCV +bool StridePad::ImplByFalconCV(Mat* mat) { + if (mat->layout != Layout::HWC) { + FDERROR << "StridePad: The input data must be Layout::HWC format!" + << std::endl; + return false; + } + if (mat->Channels() > 4) { + FDERROR << "StridePad: Only support channels <= 4." << std::endl; + return false; + } + if (mat->Channels() != value_.size()) { + FDERROR + << "StridePad: Require input channels equals to size of padding value, " + "but now channels = " + << mat->Channels() << ", the size of padding values = " << value_.size() + << "." << std::endl; + return false; + } + int origin_w = mat->Width(); + int origin_h = mat->Height(); + + int pad_h = (mat->Height() / stride_) * stride_ + + (mat->Height() % stride_ != 0) * stride_ - mat->Height(); + int pad_w = (mat->Width() / stride_) * stride_ + + (mat->Width() % stride_ != 0) * stride_ - mat->Width(); + if (pad_h == 0 && pad_w == 0) { + return true; + } + fcv::Mat* im = mat->GetFalconCVMat(); + fcv::Scalar value; + if (value_.size() == 1) { + value = fcv::Scalar(value_[0]); + } else if (value_.size() == 2) { + value = fcv::Scalar(value_[0], value_[1]); + } else if (value_.size() == 3) { + value = fcv::Scalar(value_[0], value_[1], value_[2]); + } else { + value = fcv::Scalar(value_[0], value_[1], value_[2], value_[3]); + } + fcv::Mat new_im; + // top, bottom, left, right + fcv::copy_make_border(*im, new_im, 0, pad_h, 0, pad_w, fcv::BorderTypes::BORDER_CONSTANT, value); + mat->SetMat(new_im); + mat->SetHeight(new_im.height()); + mat->SetWidth(new_im.width()); + return true; +} +#endif + bool StridePad::Run(Mat* mat, int stride, const std::vector& value, ProcLib lib) { auto p = StridePad(stride, value); diff --git a/fastdeploy/vision/common/processors/stride_pad.h b/fastdeploy/vision/common/processors/stride_pad.h index c6d2f5871..67fe692d4 100644 --- a/fastdeploy/vision/common/processors/stride_pad.h +++ b/fastdeploy/vision/common/processors/stride_pad.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { -class StridePad : public Processor { +class FASTDEPLOY_DECL StridePad : public Processor { public: // only support pad with left-top padding mode StridePad(int stride, const std::vector& value) { @@ -27,6 +27,9 @@ class StridePad : public Processor { value_ = value; } bool ImplByOpenCV(Mat* mat); +#ifdef ENABLE_FLYCV + bool ImplByFalconCV(Mat* mat); +#endif std::string Name() { return "StridePad"; } static bool Run(Mat* mat, int stride, diff --git a/fastdeploy/vision/common/processors/transform.h b/fastdeploy/vision/common/processors/transform.h index 6e99f2e35..8d1b5010d 100644 --- a/fastdeploy/vision/common/processors/transform.h +++ b/fastdeploy/vision/common/processors/transform.h @@ -30,5 +30,88 @@ #include "fastdeploy/vision/common/processors/resize_by_long.h" #include "fastdeploy/vision/common/processors/resize_by_short.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/letter_box.h" + +namespace fastdeploy { +namespace vision { + +inline void FuseNormalizeCast(std::vector>* processors) { + // Fuse Normalize and Cast + int cast_index = -1; + for (size_t i = 0; i < processors->size(); ++i) { + if ((*processors)[i]->Name() == "Cast") { + if (i == 0) { + continue; + } + if ((*processors)[i - 1]->Name() != "Normalize" && (*processors)[i - 1]->Name() != "NormalizeAndPermute") { + continue; + } + cast_index = i; + } + } + if (cast_index < 0) { + return; + } + + std::cout << dynamic_cast((*processors)[cast_index].get())->GetDtype() << "-----" << std::endl; + if (dynamic_cast((*processors)[cast_index].get())->GetDtype() != "float") { + return; + } + 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; +} + +inline void FuseNormalizeHWC2CHW(std::vector>* processors) { + // Fuse Normalize and HWC2CHW to NormalizeAndPermute + int hwc2chw_index = -1; + for (size_t i = 0; i < processors->size(); ++i) { + if ((*processors)[i]->Name() == "HWC2CHW") { + if (i == 0) { + continue; + } + if ((*processors)[i - 1]->Name() != "Normalize") { + continue; + } + hwc2chw_index = i; + } + } + + if (hwc2chw_index < 0) { + return; + } + + // Get alpha and beta of Normalize + std::vector alpha = dynamic_cast( + (*processors)[hwc2chw_index - 1].get())->GetAlpha(); + std::vector beta = dynamic_cast( + (*processors)[hwc2chw_index - 1].get())->GetBeta(); + + // Delete Normalize and HWC2CHW + processors->erase(processors->begin() + hwc2chw_index); + processors->erase(processors->begin() + hwc2chw_index - 1); + + // Add NormalizeAndPermute + std::vector mean({0.0, 0.0, 0.0}); + std::vector std({1.0, 1.0, 1.0}); + processors->push_back(std::make_shared(mean, std)); + + // Set alpha and beta + auto processor = dynamic_cast( + (*processors)[hwc2chw_index - 1].get()); + + processor->SetAlpha(alpha); + processor->SetBeta(beta); + FDINFO << "Normalize and HWC2CHW are fused to NormalizeAndPermute " + " in preprocessing pipeline." << std::endl; +} + +inline void FuseTransforms( + std::vector>* processors) { + FuseNormalizeCast(processors); + FuseNormalizeHWC2CHW(processors); +} + +} // namespace vision +} // namespace fastdeploy diff --git a/fastdeploy/vision/common/processors/utils.cc b/fastdeploy/vision/common/processors/utils.cc index d291f7812..4e5373ef2 100644 --- a/fastdeploy/vision/common/processors/utils.cc +++ b/fastdeploy/vision/common/processors/utils.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "fastdeploy/utils/utils.h" #include "fastdeploy/vision/common/processors/utils.h" namespace fastdeploy { @@ -40,7 +41,7 @@ FDDataType OpenCVDataTypeToFD(int type) { } } -#ifdef ENABLE_FALCONCV +#ifdef ENABLE_FLYCV FDDataType FalconCVDataTypeToFD(fcv::FCVImageType type) { if (type == fcv::FCVImageType::GRAY_U8) { return FDDataType::UINT8; @@ -103,11 +104,11 @@ FDDataType FalconCVDataTypeToFD(fcv::FCVImageType type) { } else if (type == fcv::FCVImageType::GRAY_F64) { return FDDataType::FP64; } - FDASSERT(false, "While calling FalconDataTypeToFD(), get unexpected type:" + std::to_string(int(type)) + "."); - return FDDataType::UNKNOWN; + FDASSERT(false, "While calling FalconDataTypeToFD(), get unexpected type:%d.", int(type)); + return FDDataType::UNKNOWN1; } -fcv::FCVImageType CreateFalconDataCVType(FDDataType type, int channel) { +fcv::FCVImageType CreateFalconCVDataType(FDDataType type, int channel) { FDASSERT(channel == 1 || channel == 3 || channel == 4, "Only support channel be 1/3/4 in Falcon."); if (type == FDDataType::UINT8) { @@ -127,9 +128,19 @@ fcv::FCVImageType CreateFalconDataCVType(FDDataType type, int channel) { return fcv::FCVImageType::PACKAGE_BGRA_F32; } } - FDASSERT(false, "Data type of " + Str(type) + " is not supported."); + FDASSERT(false, "Data type of %s is not supported.", Str(type).c_str()); return fcv::FCVImageType::PACKAGE_BGR_F32; } + +fcv::Mat ConvertOpenCVMatToFalconCV(cv::Mat& im) { + int type = im.type() % 8; + // 0: uint8; 5: float32; 6: float64 + if (type != 0 && type != 5 && type != 6) { + 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()); + return fcv::Mat(im.cols, im.rows, fcv_type, im.ptr()); +} #endif diff --git a/fastdeploy/vision/common/processors/utils.h b/fastdeploy/vision/common/processors/utils.h index c6b6eb5ad..57c1309e7 100644 --- a/fastdeploy/vision/common/processors/utils.h +++ b/fastdeploy/vision/common/processors/utils.h @@ -15,20 +15,27 @@ #pragma once #include "fastdeploy/utils/utils.h" +#include "opencv2/core/core.hpp" #include "fastdeploy/core/fd_tensor.h" +#ifdef ENABLE_FLYCV +#include "flycv.h" // NOLINT +#endif + namespace fastdeploy { namespace vision { // Convert data type of opencv to FDDataType FDDataType OpenCVDataTypeToFD(int type); -#ifdef ENABLE_FALCONCV -// Convert data type of falconcv to FDDataType +#ifdef ENABLE_FLYCV +// Convert data type of flycv to FDDataType FDDataType FalconCVDataTypeToFD(fcv::FCVImageType type); -// Create data type of falconcv by FDDataType +// Create data type of flycv by FDDataType fcv::FCVImageType CreateFalconCVDataType(FDDataType type, int channel = 1); +// Convert cv::Mat to fcv::Mat +fcv::Mat ConvertOpenCVMatToFalconCV(cv::Mat& im); #endif -} // namespace vision -} // namespace fastdeploy +} // namespace vision +} // namespace fastdeploy diff --git a/fastdeploy/vision/detection/ppdet/ppyoloe.cc b/fastdeploy/vision/detection/ppdet/ppyoloe.cc index 99f1fc392..c041d3ec2 100644 --- a/fastdeploy/vision/detection/ppdet/ppyoloe.cc +++ b/fastdeploy/vision/detection/ppdet/ppyoloe.cc @@ -160,6 +160,9 @@ bool PPYOLOE::BuildPreprocessPipelineFromConfig() { processors_.push_back(std::make_shared()); } + // Fusion will improve performance + FuseTransforms(&processors_); + return true; } @@ -259,6 +262,7 @@ bool PPYOLOE::Postprocess(std::vector& infer_result, bool PPYOLOE::Predict(cv::Mat* im, DetectionResult* result) { Mat mat(*im); + std::vector processed_data; if (!Preprocess(&mat, &processed_data)) { FDERROR << "Failed to preprocess input data while using model:" diff --git a/fastdeploy/vision/tracking/pptracking/model.cc b/fastdeploy/vision/tracking/pptracking/model.cc index 064892889..97d4e1ab9 100644 --- a/fastdeploy/vision/tracking/pptracking/model.cc +++ b/fastdeploy/vision/tracking/pptracking/model.cc @@ -14,7 +14,6 @@ #include "fastdeploy/vision/tracking/pptracking/model.h" #include "yaml-cpp/yaml.h" -#include "paddle2onnx/converter.h" namespace fastdeploy { namespace vision { diff --git a/fastdeploy/vision/vision_pybind.cc b/fastdeploy/vision/vision_pybind.cc index 89b08f119..23f80ed05 100644 --- a/fastdeploy/vision/vision_pybind.cc +++ b/fastdeploy/vision/vision_pybind.cc @@ -115,6 +115,9 @@ void BindVision(pybind11::module& m) { .def("__repr__", &vision::KeyPointDetectionResult::Str) .def("__str__", &vision::KeyPointDetectionResult::Str); + m.def("enable_flycv", &vision::EnableFlyCV, "Enable image preprocessing by FlyCV."); + m.def("disable_flycv", &vision::DisableFlyCV, "Disable image preprocessing by FlyCV, change to use OpenCV."); + BindDetection(m); BindClassification(m); BindSegmentation(m); diff --git a/python/fastdeploy/vision/__init__.py b/python/fastdeploy/vision/__init__.py index ce282daaf..6023f1842 100644 --- a/python/fastdeploy/vision/__init__.py +++ b/python/fastdeploy/vision/__init__.py @@ -25,3 +25,12 @@ from . import ocr from . import evaluation from .utils import fd_result_to_json from .visualize import * +from .. import C + + +def enable_flycv(): + return C.vision.enable_flycv() + + +def disable_flycv(): + return C.vision.disable_flycv() diff --git a/python/setup.py b/python/setup.py index fb96c0626..8b56328f5 100755 --- a/python/setup.py +++ b/python/setup.py @@ -56,6 +56,7 @@ setup_configs["ENABLE_PADDLE_BACKEND"] = os.getenv("ENABLE_PADDLE_BACKEND", setup_configs["ENABLE_POROS_BACKEND"] = os.getenv("ENABLE_POROS_BACKEND", "OFF") setup_configs["ENABLE_VISION"] = os.getenv("ENABLE_VISION", "OFF") +setup_configs["ENABLE_FLYCV"] = os.getenv("ENABLE_FLYCV", "OFF") setup_configs["ENABLE_TEXT"] = os.getenv("ENABLE_TEXT", "OFF") setup_configs["ENABLE_TRT_BACKEND"] = os.getenv("ENABLE_TRT_BACKEND", "OFF") setup_configs["WITH_GPU"] = os.getenv("WITH_GPU", "OFF") diff --git a/scripts/fastdeploy_init.sh b/scripts/fastdeploy_init.sh index e0b7175ae..af7e4c429 100644 --- a/scripts/fastdeploy_init.sh +++ b/scripts/fastdeploy_init.sh @@ -50,4 +50,15 @@ if [ -d ${INSTALLED_PREBUILT_FASTDEPLOY_DIR}/third_libs/install/paddle_inference echo "MKLML Lib: ${INSTALLED_PREBUILT_FASTDEPLOY_DIR}/third_libs/install/paddle_inference/third_party/install/mklml/lib" fi +if [ -d ${INSTALLED_PREBUILT_FASTDEPLOY_DIR}/third_libs/install/flycv ]; then + export LD_LIBRARY_PATH=${INSTALLED_PREBUILT_FASTDEPLOY_DIR}/third_libs/install/flycv/lib:${LD_LIBRARY_PATH} + echo "Fly Lib: ${INSTALLED_PREBUILT_FASTDEPLOY_DIR}/third_libs/install/flycv/lib" +fi + +if [ -d ${INSTALLED_PREBUILT_FASTDEPLOY_DIR}/third_libs/install/paddlelite ]; then + export LD_LIBRARY_PATH=${INSTALLED_PREBUILT_FASTDEPLOY_DIR}/third_libs/install/paddlelite/lib:${LD_LIBRARY_PATH} + echo "Paddle Lite Lib: ${INSTALLED_PREBUILT_FASTDEPLOY_DIR}/third_libs/install/paddlelite/lib" +fi + + cd ${CURRENT_EXE_DIR} diff --git a/tests/gtest_utils.h b/tests/gtest_utils.h index 04d3b5847..5dc0717b6 100644 --- a/tests/gtest_utils.h +++ b/tests/gtest_utils.h @@ -14,6 +14,7 @@ #pragma once #include +#include #include "gtest/gtest.h" namespace fastdeploy { @@ -30,16 +31,35 @@ struct CheckShape { struct CheckData { template - void operator()(const T* lhs_ptr, const T* rhs_ptr, int num) { + void operator()(const T* lhs_ptr, const T* rhs_ptr, int num, int atol = 0) { for (int i = 0; i < num; ++i) { - ASSERT_EQ(lhs_ptr[i], rhs_ptr[i]); +// ASSERT_FLOAT_EQ(lhs_ptr[i], rhs_ptr[i]); + int abs_diff = abs(lhs_ptr[i] - rhs_ptr[i]); + if (abs_diff > atol) { + std::cout << "lhs_ptr: " << static_cast(lhs_ptr[i]) + << " rhs_ptr: " << static_cast(rhs_ptr[i]) + << " abs_diff: " << abs_diff << std::endl; + ASSERT_EQ(1, 0); + } + ASSERT_EQ(1, 1); } } - void operator()(const float* lhs_ptr, const float* rhs_ptr, int num) { + void operator()(const float* lhs_ptr, const float* rhs_ptr, + int num, float atol = 1e-06, float rtol = 1e-06) { for (int i = 0; i < num; ++i) { - ASSERT_FLOAT_EQ(lhs_ptr[i], rhs_ptr[i]); + float abs_diff = fabs(lhs_ptr[i] - rhs_ptr[i]); + float rel_diff = abs_diff / (std::max(fabs(lhs_ptr[i]), + fabs(rhs_ptr[i])) + 1e-06); + if (abs_diff > atol && rel_diff > rtol) { + std::cout << "lhs_ptr: " << lhs_ptr[i] << " rhs_ptr: " + << rhs_ptr[i] << " abs_diff: " << abs_diff + << " rel_diff: " << rel_diff << std::endl; + ASSERT_EQ(1, 0); + } + ASSERT_EQ(1, 1); } } + void operator()(const double* lhs_ptr, const double* rhs_ptr, int num) { for (int i = 0; i < num; ++i) { ASSERT_DOUBLE_EQ(lhs_ptr[i], rhs_ptr[i]); @@ -47,4 +67,10 @@ struct CheckData { } }; +struct CheckType { + void operator()(FDDataType type1, FDDataType type2) { + ASSERT_EQ(type1, type2); + } +}; + } // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_cast.cc b/tests/vision_preprocess/test_vision_cast.cc new file mode 100644 index 000000000..55215811e --- /dev/null +++ b/tests/vision_preprocess/test_vision_cast.cc @@ -0,0 +1,51 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_cast) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); + vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_center_crop.cc b/tests/vision_preprocess/test_vision_center_crop.cc new file mode 100644 index 000000000..dd2f3dc13 --- /dev/null +++ b/tests/vision_preprocess/test_vision_center_crop.cc @@ -0,0 +1,79 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_center_crop1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); + vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); + vision::CenterCrop::Run(&mat_opencv, 20, 20, vision::ProcLib::OPENCV); + vision::CenterCrop::Run(&mat_flycv, 20, 20, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +TEST(fastdeploy, flycv_center_crop2) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::CenterCrop::Run(&mat_opencv, 20, 20, vision::ProcLib::OPENCV); + vision::CenterCrop::Run(&mat_flycv, 20, 20, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_colorspace_convert.cc b/tests/vision_preprocess/test_vision_colorspace_convert.cc new file mode 100644 index 000000000..f1253d959 --- /dev/null +++ b/tests/vision_preprocess/test_vision_colorspace_convert.cc @@ -0,0 +1,76 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_bgr2rgb) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::BGR2RGB::Run(&mat_opencv, vision::ProcLib::OPENCV); + vision::BGR2RGB::Run(&mat_flycv, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +TEST(fastdeploy, flycv_rgb2bgr) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::RGB2BGR::Run(&mat_opencv, vision::ProcLib::OPENCV); + vision::RGB2BGR::Run(&mat_flycv, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_convert.cc b/tests/vision_preprocess/test_vision_convert.cc new file mode 100644 index 000000000..25ffe4b19 --- /dev/null +++ b/tests/vision_preprocess/test_vision_convert.cc @@ -0,0 +1,85 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_convert1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + + std::vector alpha({0.1, 0.2, 0.3}); + std::vector beta({0.24, 0.77, 0.32}); + vision::Convert::Run(&mat_opencv, alpha, beta, vision::ProcLib::OPENCV); + vision::Convert::Run(&mat_flycv, alpha, beta, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +TEST(fastdeploy, flycv_convert2) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + + std::vector alpha({0.1, 0.2, 0.3}); + std::vector beta({0.24, 0.77, 0.32}); + vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); + vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); + vision::Convert::Run(&mat_opencv, alpha, beta, vision::ProcLib::OPENCV); + vision::Convert::Run(&mat_flycv, alpha, beta, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_crop.cc b/tests/vision_preprocess/test_vision_crop.cc new file mode 100644 index 000000000..e352d1cf7 --- /dev/null +++ b/tests/vision_preprocess/test_vision_crop.cc @@ -0,0 +1,79 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_crop1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); + vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); + vision::Crop::Run(&mat_opencv, 4, 8, 20, 20, vision::ProcLib::OPENCV); + vision::Crop::Run(&mat_flycv, 4, 8, 20, 20, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +TEST(fastdeploy, flycv_center_crop2) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(70, 60, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Crop::Run(&mat_opencv, 9, 16, 13, 23, vision::ProcLib::OPENCV); + vision::Crop::Run(&mat_flycv, 9, 16, 13, 23, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_hwc2chw.cc b/tests/vision_preprocess/test_vision_hwc2chw.cc new file mode 100644 index 000000000..c5fe98c71 --- /dev/null +++ b/tests/vision_preprocess/test_vision_hwc2chw.cc @@ -0,0 +1,78 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_hwc2chw1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); + vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); + vision::HWC2CHW::Run(&mat_opencv, vision::ProcLib::OPENCV); + vision::HWC2CHW::Run(&mat_flycv, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +//TEST(fastdeploy, flycv_hwc2chw2) { +// CheckShape check_shape; +// CheckData check_data; +// CheckType check_type; +// +// cv::Mat mat(64, 64, CV_8UC3); +// cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); +// cv::Mat mat1 = mat.clone(); +// +// vision::Mat mat_opencv(mat); +// vision::Mat mat_flycv(mat1); +// vision::HWC2CHW::Run(&mat_opencv, vision::ProcLib::OPENCV); +// vision::HWC2CHW::Run(&mat_flycv, vision::ProcLib::FLYCV); +// +// FDTensor opencv; +// FDTensor flycv; +// +// mat_opencv.ShareWithTensor(&opencv); +// mat_flycv.ShareWithTensor(&flycv); +// +// check_shape(opencv.shape, flycv.shape); +// check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); +// check_type(opencv.dtype, flycv.dtype); +//} +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_limit_long.cc b/tests/vision_preprocess/test_vision_limit_long.cc new file mode 100644 index 000000000..33d3fcccd --- /dev/null +++ b/tests/vision_preprocess/test_vision_limit_long.cc @@ -0,0 +1,108 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_limit_long1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(35, 69, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::LimitLong::Run(&mat_opencv, 38, -1, vision::ProcLib::OPENCV); + vision::LimitLong::Run(&mat_flycv, 38, -1, vision::ProcLib::FLYCV); + + std::cout << "opencv " << mat_opencv.GetOpenCVMat()->cols << " " << mat_opencv.GetOpenCVMat()->rows << std::endl; + std::cout << "flycv " << mat_flycv.GetFalconCVMat()->width() << " " << mat_flycv.GetFalconCVMat()->height() << std::endl; + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_type(opencv.dtype, flycv.dtype); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 20); +} + +//TEST(fastdeploy, flycv_limit_long2) { +// CheckShape check_shape; +// CheckData check_data; +// CheckType check_type; +// +// cv::Mat mat(35, 69, CV_8UC3); +// cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); +// cv::Mat mat1 = mat.clone(); +// +// vision::Mat mat_opencv(mat); +// vision::Mat mat_flycv(mat1); +// vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); +// vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); +// vision::LimitByStride::Run(&mat_opencv, 38, -1, vision::ProcLib::OPENCV); +// vision::LimitByStride::Run(&mat_flycv, 38, -1, vision::ProcLib::FLYCV); +// +// FDTensor opencv; +// FDTensor flycv; +// +// mat_opencv.ShareWithTensor(&opencv); +// mat_flycv.ShareWithTensor(&flycv); +// +// check_shape(opencv.shape, flycv.shape); +// check_type(opencv.dtype, flycv.dtype); +// check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 0, 0); +//} +// +//TEST(fastdeploy, flycv_limit_long3) { +// CheckShape check_shape; +// CheckData check_data; +// CheckType check_type; +// +// cv::Mat mat(35, 69, CV_8UC3); +// cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); +// cv::Mat mat1 = mat.clone(); +// +// vision::Mat mat_opencv(mat); +// vision::Mat mat_flycv(mat1); +// vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); +// vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); +// vision::LimitByStride::Run(&mat_opencv, 108, 50, vision::ProcLib::OPENCV); +// vision::LimitByStride::Run(&mat_flycv, 108, 50, vision::ProcLib::FLYCV); +// +// FDTensor opencv; +// FDTensor flycv; +// +// mat_opencv.ShareWithTensor(&opencv); +// mat_flycv.ShareWithTensor(&flycv); +// +// check_shape(opencv.shape, flycv.shape); +// check_type(opencv.dtype, flycv.dtype); +// check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 0, 0); +//} +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_limit_stride.cc b/tests/vision_preprocess/test_vision_limit_stride.cc new file mode 100644 index 000000000..295178f60 --- /dev/null +++ b/tests/vision_preprocess/test_vision_limit_stride.cc @@ -0,0 +1,133 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_limit_stride1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(35, 69, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::LimitByStride::Run(&mat_opencv, 38, 1, vision::ProcLib::OPENCV); + vision::LimitByStride::Run(&mat_flycv, 38, 1, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_type(opencv.dtype, flycv.dtype); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 1); +} + +TEST(fastdeploy, flycv_limit_stride2) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(35, 69, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); + vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); + vision::LimitByStride::Run(&mat_opencv, 32, 1, vision::ProcLib::OPENCV); + vision::LimitByStride::Run(&mat_flycv, 32, 1, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_type(opencv.dtype, flycv.dtype); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 0, 0); +} + +//TEST(fastdeploy, flycv_limit_stride3) { +// CheckShape check_shape; +// CheckData check_data; +// CheckType check_type; +// +// cv::Mat mat(35, 69, CV_8UC3); +// cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); +// cv::Mat mat1 = mat.clone(); +// +// vision::Mat mat_opencv(mat); +// vision::Mat mat_flycv(mat1); +//// vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); +//// vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); +// vision::LimitByStride::Run(&mat_opencv, 32, 0, vision::ProcLib::OPENCV); +// vision::LimitByStride::Run(&mat_flycv, 32, 0, vision::ProcLib::FLYCV); +// +// FDTensor opencv; +// FDTensor flycv; +// +// mat_opencv.ShareWithTensor(&opencv); +// mat_flycv.ShareWithTensor(&flycv); +// +// check_shape(opencv.shape, flycv.shape); +// check_type(opencv.dtype, flycv.dtype); +// check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 1); +//} + +//TEST(fastdeploy, flycv_limit_stride4) { +// CheckShape check_shape; +// CheckData check_data; +// CheckType check_type; +// +// cv::Mat mat(35, 69, CV_8UC3); +// cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); +// cv::Mat mat1 = mat.clone(); +// +// vision::Mat mat_opencv(mat); +// vision::Mat mat_flycv(mat1); +// vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); +// vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); +// vision::LimitByStride::Run(&mat_opencv, 32, 2, vision::ProcLib::OPENCV); +// vision::LimitByStride::Run(&mat_flycv, 32, 2, vision::ProcLib::FLYCV); +// +// FDTensor opencv; +// FDTensor flycv; +// +// mat_opencv.ShareWithTensor(&opencv); +// mat_flycv.ShareWithTensor(&flycv); +// +// check_shape(opencv.shape, flycv.shape); +// check_type(opencv.dtype, flycv.dtype); +// check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 0, 0); +//} + +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_normalize.cc b/tests/vision_preprocess/test_vision_normalize.cc new file mode 100644 index 000000000..44f5e19b4 --- /dev/null +++ b/tests/vision_preprocess/test_vision_normalize.cc @@ -0,0 +1,116 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_norm1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + + std::vector mean({0.25, 0.35, 0.45}); + std::vector std({0.33, 0.22, 0.54}); + std::vector min; + std::vector max; + vision::Normalize::Run(&mat_opencv, mean, std, true, min, max, vision::ProcLib::OPENCV); + vision::Normalize::Run(&mat_flycv, mean, std, true, min, max, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +TEST(fastdeploy, flycv_norm2) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + + std::vector mean({0.25, 0.35, 0.45}); + std::vector std({0.33, 0.22, 0.54}); + std::vector min({1, 2, 3}); + std::vector max({300, 400, 500}); + vision::Normalize::Run(&mat_opencv, mean, std, true, min, max, vision::ProcLib::OPENCV); + vision::Normalize::Run(&mat_flycv, mean, std, true, min, max, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +TEST(fastdeploy, flycv_norm3) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + + std::vector mean({0.25, 0.35, 0.45}); + std::vector std({0.33, 0.22, 0.54}); + std::vector min({1, 2, 3}); + std::vector max({300, 400, 500}); + vision::Normalize::Run(&mat_opencv, mean, std, true, min, max, vision::ProcLib::OPENCV); + vision::Normalize::Run(&mat_flycv, mean, std, true, min, max, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_normalize_and_permute.cc b/tests/vision_preprocess/test_vision_normalize_and_permute.cc new file mode 100644 index 000000000..3fefe73c4 --- /dev/null +++ b/tests/vision_preprocess/test_vision_normalize_and_permute.cc @@ -0,0 +1,65 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_norm_and_perm0) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + cv::Mat mat2 = mat.clone(); + + std::vector mean({0.25, 0.35, 0.45}); + std::vector std({0.33, 0.22, 0.54}); + std::vector min; + std::vector max; + + vision::Mat mat_opencv(mat); + vision::Mat mat_opencv1(mat); + vision::Mat mat_flycv(mat1); + + vision::Normalize::Run(&mat_opencv, mean, std, true, min, max, vision::ProcLib::OPENCV); + vision::HWC2CHW::Run(&mat_opencv, vision::ProcLib::OPENCV); + vision::NormalizeAndPermute::Run(&mat_opencv1, mean, std, true, min, max, vision::ProcLib::OPENCV); + + vision::NormalizeAndPermute::Run(&mat_flycv, mean, std, true, min, max, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor opencv1; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_opencv1.ShareWithTensor(&opencv1); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_data(reinterpret_cast(opencv1.Data()), reinterpret_cast(flycv.Data()), opencv1.Numel()); + check_type(opencv.dtype, flycv.dtype); +} +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_pad.cc b/tests/vision_preprocess/test_vision_pad.cc new file mode 100644 index 000000000..2362462b4 --- /dev/null +++ b/tests/vision_preprocess/test_vision_pad.cc @@ -0,0 +1,78 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_pad1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); + vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); + vision::Pad::Run(&mat_opencv, 1, 2, 3, 4, {1, 2, 3}, vision::ProcLib::OPENCV); + vision::Pad::Run(&mat_flycv, 1, 2, 3, 4, {1, 2, 3}, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +TEST(fastdeploy, flycv_pad2) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 64, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Pad::Run(&mat_opencv, 1, 2, 3, 4, {1, 2, 3}, vision::ProcLib::OPENCV); + vision::Pad::Run(&mat_flycv, 1, 2, 3, 4, {1, 2, 3}, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_pad_to_size.cc b/tests/vision_preprocess/test_vision_pad_to_size.cc new file mode 100644 index 000000000..c378e229c --- /dev/null +++ b/tests/vision_preprocess/test_vision_pad_to_size.cc @@ -0,0 +1,78 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_pad_to_size_1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 21, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); + vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); + vision::PadToSize::Run(&mat_opencv, 102, 77, {1, 12, 13}, vision::ProcLib::OPENCV); + vision::PadToSize::Run(&mat_flycv, 102, 77, {1, 12, 13}, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +TEST(fastdeploy, flycv_pad_to_size_2) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(64, 21, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::PadToSize::Run(&mat_opencv, 102, 77, {0.5, 12, 13}, vision::ProcLib::OPENCV); + vision::PadToSize::Run(&mat_flycv, 102, 77, {0.5, 12, 13}, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_resize.cc b/tests/vision_preprocess/test_vision_resize.cc new file mode 100644 index 000000000..70d2e48c8 --- /dev/null +++ b/tests/vision_preprocess/test_vision_resize.cc @@ -0,0 +1,104 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_limit_resize1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(35, 69, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Resize::Run(&mat_opencv, 38, 19, -1, -1, 1, false, vision::ProcLib::OPENCV); + vision::Resize::Run(&mat_flycv, 38, 19, -1, -1, 1, false, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_type(opencv.dtype, flycv.dtype); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 1); +} + +TEST(fastdeploy, flycv_limit_resize2) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(35, 69, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); + vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); + vision::Resize::Run(&mat_opencv, 38, 19, -1, -1, 1, false, vision::ProcLib::OPENCV); + vision::Resize::Run(&mat_flycv, 38, 19, -1, -1, 1, false, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_type(opencv.dtype, flycv.dtype); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 1); +} + +TEST(fastdeploy, flycv_limit_resize3) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(35, 69, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Resize::Run(&mat_opencv, 38, 19, -1, -1, 0, false, vision::ProcLib::OPENCV); + vision::Resize::Run(&mat_flycv, 38, 19, -1, -1, 0, false, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_type(opencv.dtype, flycv.dtype); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 1); +} + +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_resize_by_short.cc b/tests/vision_preprocess/test_vision_resize_by_short.cc new file mode 100644 index 000000000..58c15cd72 --- /dev/null +++ b/tests/vision_preprocess/test_vision_resize_by_short.cc @@ -0,0 +1,104 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_limit_resize_by_short1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(35, 69, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::ResizeByShort::Run(&mat_opencv, 104, 1, false, {}, vision::ProcLib::OPENCV); + vision::ResizeByShort::Run(&mat_flycv, 104, 1, false, {}, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_type(opencv.dtype, flycv.dtype); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 1); +} + +//TEST(fastdeploy, flycv_limit_resize_by_short2) { +// CheckShape check_shape; +// CheckData check_data; +// CheckType check_type; +// +// cv::Mat mat(35, 69, CV_8UC3); +// cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); +// cv::Mat mat1 = mat.clone(); +// +// vision::Mat mat_opencv(mat); +// vision::Mat mat_flycv(mat1); +// vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); +// vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); +// vision::Resize::Run(&mat_opencv, 38, 19, -1, -1, 1, false, vision::ProcLib::OPENCV); +// vision::Resize::Run(&mat_flycv, 38, 19, -1, -1, 1, false, vision::ProcLib::FLYCV); +// +// FDTensor opencv; +// FDTensor flycv; +// +// mat_opencv.ShareWithTensor(&opencv); +// mat_flycv.ShareWithTensor(&flycv); +// +// check_shape(opencv.shape, flycv.shape); +// check_type(opencv.dtype, flycv.dtype); +// check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 1); +//} +// +//TEST(fastdeploy, flycv_limit_resize_by_stride3) { +// CheckShape check_shape; +// CheckData check_data; +// CheckType check_type; +// +// cv::Mat mat(35, 69, CV_8UC3); +// cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); +// cv::Mat mat1 = mat.clone(); +// +// vision::Mat mat_opencv(mat); +// vision::Mat mat_flycv(mat1); +// vision::Resize::Run(&mat_opencv, 38, 19, -1, -1, 0, false, vision::ProcLib::OPENCV); +// vision::Resize::Run(&mat_flycv, 38, 19, -1, -1, 0, false, vision::ProcLib::FLYCV); +// +// FDTensor opencv; +// FDTensor flycv; +// +// mat_opencv.ShareWithTensor(&opencv); +// mat_flycv.ShareWithTensor(&flycv); +// +// check_shape(opencv.shape, flycv.shape); +// check_type(opencv.dtype, flycv.dtype); +// check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel(), 1); +//} + +#endif + +} // namespace fastdeploy diff --git a/tests/vision_preprocess/test_vision_stride_pad.cc b/tests/vision_preprocess/test_vision_stride_pad.cc new file mode 100644 index 000000000..c8194099d --- /dev/null +++ b/tests/vision_preprocess/test_vision_stride_pad.cc @@ -0,0 +1,78 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include "fastdeploy/vision.h" +#include "glog/logging.h" +#include "gtest/gtest.h" +#include "gtest_utils.h" + +namespace fastdeploy { + +#ifdef ENABLE_FLYCV +TEST(fastdeploy, flycv_stride_pad_1) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(65, 21, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::Cast::Run(&mat_opencv, "float", vision::ProcLib::OPENCV); + vision::Cast::Run(&mat_flycv, "float", vision::ProcLib::FLYCV); + vision::StridePad::Run(&mat_opencv, 64, {0.5, 12, 13}, vision::ProcLib::OPENCV); + vision::StridePad::Run(&mat_flycv, 64, {0.5, 12, 13}, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} + +TEST(fastdeploy, flycv_stride_pad_2) { + CheckShape check_shape; + CheckData check_data; + CheckType check_type; + + cv::Mat mat(65, 21, CV_8UC3); + cv::randu(mat, cv::Scalar::all(0), cv::Scalar::all(255)); + cv::Mat mat1 = mat.clone(); + + vision::Mat mat_opencv(mat); + vision::Mat mat_flycv(mat1); + vision::StridePad::Run(&mat_opencv, 64, {0.5, 12, 13}, vision::ProcLib::OPENCV); + vision::StridePad::Run(&mat_flycv, 64, {0.5, 12, 13}, vision::ProcLib::FLYCV); + + FDTensor opencv; + FDTensor flycv; + + mat_opencv.ShareWithTensor(&opencv); + mat_flycv.ShareWithTensor(&flycv); + + check_shape(opencv.shape, flycv.shape); + check_data(reinterpret_cast(opencv.Data()), reinterpret_cast(flycv.Data()), opencv.Numel()); + check_type(opencv.dtype, flycv.dtype); +} +#endif + +} // namespace fastdeploy