TK1之caffe配置 -05
五 – cudnn安装
5.1 安装老的cudnn-6.5, 2.0版本 (推荐)
5.2 卸载老版本cudnn
5.3 安装新cudnn-7.0, 4.0版本 (不建议)
5.4 回到老的2.0版本的cudnn-6.5以配合cuda-6.5
六 – caffe安装
6.1 准备Caffe环境
6.2 下载
6.3 config
6.4 build
6.5 runtest的CUDNN_STATUS_NOT_INITIALIZED出错
6.6 LMDB_MAP_SIZE出错
6.7 跑机 benchmarking
七 – Python and/or MATLAB
7.1 Ubuntu方式 (ok)
7.2 pip方式 (not tested)
7.3 编译python接口
7.4 环境变量
五 – cudnn安装
如果要安装caffe, 需要先安装cudnn。
对于32bit的arm-tk1,适合配置是cuda6.5 + cudnn2.0 + caffe0.13,bvlc官网1.0的msater分支不能用。
对于32bit的mini-pc, 可以尝试cuda6.5 + cudnn4.0 + cafe1.00, cuda6.5是支持32bit的维一选择。
5.1 安装老的cudnn-6.5, 2.0版本 (推荐)
下载 V2:
https://developer.nvidia.com/rdp/cudnn-archive
$ tar -zxvf cudnn-6.5-linux-ARMv7-V1.tgz
$ cd cudnn-6.5-linux-ARMv7-V2
复制文件
$ sudo cp cudnn.h /usr/local/cuda/include
$ sudo cp libcudnn* /usr/local/cuda/lib
重新加载库:
$ sudo ldconfig -v
make caffe时出错。 尝试新的cudnn4.0。原因可能caffe是新版本,所以要新版本cudn持,而和r1和v2版本部分函数名称改变。
5.2 卸载老版本cudnn
$ sudo rm /usr/local/cuda/include/cudnn.h
$ sudo rm /usr/local/cuda/lib/libcudnn*
5.3 安装新cudnn-7.0, 4.0版本 (不建议)
下载: https://developer.nvidia.com/rdp/cudnn-archive
新的4.0: cuDNN v4 Library for L4T (ARMv7)版cudnn-7.0-Linux-ARMv7-v4.0-prod.tgz
$ tar -zxvf cudnn-7.0-linux-ARMv7-v4.0-prod.tgz
产生cuda文件夹
$ cd ~/Downloads/cuda/
复制cuDNN文件:
$ sudo cp include/cudnn.h /usr/local/cuda/include/
$ sudo cp lib/libcudnn* /usr/local/cuda/lib/
$ sudo ldconfig -v
重新加载库:
$ sudo ldconfig -v
在32bit的arm体系的TK1上make runtest不成功。应该是因为github的caffe太新了。
5.4 回到老的2.0版本的cudnn-6.5以配合cuda-6.5
$ sudo rm /usr/local/cuda/include/cudnn.h
$ sudo rm /usr/local/cuda/lib/libcudnn*
# cd 6.5 cudnn v2
$ sudo cp cudnn.h /usr/local/cuda/include
$ sudo cp libcudnn* /usr/local/cuda/lib
$ sudo ldconfig -v
六 – caffe安装
6.1 准备Caffe环境
$ sudo add-apt-repository universe
$ sudo apt-get update
$ sudo apt-get install libprotobuf-dev protobuf-compiler 、
$ sudo apt-get install cmake libleveldb-dev libsnappy-dev
$ sudo apt-get install libatlas-base-dev libhdf5-serial-dev libgflags-dev
在python接口的scipy库的时候,需要fortran编译器(gfortran),如果没有报错,因此可以先安装:
$ sudo apt-get install gfortran
安装下面可能需要梯子:
$ sudo apt-get install libgoogle-glog-dev liblmdb-dev
boost这一步视情况而定。有的说boost的版本会有问题,建议降低版本到1.55版本的。因为caffe官网给的是 $ sudo apt-get install –no-install-recommends libboost-all-dev , 特意加了个–no-install-recommends。而且installation主页特意有Boost>=1.55要求。
$ sudo apt-get install libboost-dev libboost-thread-dev libboost-system-dev
但是上面这个网上常见的安装在ubuntu14.04会默认安装1.54。所以如果装了的话卸载, 完了后用这个来安装:
检查下:
$ dpkg -S /usr/include/boost/version.hpp
— 1.5.4
卸载掉:
$ sudo apt-get autoremove libboost1.54-dev 这个autoremove一下卸载了上百个包,free出 980M space, 杀伤力太大。
再安装1.55:
$ sudo apt-get install libboost1.55-all-dev libboost-thread1.55-dev libboost-system1.55-dev libboost-filesystem1.55-dev
编译器这步骤视情况而定。系统自带shi 4.8,如果出错,随时可以降级为4.7,如果编译caffe时出现编译器错误则降低版本:
$ sudo apt-get install gcc-4.7 g++-4.7 cpp-4.7
$ cd /usr/bin
$ sudo rm gcc g++ cpp
$ sudo ln -s gcc-4.9 gcc
$ sudo ln -s g++-4.9 g++
$ sudo ln -s cpp-4.9 cpp
赋予访问权限:
$ sudo usermod -a -G video $USER
all fine!
6.2 下载
# Git clone Caffe
$ git clone https://github.com/BVLC/caffe.git
$ cd caffe
$ git checkout dev
$ cp Makefile.config.example Makefile.config
6.3 config
$ vi Makefile.config
使能USE_CUDNN := 1
查看CUDA计算容量
$ /usr/local/cuda/samples/1_Utilities/deviceQuery
— 3.2
在caffe的Makefile.config文件中,禁止CUDA_ARCH *的60和61
CUDA_ARCH := -gencode arch=compute_20,code=sm_20
-gencode arch=compute_20,code=sm_21
-gencode arch=compute_21,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_50,code=compute_50
如果 make caffe时有
Error: nvcc fatal : Unsupported gpu architecture 'compute_60'
$ vi Makefile.config.
# 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.
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
6.4 build
(只要出错, 按照下面顺序来过: make clean -> make all -j4 -> make test -j4 ->make runtest -j4 )
$ make clean ok.
$ make -j 4 all 20 mins ok
$ make test -j4 5 minute ok
$ make runtest -j4 10 mins
出问题的基本都是runtest:
Q1
常见错误是sudo, 运行runtest测试不需要sudo。
Q2
Error
g++: internal compiler error: Killed (program cc1plus)
解决方法:重启即可,原因不明(有说是内存问题)
6.5 runtest的CUDNN_STATUS_NOT_INITIALIZED出错
Note: Randomizing tests’ orders with a seed of 86430 .
F1222 00:00:55.561334 15196 cudnn_softmax_layer.cpp:15] Check failed: status == CUDNN_STATUS_SUCCESS (1 vs. 0) CUDNN_STATUS_NOT_INITIALIZED
*** Check failure stack trace: ***
This indicates a CuDNN/CUDA error. I’ve seen this when the CUDA/driver version and the CuDNN version are mismatched (5.0.5 and CUDA 7.0). This is almost certainly something to do with your setup, and not a bug in Caffe.
I encountered the same issue and finally found out that there is a mysterious
so you mean in order for this to compile successfully, we need to compile Caffe without the support of CUDNN right?
Thank you verymuch . I really appreciate your help 🙂
Q1
有的说需要降低版本:
$ cd /usr/bin
$ sudo rm gcc g++ cpp
$ sudo ln -s gcc-4.9 gcc
$ sudo ln -s g++-4.9 g++
$ sudo ln -s cpp-4.9 cpp
Q2
有的说是boost的版本问题,建议降低版本到1.55版本的。因为caffe官网给的是 $ sudo apt-get install –no-install-recommends libboost-all-dev
特意加了个–no-install-recommends。而且installation主页有Boost>=1.55,可以抵触的是上面这命令默认装的是1.54。所以卸载完了后用这个安装:$ sudo apt-get install libboost1.55-all-dev , 估计没事了吧。
校验:
$ dpkg -S /usr/include/boost/version.hpp
— 1.5.4
卸载:
$ sudo apt-get autoremove libboost1.54-dev 没必要吧,估计不是?
这个autoremove以下卸载了上百个包,free出 980M space, 杀伤力太大。
安装:
$ sudo apt-get install libboost1.55-all-dev
有小故障:
cannot find -l boost_system boost_filesystem boost_thread
$ apt-cache search libboost | grep 1.55
$ sudo apt-get install libboost-system1.55-dev
$ sudo apt-get install libboost-filesystem1.55-dev
$ sudo apt-get install libboost-thread1.55-dev
CUDNN_STATUS_NOT_INITIALIZED
Q3 原因和方法
在$ git checkout dev的时候就有提示,该分支不存在, 没注意。
but not the dev branch, even dev branch does not exist now,
所以从github下载的是master分支, 已经太新了, 在tk1能用的dev分支已经不见了。。
目前这个master branch的caffe,最低是要求cuDNN 5.0以上+ CUDA 7.0以上。
而我们的是cudnn2.0 + cuda6.5
cuDNN v5.1 has different versions for CUDA 7.5 and CUDA 8.0
cuDNN v5 has different versions for CUDA 7.5 and CUDA 8.0
cuDNN v4 and v3 both require CUDA 7.0
cuDNN v2 and v1 both require CUDA 6.5
可以从这里克隆cudnn 2.x能用的caffe :
https://github.com/RadekSimkanic/caffe-for-cudnn-v2.5.48
还有一个works for CUDNN v2 on Jetson TK1的caffe: https://github.com/platotek/caffetk1
不一定惯用
$ make runtest -j4
Major revision number: 3
Minor revision number: 2
Name: GK20A
Total global memory: 1980252160
Total shared memory per block: 49152
Total registers per block: 32768
Warp size: 32
Maximum memory pitch: 2147483647
Maximum threads per block: 1024
Maximum dimension 0 of block: 1024
Maximum dimension 1 of block: 1024
Maximum dimension 2 of block: 64
Maximum dimension 0 of grid: 2147483647
Maximum dimension 1 of grid: 65535
Maximum dimension 2 of grid: 65535
Clock rate: 852000
Total constant memory: 65536
Texture alignment: 512
Concurrent copy and execution: Yes
Number of multiprocessors: 1
Kernel execution timeout: No
Unified virtual addressing: Yes
虽然还有小错LMDB_MAP_SIZE, 不过容易解决。
6.6 LMDB_MAP_SIZE出错
FAILED:
F1222 07:03:16.822439 16826 db_lmdb.hpp:14] Check failed: mdb_status == 0 (-30792 vs. 0) MDB_MAP_FULL: Environment mapsize limit reached
这个是32bit的限制太小了, 1T改为半T。
I think this issue is due to the Jetson being a 32-bit (ARM) device, and the constant LMDB_MAP_SIZE in src/caffe/util/db.cpp being too big for it to understand. Unfortunately master has a really large value for LMDB_MAP_SIZE in src/caffe/util/db.cpp, which confuses our little 32-bit ARM processor on the Jetson, eventually leading to Caffe tests failing with errors like MDB_MAP_FULL: Environment mapsize limit reached.
$ vi src/caffe/util/db_lmdb.cpp
const size_t LMDB_MAP_SIZE = 1099511627776; // 1 TB
改为 2^29 (536870912)
$ vi ./examples/mnist/convert_mnist_data.cpp
adjust the value from 1099511627776 to 536870912.
$ make runtest -j 4
… … …
[==========] 1702 tests from 251 test cases ran. (5165779 ms total)
[ PASSED ] 1702 tests.
YOU HAVE 2 DISABLED TESTS
OKAY !
6.7 跑机 benchmarking
最后,运行一下Caffe的基准代码来检测一下性能。验证cpu和gpu下运行效率:
Finally you can run Caffe’s benchmarking code to measure performance.
* 这个cpu的大概600秒
$ build/tools/caffe time –model=models/bvlc_alexnet/deploy.prototxt
… …
I1222 09:11:54.935829 19824 caffe.cpp:366] Average Forward pass: 5738.58 ms.
I1222 09:11:54.935860 19824 caffe.cpp:368] Average Backward pass: 5506.83 ms.
I1222 09:11:54.935890 19824 caffe.cpp:370] Average Forward-Backward: 11246.2 ms.
I1222 09:11:54.935921 19824 caffe.cpp:372] Total Time: 562310 ms.
I1222 09:11:54.935952 19824 caffe.cpp:373] *** Benchmark ends ***
ok.
These results are the summation of 10 iterations, so per image recognition on the Average Forward Pass is the listed result divided by 10, i.e. 227.156 ms is ~23 ms per image recognition.
* 这个gpu的大概30秒
$ build/tools/caffe time –model=models/bvlc_alexnet/deploy.prototxt –gpu=0
… …
I1222 09:16:02.577358 19857 caffe.cpp:366] Average Forward pass: 278.286 ms.
I1222 09:16:02.577504 19857 caffe.cpp:368] Average Backward pass: 318.795 ms.
I1222 09:16:02.577637 19857 caffe.cpp:370] Average Forward-Backward: 599.67 ms.
I1222 09:16:02.577800 19857 caffe.cpp:372] Total Time: 29983.5 ms.
I1222 09:16:02.577951 19857 caffe.cpp:373] *** Benchmark ends ***
ok.
It’s running 50 iterations of the recognition pipeline, and each one is analyzing 10 different crops of the input image, so look at the ‘Average Forward pass’ time and divide by 10 to get the timing per recognition result.
此后可以测试demo。 Caffe两个demo分别是mnist和cifar10,尤其是mnist,称为caffe编程的hello world。
七 – Python and/or MATLAB
编译caffe之后, 才可以对付pycaffe接口的编译。
Caffe拥有pythonC++shell接口,在Caffe使用python特别方便,在实例中都有接口的说明。
安装步骤是: Python依赖包、Matlab、Matlab engine for python
但即使有默认的python,还是要安装python-dev,why?
因为linux发行版通常会把类库的头文件和相关的pkg-config分拆成一个单独的xxx-dev(el)包。 以python为例,有些场合的应用是需要python-dev的, 例如:
要自己安装一个源外的python类库, 而这个类库内含需要编译的调用python api的c/c++文件;
自己写的一个程序编译需要链接libpythonXX.(a|so)。
在UBUNTU系统下使用python,很多时候需要安装不同的python库进行扩展。 在~/caffe/python/requirements.txt的清单文件列出了需要的依赖库 7.1 Ubuntu方式 (ok) 安装相应的build依赖包: Caffe的Python接口需要numpy库 安装scipy库 boost $ sudo apt-get install python-protobuf $ sudo apt-get install python-skimage 7.2 pip方式 (not tested) pip进行安装 // Use pip to install numpy and scipy instead for newer versions. 一句话安装: $ sudo pip install scikit-image Also note that in the Makefile.config of caffe, there is this line: 添加caffe/python 到python path变量, 把caffe下的python路径添加到到path中,
通常用到的两种方式:pip install和ubuntu系统独有的apt-get install, 区别?
* pip install的源是pyPI , apt-get 的源是ubuntu仓库。
对于python的包来说,pyPI的源要比ubuntu更多,对于同一个包,pyPI可以提供更多的版本以供下载。pip install安装的python包,可以只安装在当前工程内。
* apt-get 安装的包是系统化的包,在系统内完全安装。
* apt-get 和 pip install 中,对于相同python包,命名可能会不同:apt-get install:对于python2来说,包的名称可能是python-
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
$ sudo apt-get install build-essential
$ sudo apt-get install python-numpy
$ sudo apt-get install python-scipy
$ sudo apt-get install libboost-python1.55-dev
//$ sudo apt-get install libboost-python-dev -X
$ for req in $(cat requirements.txt); do pip install $req; done
(中间安装google的 protobuf时要翻墙)
不过网上还是看到有人说不要用他的文档,自己一个一个装比较好。
$ sudo pip install cython numpy scipy scikit-image matplotlib ipython h5py leveldb networkx nose pandas python-dateutil protobuf python-gflags pyyaml pillow six
generate a error: Exception IndexError: list index out of range
PYTHON_INCLUDE := /usr/include/python2.7 <-- correct
/usr/lib/python2.7/dist-packages/numpy/core/include <-- doesn't exist
so, try:
$ pip install -U scikit-image
-U is --upgrade,意是如果已安装就升级到最新版
按照官方建议安装anaconda包。 在anaconda官网下载.sh文件,执行,最后添加bin目录到环境变量即可。 建议安装Anaconda包,这个包能独立于系统自带的python库,并且提供大部分Caffe需要的科学运算Python库。要注意,在运行Caffe时,可能会报一些找不到libxxx.so的错误,而用 locate libxxx.so命令发现已经安装在anaconda中,这时首先想到的是在/etc/ld.so.conf.d/ 下面将 $your_anaconda_path/lib 加入 LD_LIBRARY_PATH中。 但是这样做可能导致登出后无法再进入桌面!!!原因(猜测)可能是anaconda的lib中有些内容于系统自带的lib产生冲突。
正确的做法:为了不让系统在启动时就将anaconda/lib加入系统库目录,可以在用户自己的~/.bashrc 中添加library path, 比如就在最后添加了两行
# add library path
LD_LIBRARY_PATH=your_anaconda_path/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
开启另一个终端后即生效,并且重启后能够顺利加载lightdm, 进入桌面环境。
$ vi ~/.bashrc
export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
sudo ldconfig
7.3 编译python接口
编译Python wrapper, 编译python接口
$ cd ~/caffe
$ make pycaffe -j4
--结果显示ALL TESTS PASSED
ok
$ make pytest -j4
在python中测试:
$ cd caffe-folder/python
$ python
>>>import caffe
没有报错,说明caffe安装全部完成
这样不需要进入caffe/python目录下就能调用caffe的python接口。
$ vi ~/.bashrc
#set caffe PYTHONPATH
export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH