c++ - Cannot load protocol buffer with ReadBinaryProto Tensorflow
问题描述
I am having issues loading my protocol buffer file saved from a tensorflow model in C++. I can load and execute the .pb file in Python with no issues at all, however when trying to load it in C++ with the ReadBinaryProto
function I get the error:
String field'tensorflow.MetaGraphDef.MetaInfoDef.tensorflow_version'
contains invalid UTF-8 data when parsing a protocol buffer. Use the 'bytes'
type if you intend to send raw bytes.
Non-OK-status: LoadModel(session_inception, pathToGraph ) status: Data loss:
Can't parse E:/Projects/SampleTensorflow/tmp/latestmodel.pb as binary proto
I am using Tensorflow v1.10 and a sample of the C++ code is posted below, I have tried many different methods to save the .pb file including using the freeze_graph method and the tf.write_graph
method, neither seem to fix the issue.
tensorflow::Status LoadModel(tensorflow::Session *sess, std::string
graph_fn, std::string checkpoint_fn = "") {
tensorflow::Status status;
std::string graph_fn = "E:/Projects/SampleTensorflow/tmp/latestmodel.pb";
// Read in the protobuf graph
tensorflow::MetaGraphDef graph_def;
status = ReadBinaryProto(tensorflow::Env::Default(), graph_fn,
&graph_def);
if (status != tensorflow::Status::OK())
return status;
// Create the graph
status = sess->Create(graph_def.graph_def());
if (status != tensorflow::Status::OK())
return status;
Thank you for any help!
解决方案
A possible error is that the .pb
file contains a GraphDef
and not a MetaGraphDef
.
To read a GraphDef
, simply switch to
tensorflow::GraphDef graph_def;
status = ReadBinaryProto(tensorflow::Env::Default(), graph_fn,
&graph_def);
The protobuf format is indeed just a container, which says nothing about what it contains. In tensorflow, this format is commonly used both to contain graphs and metagraphs, which can be confusing.
推荐阅读
- python - psycopg2.OperationalError:致命:不支持的前端协议 1234.5679:服务器支持 2.0 到 3.0
- javascript - 如何在按钮的单击句柄中使用 mergeMap 订阅内部可观察对象
- angular - 用于更新 ViewChild 的 Angular 8 升级示意图
- javascript - 当没有找到记录时,我需要向用户发送警报
- python - 单独的列表列表到元组
- amazon-web-services - 有没有办法找出公共 ip 属于哪个 aws 帐户?
- php - PHP PDOException 连接问题 - laravel
- angular - 如何保存我从后端获得的 Angular 重新排序列表的顺序
- c# - 如何在 C# (.NET Core 3.1) 中处理 POST 请求中的 JSON 数据
- c# - 如何使用 aspx 为用户分配角色和网页权限?