属性错误:模块“tensorflow”没有“gfile”属性。

22

我使用Google ColabTensorFlow 2.0训练了一个简单的mnist模型,并以.json格式保存它。点击这里查看我编写代码的Colab笔记本。然后在运行命令:

!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

非常感谢 :)


1
我在TF 2中使用FullTokenizer(vocab_file, do_lower_case)时遇到了相同的问题。 - bachr
5个回答

44

在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的其他命令中有任何错误吗?我想知道更改这个会不会引起其他问题。 - Aaron John Sabu
1
在2.0版本中,tf.gfile.* 被 tf.io.gfile.* 替代。因此,tf.io.gfile.GFile 总是正确的。 - JiangKui

26
简单的Tensorflow Serving并不支持Tensorflow 2.0,因为它使用了旧的API。 在Tensorflow 2.0中,gfile包已经移动到tf.io中。 因此,如果要使用Simple Tensorflow Serving,您需要将Tensorflow降级到TF 1.13。

你如何降级它? - 0xTomato
3
使用pip安装TensorFlow版本1.13。 - nessuno
1
如果有人遇到“ERROR: Could not find a version that satisfies the requirement tensorflow==1.13”的问题,请使用以下命令进行安装:pip install tensorflow==1.13.1 - inyang kpongette

4

相反,尝试使用

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

即 ".io" 可以解决您所有的问题,而不是降级您的 tf。

0

tf.gfile.GFile 更改为 tf.io.gfile.GFile

这适用于此实例,但并不适用于所有其他文件

例如,tf.io.gfile.FastGFile 抛出错误说

AttributeError: module 'tensorflow._api.v2.io.gfile' has no attribute 'FastGFile'

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Someone_who_likes_SE

0

在TensorFlow 2.0中,此功能已被弃用。 您可以使用旧版本的tensorflow(如1.13.1)来使用此API。

在我的旧项目中,我需要使用此API 在anaconda中创建了一个虚拟环境,并使用了该版本:

conda create -n tf python=3.7 tensorflow=1.13.1 
// here more modules with specific version can be added

conda activate tf // to activate environment tf

(base) D:\ff\testM>  --> (tf) D:\ff\testM> 
// After this environment changed to (base) --> (tf)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接