【问题标题】:AttributeError: module 'tensorflow' has no attribute 'gfile'AttributeError:模块“tensorflow”没有属性“gfile”
【发布时间】:2019-08-30 15:58:57
【问题描述】:

我在 Google Colab 上使用 tensorflow 2.0 训练了一个简单的 mnist 模型,并将其保存为 .json 格式。 Click here 查看我编写代码的 Colab Notebook。然后在运行命令

!simple_tensorflow_serving --model_base_path="/" --model_platform="tensorflow"

显示错误 AttributeError: module 'tensorflow' has no attribute 'gfile'

simple_tensorflow_serving 有助于将经过训练的 TensorFlow 模型轻松部署到生产环境中。

版本我正在使用:

(1) TensorFlow - 2.0

(2) simple_tensorflow_serving - 0.6.4

提前谢谢你:)

【问题讨论】:

  • 我在 TF 2 中遇到了与 FullTokenizer(vocab_file, do_lower_case) 相同的问题

标签: google-colaboratory tensorflow-serving tensorflow2.0


【解决方案1】:

在 2.0 中,tf.gfile.* 被 tf.io.gfile.* 替换。

当我收到错误时:

  File "/Users/MRJ/anaconda3/envs/python37-tf2.1/lib/python3.7/
site-packages/object_detection/utils/label_map_util.py",
line 137, in load_labelmap

with tf.gfile.GFile(path, 'r') as fid:
  AttributeError: module 'tensorflow' has no attribute 'gfile'

1.查找label_map_util.py第137行。

2.将tf.gfile.GFile替换为tf.io.gfile.GFile

它对我有用。

tensorflow issue #31315

【讨论】:

  • 你在tensorflow中的其他命令有错误吗?我想知道更改此设置是否会导致任何其他问题
  • 没有。 “在 2.0 中,tf.gfile.* 被替换为 tf.io.gfile.*。”所以 tf.io.gfile.GFile 总是正确的。
【解决方案2】:

Simple Tensorflow Serving 尚未为 Tensorflow 2.0 做好准备,因为它使用的是旧 API。 在 TensorFlow 2.0 中,gfile 包已移至 tf.io

然后,您必须使用 Simple Tensorflow Serving 将您的 Tensorflow 实例降级到 TF 1.13

【讨论】:

  • 如何降级?
  • pip install tensorflow==1.13
  • 万一有人来到这里并得到“错误:找不到满足要求 tensorflow==1.13 的版本”。请使用----> pip install tensorflow==1.13.1
【解决方案3】:

相反,试试

tf.io.gfile.GFile(
    name, mode='r'
)

即“.io”将解决您的所有问题,而不是降级您的 tf

【讨论】:

    【解决方案4】:

    tf.gfile.GFile 更改为 tf.io.gfile.GFile 它适用于这个实例,但不适用于所有其他文件 示例 tf.io.gfile.FastGFile 引发错误,提示“AttributeError: module 'tensorflow._api.v2.io.gfile' has no attribute 'FastGFile'”。我希望它可以节省您的时间。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2021-06-16
    • 2020-10-04
    • 1970-01-01
    • 2020-04-29
    • 2017-04-08
    • 2020-02-04
    • 2020-02-12
    • 2019-01-10
    • 2019-10-01
    相关资源
    最近更新 更多