TensorFlow的protobuf版本兼容问题

5 篇文章 0 订阅
4 篇文章 0 订阅

主要解决版本不匹配报错:
This file was generated by an older version of protoc which is incompatible with your Protocol Buffer headers. Please regenerate this file with a newer version of protoc.

问题描述:

在使用tensorflow时,protoc版本不一致会报错,因为tensorflow-1.15.5使用的protobuf版本为3.8.0,但我们使用的其他依赖库,是基于3.9.0编译的,所以需要保持一致。

/home/svn/tensorflow/include/tensorflow/core/lib/core/error_codes.pb.h
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers.  Please
#error regenerate this file with a newer version of protoc.

TensorFlow 编译过程使用了自己的 Protocol Buffer版本,如果想将自己的 C++ 代码与 TensorFlow 生成的头文件混合使用,则需要使用完全相同的版本。
Please use the same version with tensorflow to recompile your proto files.

有两种解决方法:

1、重新安装protobuf,编译其他依赖时使用与TensorFlow完全相同的protobuf版本。
使用源码编译TensorFlow的话(build_all_linux.sh方式), Bazel会下载的对应的版本,在tensorflow/tensorflow/contrib/makefile/gen/protobuf/bin/protoc)。可以通过protoc –version查看版本号,然后把自己的protobuf版本也改成一样的(重新安装protobuf)
2、修改TensorFlow的原生依赖,编译tensorflow时使用自己的 protoc 版本生成TensorFlow的相关头文件。
TensorFlow 使用的库版本定义在 tensorflow/workspace.bzl,原则上只要它与 TensorFlow 和所有其他依赖项兼容,就应该可以生成 TensorFlow 的自定义构建。

具体解决步骤

这里采用第2种方法,具体操作如下:
TensorFlow使用的protobuf版本是从镜像仓库,或指定的官网地址下载的,tensorflow-1.15.5/tensorflow/workspace.bzl中定义了下载地址:https://storage.googleapis.com/mirror.tensorflow.org
但是这个地址不包含全部的protobuf版本,只包含了TensorFlow现有各版本对应的protobuf版本。

修改tensorflow/workspace.bzl 中对protobuf版本的指定,并计算相应的sha256,填入相应变量。
注意需要在tensorflow的镜像仓库里加上相应的protobuf版本,并保持在前。因为:

tf_http_archive(urls) must have redundant URLs. The mirror.tensorflow.org URL must be present and it must come first. Even if you don’t have permission to mirror the file, please put the correctly formatted mirror URL there anyway, because someone will come along shortly thereafter and mirror the file.

https://github.com/tensorflow/tensorflow/blob/v1.15.5/tensorflow/workspace.bzl

    # 310ba5ee72661c081129eb878c1bbcec936b20f0 is based on 3.8.0 with a fix for protobuf.bzl.
    PROTOBUF_URLS = [
        # "https://storage.googleapis.com/mirror.tensorflow.org/github.com/protocolbuffers/protobuf/archive/310ba5ee72661c081129eb878c1bbcec936b20f0.tar.gz",
        # "https://github.com/protocolbuffers/protobuf/archive/310ba5ee72661c081129eb878c1bbcec936b20f0.tar.gz",
        "https://github.com/protocolbuffers/protobuf/archive/v3.9.0.tar.gz",
    ]
    #PROTOBUF_SHA256 = "b9e92f9af8819bbbc514e2902aec860415b70209f31dfc8c4fa72515a5df9d59"
    PROTOBUF_SHA256 = "725730117ea1722de5e2f99fe0d9de1cd2c4e48ff2c298fc7a2d8cb4e48811ef"
    #PROTOBUF_STRIP_PREFIX = "protobuf-310ba5ee72661c081129eb878c1bbcec936b20f0"
    PROTOBUF_STRIP_PREFIX = "protobuf-3.9.0"

    # protobuf depends on @zlib, it has to be renamed to @zlib_archive because "zlib" is already
    # defined using bind for grpc.
    PROTOBUF_PATCH = "//third_party/protobuf:protobuf.patch"

    tf_http_archive(
        name = "com_google_protobuf",
        patch_file = clean_dep(PROTOBUF_PATCH),
        sha256 = PROTOBUF_SHA256,
        strip_prefix = PROTOBUF_STRIP_PREFIX,
        system_build_file = clean_dep("//third_party/systemlibs:protobuf.BUILD"),
        system_link_files = {
            "//third_party/systemlibs:protobuf.bzl": "protobuf.bzl",
        },
        urls = PROTOBUF_URLS,
    )

另外,tensorflow会根据使用情况对protobuf作出一些修改,这修改通过git的patch方式生效(/home/svn/tensorflow-1.15.5/third_party/protobuf/protobuf.patch)
下图是tensorflow对protobuf-3.8.0的两处修改。但我们要使用3.9.0,所以也要对此做相应修改,只保留第一处对zlib的重命名,因为3.9.0对第二处已作出更新。
修改protobuf的BUILD文件

参考:https://stackoverflow.com/questions/46235376/tensorflow-protobuf-version-mismatch

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
问题描述中提到了protobuf版本不匹配的报错。这可能是由于安装了多个软件,导致重复安装了多个版本protobuf,并且这些版本之间可能存在冲突。为了解决这个问题,我们可以参考作者在使用protobuf中遇到的一些问题和解决方案。 其中,一种可能的解决方案是重新生成文件。报错信息中提到文件是由较旧版本protoc生成的,与当前的Protocol Buffer头文件不兼容。因此,我们需要使用较新版本protoc重新生成文件。 另一种可能的解决方案是确保系统中的protobuf版本与默认版本一致。在引用中,系统中的protobuf版本是3.13.0,而默认版本是3.18.0。如果我们发现版本不一致,我们可以使用相应的命令来更新或安装指定版本protobuf。 总之,要解决protobuf版本不匹配的问题,我们可以尝试重新生成文件或确保系统中的protobuf版本与默认版本一致。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [protobuf版本常见问题](https://blog.csdn.net/weixin_44966641/article/details/122354782)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [TensorFlowprotobuf版本兼容问题](https://blog.csdn.net/Li_suhuan/article/details/121041248)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值