Arj*_*ran 4 python protocol-buffers proto tensorflow protobuf-python
I'm new to protobuf, so I don't know how to frame the question correctly.
Anyways, I'm using this Model Config proto file. I converted it into python using this command protoc -I=. --python_out=. ./model_server_config.proto from Protocol Buffer page. Now I have some python files which I can import and work on. My objective is to create a file (for running the TensorFlow model server with multiple models) which should look like the following:
model_config_list: {
config: {
name: "name1",
base_path: "path1",
model_platform: "tensorflow"
},
config: {
name: "name2",
base_path: "path2",
model_platform: "tensorflow"
},
config: {
name: "name3",
base_path: "path3",
model_platform: "tensorflow"
},
}
Run Code Online (Sandbox Code Playgroud)
Now using the python package compiled, I made a protobuf object which looks like this when I print it out:
model_config_list {
config {
name: "name1"
base_path: "path1"
model_platform: "tensorflow"
}
config {
name: "name2"
base_path: "path2"
model_platform: "tensorflow"
}
config {
name: "name3"
base_path: "path3"
model_platform: "tensorflow"
}
}
Run Code Online (Sandbox Code Playgroud)
But while serializing the object using objectname.SerializeToString(), I get a weird output as :
b'\n\x94\x01\n \n\x04name1\x12\x0cpath1"\ntensorflow\n7\n\x08name2\x12\x1fpath2"\ntensorflow\n7\n\x08name3\x12\x1fpath3"\ntensorflow'
Run Code Online (Sandbox Code Playgroud)
I tried converting it into Json also using the protobuf for python like this:
from google.protobuf.json_format import MessageToJson
MessageToJson(objectname)
Run Code Online (Sandbox Code Playgroud)
which gave me a result like:
{
"modelConfigList": {
"config": [
{
"name": "name1",
"basePath": "path1",
"modelPlatform": "tensorflow"
},
{
"name": "name2",
"basePath": "path2",
"modelPlatform": "tensorflow"
},
{
"name": "name3",
"basePath": "path3",
"modelPlatform": "tensorflow"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
with all the objects in a list and each objects as string, which is not acceptable for TensorFlow model server config.
Any ideas on how to write it into a file correctly? Or am I creating the whole objects incorrectly? Any help is welcome, Thanks in advance.
我不知道什么系统将读取您的文件,所以我不能说您应该如何将其写入文件。这实际上取决于模型服务器期望如何读取它。
也就是说,我认为您创建消息的方式或您所展示的任何序列化方法没有任何问题。
print方法显示了“文本格式”原型,这有利于调试,有时用于存储配置文件。它不是很紧凑(字段名称存在于文件中)并且不具有二进制表示的所有向后和向前兼容的功能。它实际上在功能上与您所说的“应该看起来像”相同:冒号和逗号实际上是可选的。SerializeToString()方法使用二进制序列化格式。这可以说是协议缓冲区的构建目的。它是一种紧凑的表示形式,并提供向后和向前兼容性,但它不太适合人类阅读。json_format模块提供消息的 JSON 表示形式。如果您正在交互的系统需要 JSON,那就非常好,但这并不常见。print()附录:该google.protobuf.text_format模块具有更适合以编程方式使用文本格式的实用程序,而不是使用。要写入文件,您可以使用:
from google.protobuf import text_format
(...)
with open(file_path, 'w') as output:
text_format.PrintMessage(my_message, output)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4053 次 |
| 最近记录: |