Skip to content

Commit

Permalink
Manage unknown protobuf fields in text
Browse files Browse the repository at this point in the history
  • Loading branch information
dkurt committed Mar 21, 2018
1 parent 196f1b6 commit e3e465b
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 2,097 deletions.
36 changes: 22 additions & 14 deletions 3rdparty/protobuf/src/google/protobuf/text_format.cc
Expand Up @@ -469,8 +469,9 @@ class TextFormat::Parser::ParserImpl {
"\" has no field named \"" + field_name + "\".");
return false;
} else {
ReportWarning("Message type \"" + descriptor->full_name() +
"\" has no field named \"" + field_name + "\".");
// No warnings to let user define custom layers (see https://github.com/opencv/opencv/pull/11129)
// ReportWarning("Message type \"" + descriptor->full_name() +
// "\" has no field named \"" + field_name + "\".");
}
}
}
Expand All @@ -485,10 +486,13 @@ class TextFormat::Parser::ParserImpl {
// start with "{" or "<" which indicates the beginning of a message body.
// If there is no ":" or there is a "{" or "<" after ":", this field has
// to be a message or the input is ill-formed.
UnknownFieldSet* unknown_fields = reflection->MutableUnknownFields(message);
if (TryConsume(":") && !LookingAt("{") && !LookingAt("<")) {
return SkipFieldValue();
UnknownFieldSet* unknown_field = unknown_fields->AddGroup(unknown_fields->field_count());
unknown_field->AddLengthDelimited(0, field_name); // Add a field's name.
return SkipFieldValue(unknown_field);
} else {
return SkipFieldMessage();
return SkipFieldMessage(unknown_fields);
}
}

Expand Down Expand Up @@ -571,7 +575,7 @@ class TextFormat::Parser::ParserImpl {
}

// Skips the next field including the field's name and value.
bool SkipField() {
bool SkipField(UnknownFieldSet* unknown_fields) {
string field_name;
if (TryConsume("[")) {
// Extension name.
Expand All @@ -588,9 +592,11 @@ class TextFormat::Parser::ParserImpl {
// If there is no ":" or there is a "{" or "<" after ":", this field has
// to be a message or the input is ill-formed.
if (TryConsume(":") && !LookingAt("{") && !LookingAt("<")) {
DO(SkipFieldValue());
UnknownFieldSet* unknown_field = unknown_fields->AddGroup(unknown_fields->field_count());
unknown_field->AddLengthDelimited(0, field_name); // Add a field's name.
DO(SkipFieldValue(unknown_field));
} else {
DO(SkipFieldMessage());
DO(SkipFieldMessage(unknown_fields));
}
// For historical reasons, fields may optionally be separated by commas or
// semicolons.
Expand Down Expand Up @@ -625,11 +631,11 @@ class TextFormat::Parser::ParserImpl {

// Skips the whole body of a message including the beginning delimiter and
// the ending delimiter.
bool SkipFieldMessage() {
bool SkipFieldMessage(UnknownFieldSet* unknown_fields) {
string delimiter;
DO(ConsumeMessageDelimiter(&delimiter));
while (!LookingAt(">") && !LookingAt("}")) {
DO(SkipField());
DO(SkipField(unknown_fields));
}
DO(Consume(delimiter));
return true;
Expand Down Expand Up @@ -769,7 +775,7 @@ class TextFormat::Parser::ParserImpl {
return true;
}

bool SkipFieldValue() {
bool SkipFieldValue(UnknownFieldSet* unknown_field) {
if (LookingAtType(io::Tokenizer::TYPE_STRING)) {
while (LookingAtType(io::Tokenizer::TYPE_STRING)) {
tokenizer_.Next();
Expand All @@ -779,9 +785,9 @@ class TextFormat::Parser::ParserImpl {
if (TryConsume("[")) {
while (true) {
if (!LookingAt("{") && !LookingAt("<")) {
DO(SkipFieldValue());
DO(SkipFieldValue(unknown_field));
} else {
DO(SkipFieldMessage());
DO(SkipFieldMessage(unknown_field));
}
if (TryConsume("]")) {
break;
Expand Down Expand Up @@ -833,6 +839,8 @@ class TextFormat::Parser::ParserImpl {
return false;
}
}
// Use a tag 1 because tag 0 is used for field's name.
unknown_field->AddLengthDelimited(1, tokenizer_.current().text);
tokenizer_.Next();
return true;
}
Expand Down Expand Up @@ -1298,13 +1306,13 @@ class TextFormat::Printer::TextGenerator
TextFormat::Finder::~Finder() {
}

TextFormat::Parser::Parser()
TextFormat::Parser::Parser(bool allow_unknown_field)
: error_collector_(NULL),
finder_(NULL),
parse_info_tree_(NULL),
allow_partial_(false),
allow_case_insensitive_field_(false),
allow_unknown_field_(false),
allow_unknown_field_(allow_unknown_field),
allow_unknown_enum_(false),
allow_field_number_(false),
allow_relaxed_whitespace_(false),
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/protobuf/src/google/protobuf/text_format.h
Expand Up @@ -457,7 +457,7 @@ class LIBPROTOBUF_EXPORT TextFormat {
// For more control over parsing, use this class.
class LIBPROTOBUF_EXPORT Parser {
public:
Parser();
Parser(bool allow_unknown_field = false);
~Parser();

// Like TextFormat::Parse().
Expand Down
26 changes: 0 additions & 26 deletions doc/tutorials/dnn/dnn_custom_layers/dnn_custom_layers.md
Expand Up @@ -209,32 +209,6 @@ private:
};
~~~~~~~~~~~~~

In case of networks from Caffe we have to modify a correpsonding `.prototxt` in the following way:
```
layer {
name: "output"
type: "Interp"
bottom: "input"
top: "output"
# Replace an origin interp_param to a custom_param
# interp_param {
# height: 9
# width: 8
# }
custom_param {
value {
name: "height"
i: 9
}
value {
name: "width"
i: 8
}
}
}
```
The structure of `custom_param` can be found at https://github.com/opencv/opencv/blob/master/modules/dnn/src/caffe/opencv-caffe.proto

Next we need to register a new layer type and try to import the model.
~~~~~~~~~~~~~{.cpp}
CV_DNN_REGISTER_LAYER_CLASS(Interp, InterpLayer);
Expand Down
12 changes: 10 additions & 2 deletions modules/dnn/include/opencv2/dnn/dnn.inl.hpp
Expand Up @@ -102,9 +102,13 @@ inline int64 DictValue::get<int64>(int idx) const

return (int64)doubleValue;
}
else if (type == Param::STRING)
{
return std::atoi((*ps)[idx].c_str());
}
else
{
CV_Assert(isInt() || isReal());
CV_Assert(isInt() || isReal() || isString());
return 0;
}
}
Expand Down Expand Up @@ -146,9 +150,13 @@ inline double DictValue::get<double>(int idx) const
{
return (double)(*pi)[idx];
}
else if (type == Param::STRING)
{
return std::atof((*ps)[idx].c_str());
}
else
{
CV_Assert(isReal() || isInt());
CV_Assert(isReal() || isInt() || isString());
return 0;
}
}
Expand Down

0 comments on commit e3e465b

Please sign in to comment.