【问题标题】:While running TensorFlow's classify_image, getting AttributeError: 'module' object has no attribute 'gfile'运行 TensorFlow 的分类图像时,出现 AttributeError: 'module' object has no attribute 'gfile'
【发布时间】:2016-02-15 06:24:44
【问题描述】:

在 imagenet 模型中运行 TensorFlow 提供的示例文件时出现此错误,

File "classify_image.py", line 154, in run_inference_on_image
if not tf.gfile.Exists(image):

AttributeError: 'module' 对象没有属性 'gfile'

我也尝试在虚拟服务器上同时使用 pip 和源代码进行安装,但仍然出现此错误。

【问题讨论】:

  • 这个 gfile 东西是 6 天前在这个提交中添加的——github.com/tensorflow/tensorflow/commit/…,可能值得在 github 上提交一个问题。作为一种解决方法,您可以在本地将 gfile 内容撤消回 os.path,它只有几行

标签: tensorflow


【解决方案1】:

有两种方法可以解决这个问题:

选项 1

这是对上述 Yaroslav 评论的扩展,比选项 2 更容易。

修改classify_image.py如下:

tf.gfile.Exists 的所有实例替换为os.path.exists,并且 将tf.gfile.GFiletf.gfile.FastGFile 的所有实例替换为open

然后运行修改后的classify_image.py,它应该可以工作了。

选项 2

将 tensorflow 更新到包含 gfile 的最新版本,如 here 所述

但是,在你这样做之后,当你尝试运行classify_image.py 时,你可能会遇到以下错误:

$ python classify_image.py
>> Downloading inception-2015-12-05.tgz 100.0%
Succesfully downloaded inception-2015-12-05.tgz 88931400 bytes.
[libprotobuf ERROR google/protobuf/src/google/protobuf/io/coded_stream.cc:207] A protocol message was rejected because it was too big (more than 67108864 bytes).  To increase the limit (or to disable these warnings), see CodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.
Traceback (most recent call last):
  File "classify_image.py", line 213, in <module>
    tf.app.run()
  File "/Users/USERNAME/.virtualenvs/mlnd/lib/python2.7/site-packages/tensorflow/python/platform/default/_app.py", line 30, in run
    sys.exit(main(sys.argv))
  File "classify_image.py", line 209, in main
    run_inference_on_image(image)
  File "classify_image.py", line 159, in run_inference_on_image
    create_graph()
  File "classify_image.py", line 141, in create_graph
    graph_def.ParseFromString(f.read())
google.protobuf.message.DecodeError: Error parsing message

要解决此问题,您可以按照here 所述更改源代码中的一行,然后在您的机器上重新编译 tensorflow。

选项 2 可能有点工作,尤其是如果您使用的是 Mac。

【讨论】:

    【解决方案2】:

    以下步骤帮助我解决了问题

    1. 按照此处https://www.tensorflow.org/versions/r0.7/get_started/os_setup.html 的描述更新张量流

    2.对我来说,这导致了 protobuf 错误。为了解决我跑了

    $sudo pip 卸载 protobuf

    $sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.7.1-cp27-none-any.whl

    【讨论】:

      猜你喜欢
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 2015-12-13
      • 2022-12-01
      • 2012-02-29
      • 2022-12-01
      相关资源
      最近更新 更多