在运行TensorFlow的classify_image时,获取AttributeError:'module'对象没有属性'gfile'

n00*_*00b 1 tensorflow

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

File "classify_image.py", line 154, in run_inference_on_image
if not tf.gfile.Exists(image):
Run Code Online (Sandbox Code Playgroud)

AttributeError:'模块'对象没有属性'gfile'

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

jji*_*ing 9

解决此问题有两种方法:

选项1

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

修改classify_image.py如下:

替换的所有实例tf.gfile.Exists来os.path.exists,和替换的所有实例tf.gfile.GFile,并tf.gfile.FastGFile以open

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

选项2

更新tensorflow到最新版本,其中包括gfile描述在这里

但是,这样做之后,尝试运行时可能会遇到以下错误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
Run Code Online (Sandbox Code Playgroud)

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

选项2可能需要一些工作,尤其是在Mac上。