Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protobuf parser for DNN module #9106

Closed
wants to merge 3 commits into from
Closed

Conversation

dkurt
Copy link
Member

@dkurt dkurt commented Jul 6, 2017

This pullrequest changes

  • Use OpenCV's protocol buffers parser as optional parser for DNN module. Dependency is designed to use parser if it's available. So applications will start use it automatically after integration. It this way, Google's protobuf not link and not build. To disable this dependency set -Dopencv_dnn_USE_PROTOBUF=ON or disable parser by -DBUILD_opencv_protobuf_parser=OFF if it isn't used somewhere.
  • Require compiled caffe.proto and graph.proto by proto compiler (see protoc --descriptor_set_out).
  • caffe_importer logic was completely rewritten. tensorflow_importer was modified to support parser usage.
  • Minor changes of detection_output_layer were made to support both parsers.

Merge with contrib: opencv/opencv_contrib#1291
Merge with extra: opencv/opencv_extra#356

There are issues related to libprotobuf. This PR isn't solve them but it may help avoid them.
close opencv/opencv_contrib#823
close opencv/opencv_contrib#854
close opencv/opencv_contrib#1242
close #9549
http://answers.opencv.org/question/138047/opencv-32-includes-libmirprotobuf-and-protobuf-26-which-is-conflicting-with-protobuf-31/

@dkurt dkurt force-pushed the protobuf_parser branch 8 times, most recently from 21f431a to 25b0325 Compare July 7, 2017 12:12
@dkurt dkurt changed the title Eliminated protobuf dependency from Caffe importer Protobuf parser for DNN module Jul 7, 2017
@dkurt dkurt force-pushed the protobuf_parser branch 6 times, most recently from 777799b to 20011c2 Compare July 11, 2017 10:26
@dkurt dkurt force-pushed the protobuf_parser branch from 20011c2 to 8746371 Compare July 17, 2017 06:50
@vpisarev
Copy link
Contributor

@dkurt, I suggest you to remove the word "w i p" from the PR description. We all know that this is experimental patch; but without this keyword your PR will be automatically checked by buildbot as soon as you put new commits.

@dkurt dkurt force-pushed the protobuf_parser branch from 8746371 to 19d205f Compare July 18, 2017 11:24
@dkurt
Copy link
Member Author

dkurt commented Jul 18, 2017

@vpisarev, would it be OK if I made this usage as a single module @ opencv_contrib for a while? It let us:

  • add more parser-specific tests (origin protobuf will be required for a part of tests)
  • make softer migration: use parser if found
  • reduce protobuf dependency in other modules (currently read mode only)

@vpisarev
Copy link
Contributor

@dkurt, I'm fine with it

@vpisarev
Copy link
Contributor

@dkurt, let's merge the patch. But could you please fix the merge conflicts first?

Copy link
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMake conditions should be re-worked, for example this doesn't work properly (without contrib at least):

cmake -DBUILD_PROTOBUF=OFF <opencv_dir>
make opencv_test_dnn # compilation errors

Previously it uses system external protobuf with protoc (required for updating pb file to new version).

How to switch on this "parser"?

cmake <opencv_dir>
make opencv_test_dnn # still using libprotobuf

USE_PROTOBUF is broken and doesn't work at all.

modules/dnn/misc/caffe/caffe_proto.pb
modules/dnn/misc/tensorflow/graph_proto.pb

Custom binary blobs should be avoided, because there is no way to maintain/track changes (no tools)
I believe there is no problem to write/store parsed "AST" as C++ code instead.

ocv_option(${the_module}_USE_PROTOBUF "Force use of Protobuf library instead OpenCV's protobuf parser" OFF)

# One and only way if dnn module will be disabled - failed to link to system protobuf.
if (DEFINED BUILD_PROTOBUF AND NOT BUILD_PROTOBUF)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if should be used without space in CMake here and below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced

/wd4456 /wd4510 /wd4610 /wd4800
-wd858 -wd2196
)
if (${USE_PROTOBUF})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(USE_PROTOBUF)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced

include(${OpenCV_SOURCE_DIR}/cmake/OpenCVFindLibProtobuf.cmake)
if(NOT Protobuf_FOUND)
ocv_module_disable(opencv_dnn)
ocv_option(${the_module}_USE_PROTOBUF "Force use of Protobuf library instead OpenCV's protobuf parser" OFF)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is "_USE_PROTOBUF" because "the_module" is not defined here (before ocv_add_module()).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced under the ocv_add_module.

May I ask you to show version of system protobuf to reproduce it?

Added text descriptors parsing. So now we generate .pb from .proto using protoc. Then print it as text. I've found only one way to use python:

from google.protobuf import descriptor_pb2

msg = descriptor_pb2.FileDescriptorSet()

with open('/path/to/example.pb', 'rb') as f:
    msg.ParseFromString(f.read())
    print msg

}
DictValue dict;
if (typeid(T) == typeid(int32_t) || typeid(T) == typeid(uint32_t) ||
typeid(T) == typeid(int64_t) || typeid(T) == typeid(uint64_t))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use template specializations / type traits instead.
I believe this "if" must be handled by DictValue itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Replaced to is_integer and is_specialized for non-integers but floats and doubles.


using namespace pb;

class CaffeImporter : public Importer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need another one Caffe importer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alalek, Thanks! It's possible to use a single Caffe importer. I'll do it.

#ifndef HAVE_PROTOBUF
#include <typeinfo>

#include "../precomp.hpp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precomp.hpp must be the first include without any #ifs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@dkurt dkurt force-pushed the protobuf_parser branch 2 times, most recently from e2b14f2 to 0ead211 Compare November 1, 2017 11:55
@dkurt dkurt force-pushed the protobuf_parser branch 2 times, most recently from c492db7 to 386dfc2 Compare November 21, 2017 10:15
@ribalda
Copy link
Contributor

ribalda commented Nov 22, 2017

Hi, have you taken a look to: protocolbuffers/protobuf#1489 ?

@dkurt dkurt force-pushed the protobuf_parser branch 2 times, most recently from 8ecc554 to bc525a1 Compare November 22, 2017 15:32
@dkurt
Copy link
Member Author

dkurt commented Oct 8, 2018

I think we can close it for now.

@dkurt dkurt closed this Oct 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants