我在加载从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;谢谢你的帮助!
发布于 2018-08-14 14:14:25
一个可能的错误是.pb文件包含一个GraphDef,而不是一个MetaGraphDef。
要读取GraphDef,只需切换到
tensorflow::GraphDef graph_def;
status = ReadBinaryProto(tensorflow::Env::Default(), graph_fn,
&graph_def);protobuf格式实际上只是一个容器,它没有说明它包含了什么。在tensorflow中,这种格式通常用于包含图形和图解,这可能会使人感到困惑。
https://stackoverflow.com/questions/51843259
复制相似问题