Skip to content

Commit 514f419

Browse files
committed
Merge pull request opencv#10959 from alalek:cmake_ocl4dnn
2 parents 49dd030 + a9ebc61 commit 514f419

32 files changed

+124
-80
lines changed

cmake/OpenCVModule.cmake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,14 +747,18 @@ endmacro()
747747

748748
# finds and sets headers and sources for the standard OpenCV module
749749
# Usage:
750-
# ocv_glob_module_sources([EXCLUDE_CUDA] <extra sources&headers in the same format as used in ocv_set_module_sources>)
750+
# ocv_glob_module_sources([EXCLUDE_CUDA] [EXCLUDE_OPENCL] <extra sources&headers in the same format as used in ocv_set_module_sources>)
751751
macro(ocv_glob_module_sources)
752752
ocv_debug_message("ocv_glob_module_sources(" ${ARGN} ")")
753753
set(_argn ${ARGN})
754754
list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
755755
if(NOT exclude_cuda EQUAL -1)
756756
list(REMOVE_AT _argn ${exclude_cuda})
757757
endif()
758+
list(FIND _argn "EXCLUDE_OPENCL" exclude_opencl)
759+
if(NOT exclude_opencl EQUAL -1)
760+
list(REMOVE_AT _argn ${exclude_opencl})
761+
endif()
758762

759763
file(GLOB_RECURSE lib_srcs
760764
"${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
@@ -801,7 +805,7 @@ macro(ocv_glob_module_sources)
801805
file(GLOB cl_kernels
802806
"${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
803807
)
804-
if(cl_kernels)
808+
if(cl_kernels AND exclude_opencl EQUAL -1)
805809
set(OCL_NAME opencl_kernels_${name})
806810
add_custom_command(
807811
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" # don't add .hpp file here to optimize build process

modules/dnn/CMakeLists.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ set(the_description "Deep neural network module. It allows to load models from d
1111
ocv_add_dispatched_file_force_all("layers/layers_common" AVX AVX2 AVX512_SKX)
1212

1313
ocv_add_module(dnn opencv_core opencv_imgproc WRAP python matlab java js)
14+
15+
ocv_option(OPENCV_DNN_OPENCL "Build with OpenCL support" HAVE_OPENCL)
16+
if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
17+
add_definitions(-DCV_OCL4DNN=1)
18+
else()
19+
ocv_cmake_hook_append(INIT_MODULE_SOURCES_opencv_dnn "${CMAKE_CURRENT_LIST_DIR}/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake")
20+
endif()
21+
1422
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-shadow -Wno-parentheses -Wmaybe-uninitialized -Wsign-promo
1523
-Wmissing-declarations -Wmissing-prototypes
1624
)
@@ -63,8 +71,15 @@ else()
6371
set(fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/caffe" "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow")
6472
endif()
6573

66-
ocv_module_include_directories(${fw_inc} ${CMAKE_CURRENT_LIST_DIR}/src/ocl4dnn/include ${OPENCL_INCLUDE_DIRS})
67-
ocv_glob_module_sources(SOURCES ${fw_srcs})
74+
set(include_dirs ${fw_inc})
75+
set(sources_options "")
76+
if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
77+
list(APPEND include_dirs ${OPENCL_INCLUDE_DIRS})
78+
else()
79+
set(sources_options EXCLUDE_OPENCL)
80+
endif()
81+
ocv_module_include_directories(${include_dirs})
82+
ocv_glob_module_sources(${sources_options} SOURCES ${fw_srcs})
6883
ocv_create_module(libprotobuf ${LAPACK_LIBRARIES})
6984
ocv_add_samples()
7085
ocv_add_accuracy_tests()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
message(STATUS "opencv_dnn: filter out ocl4dnn source code")
2+
ocv_list_filterout(OPENCV_MODULE_${the_module}_SOURCES "/ocl4dnn/")
3+
ocv_list_filterout(OPENCV_MODULE_${the_module}_HEADERS "/ocl4dnn/")

modules/dnn/src/dnn.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <opencv2/imgproc.hpp>
5454

5555
#include <opencv2/core/utils/configuration.private.hpp>
56+
#include <opencv2/core/utils/logger.hpp>
5657

5758
namespace cv {
5859
namespace dnn {
@@ -846,6 +847,13 @@ struct Net::Impl
846847

847848
if (!netWasAllocated || this->blobsToKeep != blobsToKeep_)
848849
{
850+
#ifndef HAVE_OPENCL
851+
if (preferableBackend == DNN_BACKEND_DEFAULT && preferableTarget == DNN_TARGET_OPENCL)
852+
{
853+
CV_LOG_WARNING(NULL, "DNN: OpenCL target is not available in this OpenCV build, switching to CPU.")
854+
preferableTarget = DNN_TARGET_CPU;
855+
}
856+
#endif
849857
clear();
850858

851859
allocateLayers(blobsToKeep_);

modules/dnn/src/layers/batch_norm_layer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Implementation of Batch Normalization layer.
1313
#include "op_halide.hpp"
1414
#include "op_inf_engine.hpp"
1515
#include <opencv2/dnn/shape_utils.hpp>
16+
17+
#ifdef HAVE_OPENCL
1618
#include "opencl_kernels_dnn.hpp"
19+
#endif
1720

1821
namespace cv
1922
{

modules/dnn/src/layers/concat_layer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
#include "layers_common.hpp"
4545
#include "op_halide.hpp"
4646
#include "op_inf_engine.hpp"
47+
48+
#ifdef HAVE_OPENCL
4749
#include "opencl_kernels_dnn.hpp"
50+
#endif
4851

4952
namespace cv
5053
{

modules/dnn/src/layers/convolution_layer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
#include "opencv2/core/hal/hal.hpp"
4848
#include "opencv2/core/hal/intrin.hpp"
4949
#include <iostream>
50-
#include "opencl_kernels_dnn.hpp"
5150

5251
#ifdef HAVE_OPENCL
52+
#include "opencl_kernels_dnn.hpp"
5353
using namespace cv::dnn::ocl4dnn;
5454
#endif
5555

modules/dnn/src/layers/detection_output_layer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646
#include <float.h>
4747
#include <string>
4848
#include "../nms.inl.hpp"
49+
50+
#ifdef HAVE_OPENCL
4951
#include "opencl_kernels_dnn.hpp"
52+
#endif
5053

5154
namespace cv
5255
{

modules/dnn/src/layers/elementwise_layers.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@
4646
#include "op_inf_engine.hpp"
4747
#include "opencv2/imgproc.hpp"
4848
#include <opencv2/dnn/shape_utils.hpp>
49-
#include "opencl_kernels_dnn.hpp"
5049
#include <iostream>
5150

51+
#ifdef HAVE_OPENCL
52+
#include "opencl_kernels_dnn.hpp"
53+
#endif
54+
5255
namespace cv
5356
{
5457
namespace dnn

modules/dnn/src/layers/eltwise_layer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
#include "layers_common.hpp"
4545
#include "op_halide.hpp"
4646
#include "op_inf_engine.hpp"
47+
48+
#ifdef HAVE_OPENCL
4749
#include "opencl_kernels_dnn.hpp"
50+
#endif
4851

4952
namespace cv
5053
{

0 commit comments

Comments
 (0)