CMakeLists.txt 11 KB

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