【发布时间】:2018-08-14 13:58:35
【问题描述】:
我在加载从 C++ 中的 tensorflow 模型保存的协议缓冲区文件时遇到问题。我可以毫无问题地在 Python 中加载和执行 .pb 文件,但是当尝试使用 ReadBinaryProto 函数在 C++ 中加载它时出现错误:
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
我正在使用 Tensorflow v1.10,下面发布了一个 C++ 代码示例,我尝试了许多不同的方法来保存 .pb 文件,包括使用 freeze_graph 方法和 tf.write_graph方法,似乎都无法解决问题。
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;
感谢您的帮助!
【问题讨论】:
-
警告:你使用的是和 TF 一样的 protobuf 版本吗?
-
protobuf 文件随 TF 的安装一起提供,所以我假设我会这样做,除非我必须以某种方式单独安装它们?
-
您确定 pb 包含
MetaGraphDef而不是GraphDef?用python怎么读? -
@P-Gn Doh!我相当确定该图是作为 GraphDef 而不是 MetaGraphDef 导出的。我现在将尝试更新 C++ 代码以适应这种情况,或者是否有更简单的方法来导出 MetaGraphDef?
-
我认为使用
MetaGraphDefs 的首选方式实际上是通过SavedModelBundles。它相当容易使用,但需要对您的代码进行更多更改。
标签: c++ tensorflow protocol-buffers