CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. cmake_minimum_required(VERSION 3.0)
  2. project(PaddleObjectDetector CXX C)
  3. option(WITH_MKL "Compile demo with MKL/OpenBlas support,defaultuseMKL." ON)
  4. option(WITH_GPU "Compile demo with GPU/CPU, default use CPU." ON)
  5. option(WITH_TENSORRT "Compile demo with TensorRT." OFF)
  6. option(WITH_KEYPOINT "Whether to Compile KeyPoint detector" OFF)
  7. option(WITH_MOT "Whether to Compile MOT detector" OFF)
  8. SET(PADDLE_DIR "" CACHE PATH "Location of libraries")
  9. SET(PADDLE_LIB_NAME "" CACHE STRING "libpaddle_inference")
  10. SET(OPENCV_DIR "" CACHE PATH "Location of libraries")
  11. SET(CUDA_LIB "" CACHE PATH "Location of libraries")
  12. SET(CUDNN_LIB "" CACHE PATH "Location of libraries")
  13. SET(TENSORRT_INC_DIR "" CACHE PATH "Compile demo with TensorRT")
  14. SET(TENSORRT_LIB_DIR "" CACHE PATH "Compile demo with TensorRT")
  15. include(cmake/yaml-cpp.cmake)
  16. include_directories("${CMAKE_SOURCE_DIR}/")
  17. include_directories("${CMAKE_CURRENT_BINARY_DIR}/ext/yaml-cpp/src/ext-yaml-cpp/include")
  18. link_directories("${CMAKE_CURRENT_BINARY_DIR}/ext/yaml-cpp/lib")
  19. if (WITH_KEYPOINT)
  20. set(SRCS src/main_keypoint.cc src/preprocess_op.cc src/object_detector.cc src/picodet_postprocess.cc src/utils.cc src/keypoint_detector.cc src/keypoint_postprocess.cc)
  21. elseif (WITH_MOT)
  22. set(SRCS src/main_jde.cc src/preprocess_op.cc src/object_detector.cc src/jde_detector.cc src/tracker.cc src/trajectory.cc src/lapjv.cpp src/picodet_postprocess.cc src/utils.cc)
  23. else ()
  24. set(SRCS src/main.cc src/preprocess_op.cc src/object_detector.cc src/picodet_postprocess.cc src/utils.cc)
  25. endif()
  26. macro(safe_set_static_flag)
  27. foreach(flag_var
  28. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  29. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  30. if(${flag_var} MATCHES "/MD")
  31. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  32. endif(${flag_var} MATCHES "/MD")
  33. endforeach(flag_var)
  34. endmacro()
  35. if (WITH_MKL)
  36. ADD_DEFINITIONS(-DUSE_MKL)
  37. endif()
  38. if (NOT DEFINED PADDLE_DIR OR ${PADDLE_DIR} STREQUAL "")
  39. message(FATAL_ERROR "please set PADDLE_DIR with -DPADDLE_DIR=/path/paddle_influence_dir")
  40. endif()
  41. message("PADDLE_DIR IS:" ${PADDLE_DIR})
  42. if (NOT DEFINED OPENCV_DIR OR ${OPENCV_DIR} STREQUAL "")
  43. message(FATAL_ERROR "please set OPENCV_DIR with -DOPENCV_DIR=/path/opencv")
  44. endif()
  45. include_directories("${CMAKE_SOURCE_DIR}/")
  46. include_directories("${PADDLE_DIR}/")
  47. include_directories("${PADDLE_DIR}/third_party/install/protobuf/include")
  48. include_directories("${PADDLE_DIR}/third_party/install/glog/include")
  49. include_directories("${PADDLE_DIR}/third_party/install/gflags/include")
  50. include_directories("${PADDLE_DIR}/third_party/install/xxhash/include")
  51. if (EXISTS "${PADDLE_DIR}/third_party/install/snappy/include")
  52. include_directories("${PADDLE_DIR}/third_party/install/snappy/include")
  53. endif()
  54. if(EXISTS "${PADDLE_DIR}/third_party/install/snappystream/include")
  55. include_directories("${PADDLE_DIR}/third_party/install/snappystream/include")
  56. endif()
  57. include_directories("${PADDLE_DIR}/third_party/boost")
  58. include_directories("${PADDLE_DIR}/third_party/eigen3")
  59. if (EXISTS "${PADDLE_DIR}/third_party/install/snappy/lib")
  60. link_directories("${PADDLE_DIR}/third_party/install/snappy/lib")
  61. endif()
  62. if(EXISTS "${PADDLE_DIR}/third_party/install/snappystream/lib")
  63. link_directories("${PADDLE_DIR}/third_party/install/snappystream/lib")
  64. endif()
  65. link_directories("${PADDLE_DIR}/third_party/install/protobuf/lib")
  66. link_directories("${PADDLE_DIR}/third_party/install/glog/lib")
  67. link_directories("${PADDLE_DIR}/third_party/install/gflags/lib")
  68. link_directories("${PADDLE_DIR}/third_party/install/xxhash/lib")
  69. link_directories("${PADDLE_DIR}/third_party/install/paddle2onnx/lib")
  70. link_directories("${PADDLE_DIR}/third_party/install/onnxruntime/lib")
  71. link_directories("${PADDLE_DIR}/paddle/lib/")
  72. link_directories("${CMAKE_CURRENT_BINARY_DIR}")
  73. if (WIN32)
  74. include_directories("${PADDLE_DIR}/paddle/fluid/inference")
  75. include_directories("${PADDLE_DIR}/paddle/include")
  76. link_directories("${PADDLE_DIR}/paddle/fluid/inference")
  77. find_package(OpenCV REQUIRED PATHS ${OPENCV_DIR}/build/ NO_DEFAULT_PATH)
  78. else ()
  79. find_package(OpenCV REQUIRED PATHS ${OPENCV_DIR}/share/OpenCV NO_DEFAULT_PATH)
  80. include_directories("${PADDLE_DIR}/paddle/include")
  81. link_directories("${PADDLE_DIR}/paddle/lib")
  82. endif ()
  83. include_directories(${OpenCV_INCLUDE_DIRS})
  84. if (WIN32)
  85. add_definitions("/DGOOGLE_GLOG_DLL_DECL=")
  86. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /bigobj /MTd")
  87. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /bigobj /MT")
  88. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj /MTd")
  89. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /bigobj /MT")
  90. else()
  91. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -o2 -fopenmp -std=c++11")
  92. set(CMAKE_STATIC_LIBRARY_PREFIX "")
  93. endif()
  94. # TODO let users define cuda lib path
  95. if (WITH_GPU)
  96. if (NOT DEFINED CUDA_LIB OR ${CUDA_LIB} STREQUAL "")
  97. message(FATAL_ERROR "please set CUDA_LIB with -DCUDA_LIB=/path/cuda-8.0/lib64")
  98. endif()
  99. if (NOT WIN32)
  100. if (NOT DEFINED CUDNN_LIB)
  101. message(FATAL_ERROR "please set CUDNN_LIB with -DCUDNN_LIB=/path/cudnn_v7.4/cuda/lib64")
  102. endif()
  103. endif(NOT WIN32)
  104. endif()
  105. if (NOT WIN32)
  106. if (WITH_TENSORRT AND WITH_GPU)
  107. include_directories("${TENSORRT_INC_DIR}/")
  108. link_directories("${TENSORRT_LIB_DIR}/")
  109. endif()
  110. endif(NOT WIN32)
  111. if (NOT WIN32)
  112. set(NGRAPH_PATH "${PADDLE_DIR}/third_party/install/ngraph")
  113. if(EXISTS ${NGRAPH_PATH})
  114. include(GNUInstallDirs)
  115. include_directories("${NGRAPH_PATH}/include")
  116. link_directories("${NGRAPH_PATH}/${CMAKE_INSTALL_LIBDIR}")
  117. set(NGRAPH_LIB ${NGRAPH_PATH}/${CMAKE_INSTALL_LIBDIR}/libngraph${CMAKE_SHARED_LIBRARY_SUFFIX})
  118. endif()
  119. endif()
  120. if(WITH_MKL)
  121. include_directories("${PADDLE_DIR}/third_party/install/mklml/include")
  122. if (WIN32)
  123. set(MATH_LIB ${PADDLE_DIR}/third_party/install/mklml/lib/mklml.lib
  124. ${PADDLE_DIR}/third_party/install/mklml/lib/libiomp5md.lib)
  125. else ()
  126. set(MATH_LIB ${PADDLE_DIR}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX}
  127. ${PADDLE_DIR}/third_party/install/mklml/lib/libiomp5${CMAKE_SHARED_LIBRARY_SUFFIX})
  128. execute_process(COMMAND cp -r ${PADDLE_DIR}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX} /usr/lib)
  129. endif ()
  130. set(MKLDNN_PATH "${PADDLE_DIR}/third_party/install/mkldnn")
  131. if(EXISTS ${MKLDNN_PATH})
  132. include_directories("${MKLDNN_PATH}/include")
  133. if (WIN32)
  134. set(MKLDNN_LIB ${MKLDNN_PATH}/lib/mkldnn.lib)
  135. else ()
  136. set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libmkldnn.so.0)
  137. endif ()
  138. endif()
  139. else()
  140. set(MATH_LIB ${PADDLE_DIR}/third_party/install/openblas/lib/libopenblas${CMAKE_STATIC_LIBRARY_SUFFIX})
  141. endif()
  142. if (WIN32)
  143. if(EXISTS "${PADDLE_DIR}/paddle/fluid/inference/${PADDLE_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}")
  144. set(DEPS
  145. ${PADDLE_DIR}/paddle/fluid/inference/${PADDLE_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
  146. else()
  147. set(DEPS
  148. ${PADDLE_DIR}/paddle/lib/${PADDLE_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
  149. endif()
  150. endif()
  151. if (WIN32)
  152. set(DEPS ${PADDLE_DIR}/paddle/lib/${PADDLE_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
  153. else()
  154. set(DEPS ${PADDLE_DIR}/paddle/lib/${PADDLE_LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
  155. endif()
  156. message("PADDLE_LIB_NAME:" ${PADDLE_LIB_NAME})
  157. message("DEPS:" $DEPS)
  158. if (NOT WIN32)
  159. set(DEPS ${DEPS}
  160. ${MATH_LIB} ${MKLDNN_LIB}
  161. glog gflags protobuf z xxhash yaml-cpp
  162. )
  163. if(EXISTS "${PADDLE_DIR}/third_party/install/snappystream/lib")
  164. set(DEPS ${DEPS} snappystream)
  165. endif()
  166. if (EXISTS "${PADDLE_DIR}/third_party/install/snappy/lib")
  167. set(DEPS ${DEPS} snappy)
  168. endif()
  169. else()
  170. set(DEPS ${DEPS}
  171. ${MATH_LIB} ${MKLDNN_LIB}
  172. glog gflags_static libprotobuf xxhash libyaml-cppmt)
  173. set(DEPS ${DEPS} libcmt shlwapi)
  174. if (EXISTS "${PADDLE_DIR}/third_party/install/snappy/lib")
  175. set(DEPS ${DEPS} snappy)
  176. endif()
  177. if(EXISTS "${PADDLE_DIR}/third_party/install/snappystream/lib")
  178. set(DEPS ${DEPS} snappystream)
  179. endif()
  180. endif(NOT WIN32)
  181. if(WITH_GPU)
  182. if(NOT WIN32)
  183. if (WITH_TENSORRT)
  184. set(DEPS ${DEPS} ${TENSORRT_LIB_DIR}/libnvinfer${CMAKE_SHARED_LIBRARY_SUFFIX})
  185. set(DEPS ${DEPS} ${TENSORRT_LIB_DIR}/libnvinfer_plugin${CMAKE_SHARED_LIBRARY_SUFFIX})
  186. endif()
  187. set(DEPS ${DEPS} ${CUDA_LIB}/libcudart${CMAKE_SHARED_LIBRARY_SUFFIX})
  188. set(DEPS ${DEPS} ${CUDNN_LIB}/libcudnn${CMAKE_SHARED_LIBRARY_SUFFIX})
  189. else()
  190. set(DEPS ${DEPS} ${CUDA_LIB}/cudart${CMAKE_STATIC_LIBRARY_SUFFIX} )
  191. set(DEPS ${DEPS} ${CUDA_LIB}/cublas${CMAKE_STATIC_LIBRARY_SUFFIX} )
  192. set(DEPS ${DEPS} ${CUDNN_LIB}/cudnn${CMAKE_STATIC_LIBRARY_SUFFIX})
  193. endif()
  194. endif()
  195. if (NOT WIN32)
  196. set(EXTERNAL_LIB "-ldl -lrt -lgomp -lz -lm -lpthread")
  197. set(DEPS ${DEPS} ${EXTERNAL_LIB})
  198. endif()
  199. set(DEPS ${DEPS} ${OpenCV_LIBS})
  200. add_executable(main ${SRCS})
  201. ADD_DEPENDENCIES(main ext-yaml-cpp)
  202. message("DEPS:" $DEPS)
  203. target_link_libraries(main ${DEPS})
  204. if (WIN32 AND WITH_MKL)
  205. add_custom_command(TARGET main POST_BUILD
  206. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mklml/lib/mklml.dll ./mklml.dll
  207. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mklml/lib/libiomp5md.dll ./libiomp5md.dll
  208. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mkldnn/lib/mkldnn.dll ./mkldnn.dll
  209. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mklml/lib/mklml.dll ./release/mklml.dll
  210. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mklml/lib/libiomp5md.dll ./release/libiomp5md.dll
  211. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mkldnn/lib/mkldnn.dll ./release/mkldnn.dll
  212. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/paddle/lib/${PADDLE_LIB_NAME}.dll ./release/${PADDLE_LIB_NAME}.dll
  213. )
  214. endif()
  215. if (WIN32 AND NOT WITH_MKL)
  216. add_custom_command(TARGET main POST_BUILD
  217. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/openblas/lib/openblas.dll ./openblas.dll
  218. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/openblas/lib/openblas.dll ./release/openblas.dll
  219. )
  220. endif()
  221. if (WIN32)
  222. add_custom_command(TARGET main POST_BUILD
  223. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/onnxruntime/lib/onnxruntime.dll ./onnxruntime.dll
  224. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/paddle2onnx/lib/paddle2onnx.dll ./paddle2onnx.dll
  225. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/onnxruntime/lib/onnxruntime.dll ./release/onnxruntime.dll
  226. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/paddle2onnx/lib/paddle2onnx.dll ./release/paddle2onnx.dll
  227. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/paddle/lib/${PADDLE_LIB_NAME}.dll ./release/${PADDLE_LIB_NAME}.dll
  228. )
  229. endif()