即使使用命令sudo apt install caffe-cpu
成功安装了caffe,也出现了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'
答案 0 :(得分:1)
问题已解决:由于未成功完成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/
(根据此答案)
重新运行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]完成。
答案 1 :(得分:0)
如果有任何较旧版本的软件包,可能会发生这种情况。特别是,根据Traceback,主要原因可能是scikit-image软件包的旧版本。只需使用命令升级软件包。
Python 2.7
:pip install --upgrade scikit-image
和
Python 3.x
:pip3 install --upgrade scikit-image
答案 2 :(得分:0)
以cpu模式在ubuntu 18.04上安装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安装在系统中。