While running TensorFlow's classify_image, getting AttributeError: 'module' object has no attribute 'gfile'

While running TensorFlow's classify_image, getting AttributeError: 'module' object has no attribute 'gfile'

我在 运行 在 imagenet 模型中使用 TensorFlow 给出的示例文件时遇到此错误,

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

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

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

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

选项 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。

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

  1. 按照此处所述更新 tensorflow https://www.tensorflow.org/versions/r0.7/get_started/os_setup.html

2.For 我这导致了 protobuf 错误。解决我运行

$sudo pip uninstall protobuf

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