CMakeLists.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. if(WINRT)
  2. ocv_module_disable(dnn)
  3. endif()
  4. set(the_description "Deep neural network module. It allows to load models from different frameworks and to make forward pass")
  5. ocv_add_dispatched_file_force_all("layers/layers_common" AVX AVX2 AVX512_SKX RVV LASX)
  6. ocv_add_dispatched_file_force_all("int8layers/layers_common" AVX2 AVX512_SKX LASX)
  7. ocv_add_dispatched_file_force_all("layers/cpu_kernels/conv_block" AVX AVX2)
  8. ocv_add_dispatched_file_force_all("layers/cpu_kernels/conv_depthwise" AVX AVX2 RVV LASX)
  9. ocv_add_dispatched_file_force_all("layers/cpu_kernels/conv_winograd_f63" AVX AVX2)
  10. ocv_add_module(dnn opencv_core opencv_imgproc WRAP python java objc js)
  11. include(${CMAKE_CURRENT_LIST_DIR}/cmake/plugin.cmake)
  12. ocv_option(OPENCV_DNN_OPENCL "Build with OpenCL support" HAVE_OPENCL AND NOT APPLE)
  13. if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
  14. ocv_target_compile_definitions(${the_module} PRIVATE "CV_OCL4DNN=1")
  15. endif()
  16. if(WITH_WEBNN AND HAVE_WEBNN)
  17. ocv_target_compile_definitions(${the_module} PRIVATE "HAVE_WEBNN=1")
  18. endif()
  19. if(HAVE_TIMVX)
  20. ocv_target_compile_definitions(${the_module} PRIVATE "HAVE_TIMVX=1")
  21. endif()
  22. if(HAVE_CANN)
  23. ocv_target_compile_definitions(${the_module} PRIVATE "HAVE_CANN=1")
  24. endif()
  25. ocv_option(OPENCV_DNN_CUDA "Build with CUDA support"
  26. HAVE_CUDA
  27. AND HAVE_CUBLAS
  28. AND HAVE_CUDNN
  29. )
  30. if(OPENCV_DNN_CUDA)
  31. if(HAVE_CUDA AND HAVE_CUBLAS AND HAVE_CUDNN)
  32. ocv_target_compile_definitions(${the_module} PRIVATE "CV_CUDA4DNN=1")
  33. else()
  34. if(NOT HAVE_CUDA)
  35. message(SEND_ERROR "DNN: CUDA backend requires CUDA Toolkit. Please resolve dependency or disable OPENCV_DNN_CUDA=OFF")
  36. elseif(NOT HAVE_CUBLAS)
  37. message(SEND_ERROR "DNN: CUDA backend requires cuBLAS. Please resolve dependency or disable OPENCV_DNN_CUDA=OFF")
  38. elseif(NOT HAVE_CUDNN)
  39. message(SEND_ERROR "DNN: CUDA backend requires cuDNN. Please resolve dependency or disable OPENCV_DNN_CUDA=OFF")
  40. endif()
  41. endif()
  42. endif()
  43. ocv_cmake_hook_append(INIT_MODULE_SOURCES_opencv_dnn "${CMAKE_CURRENT_LIST_DIR}/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake")
  44. if(HAVE_TENGINE)
  45. ocv_target_compile_definitions(${the_module} PRIVATE "HAVE_TENGINE=1")
  46. endif()
  47. if(MSVC)
  48. add_definitions( -D_CRT_SECURE_NO_WARNINGS=1 )
  49. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146
  50. /wd4305 /wd4127 /wd4100 /wd4512 /wd4125 /wd4389 /wd4510 /wd4610
  51. /wd4702 /wd4456 /wd4457 /wd4065 /wd4310 /wd4661 /wd4506
  52. )
  53. if(MSVC_VERSION LESS 1920) # MSVS 2015/2017, .pb.cc generated files
  54. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4309) # 'static_cast': truncation of constant value
  55. endif()
  56. if(MSVC_VERSION LESS 1920) # <MSVS2019, .pb.cc generated files
  57. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4189) # local variable is initialized but not referenced
  58. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4592) # symbol will be dynamically initialized (implementation limitation)
  59. endif()
  60. else()
  61. ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-deprecated -Wmissing-prototypes -Wmissing-declarations -Wshadow
  62. -Wunused-parameter -Wsign-compare
  63. )
  64. endif()
  65. if(HAVE_CUDA)
  66. ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef)
  67. endif()
  68. if(NOT HAVE_CXX11)
  69. ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-undef) # LANG_CXX11 from protobuf files
  70. endif()
  71. if(APPLE_FRAMEWORK)
  72. ocv_warnings_disable(CMAKE_CXX_FLAGS -Wshorten-64-to-32)
  73. endif()
  74. if(ANDROID)
  75. add_definitions(-DDISABLE_POSIX_MEMALIGN -DTH_DISABLE_HEAP_TRACKING)
  76. endif()
  77. if(NOT BUILD_PROTOBUF)
  78. ocv_target_compile_definitions(${the_module} PRIVATE "OPENCV_DNN_EXTERNAL_PROTOBUF=1")
  79. endif()
  80. #suppress warnings in autogenerated caffe.pb.* files
  81. ocv_warnings_disable(CMAKE_CXX_FLAGS
  82. /wd4125 /wd4267 /wd4127 /wd4244 /wd4512 /wd4702
  83. /wd4456 /wd4510 /wd4610 /wd4800
  84. /wd4701 /wd4703 # potentially uninitialized local/pointer variable 'value' used
  85. /wd4505 # unreferenced local function has been removed
  86. /wd4458 # declaration of 'x' hides class member. GCC still works, MSVC bug is here: https://developercommunity.visualstudio.com/content/problem/219311/c-c4458-declaration-hides-class-member-warning-iss.html
  87. -wd858 -wd2196
  88. -Winvalid-offsetof # Apple Clang (attr_value.pb.cc)
  89. )
  90. set(include_dirs "")
  91. set(libs "")
  92. if(HAVE_PROTOBUF)
  93. ocv_target_compile_definitions(${the_module} PRIVATE "HAVE_PROTOBUF=1")
  94. if(PROTOBUF_UPDATE_FILES)
  95. file(GLOB proto_files "${CMAKE_CURRENT_LIST_DIR}/src/tensorflow/*.proto" "${CMAKE_CURRENT_LIST_DIR}/src/caffe/opencv-caffe.proto" "${CMAKE_CURRENT_LIST_DIR}/src/onnx/opencv-onnx.proto")
  96. set(PROTOBUF_GENERATE_CPP_APPEND_PATH ON) # required for tensorflow
  97. protobuf_generate_cpp(fw_srcs fw_hdrs ${proto_files})
  98. else()
  99. file(GLOB fw_srcs "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow/*.cc" "${CMAKE_CURRENT_LIST_DIR}/misc/caffe/opencv-caffe.pb.cc" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx/opencv-onnx.pb.cc")
  100. file(GLOB fw_hdrs "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow/*.h" "${CMAKE_CURRENT_LIST_DIR}/misc/caffe/opencv-caffe.pb.h" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx/opencv-onnx.pb.h")
  101. set(fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/caffe" "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx")
  102. endif()
  103. endif()
  104. ocv_option(OPENCV_DNN_TFLITE "Build with TFLite support" (TARGET ocv.3rdparty.flatbuffers))
  105. if(TARGET ocv.3rdparty.flatbuffers AND OPENCV_DNN_TFLITE)
  106. if(NOT HAVE_FLATBUFFERS)
  107. message(FATAL_ERROR "DNN: TFLite is not supported without enabled 'flatbuffers'. Check build configuration.")
  108. endif()
  109. list(APPEND libs ocv.3rdparty.flatbuffers)
  110. list(APPEND fw_hdrs "${CMAKE_CURRENT_LIST_DIR}/misc/tflite/schema_generated.h")
  111. list(APPEND fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/tflite")
  112. # Schema is generated by this command:
  113. #add_custom_command(
  114. # OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/schema_generated.h"
  115. # COMMAND flatbuffers::flatc --cpp -o "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_LIST_DIR}/src/tflite/schema.fbs")
  116. endif()
  117. list(APPEND include_dirs ${fw_inc})
  118. list(APPEND libs ${Protobuf_LIBRARIES})
  119. if(NOT BUILD_PROTOBUF)
  120. list(APPEND include_dirs ${Protobuf_INCLUDE_DIRS})
  121. endif()
  122. set(sources_options "")
  123. list(APPEND libs ${LAPACK_LIBRARIES})
  124. if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
  125. list(APPEND include_dirs ${OPENCL_INCLUDE_DIRS})
  126. else()
  127. set(sources_options EXCLUDE_OPENCL)
  128. endif()
  129. if(OPENCV_DNN_CUDA AND HAVE_CUDA AND HAVE_CUBLAS AND HAVE_CUDNN)
  130. list(APPEND include_dirs ${CUDA_TOOLKIT_INCLUDE} ${CUDNN_INCLUDE_DIRS})
  131. set(CC_LIST ${CUDA_ARCH_BIN})
  132. separate_arguments(CC_LIST)
  133. foreach(cc ${CC_LIST})
  134. if(cc VERSION_LESS 3.0)
  135. message(FATAL_ERROR "CUDA backend for DNN module requires CC 3.0 or higher. Please remove unsupported architectures from CUDA_ARCH_BIN option or disable OPENCV_DNN_CUDA=OFF.")
  136. endif()
  137. endforeach()
  138. unset(CC_LIST)
  139. else()
  140. set(sources_options ${sources_options} EXCLUDE_CUDA)
  141. endif()
  142. if(HAVE_TENGINE)
  143. list(APPEND include_dirs ${TENGINE_INCLUDE_DIRS})
  144. list(APPEND libs -Wl,--whole-archive ${TENGINE_LIBRARIES} -Wl,--no-whole-archive)
  145. endif()
  146. if(HAVE_TIMVX)
  147. list(APPEND include_dirs ${TIMVX_INCLUDE_DIR})
  148. list(APPEND libs -Wl,--whole-archive ${TIMVX_LIBRARY} -Wl,--no-whole-archive)
  149. endif()
  150. if(HAVE_CANN)
  151. list(APPEND include_dirs ${CANN_INCLUDE_DIRS})
  152. list(APPEND libs -Wl,--whole-archive ${CANN_LIBRARIES} -Wl,--no-whole-archive)
  153. endif()
  154. set(webnn_srcs "")
  155. if(NOT EMSCRIPTEN)
  156. if(HAVE_WEBNN)
  157. list(APPEND include_dirs ${WEBNN_HEADER_DIRS})
  158. list(APPEND include_dirs ${WEBNN_INCLUDE_DIRS})
  159. list(APPEND libs -Wl,--whole-archive ${WEBNN_LIBRARIES} -Wl,--no-whole-archive)
  160. list(APPEND webnn_srcs $ENV{WEBNN_NATIVE_DIR}/gen/src/webnn/webnn_cpp.cpp)
  161. endif()
  162. endif()
  163. ocv_module_include_directories(${include_dirs})
  164. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  165. ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-suggest-override") # GCC
  166. ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-array-bounds") # GCC 9.3.0 (Ubuntu 20.04)
  167. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  168. ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-inconsistent-missing-override") # Clang
  169. endif()
  170. set(dnn_runtime_libs "")
  171. file(GLOB_RECURSE dnn_srcs
  172. "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
  173. )
  174. file(GLOB_RECURSE dnn_int_hdrs
  175. "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp"
  176. "${CMAKE_CURRENT_LIST_DIR}/src/*.h"
  177. )
  178. set(dnn_plugin_srcs ${dnn_srcs} ${dnn_int_hdrs})
  179. ocv_list_filterout_ex(dnn_plugin_srcs
  180. "/src/dnn.cpp$|/src/dnn_utils.cpp$|/src/dnn_utils.cpp$|/src/dnn_read.cpp$|/src/registry.cpp$|/src/backend.cpp$"
  181. # importers
  182. "/src/(caffe|darknet|onnx|tensorflow|torch)/"
  183. # executors
  184. "/src/(cuda|cuda4dnn|ocl4dnn|vkcom|webnn)/"
  185. )
  186. ocv_option(OPENCV_DNN_OPENVINO "Build with OpenVINO support (2021.4+)" (TARGET ocv.3rdparty.openvino))
  187. if(TARGET ocv.3rdparty.openvino AND OPENCV_DNN_OPENVINO)
  188. if(NOT HAVE_OPENVINO AND NOT HAVE_NGRAPH)
  189. message(FATAL_ERROR "DNN: Inference Engine is not supported without enabled 'nGraph'. Check build configuration.")
  190. endif()
  191. if("openvino" IN_LIST DNN_PLUGIN_LIST OR DNN_PLUGIN_LIST STREQUAL "all")
  192. # plugin doesn't support PCH, separate directory scope is necessary
  193. # opencv_world requires absolute path
  194. add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/misc/plugin/openvino" "${CMAKE_CURRENT_BINARY_DIR}/dnn_plugin_openvino")
  195. elseif(NOT OPENCV_DNN_BUILTIN_BACKEND)
  196. list(APPEND dnn_runtime_libs ocv.3rdparty.openvino)
  197. endif()
  198. endif()
  199. ocv_install_used_external_targets(${libs} ${dnn_runtime_libs})
  200. ocv_glob_module_sources(${sources_options} SOURCES ${fw_srcs} ${webnn_srcs})
  201. ocv_create_module(${libs} ${dnn_runtime_libs})
  202. ocv_add_samples()
  203. ocv_add_accuracy_tests(${dnn_runtime_libs})
  204. set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
  205. file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
  206. file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
  207. ocv_add_perf_tests(${dnn_runtime_libs}
  208. FILES test_common "${CMAKE_CURRENT_LIST_DIR}/test/test_common.hpp" "${CMAKE_CURRENT_LIST_DIR}/test/test_common.impl.hpp"
  209. FILES Src ${perf_srcs}
  210. FILES Include ${perf_hdrs}
  211. )
  212. ocv_option(OPENCV_DNN_PERF_CAFFE "Add performance tests of Caffe framework" OFF)
  213. ocv_option(OPENCV_DNN_PERF_CLCAFFE "Add performance tests of clCaffe framework" OFF)
  214. if(BUILD_PERF_TESTS)
  215. if (OPENCV_DNN_PERF_CAFFE
  216. OR ${the_module}_PERF_CAFFE # compatibility for deprecated option
  217. )
  218. find_package(Caffe QUIET)
  219. if (Caffe_FOUND)
  220. ocv_target_compile_definitions(opencv_perf_dnn PRIVATE "HAVE_CAFFE=1")
  221. ocv_target_link_libraries(opencv_perf_dnn caffe)
  222. endif()
  223. elseif(OPENCV_DNN_PERF_CLCAFFE
  224. OR ${the_module}_PERF_CAFFE # compatibility for deprecated option
  225. )
  226. find_package(Caffe QUIET)
  227. if (Caffe_FOUND)
  228. ocv_target_compile_definitions(opencv_perf_dnn PRIVATE "HAVE_CLCAFFE=1")
  229. ocv_target_link_libraries(opencv_perf_dnn caffe)
  230. endif()
  231. endif()
  232. endif()
  233. if(DNN_ENABLE_PLUGINS)
  234. ocv_target_compile_definitions(${the_module} PRIVATE ENABLE_PLUGINS)
  235. if(TARGET opencv_test_dnn)
  236. ocv_target_compile_definitions(opencv_test_dnn PRIVATE ENABLE_PLUGINS)
  237. endif()
  238. if(OPENCV_DEBUG_POSTFIX)
  239. ocv_append_source_file_compile_definitions("${CMAKE_CURRENT_LIST_DIR}/src/backend.cpp" "DEBUG_POSTFIX=${OPENCV_DEBUG_POSTFIX}")
  240. endif()
  241. endif()
  242. ocv_option(OPENCV_TEST_DNN_OPENVINO "Build test with OpenVINO code" (TARGET ocv.3rdparty.openvino))
  243. if(TARGET ocv.3rdparty.openvino AND OPENCV_TEST_DNN_OPENVINO)
  244. if(TARGET opencv_test_dnn)
  245. ocv_target_link_libraries(opencv_test_dnn ocv.3rdparty.openvino)
  246. endif()
  247. endif()
  248. ocv_option(OPENCV_TEST_DNN_CANN "Build test with CANN" (TARGET ocv.3rdparty.cann))
  249. if(TARGET ocv.3rdparty.cann AND OPENCV_TEST_DNN_CANN)
  250. if(TARGET opencv_test_dnn)
  251. ocv_target_link_libraries(opencv_test_dnn ocv.3rdparty.cann)
  252. endif()
  253. endif()
  254. ocv_option(OPENCV_TEST_DNN_TFLITE "Build test with TFLite" (OPENCV_DNN_TFLITE))
  255. if(OPENCV_TEST_DNN_TFLITE)
  256. if(TARGET opencv_test_dnn)
  257. ocv_target_compile_definitions(opencv_test_dnn PRIVATE "OPENCV_TEST_DNN_TFLITE=1")
  258. endif()
  259. if(TARGET opencv_perf_dnn)
  260. ocv_target_compile_definitions(opencv_perf_dnn PRIVATE "OPENCV_TEST_DNN_TFLITE=1")
  261. endif()
  262. endif()