From 5e4150826fea6f37276f348c65d94ce4847d1211 Mon Sep 17 00:00:00 2001 From: Kumataro Date: Sat, 7 Oct 2023 10:11:25 +0900 Subject: [PATCH] 3rdparty: supporting protobuf v22 and later Upstream-Status: Submitted [https://github.com/opencv/opencv/pull/24372] --- cmake/OpenCVFindProtobuf.cmake | 37 ++++++++++++++++++++++++++++++++++ modules/dnn/CMakeLists.txt | 9 +++++++++ 2 files changed, 46 insertions(+) diff --git a/cmake/OpenCVFindProtobuf.cmake b/cmake/OpenCVFindProtobuf.cmake index 8835347d1d..9bd5c28db8 100644 --- a/cmake/OpenCVFindProtobuf.cmake +++ b/cmake/OpenCVFindProtobuf.cmake @@ -67,6 +67,38 @@ else() endif() endif() +# See https://github.com/opencv/opencv/issues/24369 +# In Protocol Buffers v22.0 and later drops C++11 support and depends abseil-cpp. +# Details: https://protobuf.dev/news/2022-08-03/ +# And if std::text_view is in abseil-cpp requests C++17 and later. +if(HAVE_PROTOBUF) + if("${Protobuf_VERSION}" MATCHES [[[0-9]+.([0-9]+).[0-9]+]]) + string(COMPARE GREATER_EQUAL "${CMAKE_MATCH_1}" "22" REQUEST_ABSL) + + if(REQUEST_ABSL) + string(COMPARE GREATER_EQUAL "${CMAKE_CXX_STANDARD}" "17" USED_AFTER_CXX17) + if(NOT USED_AFTER_CXX17) + message("CMAKE_CXX_STANDARD : ${CMAKE_CXX_STANDARD}") + message("protobuf : ${Protobuf_VERSION}") + message(FATAL_ERROR "protobuf(v22 and later) and abseil-cpp request CMAKE_CXX_STANDARD=17 and later.") + endif() + + ocv_check_modules(ABSL_STRINGS absl_strings) + if(NOT ABSL_STRINGS_FOUND) + message(FATAL_ERROR "protobuf(v22 and later) requests abseil-cpp(strings), but missing.") + endif() + + ocv_check_modules(ABSL_LOG absl_log) + if(NOT ABSL_LOG_FOUND) + message(FATAL_ERROR "protobuf(v22 and later) requests abseil-cpp(log), but missing.") + endif() + + endif() + else() + message(FATAL_ERROR "Protobuf version(${Protobuf_VERSION}) is unexpected to split.") + endif() +endif() + if(HAVE_PROTOBUF AND PROTOBUF_UPDATE_FILES AND NOT COMMAND PROTOBUF_GENERATE_CPP) message(FATAL_ERROR "Can't configure protobuf dependency (BUILD_PROTOBUF=${BUILD_PROTOBUF} PROTOBUF_UPDATE_FILES=${PROTOBUF_UPDATE_FILES})") endif() @@ -89,3 +121,8 @@ if(HAVE_PROTOBUF) BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})" ELSE "${__location} (${Protobuf_VERSION})") endif() + +if(HAVE_ABSL_STRINGS AND HAVE_ABSL_LOG) + list(APPEND CUSTOM_STATUS absl) + list(APPEND CUSTOM_STATUS_absl " abseil-cpp:" "YES (${ABSL_STRINGS_VERSION})" ) +endif() diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index 804b78ead2..d32007b37e 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -149,6 +149,15 @@ if(NOT BUILD_PROTOBUF) list(APPEND include_dirs ${Protobuf_INCLUDE_DIRS}) endif() +if(HAVE_ABSL_STRINGS) + list(APPEND libs ${ABSL_STRINGS_LIBRARIES}) + list(APPEND include_dirs ${ABSL_STRTRINGS_INCLUDE_DIRS}) +endif() +if(HAVE_ABSL_LOG) + list(APPEND libs ${ABSL_LOG_LIBRARIES}) + list(APPEND include_dirs ${ABSL_LOG_INCLUDE_DIRS}) +endif() + set(sources_options "") list(APPEND libs ${LAPACK_LIBRARIES}) From 06a7669521d205f647d3e718322ccd153cdbbb77 Mon Sep 17 00:00:00 2001 From: Kumataro Date: Sun, 8 Oct 2023 09:39:35 +0900 Subject: [PATCH] dnn: disable some tests for external protobuf --- modules/dnn/CMakeLists.txt | 5 +++++ modules/dnn/src/caffe/caffe_io.cpp | 24 ++++++++++++++++++++++-- modules/dnn/test/test_layers.cpp | 16 ++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index d32007b37e..face38465f 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -254,6 +254,12 @@ ocv_create_module(${libs} ${dnn_runtime_libs}) ocv_add_samples() ocv_add_accuracy_tests(${dnn_runtime_libs}) +if(NOT BUILD_PROTOBUF) + if(TARGET opencv_test_dnn) + ocv_target_compile_definitions(opencv_test_dnn PRIVATE "OPENCV_DNN_EXTERNAL_PROTOBUF=1") + endif() +endif() + set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf") file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp") file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h") diff --git a/modules/dnn/src/caffe/caffe_io.cpp b/modules/dnn/src/caffe/caffe_io.cpp index ebecf95eea..ebceca84cf 100644 --- a/modules/dnn/src/caffe/caffe_io.cpp +++ b/modules/dnn/src/caffe/caffe_io.cpp @@ -1130,7 +1130,17 @@ bool ReadProtoFromTextFile(const char* filename, Message* proto) { parser.AllowUnknownField(true); parser.SetRecursionLimit(1000); #endif - return parser.Parse(&input, proto); + const bool ret = parser.Parse(&input, proto); + +#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF + if(!ret) + { + LOG(ERROR) << "Some data requires patched protobuf (available in OpenCV source tree only)."; + CV_Error_(Error::StsError,("Some data requires patched protobuf (available in OpenCV source tree only).")); + } +#endif + + return ret; } bool ReadProtoFromBinaryFile(const char* filename, Message* proto) { @@ -1148,7 +1158,17 @@ bool ReadProtoFromTextBuffer(const char* data, size_t len, Message* proto) { parser.AllowUnknownField(true); parser.SetRecursionLimit(1000); #endif - return parser.Parse(&input, proto); + const bool ret = parser.Parse(&input, proto); + +#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF + if(!ret) + { + LOG(ERROR) << "Some data requires patched protobuf (available in OpenCV source tree only)."; + CV_Error_(Error::StsError,("Some data requires patched protobuf (available in OpenCV source tree only).")); + } +#endif + + return ret; } diff --git a/modules/dnn/test/test_layers.cpp b/modules/dnn/test/test_layers.cpp index 763d94b99c..a07d442d76 100644 --- a/modules/dnn/test/test_layers.cpp +++ b/modules/dnn/test/test_layers.cpp @@ -754,7 +754,11 @@ TEST_F(Layer_RNN_Test, get_set_test) EXPECT_EQ(shape(outputs[1]), shape(nT, nS, nH)); } +#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF TEST_P(Test_Caffe_layers, Accum) +#else +TEST_P(Test_Caffe_layers, DISABLED_Accum) // requires patched protobuf (available in OpenCV source tree only) +#endif { if (backend == DNN_BACKEND_OPENCV && target != DNN_TARGET_CPU) applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16); @@ -778,7 +782,11 @@ TEST_P(Test_Caffe_layers, ChannelNorm) testLayerUsingCaffeModels("channel_norm", false, false); } +#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF TEST_P(Test_Caffe_layers, DataAugmentation) +#else +TEST_P(Test_Caffe_layers, DISABLED_DataAugmentation) // requires patched protobuf (available in OpenCV source tree only) +#endif { if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16); @@ -787,7 +795,11 @@ TEST_P(Test_Caffe_layers, DataAugmentation) testLayerUsingCaffeModels("data_augmentation_8x6", true, false); } +#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF TEST_P(Test_Caffe_layers, Resample) +#else +TEST_P(Test_Caffe_layers, DISABLED_Resample) // requires patched protobuf (available in OpenCV source tree only) +#endif { if (backend != DNN_BACKEND_OPENCV) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); @@ -795,7 +807,11 @@ TEST_P(Test_Caffe_layers, Resample) testLayerUsingCaffeModels("nearest", false, false); } +#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF TEST_P(Test_Caffe_layers, Correlation) +#else +TEST_P(Test_Caffe_layers, DISABLED_Correlation) // requires patched protobuf (available in OpenCV source tree only) +#endif { if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, From 2c33798f41942aefc203183d673ce4846f32dba4 Mon Sep 17 00:00:00 2001 From: Kumataro Date: Sun, 8 Oct 2023 13:28:40 +0900 Subject: [PATCH] use GREATER instead of GREATER_EQUAL and remove new blank line at EOF --- cmake/OpenCVFindProtobuf.cmake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmake/OpenCVFindProtobuf.cmake b/cmake/OpenCVFindProtobuf.cmake index 9bd5c28db8..ba147526d6 100644 --- a/cmake/OpenCVFindProtobuf.cmake +++ b/cmake/OpenCVFindProtobuf.cmake @@ -71,12 +71,14 @@ endif() # In Protocol Buffers v22.0 and later drops C++11 support and depends abseil-cpp. # Details: https://protobuf.dev/news/2022-08-03/ # And if std::text_view is in abseil-cpp requests C++17 and later. + if(HAVE_PROTOBUF) if("${Protobuf_VERSION}" MATCHES [[[0-9]+.([0-9]+).[0-9]+]]) - string(COMPARE GREATER_EQUAL "${CMAKE_MATCH_1}" "22" REQUEST_ABSL) + string(COMPARE GREATER "${CMAKE_MATCH_1}" "21" REQUEST_ABSL) # >=22 if(REQUEST_ABSL) - string(COMPARE GREATER_EQUAL "${CMAKE_CXX_STANDARD}" "17" USED_AFTER_CXX17) + string(COMPARE GREATER "${CMAKE_CXX_STANDARD}" "16" USED_AFTER_CXX17) # >=17 + if(NOT USED_AFTER_CXX17) message("CMAKE_CXX_STANDARD : ${CMAKE_CXX_STANDARD}") message("protobuf : ${Protobuf_VERSION}") From cd709eefbeedd116bf3495b42280323b932791ff Mon Sep 17 00:00:00 2001 From: Kumataro Date: Mon, 9 Oct 2023 21:51:04 +0900 Subject: [PATCH] fix for review --- cmake/OpenCVFindProtobuf.cmake | 5 ---- modules/dnn/src/caffe/caffe_io.cpp | 24 ++---------------- modules/dnn/test/test_layers.cpp | 40 +++++++++++++++--------------- 3 files changed, 22 insertions(+), 47 deletions(-) diff --git a/cmake/OpenCVFindProtobuf.cmake b/cmake/OpenCVFindProtobuf.cmake index ba147526d6..2faf1010bf 100644 --- a/cmake/OpenCVFindProtobuf.cmake +++ b/cmake/OpenCVFindProtobuf.cmake @@ -123,8 +123,3 @@ if(HAVE_PROTOBUF) BUILD_PROTOBUF THEN "build (${Protobuf_VERSION})" ELSE "${__location} (${Protobuf_VERSION})") endif() - -if(HAVE_ABSL_STRINGS AND HAVE_ABSL_LOG) - list(APPEND CUSTOM_STATUS absl) - list(APPEND CUSTOM_STATUS_absl " abseil-cpp:" "YES (${ABSL_STRINGS_VERSION})" ) -endif() diff --git a/modules/dnn/src/caffe/caffe_io.cpp b/modules/dnn/src/caffe/caffe_io.cpp index ebceca84cf..ebecf95eea 100644 --- a/modules/dnn/src/caffe/caffe_io.cpp +++ b/modules/dnn/src/caffe/caffe_io.cpp @@ -1130,17 +1130,7 @@ bool ReadProtoFromTextFile(const char* filename, Message* proto) { parser.AllowUnknownField(true); parser.SetRecursionLimit(1000); #endif - const bool ret = parser.Parse(&input, proto); - -#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF - if(!ret) - { - LOG(ERROR) << "Some data requires patched protobuf (available in OpenCV source tree only)."; - CV_Error_(Error::StsError,("Some data requires patched protobuf (available in OpenCV source tree only).")); - } -#endif - - return ret; + return parser.Parse(&input, proto); } bool ReadProtoFromBinaryFile(const char* filename, Message* proto) { @@ -1158,17 +1148,7 @@ bool ReadProtoFromTextBuffer(const char* data, size_t len, Message* proto) { parser.AllowUnknownField(true); parser.SetRecursionLimit(1000); #endif - const bool ret = parser.Parse(&input, proto); - -#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF - if(!ret) - { - LOG(ERROR) << "Some data requires patched protobuf (available in OpenCV source tree only)."; - CV_Error_(Error::StsError,("Some data requires patched protobuf (available in OpenCV source tree only).")); - } -#endif - - return ret; + return parser.Parse(&input, proto); } diff --git a/modules/dnn/test/test_layers.cpp b/modules/dnn/test/test_layers.cpp index a07d442d76..5c6fc541d7 100644 --- a/modules/dnn/test/test_layers.cpp +++ b/modules/dnn/test/test_layers.cpp @@ -754,17 +754,17 @@ TEST_F(Layer_RNN_Test, get_set_test) EXPECT_EQ(shape(outputs[1]), shape(nT, nS, nH)); } -#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF TEST_P(Test_Caffe_layers, Accum) -#else -TEST_P(Test_Caffe_layers, DISABLED_Accum) // requires patched protobuf (available in OpenCV source tree only) -#endif { +#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF + throw SkipTestException("Requires patched protobuf"); +#else if (backend == DNN_BACKEND_OPENCV && target != DNN_TARGET_CPU) applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16); testLayerUsingCaffeModels("accum", false, false, 0.0, 0.0, 2); testLayerUsingCaffeModels("accum_ref", false, false, 0.0, 0.0, 2); +#endif } TEST_P(Test_Caffe_layers, FlowWarp) @@ -782,42 +782,42 @@ TEST_P(Test_Caffe_layers, ChannelNorm) testLayerUsingCaffeModels("channel_norm", false, false); } -#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF TEST_P(Test_Caffe_layers, DataAugmentation) -#else -TEST_P(Test_Caffe_layers, DISABLED_DataAugmentation) // requires patched protobuf (available in OpenCV source tree only) -#endif { +#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF + throw SkipTestException("Requires patched protobuf"); +#else if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16); testLayerUsingCaffeModels("data_augmentation", true, false); testLayerUsingCaffeModels("data_augmentation_2x1", true, false); testLayerUsingCaffeModels("data_augmentation_8x6", true, false); +#endif } -#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF TEST_P(Test_Caffe_layers, Resample) -#else -TEST_P(Test_Caffe_layers, DISABLED_Resample) // requires patched protobuf (available in OpenCV source tree only) -#endif { +#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF + throw SkipTestException("Requires patched protobuf"); +#else if (backend != DNN_BACKEND_OPENCV) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH); testLayerUsingCaffeModels("nearest_2inps", false, false, 0.0, 0.0, 2); testLayerUsingCaffeModels("nearest", false, false); +#endif } -#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF TEST_P(Test_Caffe_layers, Correlation) -#else -TEST_P(Test_Caffe_layers, DISABLED_Correlation) // requires patched protobuf (available in OpenCV source tree only) -#endif { +#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF + throw SkipTestException("Requires patched protobuf"); +#else if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16); testLayerUsingCaffeModels("correlation", false, false, 0.0, 0.0, 2); } +#endif TEST_P(Test_Caffe_layers, Convolution2Inputs) { @@ -1644,12 +1644,11 @@ private: int outWidth, outHeight, zoomFactor; }; -#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF TEST_P(Test_Caffe_layers, Interp) -#else -TEST_P(Test_Caffe_layers, DISABLED_Interp) // requires patched protobuf (available in OpenCV source tree only) -#endif { +#ifdef OPENCV_DNN_EXTERNAL_PROTOBUF + throw SkipTestException("Requires patched protobuf"); +#else #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_EQ(2022010000) // Cannot get memory! if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_CPU) @@ -1686,6 +1685,7 @@ TEST_P(Test_Caffe_layers, DISABLED_Interp) // requires patched protobuf (availa // Test an implemented layer. testLayerUsingCaffeModels("layer_interp", false, false); +#endif } INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_Caffe_layers, dnnBackendsAndTargets());