本文主要是介绍Ubuntu配置caffe,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Ubuntu是以桌为主面应用的Linux发行版。本人使用Ubuntu 16.04.3版本。可通过以下命令查看本机Ubuntu的内核版本和发行版本号。
cat /etc/issue
输出如下:
Ubuntu 16.04.3 LTS \n \l
本人显卡:TITAN Xp
NVIDIA Titan Xp显卡驱动和CUDA安装
本人安装CUDA Toolkit 9.0版本。下载链接:https://developer.nvidia.com/cuda-90-download-archive
具体选择如下:
整个机器只安装Ubuntu16.04.3系统后,需要先将Ubuntu默认安装并使用的nouveau集成显卡驱动禁用掉。
(1)改变blacklist.conf文件的属性,使之可编辑
sudo chmod 666 /etc/modprobe.d/blacklist.conf
(2)用gedit软件打开,用vi也可以
sudo gedit /etc/modprobe.d/blacklist.conf
(3)在文件末尾加入以下几句话
blacklist vga16fb
blacklist nouveau
blacklist rivafb
blacklist rivatv
blacklist nvidiafb
(4)保存
(5)按下ctrl+alt+F1进入控制台模式
sudo service lightdm stop
(6)输入以下命令,使得(1)到(4)步的配置生效,这一步很关键,否则安装显卡驱动时会一直提示目前运行的是nouveau驱动
sudo update-initramfs -u
(7)重启,按下ctrl+alt+F1进入控制台模式
(8)安装显卡驱动
sudo service lightdm stop #关掉界面
sudo sh cuda_9.1.85_387.26_linux.run
(9)sudo service lightdm start进入图形界面,界面恢复正常表示驱动安装成功
(10)设置环境变量
终端中输入 $ sudo gedit /etc/profile
在打开的文件末尾,添加以下两行。
$ export PATH=/usr/local/cuda-7.5/bin:$PATH
$ export LD_LIBRARY_PATH=/usr/local/cuda7.5/lib64
保存文件。
安装CuDNN
cuDNN是GPU加速计算深层神经网络的库。下载版本:cudnn-9.0-linux-x64-v7.tgz。下载地址:https://developer.nvidia.com/rdp/cudnn-download
解压:
tar -zxvf cudnn-9.0-linux-x64-v7.tgz
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
cd /usr/local/cuda/lib64/
ls
sudo chmod +r libcudnn.so.7.0.5(具体的版本要自己查)
sudo rm -rf libcudnn.so libcudnn.so.7 #删除原有动态文件
sudo ln -s libcudnn.so.7.0.5 libcudnn.so.7 #生成软衔接(注意这里要和自己下载的cudnn版本对应,可以在/usr/local/cuda/lib64下查看自己libcudnn的版本)
sudo ln -s libcudnn.so.7 libcudnn.so #生成软链接
sudo ldconfig -v
Caffe安装
1、安装caffe所需要的依赖库
sudo apt-get install python-pipsudo pip install easydict protobuf pydot sudo apt-get install graphviz libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler libopenblas-dev liblapack-dev libatlas-base-dev libgflags-dev libgoogle-glog-dev liblmdb-dev python-tk python-numpy python-scipy python-matplotlibpython-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflagssudo apt-get install --no-install-recommends libboost-all-dev
2、下载caffe
如果没有安装git,则sudo apt-get install git进行安装
sudo git clone https://github.com/BVLC/caffe.git
3、配置Caffe文件
OPENCV_VERSION := 3
4、编译Caffe
5、检查编译是否成功
import caffe
没有错误提示表示成功。
错误和解决方案
1..build_release/src/caffe/proto/caffe.pb.h:9:42: fatal error: google/protobuf/stubs/common.h: 没有那个文件或目录 CXX .build_release/src/caffe/proto/caffe.pb.cc
In file included from .build_release/src/caffe/proto/caffe.pb.cc:5:0:
.build_release/src/caffe/proto/caffe.pb.h:9:42: fatal error: google/protobuf/stubs/common.h: 没有那个文件或目录
#include <google/protobuf/stubs/common.h>
^
compilation terminated.
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
没有安装依赖导致:
sudo apt-get install git 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 libatlas-base-dev sudo apt-get install python-dev sudo apt-get insall libgflags-dev libgoogle-glog-dev liblmdb-dev
nvcc fatal : Unsupported gpu architecture 'compute_20'
Makefile:594: recipe for target '.build_release/cuda/src/caffe/util/im2col.o' failed
make: *** [.build_release/cuda/src/caffe/util/im2col.o] Error 1
异常原因是 caffe的makefile.config中有cuda版本限制,设置的参数有可能cuda不支持。打开makefile.config
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
-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
修改的方法是,注释掉部分内容,如下:
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
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
#-gencode arch=compute_20,code=sm_20 \
#-gencode arch=compute_20,code=sm_21 \
3、ImportError: No module named skimage.io
pip install scikit-image
4、ImportError:"No module named google.protobuf.internal"
pip install protobuf
5、No module named caffe
>>>import caffe
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named caffe
sudo gedit ~/.bashrc
添加:
- export PYTHONPATH=~/caffe/python:$PYTHONPATH
- source ~/.bashrc
一些小问题
ubuntu没有权限
创建文件夹:在终端直接运行 sudo nautilus ,弹出来的nautilus可以直接GUI操作,中途别关终端。
Ubuntu解压:点击打开链接
笔记:
cuda(CUDA Toolkit目录)安装位置:/usr/local/targets/x86_64-linux/
这篇关于Ubuntu配置caffe的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!