CMakeLists.txt 11 KB

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