TensorFlow的protobuf版本兼容问题

主要解决方案是在版本不匹配时报告错误:

[En]

The main solution is to report an error when the version does not match:

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

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


    PROTOBUF_URLS = [

        "https://github.com/protocolbuffers/protobuf/archive/v3.9.0.tar.gz",
    ]

    PROTOBUF_SHA256 = "725730117ea1722de5e2f99fe0d9de1cd2c4e48ff2c298fc7a2d8cb4e48811ef"

    PROTOBUF_STRIP_PREFIX = "protobuf-3.9.0"

    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对第二处已作出更新。

TensorFlow的protobuf版本兼容问题

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

Original: https://blog.csdn.net/Li_suhuan/article/details/121041248
Author: Li_suhuan
Title: TensorFlow的protobuf版本兼容问题

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/511198/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球