1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-21 19:33:03 +04:00

Merge pull request #29425 from fkenmar:dnn_remove_caffe_leftovers

dnn: remove Caffe protobuf leftovers, fix builds with external protobuf
This commit is contained in:
Alexander Smorkalov
2026-07-07 12:52:37 +03:00
committed by GitHub
12 changed files with 221 additions and 80340 deletions
+5 -5
View File
@@ -109,7 +109,7 @@ if(NOT BUILD_PROTOBUF)
ocv_target_compile_definitions(${the_module} PRIVATE "OPENCV_DNN_EXTERNAL_PROTOBUF=1")
endif()
#suppress warnings in autogenerated caffe.pb.* files
#suppress warnings in autogenerated *.pb.* files
ocv_warnings_disable(CMAKE_CXX_FLAGS
/wd4125 /wd4267 /wd4127 /wd4244 /wd4512 /wd4702
/wd4456 /wd4510 /wd4610 /wd4800
@@ -425,9 +425,9 @@ if(HAVE_PROTOBUF)
list(FILTER fw_hdrs INCLUDE REGEX ".+\.h$")
endif()
else()
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")
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")
set(fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/caffe" "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx")
file(GLOB fw_srcs "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow/*.cc" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx/opencv-onnx.pb.cc")
file(GLOB fw_hdrs "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow/*.h" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx/opencv-onnx.pb.h")
set(fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx")
endif()
endif()
@@ -546,7 +546,7 @@ set(dnn_plugin_srcs ${dnn_srcs} ${dnn_int_hdrs})
ocv_list_filterout_ex(dnn_plugin_srcs
"/src/dnn.cpp$|/src/dnn_utils.cpp$|/src/dnn_read.cpp$|/src/registry.cpp$|/src/backend.cpp$"
# importers
"/src/(caffe|onnx|tensorflow)/"
"/src/(onnx|tensorflow)/|/src/protobuf_io.cpp$"
# executors
"/src/(cuda|cuda4dnn|ocl4dnn|vkcom|webnn)/"
)
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -39,8 +39,8 @@
//
//M*/
#ifndef __OPENCV_DNN_CAFFE_GLOG_EMULATOR_HPP__
#define __OPENCV_DNN_CAFFE_GLOG_EMULATOR_HPP__
#ifndef __OPENCV_DNN_GLOG_EMULATOR_HPP__
#define __OPENCV_DNN_GLOG_EMULATOR_HPP__
#include <cstdlib>
#include <iostream>
#include <sstream>
+29 -13
View File
@@ -8,8 +8,6 @@
#include "layers_common.hpp"
#include "../net_impl.hpp"
#include "opencv-onnx.pb.h"
namespace cv { namespace dnn {
// ONNX Cast operator
@@ -367,21 +365,39 @@ private:
bool hasToParam = false;
int toCvDepth_ = -1;
// ONNX TensorProto::DataType values (see opencv-onnx.proto); the 'to'
// attribute stores the raw ONNX value. Fixed by the ONNX specification,
// duplicated here to keep this layer independent of protobuf-generated headers.
enum OnnxDataType
{
ONNX_DT_FLOAT = 1,
ONNX_DT_UINT8 = 2,
ONNX_DT_INT8 = 3,
ONNX_DT_UINT16 = 4,
ONNX_DT_INT16 = 5,
ONNX_DT_INT32 = 6,
ONNX_DT_INT64 = 7,
ONNX_DT_BOOL = 9,
ONNX_DT_FLOAT16 = 10,
ONNX_DT_DOUBLE = 11,
ONNX_DT_BFLOAT16 = 16
};
static int mapToCvDepth(int v)
{
switch (v)
{
case opencv_onnx::TensorProto_DataType_FLOAT: return CV_32F;
case opencv_onnx::TensorProto_DataType_UINT8: return CV_8U;
case opencv_onnx::TensorProto_DataType_INT8: return CV_8S;
case opencv_onnx::TensorProto_DataType_UINT16: return CV_16U;
case opencv_onnx::TensorProto_DataType_INT16: return CV_16S;
case opencv_onnx::TensorProto_DataType_INT32: return CV_32S;
case opencv_onnx::TensorProto_DataType_INT64: return CV_64S;
case opencv_onnx::TensorProto_DataType_BOOL: return CV_Bool;
case opencv_onnx::TensorProto_DataType_FLOAT16: return CV_16F;
case opencv_onnx::TensorProto_DataType_DOUBLE: return CV_64F;
case opencv_onnx::TensorProto_DataType_BFLOAT16: return CV_16BF;
case ONNX_DT_FLOAT: return CV_32F;
case ONNX_DT_UINT8: return CV_8U;
case ONNX_DT_INT8: return CV_8S;
case ONNX_DT_UINT16: return CV_16U;
case ONNX_DT_INT16: return CV_16S;
case ONNX_DT_INT32: return CV_32S;
case ONNX_DT_INT64: return CV_64S;
case ONNX_DT_BOOL: return CV_Bool;
case ONNX_DT_FLOAT16: return CV_16F;
case ONNX_DT_DOUBLE: return CV_64F;
case ONNX_DT_BFLOAT16: return CV_16BF;
default: break;
}
+1 -1
View File
@@ -41,7 +41,7 @@
#ifndef _OPENCV_LIBDNN_COMMON_HPP_
#define _OPENCV_LIBDNN_COMMON_HPP_
#include "../../caffe/glog_emulator.hpp"
#include "../../glog_emulator.hpp"
#include <opencv2/core/opencl/runtime/opencl_core.hpp>
// Macro to select the single (_float) or double (_double) precision kernel
+3 -3
View File
@@ -4320,15 +4320,15 @@ Mat readTensorFromONNX(const String& path)
#define DNN_PROTOBUF_UNSUPPORTED() CV_Error(Error::StsError, "DNN/ONNX: Build OpenCV with Protobuf to import ONNX models")
Net readNetFromONNX(const String&) {
Net readNetFromONNX(const String&, int) {
DNN_PROTOBUF_UNSUPPORTED();
}
Net readNetFromONNX(const char*, size_t) {
Net readNetFromONNX(const char*, size_t, int) {
DNN_PROTOBUF_UNSUPPORTED();
}
Net readNetFromONNX(const std::vector<uchar>&) {
Net readNetFromONNX(const std::vector<uchar>&, int) {
DNN_PROTOBUF_UNSUPPORTED();
}
+171
View File
@@ -0,0 +1,171 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
/*M///////////////////////////////////////////////////////////////////////////////////////
//COPYRIGHT
//
//All contributions by the University of California:
//Copyright (c) 2014, The Regents of the University of California (Regents)
//All rights reserved.
//
//All other contributions:
//Copyright (c) 2014, the respective contributors
//All rights reserved.
//
//Caffe uses a shared copyright model: each contributor holds copyright over
//their contributions to Caffe. The project versioning records all such
//contribution and copyright details. If a contributor wants to further mark
//their specific copyright on a particular contribution, they should indicate
//their copyright solely in the commit message of the change when it is
//committed.
//
//LICENSE
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions are met:
//
//1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
//ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
//ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//CONTRIBUTION AGREEMENT
//
//By contributing to the BVLC/caffe repository through pull-request, comment,
//or otherwise, the contributor releases their content to the
//license and copyright terms herein.
//
//M*/
#include "precomp.hpp"
#ifdef HAVE_PROTOBUF
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <opencv2/core.hpp>
#include <climits>
#include <fstream>
#include "protobuf_io.hpp"
#include "glog_emulator.hpp"
namespace cv {
namespace dnn {
using namespace ::google::protobuf;
using namespace ::google::protobuf::io;
static const int kProtoReadBytesLimit = INT_MAX; // Max size of 2 GB minus 1 byte.
static bool ReadProtoFromBinary(ZeroCopyInputStream* input, MessageLite *proto) {
CodedInputStream coded_input(input);
#if defined(GOOGLE_PROTOBUF_VERSION) && GOOGLE_PROTOBUF_VERSION < 3006000
coded_input.SetTotalBytesLimit(kProtoReadBytesLimit, 536870912);
#else
coded_input.SetTotalBytesLimit(kProtoReadBytesLimit);
#endif
return proto->ParseFromCodedStream(&coded_input);
}
bool ReadProtoFromTextFile(const char* filename, Message* proto) {
std::ifstream fs(filename, std::ifstream::in);
CHECK(fs.is_open()) << "Can't open \"" << filename << "\"";
IstreamInputStream input(&fs);
google::protobuf::TextFormat::Parser parser;
#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
parser.AllowUnknownField(true);
parser.SetRecursionLimit(1000);
#endif
return parser.Parse(&input, proto);
}
bool ReadProtoFromTextFile(const char* filename, MessageLite* proto) {
CV_Error(Error::StsError, "DNN/Protobuf: do not have your message be a MessageLite");
return false;
}
bool ReadProtoFromBinaryFile(const char* filename, MessageLite* proto) {
std::ifstream fs(filename, std::ifstream::in | std::ifstream::binary);
CHECK(fs.is_open()) << "Can't open \"" << filename << "\"";
IstreamInputStream raw_input(&fs);
return ReadProtoFromBinary(&raw_input, proto);
}
bool ReadProtoFromTextBuffer(const char* data, size_t len, Message* proto) {
ArrayInputStream input(data, len);
google::protobuf::TextFormat::Parser parser;
#ifndef OPENCV_DNN_EXTERNAL_PROTOBUF
parser.AllowUnknownField(true);
parser.SetRecursionLimit(1000);
#endif
return parser.Parse(&input, proto);
}
bool ReadProtoFromTextBuffer(const char* data, size_t len, MessageLite* proto) {
CV_Error(Error::StsError, "DNN/Protobuf: do not have your message be a MessageLite");
return false;
}
bool ReadProtoFromBinaryBuffer(const char* data, size_t len, MessageLite* proto) {
ArrayInputStream raw_input(data, len);
return ReadProtoFromBinary(&raw_input, proto);
}
}
}
#endif
@@ -87,37 +87,18 @@
//
//M*/
#ifndef __OPENCV_DNN_CAFFE_IO_HPP__
#define __OPENCV_DNN_CAFFE_IO_HPP__
#ifndef __OPENCV_DNN_PROTOBUF_IO_HPP__
#define __OPENCV_DNN_PROTOBUF_IO_HPP__
#ifdef HAVE_PROTOBUF
#if defined(__GNUC__) && __GNUC__ >= 5
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-override"
#endif
#include "opencv-caffe.pb.h"
#if defined(__GNUC__) && __GNUC__ >= 5
#pragma GCC diagnostic pop
#endif
#include <cstddef>
namespace caffe { using namespace opencv_caffe; } // avoid massive renames from caffe proto package
namespace google { namespace protobuf { class Message; class MessageLite; } }
namespace cv {
namespace dnn {
// Read parameters from a file into a NetParameter proto message.
void ReadNetParamsFromTextFileOrDie(const char* param_file,
caffe::NetParameter* param);
void ReadNetParamsFromBinaryFileOrDie(const char* param_file,
caffe::NetParameter* param);
// Read parameters from a memory buffer into a NetParammeter proto message.
void ReadNetParamsFromBinaryBufferOrDie(const char* data, size_t len,
caffe::NetParameter* param);
void ReadNetParamsFromTextBufferOrDie(const char* data, size_t len,
caffe::NetParameter* param);
// Utility functions used internally by Caffe and TensorFlow loaders
// Utility functions used internally by model loaders
bool ReadProtoFromTextFile(const char* filename, ::google::protobuf::Message* proto);
bool ReadProtoFromTextFile(const char* filename, ::google::protobuf::MessageLite* proto);
bool ReadProtoFromBinaryFile(const char* filename, ::google::protobuf::MessageLite* proto);
+3 -3
View File
@@ -3373,15 +3373,15 @@ void writeTextGraph(const String& _model, const String& output)
#define DNN_PROTOBUF_UNSUPPORTED() CV_Error(Error::StsError, "DNN/TF: Build OpenCV with Protobuf to import TensorFlow models")
Net readNetFromTensorflow(const String &, const String &) {
Net readNetFromTensorflow(const String &, const String &, int, const std::vector<String>&) {
DNN_PROTOBUF_UNSUPPORTED();
}
Net readNetFromTensorflow(const char*, size_t, const char*, size_t) {
Net readNetFromTensorflow(const char*, size_t, const char*, size_t, int, const std::vector<String>&) {
DNN_PROTOBUF_UNSUPPORTED();
}
Net readNetFromTensorflow(const std::vector<uchar>&, const std::vector<uchar>&) {
Net readNetFromTensorflow(const std::vector<uchar>&, const std::vector<uchar>&, int, const std::vector<String>&) {
DNN_PROTOBUF_UNSUPPORTED();
}
+2 -2
View File
@@ -25,8 +25,8 @@ Implementation of various functions which are related to Tensorflow models readi
#include "tf_io.hpp"
#include "../caffe/caffe_io.hpp"
#include "../caffe/glog_emulator.hpp"
#include "../protobuf_io.hpp"
#include "../glog_emulator.hpp"
namespace cv {
namespace dnn {