caffe import error: even after installing it successfully, on ubuntu 18.04
caffe import error: even after installing it successfully, on ubuntu 18.04
即使在使用命令 sudo apt install caffe-cpu
成功安装后,我仍然收到 caffe 导入错误。我能够在 /usr/lib/python3/dist-packages/caffe
找到 caffe 文件(路径已添加到 PYTHONPATH)。 caffe 目录的 requirements.txt 文件中提到的所有要求也已安装。
我正在使用 Ubuntu 18.04 LTS,Python3。
谁能帮我解决这个错误?
import caffe
Traceback (most recent call last):
File "6_reconstruct_alphabet_image.py", line 17, in <module>
import caffe
File "/usr/lib/python3/dist-packages/caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/usr/lib/python3/dist-packages/caffe/pycaffe.py", line 15, in <module>
import caffe.io
File "/usr/lib/python3/dist-packages/caffe/io.py", line 2, in <module>
import skimage.io
File "/usr/lib/python3/dist-packages/skimage/__init__.py", line 158, in <module>
from .util.dtype import *
File "/usr/lib/python3/dist-packages/skimage/util/__init__.py", line 7, in <module>
from .arraycrop import crop
File "/usr/lib/python3/dist-packages/skimage/util/arraycrop.py", line 8, in <module>
from numpy.lib.arraypad import _validate_lengths
ImportError: cannot import name '_validate_lengths'
问题已解决: 出现错误是因为 caffe 构建没有成功完成。我建议不要使用 sudo apt install caffe-cpu
命令(在 Ubuntu 的官方 caffe 安装指南中提到);因为它最终会出现上述错误。最好从源码安装
让我逐步指导如何在 Ubuntu 18.04 LTS 中成功安装 caffe:
1] sudo apt-get install -y --no-install-recommends libboost-all-dev
2] sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev \ libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
3] git clone https://github.com/BVLC/caffe
cd caffe
cp Makefile.config.example Makefile.config
4] sudo pip install scikit-image protobuf
cd python
for req in $(cat requirements.txt); do sudo pip install $req; done
5]修改Makefile.config文件:
取消注释行 CPU_ONLY := 1
和行 OPENCV_VERSION := 3
。
6]在Makefile中找到LIBRARIES
行,改成如下:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5 \
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
7] make all
现在你可能会遇到这样的错误:
CXX src/caffe/net.cpp
src/caffe/net.cpp:8:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:575: recipe for target '.build_release/src/caffe/net.o' failed
make: *** [.build_release/src/caffe/net.o] Error 1
要解决此错误,请执行步骤 8。
8] install libhdf5-dev
打开 Makefile.config,找到包含 LIBRARY_DIRS
的行并追加 /usr/lib /x86_64-linux-gnu/hdf5/serial
找到 INCLUDE_DIRS
并附加 /usr/include/hdf5/serial/
(根据这个 SO answer)
重新运行 make all
9]make test
10]make runtest
11] make pycaffe
现在你可能会遇到这样的错误:
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
compilation terminated.
Makefile:501: recipe for target 'python/caffe/_caffe.so' failed
make: *** [python/caffe/_caffe.so] Error 1
要解决此错误,请执行步骤 12。
12] 在 Makefile.config 中找到 PYTHON_INCLUDE
行,并进行如下更改:
`PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/dist-packages/numpy/core/include`
13] 通过将此行添加到 ~/.bashrc 文件的末尾,将模块目录添加到我们的 $PYTHONPATH 中:
`sudo vim ~/.bashrc`
`export PYTHONPATH=$HOME/Downloads/caffe/python:$PYTHONPATH`
`source ~/.bashrc'
14] 完成。
如果有任何旧版本的软件包,就会发生这种情况。特别是,根据 Traceback,主要原因可能是旧版本的 scikit-image 包。只需使用命令升级包即可。
对于 Python 2.7
: pip install --upgrade scikit-image
和
对于 Python 3.x
: pip3 install --upgrade scikit-image
在 ubuntu 18.04 上以 cpu 模式安装 Caffe。
conda update conda
conda create -n testcaffe python=3.5
source activate testcaffe
conda install -c menpo opencv3
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev protobuf-compiler
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y --no-install-recommends libboost-all-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev
mkdir build
cd build
make -j8 l8
make all
make install
conda install cython scikit-image ipython h5py nose pandas protobuf pyyaml jupyter
sed -i -e 's/python-dateutil>=1.4,<2/python-dateutil>=2.0/g' requirements.txt
for req in $(cat requirements.txt); do pip install $req; done
cd ../build
cd ../python
export PYTHONPATH=pwd${PYTHONPATH:+:${PYTHONPATH}}
python -c "import caffe;print(caffe.version)"
jupyter notebook
只需要运行这些命令就可以完成,Caffe就会安装到系统中了。
即使在使用命令 sudo apt install caffe-cpu
成功安装后,我仍然收到 caffe 导入错误。我能够在 /usr/lib/python3/dist-packages/caffe
找到 caffe 文件(路径已添加到 PYTHONPATH)。 caffe 目录的 requirements.txt 文件中提到的所有要求也已安装。
我正在使用 Ubuntu 18.04 LTS,Python3。
谁能帮我解决这个错误?
import caffe
Traceback (most recent call last):
File "6_reconstruct_alphabet_image.py", line 17, in <module>
import caffe
File "/usr/lib/python3/dist-packages/caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/usr/lib/python3/dist-packages/caffe/pycaffe.py", line 15, in <module>
import caffe.io
File "/usr/lib/python3/dist-packages/caffe/io.py", line 2, in <module>
import skimage.io
File "/usr/lib/python3/dist-packages/skimage/__init__.py", line 158, in <module>
from .util.dtype import *
File "/usr/lib/python3/dist-packages/skimage/util/__init__.py", line 7, in <module>
from .arraycrop import crop
File "/usr/lib/python3/dist-packages/skimage/util/arraycrop.py", line 8, in <module>
from numpy.lib.arraypad import _validate_lengths
ImportError: cannot import name '_validate_lengths'
问题已解决: 出现错误是因为 caffe 构建没有成功完成。我建议不要使用 sudo apt install caffe-cpu
命令(在 Ubuntu 的官方 caffe 安装指南中提到);因为它最终会出现上述错误。最好从源码安装
让我逐步指导如何在 Ubuntu 18.04 LTS 中成功安装 caffe:
1] sudo apt-get install -y --no-install-recommends libboost-all-dev
2] sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev \ libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
3] git clone https://github.com/BVLC/caffe
cd caffe
cp Makefile.config.example Makefile.config
4] sudo pip install scikit-image protobuf
cd python
for req in $(cat requirements.txt); do sudo pip install $req; done
5]修改Makefile.config文件:
取消注释行 CPU_ONLY := 1
和行 OPENCV_VERSION := 3
。
6]在Makefile中找到LIBRARIES
行,改成如下:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5 \
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
7] make all
现在你可能会遇到这样的错误:
CXX src/caffe/net.cpp
src/caffe/net.cpp:8:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:575: recipe for target '.build_release/src/caffe/net.o' failed
make: *** [.build_release/src/caffe/net.o] Error 1
要解决此错误,请执行步骤 8。
8] install libhdf5-dev
打开 Makefile.config,找到包含 LIBRARY_DIRS
的行并追加 /usr/lib /x86_64-linux-gnu/hdf5/serial
找到 INCLUDE_DIRS
并附加 /usr/include/hdf5/serial/
(根据这个 SO answer)
重新运行 make all
9]make test
10]make runtest
11] make pycaffe
现在你可能会遇到这样的错误:
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
compilation terminated.
Makefile:501: recipe for target 'python/caffe/_caffe.so' failed
make: *** [python/caffe/_caffe.so] Error 1
要解决此错误,请执行步骤 12。
12] 在 Makefile.config 中找到 PYTHON_INCLUDE
行,并进行如下更改:
`PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/dist-packages/numpy/core/include`
13] 通过将此行添加到 ~/.bashrc 文件的末尾,将模块目录添加到我们的 $PYTHONPATH 中:
`sudo vim ~/.bashrc`
`export PYTHONPATH=$HOME/Downloads/caffe/python:$PYTHONPATH`
`source ~/.bashrc'
14] 完成。
如果有任何旧版本的软件包,就会发生这种情况。特别是,根据 Traceback,主要原因可能是旧版本的 scikit-image 包。只需使用命令升级包即可。
对于 Python 2.7
: pip install --upgrade scikit-image
和
对于 Python 3.x
: pip3 install --upgrade scikit-image
在 ubuntu 18.04 上以 cpu 模式安装 Caffe。
conda update conda
conda create -n testcaffe python=3.5
source activate testcaffe
conda install -c menpo opencv3
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev protobuf-compiler
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y --no-install-recommends libboost-all-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev
mkdir build
cd build
make -j8 l8
make all
make install
conda install cython scikit-image ipython h5py nose pandas protobuf pyyaml jupyter
sed -i -e 's/python-dateutil>=1.4,<2/python-dateutil>=2.0/g' requirements.txt
for req in $(cat requirements.txt); do pip install $req; done
cd ../build
cd ../python
export PYTHONPATH=pwd${PYTHONPATH:+:${PYTHONPATH}}
python -c "import caffe;print(caffe.version)"
jupyter notebook
只需要运行这些命令就可以完成,Caffe就会安装到系统中了。