CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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}/paddle/lib/")
  70. link_directories("${CMAKE_CURRENT_BINARY_DIR}")
  71. if (WIN32)
  72. include_directories("${PADDLE_DIR}/paddle/fluid/inference")
  73. include_directories("${PADDLE_DIR}/paddle/include")
  74. link_directories("${PADDLE_DIR}/paddle/fluid/inference")
  75. find_package(OpenCV REQUIRED PATHS ${OPENCV_DIR}/build/ NO_DEFAULT_PATH)
  76. else ()
  77. find_package(OpenCV REQUIRED PATHS ${OPENCV_DIR}/share/OpenCV NO_DEFAULT_PATH)
  78. include_directories("${PADDLE_DIR}/paddle/include")
  79. link_directories("${PADDLE_DIR}/paddle/lib")
  80. endif ()
  81. include_directories(${OpenCV_INCLUDE_DIRS})
  82. if (WIN32)
  83. add_definitions("/DGOOGLE_GLOG_DLL_DECL=")
  84. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /bigobj /MTd")
  85. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /bigobj /MT")
  86. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj /MTd")
  87. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /bigobj /MT")
  88. else()
  89. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -o2 -fopenmp -std=c++11")
  90. set(CMAKE_STATIC_LIBRARY_PREFIX "")
  91. endif()
  92. # TODO let users define cuda lib path
  93. if (WITH_GPU)
  94. if (NOT DEFINED CUDA_LIB OR ${CUDA_LIB} STREQUAL "")
  95. message(FATAL_ERROR "please set CUDA_LIB with -DCUDA_LIB=/path/cuda-8.0/lib64")
  96. endif()
  97. if (NOT WIN32)
  98. if (NOT DEFINED CUDNN_LIB)
  99. message(FATAL_ERROR "please set CUDNN_LIB with -DCUDNN_LIB=/path/cudnn_v7.4/cuda/lib64")
  100. endif()
  101. endif(NOT WIN32)
  102. endif()
  103. if (NOT WIN32)
  104. if (WITH_TENSORRT AND WITH_GPU)
  105. include_directories("${TENSORRT_INC_DIR}/")
  106. link_directories("${TENSORRT_LIB_DIR}/")
  107. endif()
  108. endif(NOT WIN32)
  109. if (NOT WIN32)
  110. set(NGRAPH_PATH "${PADDLE_DIR}/third_party/install/ngraph")
  111. if(EXISTS ${NGRAPH_PATH})
  112. include(GNUInstallDirs)
  113. include_directories("${NGRAPH_PATH}/include")
  114. link_directories("${NGRAPH_PATH}/${CMAKE_INSTALL_LIBDIR}")
  115. set(NGRAPH_LIB ${NGRAPH_PATH}/${CMAKE_INSTALL_LIBDIR}/libngraph${CMAKE_SHARED_LIBRARY_SUFFIX})
  116. endif()
  117. endif()
  118. if(WITH_MKL)
  119. include_directories("${PADDLE_DIR}/third_party/install/mklml/include")
  120. if (WIN32)
  121. set(MATH_LIB ${PADDLE_DIR}/third_party/install/mklml/lib/mklml.lib
  122. ${PADDLE_DIR}/third_party/install/mklml/lib/libiomp5md.lib)
  123. else ()
  124. set(MATH_LIB ${PADDLE_DIR}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX}
  125. ${PADDLE_DIR}/third_party/install/mklml/lib/libiomp5${CMAKE_SHARED_LIBRARY_SUFFIX})
  126. execute_process(COMMAND cp -r ${PADDLE_DIR}/third_party/install/mklml/lib/libmklml_intel${CMAKE_SHARED_LIBRARY_SUFFIX} /usr/lib)
  127. endif ()
  128. set(MKLDNN_PATH "${PADDLE_DIR}/third_party/install/mkldnn")
  129. if(EXISTS ${MKLDNN_PATH})
  130. include_directories("${MKLDNN_PATH}/include")
  131. if (WIN32)
  132. set(MKLDNN_LIB ${MKLDNN_PATH}/lib/mkldnn.lib)
  133. else ()
  134. set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libmkldnn.so.0)
  135. endif ()
  136. endif()
  137. else()
  138. set(MATH_LIB ${PADDLE_DIR}/third_party/install/openblas/lib/libopenblas${CMAKE_STATIC_LIBRARY_SUFFIX})
  139. endif()
  140. if (WIN32)
  141. if(EXISTS "${PADDLE_DIR}/paddle/fluid/inference/${PADDLE_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}")
  142. set(DEPS
  143. ${PADDLE_DIR}/paddle/fluid/inference/${PADDLE_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
  144. else()
  145. set(DEPS
  146. ${PADDLE_DIR}/paddle/lib/${PADDLE_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
  147. endif()
  148. endif()
  149. if (WIN32)
  150. set(DEPS ${PADDLE_DIR}/paddle/lib/${PADDLE_LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
  151. else()
  152. set(DEPS ${PADDLE_DIR}/paddle/lib/${PADDLE_LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
  153. endif()
  154. message("PADDLE_LIB_NAME:" ${PADDLE_LIB_NAME})
  155. message("DEPS:" $DEPS)
  156. if (NOT WIN32)
  157. set(DEPS ${DEPS}
  158. ${MATH_LIB} ${MKLDNN_LIB}
  159. glog gflags protobuf z xxhash yaml-cpp
  160. )
  161. if(EXISTS "${PADDLE_DIR}/third_party/install/snappystream/lib")
  162. set(DEPS ${DEPS} snappystream)
  163. endif()
  164. if (EXISTS "${PADDLE_DIR}/third_party/install/snappy/lib")
  165. set(DEPS ${DEPS} snappy)
  166. endif()
  167. else()
  168. set(DEPS ${DEPS}
  169. ${MATH_LIB} ${MKLDNN_LIB}
  170. glog gflags_static libprotobuf xxhash libyaml-cppmt)
  171. set(DEPS ${DEPS} libcmt shlwapi)
  172. if (EXISTS "${PADDLE_DIR}/third_party/install/snappy/lib")
  173. set(DEPS ${DEPS} snappy)
  174. endif()
  175. if(EXISTS "${PADDLE_DIR}/third_party/install/snappystream/lib")
  176. set(DEPS ${DEPS} snappystream)
  177. endif()
  178. endif(NOT WIN32)
  179. if(WITH_GPU)
  180. if(NOT WIN32)
  181. if (WITH_TENSORRT)
  182. set(DEPS ${DEPS} ${TENSORRT_LIB_DIR}/libnvinfer${CMAKE_SHARED_LIBRARY_SUFFIX})
  183. set(DEPS ${DEPS} ${TENSORRT_LIB_DIR}/libnvinfer_plugin${CMAKE_SHARED_LIBRARY_SUFFIX})
  184. endif()
  185. set(DEPS ${DEPS} ${CUDA_LIB}/libcudart${CMAKE_SHARED_LIBRARY_SUFFIX})
  186. set(DEPS ${DEPS} ${CUDNN_LIB}/libcudnn${CMAKE_SHARED_LIBRARY_SUFFIX})
  187. else()
  188. set(DEPS ${DEPS} ${CUDA_LIB}/cudart${CMAKE_STATIC_LIBRARY_SUFFIX} )
  189. set(DEPS ${DEPS} ${CUDA_LIB}/cublas${CMAKE_STATIC_LIBRARY_SUFFIX} )
  190. set(DEPS ${DEPS} ${CUDNN_LIB}/cudnn${CMAKE_STATIC_LIBRARY_SUFFIX})
  191. endif()
  192. endif()
  193. if (NOT WIN32)
  194. set(EXTERNAL_LIB "-ldl -lrt -lgomp -lz -lm -lpthread")
  195. set(DEPS ${DEPS} ${EXTERNAL_LIB})
  196. endif()
  197. set(DEPS ${DEPS} ${OpenCV_LIBS})
  198. add_executable(main ${SRCS})
  199. ADD_DEPENDENCIES(main ext-yaml-cpp)
  200. message("DEPS:" $DEPS)
  201. target_link_libraries(main ${DEPS})
  202. if (WIN32 AND WITH_MKL)
  203. add_custom_command(TARGET main POST_BUILD
  204. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mklml/lib/mklml.dll ./mklml.dll
  205. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mklml/lib/libiomp5md.dll ./libiomp5md.dll
  206. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mkldnn/lib/mkldnn.dll ./mkldnn.dll
  207. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mklml/lib/mklml.dll ./release/mklml.dll
  208. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mklml/lib/libiomp5md.dll ./release/libiomp5md.dll
  209. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/third_party/install/mkldnn/lib/mkldnn.dll ./release/mkldnn.dll
  210. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/paddle/lib/${PADDLE_LIB_NAME}.dll ./release/${PADDLE_LIB_NAME}.dll
  211. )
  212. endif()
  213. if (WIN32)
  214. add_custom_command(TARGET main POST_BUILD
  215. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PADDLE_DIR}/paddle/lib/${PADDLE_LIB_NAME}.dll ./release/${PADDLE_LIB_NAME}.dll
  216. )
  217. endif()