从0开始到安装openCV均可参考
https://blog.csdn.net/pengchengliu/article/details/90749483 这篇博客
本文主要对caffe编译,配置,以及python接口的成功调用做补充
一、安装Nvidia显卡驱动
1.到官网上下载自己GPU对应版本的显卡驱动。
下载地址:https://www.nvidia.cn/Download/index.aspx?lang=cn
我下载的是NVIDIA-Linux-x86_64-418.43.run,有101M。
下载完为:
放入到/home/lpc/Downloads/下面。
2.屏蔽自带的显卡驱动
1) vim /etc/modprobe.d/blacklist.conf
2) 在最后一行加上:blacklist nouveau ,这里是将Ubuntu自带的显卡驱动加入黑名单
3) 在终端输入:sudo update-initramfs –u,使修改生效
4) 重启电脑
重启电脑之后,还是能进入图像界面,但是分辨率已经很低了:
1)关闭X-Window,很简单:
sudo service lightdm stop
2)Ctrl + Alt + F1进入命令行界面:
3)输入root用户名密码
4)cd /home/lpc/Downloads/
sudo chmod +x NVIDIA-Linux-x86_64-430.14.run
sudo ./NVIDIA-Linux-x86_64-430.14.run(根据自己的安装包修改命令)
sudo service lightdm start
发现分辨率高了很多,非常清晰了。输入
nvidi-smi
参考了:https://www.cnblogs.com/xinyf/p/5960480.html
二、安装CUDA
1.下载CUDA
下载地址:https://developer.nvidia.com/cuda-downloads
下载完为:
保存到/home/lpc/Downloads/下
2.安装
sudo ./cuda_10.1.168_418.67_linux.run
sudo vim ~/.bashrc
source ~/.bashrc
3.测试是否安装成功(这步我跳过了没有测试)
cd /usr/local/cuda/samples/1_Utilities/deviceQuery
make
./deviceQuery
1.下载
网址:https://developer.nvidia.com/rdp/cudnn-download
下载后为:
有400多兆。
放入到/home/lpc/Downloads/下。
2.安装
tar -xvf cudnn-10.1-linux-x64-v7.6.0.64.tgz
解压后为一个目录cuda,里面有一个include文件夹和lib64文件夹
sudo cp include/cudnn.h /usr/local/cuda/include/
sudo cp lib64/lib* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
因为我的/usr/local/duda/lib64本来也没有libcudnn.so和libcudnn.so.7等文件,所以不需要增加软连接。如果之前有的话还需要新建文件,增加软连接。
我没有测,如果想测的话可以参考:
https://blog.csdn.net/caicaiatnbu/article/details/87626491
我没试。
解决/libcudnn.so.7 is not a symbolic link 不是符号连接
查到了这个问题的解决方法:
1.check the link:
sudo ldconfig -v
libcudnn.so.7是一个文件,它本应是一个软连接。于是创建软接连:
sudo ln -sf /usr/local/cuda-10.0/targets/x86_64-linux/lib/libcudnn.so.7.4.2 /usr/local/cuda-10.0/targets/x86_64-linux/lib/libcudnn.so.7
四、安装anaconda(可以不用装且最好不用装)
如果使用单一环境,建议不要装anaconda。装了之后,后面编译Caffe的时候可能会报奇怪的错误。我这次装了,后面编译也通过了。
但是在装Ubuntu14.04的时候就没通过,然后卸载掉anaconda再装才好了。
下载anaconda:
https://www.anaconda.com/distribution/
下载后为:
有652M
放到/home/lpc/Downloads下
sudo ./Anaconda3-2018.12-Linux-x86_64.sh
sudo vim ~/.bashrc
source ~/.bashrc
8)验证
五、安装python
1 解压python
tar -xvf Python-3.7.0.tar.xz
cd Python-3.7.0
2 生成项目文件(Makefile)
./configure -h 查看所有支持的配置熟悉,附在文件的结尾处
我们使用默认参数,只设置如下参数。
./configure --enable-shared
其中enable-shared是编译so动态库,默认不编译
安装路径不用设置,使用默认路径会安装到系统路径,如果设置,需要在设置运行的环境变量
3 编译
make -j8 #其中j8表示8线程编译,多核cpu可以加快编译速度
4 安装
make install #安装编译好的文件
如果设置的默认路径,会安装如下文件和目录
/usr/bin/python3 ##执行文件
/usr/local/bin/python3.7 #执行文件
/usr/local/lib/libpython3.7m.so #动态库
/usr/local/lib/python3.7/ #目录下放的是python的扩展库
/usr/local/include/python3.7m #源码头文件
如果有的系统/usr/local/lib不是默认的库加载路径,那就把所有的.so文件复制到/usr/lib目录下
5 若步骤2执行./configure --prefix=/usr/local/python3.7.1,则需要添加环境变量。步骤2是./configure的跳过此步骤
添加环境变量
PATH=$PATH:$HOME/bin:/usr/local/python3.7.1/bin
6 查看安装目录
可以看到此时python3.7安装到了/usr/local/lib/(若步骤2执行./configure --prefix=/usr/local/python3.7.1,python3.7安装到了/usr/local/python3.7.1/lib/)
7 测试,输入python3
第二部分 更新python默认指向为python3.7
(引自https://blog.csdn.net/u014775723/article/details/85213793)
1 查看python的指向。
ls -l /usr/bin | grep python
2 若如步骤1的图,若要安装python3.4则,由于python3.4为系统自带的,直接使用以下命令并跳过步骤3:
删除原有链接
rm /usr/bin/python
建立新链接
ln -s /usr/bin/python3.4 /usr/bin/python
步骤3:由于python3.7是自己安装的,不在/usr/bin下,而在usr/local/bin或者/usr/local/python3.7.1/bin下(取决于前面执行的./configure还是./configure --prefix=/usr/local/python3.7.1。因此需要先加一条软链接并且把之前的python命令改为python.bak,同时pip也需要更改。依次执行以下命令
若python3.7安装时,执行的是./configure,则:
mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/bin/python3 /usr/bin/python
mv /usr/bin/pip /usr/bin/pip.bak
ln -s /usr/local/bin/pip3 /usr/bin/pip
若python3.7安装时,执行的是./configure --prefix=/usr/local/python3.7.1,则为:
mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/python3.7.1/bin/python3.7 /usr/bin/python
mv /usr/bin/pip /usr/bin/pip.bak
ln -s /usr/local/python3.7.1/bin/pip3 /usr/bin/pip
将/usr/local/lib/python3.7/config-3.7m-x86_64-linux-gnu下的libpython3.7m.a建立软连接
sudo ln -s libpython3.7m.a /usr/lib/x86_64-linux-gnu/libpython3.7m.a
踩坑:
1.运行出现
error while loading shared libraries: libpython3.7m.so.1.0: cannot open shared object file: No such file or directory
解决措施:
默认安装路径的情况下,.so文件在/usr/local/lib下
cp libpython3.6m.so.1.0 /usr/local/lib64/
cp libpython3.6m.so.1.0 /usr/lib/
cp libpython3.6m.so.1.0 /usr/lib64/
六、安装openCV(不安装不影响Caffe的编译)
1.下载
网址:https://pypi.org/project/opencv-python/#files
因为安装的python是3.7的,所以opencv名字中要是"cp37"的。
想要安装opencv3,所以名字中要为opencv_python-3.****
我的系统是linux 64位的的,所以名字要是****linux1_x86_64**
最终下载了这一个:
有24.2M
放到/home/lpc/Downloads下。
2.安装
pip install opencv_python-4.0.0.21-cp37-cp37m-manylinux1_x86_64.whl
如果还不放心,可以编写一个例子试试:
python test.py
这样有可能会有问题
输入以下命令查看 当前电脑opencv版本号
pkg-config --modversion opencv
如不是3.x,则重新编译(出自https://www.cnblogs.com/uestc-mm/p/7338244.html)
开始安装Opencv For Python:
1、找到下载的Opencv3.3的目录:
2、执行下面的指令(在执行指令的过程之前,我们可以切换到su超级权限下进行操作,也可以不切换到su权限下,切换到那个用户,以后就只能在那个用户下调用对应的cv2的图像处理库):
unzip opencv-3.2.0.zip #解压
sudo cp ./opencv-3.2.0 /home #将文件复制到home目录下
sudo mv opencv-3.2.0 opencv #更名
//创建编译文件夹
cd opencv
mkdir build#创建build文件夹
cd build
//配置
sudo apt install cmake
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
//编译
make -j16 #如果机器性能不太好,可以直接make
//编译成功后,开始安装opencv
sudo make install
安装好的opencv版本查看
pkg-config --modversion opencv
安装过程之中碰到的问题:
“FATAL: In-source builds are not allowed.
You should create separate directory for build files.”
意思就是说不要在源代码内部直接编译程序,以免污染源代码,所以在opencv-2.4.13目录之外建立文件夹my_build_dir,然后在这个目录里面执行cmake。
详细问题参照下面的链接:
https://stackoverflow.com/questions/45518317/in-source-builds-are-not-allowed-in-cmake
Ubuntu下卸载重装Opencv的教程参考:
http://www.cnblogs.com/txg198955/p/5990295.html
注:安装之后可能遇到的问题
Question-1:
简单的意思就是调用了window相关的程序,但是没有安装GTK相关的包,函数function不能实现,需要安装libgtk2.0-dev 和 pkg-config。
如果没有使用到opencv的显示接口的话,基本的opencv的函数是能够使用的,在通过numpy的相关的数学运算包转化一下数据格式就可以使用matlibplot等包显示输出图像了。
如果想解决上述的问题,就一定记得安装libgtk2.0-dev和pkg-config之后在cmake —》make —》 make install.Opencv就行了。
Question-2:
我们发现在安装libgtk2.0-dev的软件包的时候,很难安装成功,全是提示说安装包和其他的依赖的安装包存在版本不兼容的问题,如下图所示:
# apt-get install libgtk2.0-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
libgtk2.0-dev: Depends: libgtk2.0-0 (= 2.20.0-0ubuntu4) but 2.20.1-0ubuntu2.1 is to be installed
Depends: libglib2.0-dev (>= 2.21.3) but it is not going to be installed
Depends: libpango1.0-dev (>= 1.20) but it is not going to be installed
Depends: libatk1.0-dev (>= 1.13.0) but it is not going to be installed
Depends: libcairo2-dev (>= 1.6.4-6.1) but it is not going to be installed
Depends: libx11-dev (>= 2:1.0.0-6) but it is not going to be installed
Depends: libxext-dev (>= 1:1.0.1-2) but it is not going to be installed
Depends: libxinerama-dev (>= 1:1.0.1-4.1) but it is not going to be installed
Depends: libxi-dev (>= 1:1.0.1-4) but it is not going to be installed
Depends: libxrandr-dev (>= 1:1.2.99) but it is not going to be installed
Depends: libxcursor-dev but it is not going to be installed
Depends: libxfixes-dev (>= 1:3.0.0-3) but it is not going to be installed
Depends: libxcomposite-dev (>= 1:0.2.0-3) but it is not going to be installed
Depends: libxdamage-dev (>= 1:1.0.1-3) but it is not going to be installed
E: Broken packages
将较新的库降级为满足依赖关系的库,按照提示逐步操作即可。
这里我们就需要使用如下的指令来完成安装:
sudo aptitude install libgtk2.0-dev
运行后会出现一大堆消息,给出第一个解决方案一般是保留这些库文件,让选择y/n/q?,这里选择“n”;然后会继续找解决方案,有时会超时找不到,点击y继续找,直到给出得解决方案是“降级”这些库文件,选择“y”,然后等待结束就完了。
完成后可以再输入“sudo apt-get install libgtk2.0-dev”进行测试,出现已经安装对应得库文件,说明安装成功了。(此方法同样适用于安装opencv时其他不能正常安装的预备库文件)
Question3
make: 警告:检测到时钟错误。您的创建可能是不完整的
解决办法:
用touch 命令把源程序的时间戳改一下
find . -type f -exec touch {} \;
make clean
make
1.下载caffe
git clone https://github.com/BVLC/caffe.git
下载完之后是一个目录caffe
2. 安装依赖项
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 liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
3.安装python的一些包
另外,为了支持python,还要装一些python的包。
进入到caffe/python/下,查看requirements.txt
python-dateutil可以>2
装的时候检查检查,尤其是用anaconda的时候,大部分都已经装了。不用再重装。
cd caffe
cp Makefile.config.example Makefile.config
vim Makefile.config
(1)将USE_CUDNN注释去掉。表示我们使用cuDNN
(2)将OPENCV_VERSION注释去掉。表示我们使用opencv3版本。
(3)若要使用python来编写layer,则将WITH_PYTHON_LAYER注释去掉。
(4) 将INCLUDE_DIRS以及LIBRARY_DIRS后面增加几个目录
(5) 如安装了anaconda则按照下图进行配置修改
如没有,则注释掉ANACONDA_HOME,以及与conda有关的条目,选择没有ANACONDA的选项(注释掉ANACONDA_HOME,PYTHON_INCLUDE,取消ANACONDA一栏之下的python3的PYTHON_INCLUDE的注释,注释含有ANACONDA_HONE的PYTHON_LIB的一行,取消没有的一行注释)
(6)
(7)
修改为:
vim Makefile
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
改为
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
5.编译
sudo make all -j8
-j8是指用8个线程
如果编译失败则需要
sudo make clean
sudo make runtest -j8
出现以下结果说明成功。
有可能会出现以下情况。解决方案在后面
坑1:Ubuntu 16.04下安装Caffe解决 undefined symbol: _ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE(一般向上面一样配置是不会出现该错误的)
解决方案:
Google该错误,发现原因是boost_python的版本不匹配,默认python版本为3.5,而boost_python为2.7。
解决方法:
先在系统的/usr/lib/x86_64-linux-gnu/路径下查找boost_python文件的版本是否与python版本匹配,我的电脑中文件名为:
libboost_python-py35.so.1.58.0
说明版本为python3.5,匹配。若不匹配则安装对应的boost_python版本。
接下来打开Caffe文件夹下的Makefile文件,将第208行的
PYTHON_LIBRARIES := boost_python python2.7
改为
PYTHON_LIBRARIES := boost_python-py35 python3.7m #py35需要改为你自己电脑x86_64-linux-gnu路径下可以查到的python的最高版本,如py34,py33,python3.7m为你系统现在的python版本
之后重新编译文件
make clean
sudo ldconfig
make caffe
make all
make test
/usr/lib/x86_64-linux-gnu
中找到libboost_python-py3.5.so
, 则
sudo ln -s libboost_python-py3.5.so libboost_python3.so
即可使其被Caffe找到.
八、编译pycaffe
前面已经安装了库了。
make pycaffe -j8
这样就编译成功了。
vim ~/.bashrc
source ~/.bashrc
然后,python
import caffe看看能不能成功。
我在import的时候遇到了以下问题:
解决方案 :
重新安装protobuf
protobuf网址重新下载protobuf
首先安装工具:
sudo apt-get install autoconf automake libtool
sudo apt-get install libffi-dev
sudo apt-get install curl
#解压进入目录
./autogen.sh
./configure --prefix=/usr/local/protobuf
make
make check
sudo make install
然后加环境变量,vim /etc/profile:
export PATH=$PATH:/usr/local/protobuf/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib
在protobuf-2.6.1的文件夹内,有python文件夹的README.txt里面写了怎么安装python的支持。
1.终端定位到protobuf-2.6.1/python中,然后输入:
python setup.py build
安装完之后,就可以了。
如果继续输入
python setup.py google_test
2.如果到此处,只有当终端在protobuf-2.6.1/python目录内时,python中import caffe才不会报错。当终端在其他文件夹内时,import caffe依旧会出错。在终端中输入如下命令:
export PYTHONPATH=/home/XXX/protobuf-2.6.1/python:$PYTHONPATH
之后输入:
source ~/.bashrc
假如出现找不到动态库的错误
protoc: error while loading shared libraries: libprotobuf.so.7: cannot open shared object file: No such file or directory
问题描述
PROTOC src/caffe/proto/caffe.proto
CXX .build_release/src/caffe/proto/caffe.pb.cc
In file included from /usr/include/c++/5/mutex:35:0,
from /usr/local/include/google/protobuf/stubs/mutex.h:33,
from /usr/local/include/google/protobuf/stubs/common.h:52,
from .build_release/src/caffe/proto/caffe.pb.h:9,
from .build_release/src/caffe/proto/caffe.pb.cc:4:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support
^
In file included from /usr/local/include/google/protobuf/stubs/common.h:52:0,
from .build_release/src/caffe/proto/caffe.pb.h:9,
from .build_release/src/caffe/proto/caffe.pb.cc:4:
/usr/local/include/google/protobuf/stubs/mutex.h:58:8: error: ‘mutex’ in namespace ‘std’ does not name a type
std::mutex mu_;
^
/usr/local/include/google/protobuf/stubs/mutex.h: In member function ‘void google::protobuf::internal::WrappedMutex::Lock()’:
/usr/local/include/google/protobuf/stubs/mutex.h:51:17: error: ‘mu_’ was not declared in this scope
void Lock() { mu_.lock(); }
^
/usr/local/include/google/protobuf/stubs/mutex.h: In member function ‘void google::protobuf::internal::WrappedMutex::Unlock()’:
/usr/local/include/google/protobuf/stubs/mutex.h:52:19: error: ‘mu_’ was not declared in this scope
void Unlock() { mu_.unlock(); }
^
/usr/local/include/google/protobuf/stubs/mutex.h: At global scope:
/usr/local/include/google/protobuf/stubs/mutex.h:61:7: error: expected nested-name-specifier before ‘Mutex’
using Mutex = WrappedMutex;
^
/usr/local/include/google/protobuf/stubs/mutex.h:66:28: error: expected ‘)’ before ‘*’ token
explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
^
/usr/local/include/google/protobuf/stubs/mutex.h:69:3: error: ‘Mutex’ does not name a type
Mutex const mu_;
^
/usr/local/include/google/protobuf/stubs/mutex.h: In destructor ‘google::protobuf::internal::MutexLock::~MutexLock()’:
/usr/local/include/google/protobuf/stubs/mutex.h:67:24: error: ‘class google::protobuf::internal::MutexLock’ has no member named ‘mu_’
~MutexLock() { this->mu_->Unlock(); }
^
/usr/local/include/google/protobuf/stubs/mutex.h: At global scope:
/usr/local/include/google/protobuf/stubs/mutex.h:80:33: error: expected ‘)’ before ‘’ token
explicit MutexLockMaybe(Mutex *mu) :
^
In file included from /usr/local/include/google/protobuf/arena.h:48:0,
from .build_release/src/caffe/proto/caffe.pb.h:23,
from .build_release/src/caffe/proto/caffe.pb.cc:4:
/usr/include/c++/5/typeinfo:39:37: error: expected ‘}’ before end of line
/usr/include/c++/5/typeinfo:39:37: error: expected unqualified-id before end of line
/usr/include/c++/5/typeinfo:39:37: error: expected ‘}’ before end of line
/usr/include/c++/5/typeinfo:39:37: error: expected ‘}’ before end of line
/usr/include/c++/5/typeinfo:39:37: error: expected ‘}’ before end of line
/usr/include/c++/5/typeinfo:39:37: error: expected declaration before end of line
Makefile:588: recipe for target '.build_release/src/caffe/proto/caffe.pb.o' failed
make: *** [.build_release/src/caffe/proto/caffe.pb.o] Error 1
Tried solutions
add 'CXXFLAGs += -std=c++11' in Makefile didn't solve it
System configuration
Operating system: ubuntu 16.04
Compiler:
CUDA version (if applicable): 9.0
CUDNN version (if applicable): 7.0.4
Python version (if using pycaffe): tried both 2.7 and 3.6
解决方案
https://github.com/BVLC/caffe/issues/6359
修改 CMakeList.txt
# ---[ Flags
if(UNIX OR APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall")
endif()if()
Modify
# ---[ Flags
if(UNIX OR APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -std=c++11")
endif()if()
同时修改makefile
CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS) -std=c++11
LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11
参考引用:
https://www.cnblogs.com/shaobin0604/archive/2011/11/01/2232144.html