简体   繁体   中英

caffe import error: even after installing it successfully, on ubuntu 18.04

I'm getting caffe import error even after installing it successfully using the command sudo apt install caffe-cpu . I was able to find caffe file at /usr/lib/python3/dist-packages/caffe (Path was added to PYTHONPATH). All requirements mentioned in the requirements.txt file of caffe directory was also installed.

I'm using Ubuntu 18.04 LTS, Python3.

Could anyone help me with this error?

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'

Problem solved: The error came up because caffe build wasn't done successfully. I recommend not to go up with the sudo apt install caffe-cpu command (Which is mentioned in the official caffe installation guide for Ubuntu); because it will end up in the error as above. It's better to install from the source.

Let me give step by step guidance to install caffe successfully in Ubuntu 18.04 LTS:

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 for req in $(cat requirements.txt); do sudo pip install $req; done

5] Modify the Makefile.config file: Uncomment the line CPU_ONLY := 1 , and the line OPENCV_VERSION := 3 .

6] Find LIBRARIES line in Makefile and change it to as follows:

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5 \\ opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

7] make all

Now you could get some error like this:

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

To solve this error follow step 8.

8] install libhdf5-dev open Makefile.config, locate line containing LIBRARY_DIRS and append /usr/lib /x86_64-linux-gnu/hdf5/serial locate INCLUDE_DIRS and append /usr/include/hdf5/serial/ (per this SO answer) rerun make all

9] make test

10] make runtest

11] make pycaffe

Now you could get some error like this:

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

To solve this error follow step 12.

12] Find PYTHON_INCLUDE line in Makefile.config and do the changes as follows:

`PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/dist-packages/numpy/core/include`

13] Add the module directory to our $PYTHONPATH by adding this line to the end of ~/.bashrc file:

`sudo vim ~/.bashrc`
`export PYTHONPATH=$HOME/Downloads/caffe/python:$PYTHONPATH`
`source ~/.bashrc'

14] Done.

This can be happened if there is any older version of the packages. Particularly, according to the Traceback, main reason can be the older version of scikit-image package. Just upgrade the package using the command.

for Python 2.7 : pip install --upgrade scikit-image

and

for Python 3.x : pip3 install --upgrade scikit-image

Installation of Caffe on ubuntu 18.04 in cpu mode .

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

Complete steps just run these command as it is, Caffe will be installed in the system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM