From 15ad234a7aaccb36e8426f0df93cda79f08aa5fb Mon Sep 17 00:00:00 2001 From: Chen Xin Date: Tue, 17 Jan 2023 18:52:58 +0800 Subject: [PATCH] Cherry-pick PR 1366, 1370, 1460, 1577, 1598, 1601, 1612, 1626 to dev-1.x (#1635) * [Fix] Fix example standalone build for msvc (#1626) * fix example build for msvc * move /Zc:__cplusplus to core * [FIX] Fix csharp net48 and batch inference (#1370) * fix csharp net48 * add missing file * update * fix batch inference * update demo * update * update version * update nuget package version & fix project name * build monolithic SDK by default (#1577) * Fix debug build for PoseTracker (#1601) * [Fix] Fix aligned allocations on Android (#1612) * fix android alignment * fix typo * fix size * Fix build error on windows-cuda platform (#1598) * [Fix] Relax module adapter template constraints (#1366) * relax module adapter constraint * remove forwarding `operator()` * fix YOLOV5Head naming Co-authored-by: Li Zhang --- CMakeLists.txt | 6 ++---- csrc/mmdeploy/apis/csharp/MMDeploy.sln | 2 +- csrc/mmdeploy/apis/csharp/MMDeploy/APIs/common.cs | 1 + .../{MMDeploy.csproj => MMDeployCSharp.csproj} | 13 +++++++++++-- .../apis/csharp/MMDeploy/MMDeployCSharp.props | 11 +++++++++++ csrc/mmdeploy/apis/csharp/README.md | 4 ++-- csrc/mmdeploy/codebase/mmdet/yolo_head.cpp | 12 ++---------- csrc/mmdeploy/codebase/mmdet/yolo_head.h | 4 +--- csrc/mmdeploy/core/CMakeLists.txt | 5 +++++ csrc/mmdeploy/device/cpu/cpu_device.cpp | 5 ++++- csrc/mmdeploy/experimental/module_adapter.h | 8 ++++---- csrc/mmdeploy/operation/cuda/warp_affine.cpp | 1 + demo/csharp/image_classification/FormDemo.cs | 1 + .../image_classification.csproj | 2 +- demo/csharp/image_restorer/Program.cs | 1 + demo/csharp/image_restorer/image_restorer.csproj | 2 +- demo/csharp/image_segmentation/Program.cs | 1 + .../image_segmentation/image_segmentation.csproj | 2 +- demo/csharp/object_detection/Program.cs | 1 + .../csharp/object_detection/object_detection.csproj | 4 ++-- demo/csharp/ocr_detection/Program.cs | 1 + demo/csharp/ocr_detection/ocr_detection.csproj | 2 +- demo/csharp/ocr_recognition/Program.cs | 1 + demo/csharp/ocr_recognition/ocr_recognition.csproj | 2 +- demo/csharp/pose_detection/Program.cs | 1 + demo/csharp/pose_detection/pose_detection.csproj | 2 +- demo/csrc/cpp/pose_tracker.cpp | 4 ++-- 27 files changed, 62 insertions(+), 37 deletions(-) rename csrc/mmdeploy/apis/csharp/MMDeploy/{MMDeploy.csproj => MMDeployCSharp.csproj} (77%) create mode 100644 csrc/mmdeploy/apis/csharp/MMDeploy/MMDeployCSharp.props diff --git a/CMakeLists.txt b/CMakeLists.txt index 97f36f2c9b..839681a7d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,9 +22,9 @@ endif () set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # options -option(MMDEPLOY_SHARED_LIBS "build shared libs" ON) +option(MMDEPLOY_SHARED_LIBS "build shared libs" OFF) option(MMDEPLOY_BUILD_SDK "build MMDeploy SDK" OFF) -option(MMDEPLOY_BUILD_SDK_MONOLITHIC "build single lib for SDK API" OFF) +option(MMDEPLOY_BUILD_SDK_MONOLITHIC "build single lib for SDK API" ON) option(MMDEPLOY_BUILD_TEST "build unittests" OFF) option(MMDEPLOY_BUILD_SDK_PYTHON_API "build SDK Python API" OFF) option(MMDEPLOY_BUILD_SDK_CXX_API "build SDK C++ API" OFF) @@ -73,8 +73,6 @@ endif () if (MSVC) add_compile_options($<$:/diagnostics:classic>) - add_compile_options($<$:/Zc:preprocessor>) # /experimental:preprocessor on VS2017 - add_compile_options($<$:/Zc:__cplusplus>) add_compile_options($<$:/wd4251>) endif () diff --git a/csrc/mmdeploy/apis/csharp/MMDeploy.sln b/csrc/mmdeploy/apis/csharp/MMDeploy.sln index accd1cb560..72cbead791 100644 --- a/csrc/mmdeploy/apis/csharp/MMDeploy.sln +++ b/csrc/mmdeploy/apis/csharp/MMDeploy.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31729.503 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MMDeploy", "MMDeploy\MMDeploy.csproj", "{3DC914EB-A8FB-4A89-A7CF-7DF9CC5284A6}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MMDeploy", "MMDeploy\MMDeployCSharp.csproj", "{3DC914EB-A8FB-4A89-A7CF-7DF9CC5284A6}" EndProject Global diff --git a/csrc/mmdeploy/apis/csharp/MMDeploy/APIs/common.cs b/csrc/mmdeploy/apis/csharp/MMDeploy/APIs/common.cs index 0bf302bcdc..be59983623 100644 --- a/csrc/mmdeploy/apis/csharp/MMDeploy/APIs/common.cs +++ b/csrc/mmdeploy/apis/csharp/MMDeploy/APIs/common.cs @@ -53,6 +53,7 @@ public unsafe struct Mat public int Channel; public PixelFormat Format; public DataType Type; + public void* Device; } /// diff --git a/csrc/mmdeploy/apis/csharp/MMDeploy/MMDeploy.csproj b/csrc/mmdeploy/apis/csharp/MMDeploy/MMDeployCSharp.csproj similarity index 77% rename from csrc/mmdeploy/apis/csharp/MMDeploy/MMDeploy.csproj rename to csrc/mmdeploy/apis/csharp/MMDeploy/MMDeployCSharp.csproj index 8b723f6834..ac140348d9 100644 --- a/csrc/mmdeploy/apis/csharp/MMDeploy/MMDeploy.csproj +++ b/csrc/mmdeploy/apis/csharp/MMDeploy/MMDeployCSharp.csproj @@ -4,7 +4,7 @@ netcoreapp3.1;net48; 9.0 enable - MMDeploy + MMDeployCSharp MMDeploy true true @@ -17,7 +17,16 @@ $(MSBuildThisFileDirectory)\..\..\..\..\.. - + + PreserveNewest + true + runtimes\win-x64\native\mmdeploy.dll + + + PreserveNewest + true + build\net48\MMDeployCSharp.props + diff --git a/csrc/mmdeploy/apis/csharp/MMDeploy/MMDeployCSharp.props b/csrc/mmdeploy/apis/csharp/MMDeploy/MMDeployCSharp.props new file mode 100644 index 0000000000..f893a03766 --- /dev/null +++ b/csrc/mmdeploy/apis/csharp/MMDeploy/MMDeployCSharp.props @@ -0,0 +1,11 @@ + + + $(MSBuildThisFileDirectory)..\..\runtimes + + + + mmdeploy.dll + PreserveNewest + + + diff --git a/csrc/mmdeploy/apis/csharp/README.md b/csrc/mmdeploy/apis/csharp/README.md index 0a0239443d..16526cc1a9 100644 --- a/csrc/mmdeploy/apis/csharp/README.md +++ b/csrc/mmdeploy/apis/csharp/README.md @@ -33,10 +33,10 @@ There are two methods to build the nuget package. (*option 1*) Use the command. -If your environment is well prepared, you can just go to the `csrc\apis\csharp` folder, open a terminal and type the following command, the nupkg will be built in `csrc\apis\csharp\MMDeploy\bin\Release\MMDeploy.1.0.0.nupkg`. +If your environment is well prepared, you can just go to the `csrc\apis\csharp` folder, open a terminal and type the following command, the nupkg will be built in `csrc\apis\csharp\MMDeploy\bin\Release\MMDeployCSharp.1.0.0-rc1.nupkg`. ```shell -dotnet build --configuration Release -p:Version=1.0.0 +dotnet build --configuration Release -p:Version=1.0.0-rc1 ``` (*option 2*) Open MMDeploy.sln && Build. diff --git a/csrc/mmdeploy/codebase/mmdet/yolo_head.cpp b/csrc/mmdeploy/codebase/mmdet/yolo_head.cpp index e9f0c8ce0f..fd746d0bc0 100644 --- a/csrc/mmdeploy/codebase/mmdet/yolo_head.cpp +++ b/csrc/mmdeploy/codebase/mmdet/yolo_head.cpp @@ -197,10 +197,6 @@ Result YOLOHead::GetBBoxes(const Value& prep_res, return objs; } -Result YOLOV3Head::operator()(const Value& prep_res, const Value& infer_res) { - return YOLOHead::operator()(prep_res, infer_res); -} - std::array YOLOV3Head::yolo_decode(float box_x, float box_y, float box_w, float box_h, float stride, const std::vector>& anchor, int j, @@ -212,11 +208,7 @@ std::array YOLOV3Head::yolo_decode(float box_x, float box_y, float box return std::array{box_x, box_y, box_w, box_h}; } -Result YOLOv5Head::operator()(const Value& prep_res, const Value& infer_res) { - return YOLOHead::operator()(prep_res, infer_res); -} - -std::array YOLOv5Head::yolo_decode(float box_x, float box_y, float box_w, float box_h, +std::array YOLOV5Head::yolo_decode(float box_x, float box_y, float box_w, float box_h, float stride, const std::vector>& anchor, int j, int i, int a) const { @@ -232,6 +224,6 @@ std::array YOLOv5Head::yolo_decode(float box_x, float box_y, float box } MMDEPLOY_REGISTER_CODEBASE_COMPONENT(MMDetection, YOLOV3Head); -MMDEPLOY_REGISTER_CODEBASE_COMPONENT(MMDetection, YOLOv5Head); +MMDEPLOY_REGISTER_CODEBASE_COMPONENT(MMDetection, YOLOV5Head); } // namespace mmdeploy::mmdet diff --git a/csrc/mmdeploy/codebase/mmdet/yolo_head.h b/csrc/mmdeploy/codebase/mmdet/yolo_head.h index 34ba4a27fc..dd3be05e8f 100644 --- a/csrc/mmdeploy/codebase/mmdet/yolo_head.h +++ b/csrc/mmdeploy/codebase/mmdet/yolo_head.h @@ -33,16 +33,14 @@ class YOLOHead : public MMDetection { class YOLOV3Head : public YOLOHead { public: using YOLOHead::YOLOHead; - Result operator()(const Value& prep_res, const Value& infer_res); std::array yolo_decode(float box_x, float box_y, float box_w, float box_h, float stride, const std::vector>& anchor, int j, int i, int a) const override; }; -class YOLOv5Head : public YOLOHead { +class YOLOV5Head : public YOLOHead { public: using YOLOHead::YOLOHead; - Result operator()(const Value& prep_res, const Value& infer_res); std::array yolo_decode(float box_x, float box_y, float box_w, float box_h, float stride, const std::vector>& anchor, int j, int i, int a) const override; diff --git a/csrc/mmdeploy/core/CMakeLists.txt b/csrc/mmdeploy/core/CMakeLists.txt index eb2d408536..33d0f76c57 100644 --- a/csrc/mmdeploy/core/CMakeLists.txt +++ b/csrc/mmdeploy/core/CMakeLists.txt @@ -54,6 +54,11 @@ target_include_directories(${PROJECT_NAME} $ ) +if (MSVC) + target_compile_options(${PROJECT_NAME} PUBLIC + $<$:/Zc:preprocessor;/Zc:__cplusplus>) +endif () + if (MMDEPLOY_STATUS_USE_STACKTRACE) include(${CMAKE_SOURCE_DIR}/cmake/stacktrace.cmake) else () diff --git a/csrc/mmdeploy/device/cpu/cpu_device.cpp b/csrc/mmdeploy/device/cpu/cpu_device.cpp index 193ce89e02..23c90d95f5 100644 --- a/csrc/mmdeploy/device/cpu/cpu_device.cpp +++ b/csrc/mmdeploy/device/cpu/cpu_device.cpp @@ -11,9 +11,12 @@ class CpuHostMemory : public NonCopyable { public: CpuHostMemory() : size_(), data_(), owned_data_{false} {} Result Init(size_t size, size_t alignment) { - size_t space = (size + alignment - 1) / alignment * alignment; + alignment = std::max(alignment, sizeof(void*)); + auto space = (size + alignment - 1) / alignment * alignment; #ifdef _MSC_VER data_ = _aligned_malloc(space, alignment); +#elif defined(ANDROID) + posix_memalign(&data_, alignment, space); #else data_ = std::aligned_alloc(alignment, space); #endif diff --git a/csrc/mmdeploy/experimental/module_adapter.h b/csrc/mmdeploy/experimental/module_adapter.h index e5e2985ec2..74dd9d9b86 100644 --- a/csrc/mmdeploy/experimental/module_adapter.h +++ b/csrc/mmdeploy/experimental/module_adapter.h @@ -63,12 +63,12 @@ Result Invoke(Ret (*f)(Args...), const Value& args) { } // member function pointer -template -Result Invoke(Ret (C::*f)(Args...) const, C* inst, const Value& args) { +template +Result Invoke(Ret (C0::*f)(Args...) const, C1* inst, const Value& args) { return InvokeImpl::apply(f, args, inst); } -template -Result Invoke(Ret (C::*f)(Args...), C* inst, const Value& args) { +template +Result Invoke(Ret (C0::*f)(Args...), C1* inst, const Value& args) { return InvokeImpl::apply(f, args, inst); } diff --git a/csrc/mmdeploy/operation/cuda/warp_affine.cpp b/csrc/mmdeploy/operation/cuda/warp_affine.cpp index 4f2071c068..6c4834f018 100644 --- a/csrc/mmdeploy/operation/cuda/warp_affine.cpp +++ b/csrc/mmdeploy/operation/cuda/warp_affine.cpp @@ -1,4 +1,5 @@ // Copyright (c) OpenMMLab. All rights reserved. +#include #include "mmdeploy/core/utils/formatter.h" #include "mmdeploy/operation/vision.h" diff --git a/demo/csharp/image_classification/FormDemo.cs b/demo/csharp/image_classification/FormDemo.cs index 9cccc55929..1a1d9278e4 100644 --- a/demo/csharp/image_classification/FormDemo.cs +++ b/demo/csharp/image_classification/FormDemo.cs @@ -82,6 +82,7 @@ static void CvMatToMat(CvMat[] cvMats, out Mat[] mats) mats[i].Channel = cvMats[i].Dims; mats[i].Format = PixelFormat.BGR; mats[i].Type = DataType.Int8; + mats[i].Device = null; } } } diff --git a/demo/csharp/image_classification/image_classification.csproj b/demo/csharp/image_classification/image_classification.csproj index 6d30462441..88e059b63e 100644 --- a/demo/csharp/image_classification/image_classification.csproj +++ b/demo/csharp/image_classification/image_classification.csproj @@ -14,7 +14,7 @@ - + diff --git a/demo/csharp/image_restorer/Program.cs b/demo/csharp/image_restorer/Program.cs index d70809bd18..cc4d60e31c 100644 --- a/demo/csharp/image_restorer/Program.cs +++ b/demo/csharp/image_restorer/Program.cs @@ -28,6 +28,7 @@ static void CvMatToMat(CvMat[] cvMats, out Mat[] mats) mats[i].Channel = cvMats[i].Dims; mats[i].Format = PixelFormat.BGR; mats[i].Type = DataType.Int8; + mats[i].Device = null; } } } diff --git a/demo/csharp/image_restorer/image_restorer.csproj b/demo/csharp/image_restorer/image_restorer.csproj index 693587bcd9..d36f2e148c 100644 --- a/demo/csharp/image_restorer/image_restorer.csproj +++ b/demo/csharp/image_restorer/image_restorer.csproj @@ -14,7 +14,7 @@ - + diff --git a/demo/csharp/image_segmentation/Program.cs b/demo/csharp/image_segmentation/Program.cs index 9395fca67f..1318c001f4 100644 --- a/demo/csharp/image_segmentation/Program.cs +++ b/demo/csharp/image_segmentation/Program.cs @@ -23,6 +23,7 @@ static void CvMatToMat(OpenCvSharp.Mat[] cvMats, out MMDeploy.Mat[] mats) mats[i].Channel = cvMats[i].Dims; mats[i].Format = PixelFormat.BGR; mats[i].Type = DataType.Int8; + mats[i].Device = null; } } } diff --git a/demo/csharp/image_segmentation/image_segmentation.csproj b/demo/csharp/image_segmentation/image_segmentation.csproj index 693587bcd9..d36f2e148c 100644 --- a/demo/csharp/image_segmentation/image_segmentation.csproj +++ b/demo/csharp/image_segmentation/image_segmentation.csproj @@ -14,7 +14,7 @@ - + diff --git a/demo/csharp/object_detection/Program.cs b/demo/csharp/object_detection/Program.cs index de146d5499..82236b2592 100644 --- a/demo/csharp/object_detection/Program.cs +++ b/demo/csharp/object_detection/Program.cs @@ -20,6 +20,7 @@ static void CvMatToMat(OpenCvSharp.Mat[] cvMats, out MMDeploy.Mat[] mats) mats[i].Channel = cvMats[i].Dims; mats[i].Format = PixelFormat.BGR; mats[i].Type = DataType.Int8; + mats[i].Device = null; } } } diff --git a/demo/csharp/object_detection/object_detection.csproj b/demo/csharp/object_detection/object_detection.csproj index 693587bcd9..3d33080ffd 100644 --- a/demo/csharp/object_detection/object_detection.csproj +++ b/demo/csharp/object_detection/object_detection.csproj @@ -1,4 +1,4 @@ - + Exe @@ -14,7 +14,7 @@ - + diff --git a/demo/csharp/ocr_detection/Program.cs b/demo/csharp/ocr_detection/Program.cs index 0c96fcedcd..2f22f62958 100644 --- a/demo/csharp/ocr_detection/Program.cs +++ b/demo/csharp/ocr_detection/Program.cs @@ -20,6 +20,7 @@ static void CvMatToMat(OpenCvSharp.Mat[] cvMats, out MMDeploy.Mat[] mats) mats[i].Channel = cvMats[i].Dims; mats[i].Format = PixelFormat.BGR; mats[i].Type = DataType.Int8; + mats[i].Device = null; } } } diff --git a/demo/csharp/ocr_detection/ocr_detection.csproj b/demo/csharp/ocr_detection/ocr_detection.csproj index 693587bcd9..d36f2e148c 100644 --- a/demo/csharp/ocr_detection/ocr_detection.csproj +++ b/demo/csharp/ocr_detection/ocr_detection.csproj @@ -14,7 +14,7 @@ - + diff --git a/demo/csharp/ocr_recognition/Program.cs b/demo/csharp/ocr_recognition/Program.cs index 04ed3bfc28..e4421418ab 100644 --- a/demo/csharp/ocr_recognition/Program.cs +++ b/demo/csharp/ocr_recognition/Program.cs @@ -20,6 +20,7 @@ static void CvMatToMat(OpenCvSharp.Mat[] cvMats, out MMDeploy.Mat[] mats) mats[i].Channel = cvMats[i].Dims; mats[i].Format = PixelFormat.BGR; mats[i].Type = DataType.Int8; + mats[i].Device = null; } } } diff --git a/demo/csharp/ocr_recognition/ocr_recognition.csproj b/demo/csharp/ocr_recognition/ocr_recognition.csproj index 693587bcd9..d36f2e148c 100644 --- a/demo/csharp/ocr_recognition/ocr_recognition.csproj +++ b/demo/csharp/ocr_recognition/ocr_recognition.csproj @@ -14,7 +14,7 @@ - + diff --git a/demo/csharp/pose_detection/Program.cs b/demo/csharp/pose_detection/Program.cs index 67367f400e..5cfcc59f79 100644 --- a/demo/csharp/pose_detection/Program.cs +++ b/demo/csharp/pose_detection/Program.cs @@ -20,6 +20,7 @@ static void CvMatToMat(OpenCvSharp.Mat[] cvMats, out MMDeploy.Mat[] mats) mats[i].Channel = cvMats[i].Dims; mats[i].Format = PixelFormat.BGR; mats[i].Type = DataType.Int8; + mats[i].Device = null; } } } diff --git a/demo/csharp/pose_detection/pose_detection.csproj b/demo/csharp/pose_detection/pose_detection.csproj index 693587bcd9..d36f2e148c 100644 --- a/demo/csharp/pose_detection/pose_detection.csproj +++ b/demo/csharp/pose_detection/pose_detection.csproj @@ -14,7 +14,7 @@ - + diff --git a/demo/csrc/cpp/pose_tracker.cpp b/demo/csrc/cpp/pose_tracker.cpp index 1ddab89784..ec95afe03d 100644 --- a/demo/csrc/cpp/pose_tracker.cpp +++ b/demo/csrc/cpp/pose_tracker.cpp @@ -489,8 +489,8 @@ class Tracker { float ObjectKeypointSimilarity(const Points& pts_a, const Bbox& box_a, const Points& pts_b, const Bbox& box_b) { - assert(pts_a.size() == sigmas.size()); - assert(pts_b.size() == sigmas.size()); + assert(pts_a.size() == params.sigmas.size()); + assert(pts_b.size() == params.sigmas.size()); auto scale = [](const Bbox& bbox) -> float { auto a = bbox[2] - bbox[0]; auto b = bbox[3] - bbox[1];