电脑配置
- 系统:Ubuntu14.04
- GPU:NVIDIA GTX1080
-
安装caffe过程 参考博客:http://blog.csdn.net/xuzhongxiong/article/details/52717285 和http://blog.csdn.net/u012177034/article/details/52102676
1.安装相关依赖项
- 1
- 2
- 1
- 2
- 3
- 4
(4)测试CUDA的sammples
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuerymakesudo ./deviceQuery
如果现实一些关于GPU的信息,则说明安装成功。- 1
- 2
4.配置cuDNN
cuDNN是GPU加速计算深层神经网络的库。 首先去官网(https://developer.nvidia.com/rdp/cudnn-download)下载cuDNN,可能需要注册一个账号才能下载。由于本人的显卡是GTX1080,所以下载版本号如下图:
图4.cuDNN下载 下载cuDNN5.1之后进行解压,cd进入cuDNN5.1解压之后的include目录,在命令行进行如下操作:
sudo cp cudnn.h /usr/local/cuda/include/ #复制头文件
再将cd进入lib64目录下的动态文件进行复制和链接:
sudo cp lib* /usr/local/cuda/lib64/ #复制动态链接库
cd /usr/local/cuda/lib64/sudo rm -rf libcudnn.so libcudnn.so.5 #删除原有动态文件
sudo ln -s libcudnn.so.5.0.5 libcudnn.so.5 #生成软衔接
sudo ln -s libcudnn.so.5 libcudnn.so #生成软链接5.安装opencv3.1
使用完上面的命令后,依赖已经安装完毕,但是由于Ubuntu 14.04版本的原因,导致opencv相关的环境不能够正常的work。所以,我重新编译了一个OpenCV,版本为3.1.0。
在解压后的目录中执行:
bigtop@bigtop-SdcOS-Hypervisor:~/tools/opencv-3.1.0$ cmake -DBUILD_TIFF=ON
然后执行make 和make install
以上是我亲自试过的。。
从官网(http://opencv.org/downloads.html)下载OpenCV,并将其解压到你要安装的位置,假设解压到了/home/opencv。
安装前准备,创建编译文件夹:
cd ~/opencv
mkdir build
cd build
配置:
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
编译:
make -j8 #-j8表示并行计算,根据自己电脑的配置进行设置,配置比较低的电脑可以将数字改小或不使用,直接输make。
以上只是将opencv编译成功,还没将opencv安装,需要运行下面指令进行安装:
sudo make install
可能会出现的错误:
错误内容1:
gcc-4.9: error trying to exec 'cc1plus': execvp:
没有那个文件或目录
说明gcc与g++版本不兼容,解决办法跟gcc版本太高时一样:
安装低版本gcc与g++:
sudo apt-get install gcc-4.9 g++-4.9
之后进入/usr/bin:
cd /usr/bin
先删除和gcc5.0关联的gcc:
sudo rm gcc
sudo rm g++
再建个软连接
sudo ln -s gcc-4.9 gcc
sudo ln -s g++-4.9 g++
错误内容2:
modules/cudalegacy/src/graphcuts.cpp:120:54: error:
‘NppiGraphcutState’ has not been declared
typedef NppStatus (*init_func_t)(NppiSize oSize,
NppiGraphcutState** ppState, Npp8u* pDeviceMem);- 1
这是因为opecv3.0与cuda8.0不兼容导致的。解决办法:
修改 ~/opencv/modules/cudalegacy/src/graphcuts.cpp文件内容,如图:
图5.文件修改6.配置caffe
(1)将终端cd到要安装caffe的位置。
(2)从github上获取caffe:
git clone https://github.com/BVLC/caffe.git
注意:若没有安装Git,需要先安装Git:
sudo apt-get install git
(3)因为make指令只能make Makefile.config文件,而Makefile.config.example是caffe给出的makefile例子,因此,首先将Makefile.config.example的内容复制到Makefile.config:
sudo cp Makefile.config.example Makefile.config
(4)打开并修改配置文件:
sudo gedit Makefile.config #打开Makefile.config文件
根据个人情况修改文件:
a.若使用cudnn,则
将
#USE_CUDNN := 1
修改成:
USE_CUDNN := 1
b.若使用的opencv版本是3的,则
将
#OPENCV_VERSION := 3
修改为:
OPENCV_VERSION := 3- 1
- 1
- 2
- 1
- 1
- 2
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
c.若要使用Python来编写layer,则
1配置python layers
#In your Makefile.config, make sure to have this line uncommented WITH_PYTHON_LAYER := 1 # Unrelatedly, it's also recommended that you use CUDNN USE_CUDNN := 1
- 1
- 2
- 3
- 4
2安装几个依赖
cython, python-opencv, easydict
- 1
sudo apt-get install python-opencv sudo pip install cython easydict
安装依赖库:
编译:
$ cd ~/caffe
$ make pycaffe
添加~/caffe/python到$PYTHONPATH:$ sudo gedit /etc/profile
- 1
- 1
# 添加: export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
$ source /etc/profile # 使之生效
测试是否可以引用: cd caffe/python$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>>- 1
- 1
- 2
- 3
在此过程中,可能会出现各种和python相关的包缺失问题,这里记录下,以便查询:
A》将caffe-fast-rcnn/python目录下的requirements下的依赖都装一遍:
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/caffe-fast-rcnn/python$ cat requirements.txt Cython>=0.19.2 numpy>=1.7.1 scipy>=0.13.2 scikit-image>=0.9.3 matplotlib>=1.3.1 ipython>=3.0.0 h5py>=2.2.0 leveldb>=0.191 networkx>=1.8.1 nose>=1.3.0 pandas>=0.12.0 python-dateutil>=1.4,<2 protobuf>=2.5.0 python-gflags>=2.0 pyyaml>=3.10 Pillow>=2.3.0 six>=1.1.0
执行如下命令:
for req in $(cat requirements.txt); do pip install $req; done
这里有一个小技巧,因为pip这个工具对应的网络非常的烂:
这个时候,可以将其改为国内的镜像网站,速度将提升几个数量级,方法如下:
新建~/.pip/pip.confg文件,内容如下:
[global] index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
或者在安装一个软件包的时候使用-i选项:
在我安装requirements.txt中涉及的依赖包的过程中,发现matplotlib始终没有安装成功,最后采用apt-get的方式进行了安装,如下:
sudo apt-get install python-matplotlib
d. 重要的一项 :
将# Whatever else you find you need goes here.下面的- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
这是因为ubuntu14.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,所以需要更改这一路径.
(5)修改makefile文件
打开makefile文件,做如下修改:- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
(6)编辑/usr/local/cuda/include/host_config.h
将其中的第115行注释掉:- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
(7)编译
- 1
- 1
- 1
编译过程中可能会出现如下错误:
错误内容1:- 1
- 1
- 1
解决办法:
step1:在Makefile.config文件的第85行,添加/usr/include/hdf5/serial/ 到 INCLUDE_DIRS,也就是把下面第一行代码改为第二行代码。- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
stept2:在Makefile文件的第173行,把 hdf5_hl 和hdf5修改为hdf5_serial_hl 和 hdf5_serial,也就是把下面第一行代码改为第二行代码。
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
错误内容2:
- 1
- 1
- 1
解决办法是将一些文件复制到/usr/local/lib文件夹下:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
(8)测试
- 1
图6.caffe测试结果
到此caffe配置完毕!MNIST数据集测试
配置caffe完成后,我们可以利用MNIST数据集对caffe进行测试,过程如下:
1.将终端定位到Caffe根目录2.下载MNIST数据库并解压缩
3.将其转换成Lmdb数据库格式
4.训练网络
训练的时候可以看到损失与精度数值,如下图:
图7.MNIST数据集训练
可以看到最终训练精度是0.9914。
faster-rcnn提出论文: 《Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks》
faster-rcnn 的算法详解可看这篇博文(清晰易懂,良心博文!):http://blog.csdn.net/shenxiaolu1984/article/details/51152614
faster-rcnn Python版本源码地址:https://github.com/rbgirshick/py-faster-rcnn
这篇文章主要介绍搭建用faster-rcnn进行目标检测所需的环境。参考博客地址:http://blog.csdn.net/u011070171/article/details/52399317
1.配置caffe所需的环境
上面已经配置完caffe环境:依赖库 修改Makeconfig,opencv3.1,python
2.下载faster-rcnn python版本源码
- git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
3. 进入/py-faster-rcnn/lib 进行编译
- cd py-faster-rcnn/lib
- make
4.编译/py-faster-rcnn/caffe-fast-rcnn
- cd py-faster-rcnn/caffe-fast-rcnn
- cp Makefile.config.example Makefile.config
更改Makefile.config文件:
- # In your Makefile.config, make sure to have this line uncommented
- WITH_PYTHON_LAYER := 1
- # Unrelatedly, it's also recommended that you use CUDNN
- USE_CUDNN := 1
进行编译:
- make -j8 && make pycaffe
因为这个版本所用的cudnn为旧版本的,可能与新环境的cudnn不兼容,导致出现如下错误:
- In file included from ./include/caffe/util/cudnn.hpp:5:0,
- from ./include/caffe/util/device_alternate.hpp:40,
- from ./include/caffe/common.hpp:19,
- from ./include/caffe/util/db.hpp:6,
- from src/caffe/util/db.cpp:1:
- /usr/local/cuda/include/cudnn.h:803:27: note: declared here
- cudnnStatus_t CUDNNWINAPI cudnnSetPooling2dDescriptor(
- ^
- make: *** [.build_release/src/caffe/util/db.o] Error 1
- make: *** Waiting for unfinished jobs....
解决办法:
1).将/py-faster-rcnn/caffe-fast-rcnn/include/caffe/util/cudnn.hpp 换成最新版的caffe里的cudnn的实现,即相应的cudnn.hpp.
2).将/py-faster-rcnn/caffe-fast-rcnn/src/caffe/layer里的,所有以cudnn开头的文件,例如cudnn_lrn_layer.cu,cudnn_pooling_layer.cpp,cudnn_sigmoid_layer.cu。
都替换成最新版的caffe里的相应的同名文件。5.运行faster-rcnn里的demo
- cd py-faster-rcnn/tools
- ./tools/demo.py
如果还有问题
faster rcnn 代码默认是使用的cudnn v4, 但是为了体验最新的v5, 或者使用GTX1080 ,我们编译faster rcnn的时候就会报错:In file included from ./include/caffe/util/cudnn.hpp:5:0,
from ./include/caffe/util/device_alternate.hpp:40,
from ./include/caffe/common.hpp:19,
from src/caffe/data_reader.cpp:6:
/usr/local/cuda/include/cudnn.h:799:27: note: declared here
cudnnStatus_t CUDNNWINAPI cudnnSetPooling2dDescriptor(
为此提供2种解决方案:
(1)取自github @manipopopo
cd caffe-fast-rcnn Git remote add caffe https://github.com/BVLC/caffe.git git fetch caffe git merge caffe/master Remove self_.attr("phase") = static_cast<int>(this->phase_); from include/caffe/layers/python_layer.hpp after merging.
(2)手动修改文件,参考了卜居大神的博客 http://blog.csdn.net/kkk584520/article/details/51163564
方案1简单方便,但是当我们编译的是其他人修改过得源码,可能就会出错。方案2 步骤如下:
1. 用最新caffe源码的以下文件替换掉faster rcnn 的对应文件
include/caffe/layers/cudnn_relu_layer.hpp, src/caffe/layers/cudnn_relu_layer.cpp, src/caffe/layers/cudnn_relu_layer.cu
include/caffe/layers/cudnn_sigmoid_layer.hpp, src/caffe/layers/cudnn_sigmoid_layer.cpp, src/caffe/layers/cudnn_sigmoid_layer.cu
include/caffe/layers/cudnn_tanh_layer.hpp, src/caffe/layers/cudnn_tanh_layer.cpp, src/caffe/layers/cudnn_tanh_layer.cu
2. 用caffe源码中的这个文件替换掉faster rcnn 对应文件
include/caffe/util/cudnn.hpp
3. 将 faster rcnn 中的 src/caffe/layers/cudnn_conv_layer.cu 文件中的所有
cudnnConvolutionBackwardData_v3 函数名替换为 cudnnConvolutionBackwardData
cudnnConvolutionBackwardFilter_v3函数名替换为 cudnnConvolutionBackwardFilter
问题二:opencv环境和caffe-fast-rcnn默认的Makefile配置有点小问题,cv::imread(cv:: String const&, int)找不到:
解决方案:
在一切都正常的情况下,对caffe-fast-rcnn进行make和make pycaffe的结果如下:
编译好caffe-fast-rcnn后,在py-faster-rcnn/lib中执行make命令:
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/lib$ make python setup.py build_ext --inplace running build_ext skipping 'utils/bbox.c' Cython extension (up-to-date) skipping 'nms/cpu_nms.c' Cython extension (up-to-date) skipping 'pycocotools/_mask.c' Cython extension (up-to-date) rm -rf build bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/lib$
No module named skimage.io:
问题四:下面这个问题是因为缺少,easydict,使用 sudo pip install easydict可以解决:
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/tools$ python demo.py --cpu Traceback (most recent call last): File "demo.py", line 17, in <module> from fast_rcnn.config import cfg File "/home/bigtop/py-faster-rcnn/tools/../lib/fast_rcnn/config.py", line 23, in <module> from easydict import EasyDict as edict ImportError: No module named easydict bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/tools$ sudo pip install easydict Downloading/unpacking easydict Downloading easydict-1.6.zip Running setup.py (path:/tmp/pip_build_root/easydict/setup.py) egg_info for package easydict Installing collected packages: easydict Running setup.py install for easydict Could not find .egg-info directory in install record for easydict Successfully installed easydict Cleaning up... sudo pip install easydict
这个问题是因为scipy安装出现问题,将其删掉:rm -fr /tmp/pip_build_root/scipy/,然后重新安装可以解决:
d --compile failed with error code 1 in /tmp/pip_build_root/scipy Traceback (most recent call last): File "/usr/bin/pip", line 9, in <module> load_entry_point('pip==1.5.4', 'console_scripts', 'pip')() File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 235, in main return command.main(cmd_args) File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 161, in main text = '\n'.join(complete_log) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 98: ordinal not in range(128)
报下面这个错误,是因为caffe的环境都没有准备好,很有可能是没有执行make pycaffe:
Traceback (most recent call last): File "detector.py", line 29, in <module> import caffe File "/python/caffe/__init__.py", line 1, in <module> from .pycaffe import Net File "/caffe/pycaffe.py", line 6, in <module> from ._caffe import CaffeNet ImportError: No module named _caffe
error: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'
error: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'
这个就是上文中提到的,使用ubuntu自带的opencv库会出现的问题,解决办法就是重新编译opencv。
问题八:ImportError: No module named yaml
problem : I got the "ImportError: No module named yaml " error when I attempted to install NLTK toolkit to my ubuntu system.
solution:
yaml library for python seems to not be installed on your system.
On ubuntu system just do a :
sudo apt-get install python-yaml
On windows
easy_install python-yaml
easy_install rosinstall