如何使用Google Protobuf解析,编辑和生成object_detection / pipeline.config文件

时间:2019-02-10 11:34:12

标签: python machine-learning protocol-buffers config

  • 我正在一个通用的Ensemble学习范例中训练多个模型, 目前我正在使用一些探测器,每次训练时 必须编辑每个检测器的配置文件,这显然导致 混乱,有几次我开始使用错误的配置进行训练 文件。
  • 作为一种解决方案,我正在尝试为Google Object Detection API构建编辑器 配置文件。配置文件可与Google Protocol Buffer一起使用。
  • 链接到我使用的文件:pipeline.protoobject_detection/protos,例如.config file

我尝试了以下代码:

from object_detection.protos import input_reader_pb2
with open('/models/research/object_detection/samples/configs/ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync.config', 'rb') as f:
    config = f.read()

read = input_reader_pb2.InputReader().ParseFromString(config)

然后出现以下错误:

    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-19-8043e6bb108f>", line 1, in <module>
    input_reader_pb2.InputReader().ParseFromString(txt)
google.protobuf.message.DecodeError: Error parsing message

我在这里想念什么?解析和编辑配置文件的合适方法是什么?

谢谢

上帝

2 个答案:

答案 0 :(得分:1)

使用以下代码,我能够解析配置文件。

import tensorflow as tf
from google.protobuf import text_format

from object_detection.protos import pipeline_pb2

def get_configs_from_pipeline_file(pipeline_config_path, config_override=None):

  '''
  read .config and convert it to proto_buffer_object
  '''

  pipeline_config = pipeline_pb2.TrainEvalPipelineConfig()
  with tf.gfile.GFile(pipeline_config_path, "r") as f:
    proto_str = f.read()
    text_format.Merge(proto_str, pipeline_config)
  if config_override:
    text_format.Merge(config_override, pipeline_config)
  #print(pipeline_config)
  return pipeline_config


def create_configs_from_pipeline_proto(pipeline_config):
  '''
  Returns the configurations as dictionary
  '''

  configs = {}
  configs["model"] = pipeline_config.model
  configs["train_config"] = pipeline_config.train_config
  configs["train_input_config"] = pipeline_config.train_input_reader
  configs["eval_config"] = pipeline_config.eval_config
  configs["eval_input_configs"] = pipeline_config.eval_input_reader
  # Keeps eval_input_config only for backwards compatibility. All clients should
  # read eval_input_configs instead.
  if configs["eval_input_configs"]:
    configs["eval_input_config"] = configs["eval_input_configs"][0]
  if pipeline_config.HasField("graph_rewriter"):
    configs["graph_rewriter_config"] = pipeline_config.graph_rewriter

  return configs


configs = get_configs_from_pipeline_file('faster_rcnn_resnet101_pets.config')
config_as_dict = create_configs_from_pipeline_proto(configs)

引自here

答案 1 :(得分:1)

由于您安装了 object_detection API,您只需执行以下操作:

from object_detection.utils import config_util

pipeline_config = config_util.get_configs_from_pipeline_file('/path/to/config/file')