如何动态编辑外部 .config 文件?

Sh0*_*tey 3 python protocol-buffers tensorflow

我正在使用 tensorflow 对象检测 api 开发一个主动机器学习管道。我的目标是动态更改网络的 .config 文件中的路径。

标准配置如下所示:

    train_input_reader: {
       tf_record_input_reader {
       input_path: "/PATH_TO_CONFIGURE/train.record"
       }
       label_map_path: "/PATH_TO_CONFIGURE/label_map.pbtxt"
    }
Run Code Online (Sandbox Code Playgroud)

“PATH_TO_CONFIGURE”应该从我的 jupyter notebook 单元中动态替换。

Luk*_*ski 6

对象检测 API 配置文件具有protobuf格式。以下是您阅读、编辑和保存的大致方法。

import tensorflow as tf
from google.protobuf import text_format
from object_detection.protos import pipeline_pb2

pipeline = pipeline_pb2.TrainEvalPipelineConfig()                                                                                                                                                                                                          

with tf.gfile.GFile('config path', "r") as f:                                                                                                                                                                                                                     
    proto_str = f.read()                                                                                                                                                                                                                                          
    text_format.Merge(proto_str, pipeline)

pipeline.train_input_reader.tf_record_input_reader.input_path[:] = ['your new entry'] # it's a repeated field 
pipeline.train_input_reader.label_map_path = 'your new entry'

config_text = text_format.MessageToString(pipeline)                                                                                                                                                                                                        
with tf.gfile.Open('config path', "wb") as f:                                                                                                                                                                                                                       
    f.write(config_text)
Run Code Online (Sandbox Code Playgroud)

您将不得不调整代码,但总体思路应该是清楚的。我建议将其重构为函数并调用 Jupyter。

  • 您可以尝试“pipeline.train_input_reader.tf_record_input_reader.input_path[:] = ..”(不是冒号)吗?如果它是复合的,您可能必须先删除它,请参阅/sf/ask/1660843481/ (2认同)