【问题标题】:module 'caffe' has no attribute 'set_mode_gpu'模块“caffe”没有属性“set_mode_gpu”
【发布时间】:2018-10-18 17:09:23
【问题描述】:

我已经从源代码安装了 caffe。我已经使用 Cmake 进行安装。我也更新了各自的路径。 我的 caffe 根目录是:/home/ashj/caffe 我已将 PYTHON 路径更新为: export PYTHONPATH=<caffe-home>/python:$PYTHONPATH 这是通过使用 **export PYTHONPATH=/home/ashj/caffe/python:$PYTHONPATH**

我可以加载导入模块 caffe。但是,我无法访问 caffe 中的任何方法或任何层,例如 set_mode_gpu()、set_mode_cpu() 或层或参数。我收到如下错误:

我用的时候

导入咖啡

caffe.set_mode_gpu()

我收到以下错误:

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: module 'caffe' has no attribute 'set_mode_gpu'

PS:我也尝试过使用 this link. 中提到的 caffe.__caffe.set_mode_gpu() 但它对我不起作用。 我的系统规格:Ubuntu 18.04

TIA

【问题讨论】:

  • 你也有make pycaffemake distribute吗?
  • 我做了'make pycaffe'但我没有做'make distribution'。 'make pycaffe' 没有抛出任何错误。

标签: python caffe pycaffe


【解决方案1】:

虽然可能会迟到,但我遇到了同样的问题并找到了解决方法:

sys.path.insert(0, '/path/to/caffe/python')
import caffe
caffe.set_mode_gpu()

即在import caffe之前添加caffe/pathon路径sys.path
写了post here详细分析,希望有帮助。


这个问题可能是包caffe的路径导致的。

对我来说,如果我从 Ubuntu 终端执行以下操作,一切都会正常:

但如果我从 Pycharm IDE 中执行,则会发生错误:

注意,我用两种方式测试了包caffe的路径,得到了不同的结果:
- 在 Ubuntu 终端中,即顺利的方式,我得到了

'/home/CVAR-B/softwares/caffe/caffe/python/caffe/__init__.pyc'

这是预期的结果; - 在Pycharm IDE方式中,即错误发生的方式,我得到了

'/usr/local/lib/python2.7/dist-packages/caffe/__init__.pyc'

这不是预期的结果。

鉴于这个发现,我又做了一件事情来处理错误:

sys.path.insert(0, '/path/to/caffe/python')
import caffe
caffe.set_mode_gpu()

即,在import caffe 之前添加sys.pathcaffe/pathon 路径。

结果表明这可能是一种解决方法:
(来源:ax2x.com

查看caffe.__file__ 的结果,现在返回预期的路径。

【讨论】:

    【解决方案2】:

    尝试这些步骤,然后设置您的 python PATH: 您可能已经完成了第 1 步和第 3 步。

    make all 
    make pycaffe
    make distribute
    mkdir ~/python 
    mv distribute/python/caffe ~/python
    

    在此之后设置您的 PYTHONPATH — 这应该是一些像 caffe/python/caffe 这样的目录

    【讨论】:

    • 我又做了第 1 步和第 2 步。但是,第 3 步返回,错误如下:make: *** No rule to make target 'distribute'. Stop.
    • 嗯。我假设你在运行make pycaffe 之前在Makefile.config 中设置了你的python 路径。你能仔细检查一下吗?
    • 感谢您的回复 LeKhan。但是我正在使用下面这个链接中给出的 cmake 步骤,因此在运行“make pycaffe”之前没有设置 python 路径。 caffe.berkeleyvision.org/installation.html#compilation
    • 嗯,不熟悉cmake的步骤。但是,我仍然会确保在 requirements.txt 中安装依赖项。我还会仔细检查您的 Python 路径在 Makefile 中是否正常。
    • 终于可以在conda中使用虚拟环境安装caffe了。
    最近更新 更多