ImportError: No module named caffe
我滴天,这咖啡真不好喝!
我感觉我为了解决这个问题,都能写一篇论文了,整整搞了两天,是真麻烦,所以,我建议,如果能换成不用caffe的代码的话,尽量别用caffe跑了,太难了~
首先,网上推荐的是用编译的方式进行安装配置caffe。具体如下:
conda create -n caffe_env python=2.7.18
conda activate caffe_env
pip install pip -U
pip install numpy
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev
sudo apt install libatlas-base-dev
sudo apt install libgflags-dev libgoogle-glog-dev liblmdb-dev
下载 caffe
解压 unzip caffe-master.zip
进入caffe根目录:
复制Makefile.config.bak为Makefile.config
修改Makefile.config和Makefile文件。
Makefile.confg参考如下:
USE_CUDNN := 1
OPENCV_VERSION := 3
CUDA_DIR := /usr/local/cuda
# 如果cuda版本大于等于9.0,则注释以下两行:
# -gencode arch=compute_20,code=sm_20 \
# -gencode arch=compute_20,code=sm_21
CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
BLAS := atlas
ANACONDA_HOME := $(HOME)/conda/envs/py3_caffe_source
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.6m \
$(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
PYTHON_LIB := $(ANACONDA_HOME)/lib
WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
TEST_GPUID := 0
Q ?= @
Makefile内容主要修改以下两项:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
PYTHON_LIBRARIES ?= boost_python3 python3.6m
make all -j8
make test -j8
make pycaffe -j8
# 如果报错 需重新编译 make clean
cp -rf caffe/ ~/miniconda3/envs/py3_caffe_source/lib/python3.6/
cp -rf ../.build_release/lib/* ~/miniconda3/envs/py3_caffe_source/lib/
进入conda虚拟环境安装caffe依赖的Python库
pip3 install numpy
pip3 install scikit-image
pip3 install protobuf
大概流程就这样,具体报错就网上搜吧。
conda create -n caffe_env python=3.6.10
conda activate caffe_env
conda install -c defaults caffe-gpu
(caffe_env) user@user-Ubuntu:~/caffe_env$ python
Python 3.6.10 |Anaconda, Inc.| (default, May 5 2021, 11:02:1)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>> exit()
如果不报错,就成了,我完成时成了,挣了两天的活,最后,一句命令搞定了,既开心又生气。
反正希望能帮到你们,能解决问题最好,每个人的情况不一样,我建议首先试试第2步的大杀器,太棒了。