TensorFlow protobuf 版本不匹配

TensorFlow protobuf version mismatch

我已经通过 virtualenv 安装了 TensorFlow。而且效果很好。

现在我想使用 C++ 加载模型并进行预测。但是由于 protobuf 版本不匹配,我无法编译我的程序。错误如:

tensorflow/core/framework/device_attributes.pb.h:17:2: error: #error This file was generated by an older version of protoc which is
 #error This file was generated by an older version of protoc which is
  ^
tensorflow/core/framework/device_attributes.pb.h:18:2: error: #error incompatible with your Protocol Buffer headers. Please
 #error incompatible with your Protocol Buffer headers.  Please
  ^
tensorflow/core/framework/device_attributes.pb.h:19:2: error: #error regenerate this file with a newer version of protoc.
 #error regenerate this file with a newer version of protoc.

在虚拟环境中:

$ pip show protobuf
Name: protobuf
Version: 3.4.0
Summary: Protocol Buffers

在shell中:

$ protoc --version
libprotoc 3.4.0

我以前的环境中有 protobuf-2.6.1,但现在升级到 3.4.0

ubuntu 16.04

问题是 TensorFlow 编译过程使用了它自己的协议缓冲区分配。从 TensorFlow v1.3.0 开始,这个发行版是 protocol buffers 3.3.0。如果您想将自己的 C++ 代码与生成的 TensorFlow 混合使用 headers,您需要使用完全相同的版本(或者简单地使用脚本来使用 Bazel 下载的分发版)。

另一种方法是使用您自己的 protoc 从原始消息描述文件生成您自己的 headers。

编辑:

TensorFlow 使用的库版本当前 (TF v1.9) 在 tensorflow/workspace.bzl 中定义。原则上,只要它与 TensorFlow 和所有其他依赖项兼容(请注意,出于源代码中解释的原因,有是 Protocol Buffers 的三个 HTTP 存档,protobuf_archivecom_google_protobufcom_google_protobuf_cc,因此您需要修改其中的三个。

请使用与 tensorflow 相同的版本重新编译您的原型文件。如果您使用 build_all_linux.sh.

从源代码构建 tensorflow,您将在 tensorflow/tensorflow/contrib/makefile/gen/protobuf/bin/protoc 中找到它

有一个我努力解决的类似问题:编译脚本总是能够在某处找到过时版本的 protoc。

检查协议位置:

which protoc # /usr/bin/protoc
sudo apt-get uninstall protobuf-compiler
conda uninstall protobuf
conda uninstall libprotobuf
exit # resign in

这意味着这里有一个协议二进制文件 /usr/bin/protoc 和头文件 /usr/include/google/protoc。删除 protoc 安装不一定会删除 include 文件夹中的头文件,这会导致 reappers 出错。如果您在安装 protobuf 后仍然看到错误,请尝试清理每个 protobuf 位置的包含文件。

之后,您可以随意下载适用于您的平台的已编译二进制文件(在我的例子中是 protoc-3.10.1-linux-x86_64.zip)并将其放置在方便的位置:

mkdir ~/tmp/
cd ~/tmp
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.10.1/protoc-3.10.1-linux-x86_64.zip
sudo rm -rf ./protoc
unzip protoc-3.10.1-linux-x86_64.zip -d protoc
chmod 755 -R protoc
BASE=/usr/local
sudo rm -rf $BASE/include/google/protobuf/
sudo cp protoc/bin/protoc $BASE/bin 
sudo cp -R protoc/include/* $BASE/include 

尽管在解决问题的过程中同时看到了"your protobuf version is older" 和 "your protobuf version is newer",但最新版本的 protoc 起作用了。