Mxnet C++接口
install
C++接口需要从源码编译安装 跟着教程来也很简单(无脑复制粘贴)
官方的STEP1里安装了OpenBLAS
数学库(后面STEP3是三个命令选一个,我选For a CPU-only build with OpenBLAS math library run:)
跟随官网教程做到STEP3时候需要注意的点:
- 运行cmake时候要添加参数
-USE_CPP_PACKAGE=1
,这样才会开启C++接口,比如这样:
rm -rf build
mkdir -p build && cd build
cmake -GNinja \
-DUSE_CUDA=OFF \
-DUSE_MKL_IF_AVAILABLE=OFF \
-DCMAKE_BUILD_TYPE=Release \
-USE_CPP_PACKAGE=1 \
..
ninja
- 如果是笔记本推荐直接用CPU的跑就行了,如果选GPU,我没折腾过不知道会不会有什么坑。。。我选择的是For a CPU-only build with OpenBLAS math library run:因为STEP1里安装了
- 完成编译之后,就会生成.so和.a两个文件,文件位置可能在lib里(我跟着mac的教程是生成在lib里)也有可能在build文件夹里(官网说ubuntu的是在这里)。然后可以继续跟着教程安装python或者其他语言接口,要不要安装python接口看你自己了(我自己是不安装了python语言接口,要是安装:pip卸载之前安装的,然后跟着教程安装,这样能使用tensorrt,当然这种需要在前面STEP3那儿开启CUDA支持)
关于mxnet的C/C++接口的资料
先熟悉下cmake,寻找头文件以及导入第三方库或者自己写的静态动态库都需要cmake,推荐IDE:Clion
C和C++的区别: 在官方C++接口的Tutorials中提到了C++的前向传播接口是没有测试过线程安全的,如果需要多线程请使用C接口(但是这个貌似是很久之前的文档,不知道现在的版本C++接口是否线程安全,我正在纠结这个)
我的cmakelist.txt文件
一共两个,第一个自己写的,比较简单,第二个是YOLO3里的(可能有错)
自行根据自己的项目、环境进行修改
cmake_minimum_required(VERSION 3.15)
project(HellowMxnet)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/src/*.cpp")
include_directories(
${OpenCV_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}/include
/Users/rainweic/DeepLearning/mxnet/cpp-package/include
/Users/rainweic/DeepLearning/mxnet/include
)
link_directories(
${OpenCV_LIBRARY_DIRS}
/Users/rainweic/DeepLearning/mxnet/lib
)
#add_library(HellowMxnet SHARED ${SOURCES})
add_executable(HellowMxnet ${SOURCES})
target_link_libraries(HellowMxnet
${OpenCV_LIBS}
mxnet)
cmake_minimum_required(VERSION 3.1)
project(YOLOV3)
set(CMAKE_CXX_STANDARD 11)
set(MXNET_ROOT “/Users/rainweic/DeepLearning/mxnet”)
# — MXNet
include_directories(
${MXNET_ROOT}/include
${MXNET_ROOT}/cpp-package/include
${MXNET_ROOT}/3rdparty/dlpack/include
${MXNET_ROOT}/3rdparty/dmlc-core/include
${MXNET_ROOT}/3rdparty/mkldnn/include
${MXNET_ROOT}/3rdparty/mshadow
${MXNET_ROOT}/3rdparty/tvm/nnvm/include
${CMAKE_CURRENT_LIST_DIR}/src
)
SET(CMAKE_FIND_LIBRARY_PREFIXES “lib”)
find_library(MXNET_LIBS NAMES mxnet HINTS “${MXNET_ROOT}/lib”)
message(STATUS MXNET_LIBS: ${MXNET_LIBS})
message(STATUS CURRENT_DIR: ${CMAKE_CURRENT_LIST_DIR})
set(LINKER_LIBS ${MXNET_LIBS})
# — OpenCV
find_package(OpenCV COMPONENTS core highgui imgproc imgcodecs)
if(NOT OpenCV_FOUND) # if not OpenCV 3.x, then imgcodecs are not found
message(STATUS “OpenCV imgcodecs missing”)
find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc)
endif()
include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS})
list(APPEND LINKER_LIBS ${OpenCV_LIBS})
message(STATUS “ OpenCV_LIBS=${OpenCV_LIBS}”)
message(STATUS "OpenCV ${OpenCV_VERSION} found (${OpenCV_CONFIG_PATH})")
# — Targets and Installation
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_LIST_DIR})
add_executable(gluoncv-detect src/detect.cpp)
target_link_libraries(gluoncv-detect ${LINKER_LIBS})
set(EXECS gluoncv-detect)
install(TARGETS ${EXECS} DESTINATION install)
if(APPLE)
install(FILES ${MXNET_LIBS} DESTINATION install/lib)
elseif(MSVC)
install(FILES ${MXNET_ROOT}/lib/libmxnet.dll DESTINATION install)
file(GLOB OPENBLAS_FILES $ENV{OpenBLAS_HOME}/*.dll $ENV{OpenBLAS_HOME}/bin/*.dll)
file(GLOB OPENCV_FILES ${_OpenCV_LIB_PATH}/*core*.dll ${_OpenCV_LIB_PATH}/*highgui*.dll ${_OpenCV_LIB_PATH}/*imgproc*.dll ${_OpenCV_LIB_PATH}/*imgcodecs*.dll ${_OpenCV_LIB_PATH}/*videoio*.dll)
install(FILES ${OPENBLAS_FILES} DESTINATION install)
install(FILES ${OPENCV_FILES} DESTINATION install)
else()
install(FILES ${MXNET_LIBS} DESTINATION install)
endif()