【问题标题】:How to parse, edit and generate object_detection/pipeline.config files using Google Protobuf如何使用 Google Protobuf 解析、编辑和生成 object_detection/pipeline.config 文件
【发布时间】:2019-07-04 02:01:42
【问题描述】:
  • 我正在使用通用集成学习范式训练多个模型, 目前我正在使用一些检测器,每次训练时我 必须编辑每个探测器的配置文件,这显然会导致 困惑,有几次我开始使用错误的配置进行训练 文件。
  • 作为一种解决方案,我正在尝试为 Google 对象检测 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

我在这里缺少什么?解析和编辑配置文件的适当方法是什么?

谢谢,

霍德

【问题讨论】:

  • .config 文件是文本; GPB 的默认线格式是二进制。我不知道 GPB 是否有文本线格式,但如果有的话,可能需要告诉解析器期望什么线格式。此外,tf_record_input_reader 字段似乎是在名为 input_reader 的oneof 内的 input_reader.proto 中定义的,因此 .config 文件中可能也需要该名称(input_reader)。希望这有助于您朝着正确的方向前进。

标签: python machine-learning protocol-buffers config


【解决方案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

【讨论】:

  • 谢谢,我现在就测试一下!
【解决方案2】:

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

from object_detection.utils import config_util

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

【讨论】:

    【解决方案3】:

    这是我发现覆盖对象检测 pipeline.config 的有用方法:

    from object_detection.utils import config_util
    from object_detection import model_lib_v2
    
    PIPELINE_CONFIG_PATH = 'path_to_your_pipeline.config'
    
    # Load the pipeline config as a dictionary
    pipeline_config_dict = config_util.get_configs_from_pipeline_file(PIPELINE_CONFIG_PATH)
    
    # OVERRIDE EXAMPLES
    # Example 1: Override the train tfrecord path
    pipeline_config_dict['train_input_config'].tf_record_input_reader.input_path[0] = 'your/override/path/to/train.record'
    # Example 2: Override the eval tfrecord path
    pipeline_config_dict['eval_input_config'].tf_record_input_reader.input_path[0] = 'your/override/path/to/test.record'
    
    # Convert the pipeline dict back to a protobuf object
    pipeline_config = config_util.create_pipeline_proto_from_configs(pipeline_config_dict)
    
    # EXAMPLE USAGE:
    # Example 1: Run the object detection train loop with your overrides (has to be string representation)
    model_lib_v2.train_loop(config_override=str(pipeline_config)
    # Example 2: Save the pipeline config to disk
    config_util.save_pipeline_config(config, 'path/to/save/new/pipeline.config)
    

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多