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

标签 python tensorflow protocol-buffers

我正在使用 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"
    }

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

最佳答案

对象检测 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)

您必须调整代码,但总体原理应该很清楚。我建议将其重构为函数并调用 Jupyter。

关于python - 如何动态编辑外部 .config 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58958905/

相关文章:

Python:查找唯一的 XML 属性

python 按类名切换?

c++ - 正确释放 Protocol Buffer 内存

python - 存储为 Oracle BLOB : python retrieve fails 的 Protocol Buffer

具有多个函数的 Python C 扩展

python - 使用 bulkloader 上传数据

google-cloud-platform - 为什么在 google cloud ml 上训练模型时出现内存不足异常?

python - 异常训练Resnet50 : "The shape of the input to "Flatten"is not fully defined"

python - TensorFlow 库被编译为使用 SSE4.1 指令,但这些在您的机器上不可用。中止(核心转储)

java - 不要在java protobuf中生成*Count方法