Linux服务器端无权限搭建 Caffe + CUDA9.0记录(上)

Linux服务器端无权限搭建 Caffe + CUDA9.0记录(上)

搭建过程主要参考了

[1] Linux服务器无权限安装caffe教程
[2] linux-no-root-install-log
[3] Ubuntu下编译caffe

官方安装教程

Caffe Installation

前言

网上大多数教程都是在本地环境中搭建caffe,依赖只需要通过sudo apt-get就可以进行安装。由于自己的电脑性能无法满足训练的需要,需要在服务器上进行训练,而实验室分配的账号并没有root权限,因此无法使用sudo命令,此时需要自己手动下载和安装所有的依赖,并添加对应的环境变量来使得caffe能够成功编译安装。文章是对自己安装过程的记录,主要的命令代码都是参照Linux服务器无权限安装caffe教程,有一些小的更改和整合。如果遇到相同的问题,希望可以给到一些帮助。

1、CUDA的安装配置

大多数服务器已经安装好了CUDA,输入:

nvcc -V(大写V)

如果成功输出:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:03_CDT_2017
Cuda compilation tools, release 9.0, V9.0.176 //这个部分为你所安装的版本号

说明你的CUDA已经安装成功。此时需要将CUDA添加在你的环境变量中:

vi ~/.bashrc
//在文件的末尾添加:
export PATH=$PATH:/usr/local/cuda-9.0/bin  //cuda的路径可以使用 whereis cuda-(版本号)查看
export LD_LIBRARY_PATH=:$LD_LIBRARY_PATH:/usr/local/cuda-9.0/lib64 
//退出编辑后使用:
source ~/.bashrc  //使环境变量生效

如果需要自己安装CUDA可以参考Linux无root无sudo权限在用户目录安装cuda9.2和cudnn7.1

Tip1:

如果遇到bashrc文件编辑出错,无法使用vi/vim编辑,可通过输入如下命令再打开文件即可:

export PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  //恢复编辑
2、依赖的安装配置

根据参考教程[1]首先我们需要在自己的服务器文件夹中创建两个文件夹,依次执行:

mkdir temp //存放所有的压缩文件、源码
mkdir local  //存放所有的头文件、库、和可执行程序

在后文中所有的root_path都要更改为你自己的根目录,几乎所有的依赖都可以通过命令顺利的下载,这里将我已经下载过的文件放在网盘里,如果有下载不顺利的情况可以自取:https://pan.baidu.com/s/115mJhWn0UGVJhxxVqEtu6w 提取码:txhu ,下载之后将文件放至temp中。

2.1、安装配置过程过程中所需要的工具
(1)m4
 cd temp
 //如果之前以已经下载过网盘中的依赖可跳过 wget,后面同理
 wget http://ftp.gnu.org/gnu/m4/m4-1.4.18.tar.gz
 tar zxvf m4-1.4.18.tar.gz
 cd m4-1.4.18
 ./configure --prefix=/root_path/local
 make
 make install
(2)autoconf
 cd temp
 wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
 tar zxvf autoconf-2.69.tar.gz
 cd autoconf-2.69
 ./configure --prefix=/root_path/local
 make
 make install
(3)automake
 cd temp
 wget http://ftp.gnu.org/gnu/automake/automake-1.16.1.tar.gz
 tar zxvf automake-1.16.1.tar.gz
 cd automake-1.16.1
 ./configure --prefix=/root_path/local
 make
 make install
(4)libtool
 cd temp
 wget http://mirror.csclub.uwaterloo.ca/gnu/libtool/libtool-2.4.6.tar.gz
 cd libtool-2.4.6
 tar zxvf libtool-2.4.6.tar.gz
 ./configure --prefix=/root_path/local
 make
 make install

安装编译完成之后需要将其添加到环境变量中,使用:

vi ~/.bashrc
//按i进入编辑模式,在文件末尾输入以下
export PATH=$PATH:/root_path/local/bin
export LIBRARY_PATH=$LIBRARY_PATH:/root_path/local/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root_path/local/lib
//按ESC退出编辑模式,在输入:wq 保存并退出
//退出文件后输入:
source ~/.bashrc  //更新环境变量

//最后可以输入以下命令来判断是否安装成功:
m4 --version
autoconf --version
automake --version
libtool --version

其中LIBRARY_PATH是程序编译期间查找动态链接库时指定查找共享库的路径,LD_LIBRARY_PATH是程序加载运行期间查找动态链接库时指定除了系统默认路径之外的其他路径。

2.2、安装依赖
(1)boost
cd temp
wget https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.tar.gz
tar zxvf boost_1_67_0.tar.gz
cd boost_1_67_0
./bootstrap.sh --prefix=/root_path/local
./b2 -j4
./b2 install

在这里参考教程[1]提到需要将boost的库头文件写入环境变量,即在.bashrc中添加

export BOOST_INCLUDEDIR="/your_root_path/local/include:$BOOST_INCLUDEDIR"

在我的配置过程中,发现这一行并没有起到作用。根据查找的资料,作者的目的是需要将boost的库添加到环境变量中。防止在后面的编译过程中找不到库中的文件。因此,我在环境变量中添加了:

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/root_path/local/include/
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/root_path/local/include

通过这两行来指定后续编译过程中所使用的头文件,库文件搜索路径。
退出编辑后别忘了使用source ~/.bashrc激活环境变量

(2)Glog
git clone https://github.com/google/glog.git
cd glog
./autogen.sh
./configure --prefix=/root_path/local
 make
 make install
(3)Gflags
git clone https://github.com/gflags/gflags.git
cd gflags
mkdir build
cd build
CXXFLAGS="-fPIC" cmake -D CMAKE_INSTALL_PREFIX=/root_pat/local ..
make
make install
(4)protobuf
cd temp
wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-cpp-3.5.1.tar.gz
tar zxvf protobuf-cpp-3.5.1.tar.gz
cd protobuf-3.5.1
./autogen.sh
./configure --prefix=/root_path/local
 make
 make install
(5)hdf5
cd temp
wget https://support.hdfgroup.org/ftp/HDF5/current/src/hdf5-1.10.1.tar
tar xvf hdf5-1.10.1.tar
cd hdf5-1.10.1
./autogen.sh
./configure --prefix=/root_path/local
make -j4  //j后面跟的数字可以根据自己电脑的性能改变481632
make check
make install
make check-install
(6)snappy
cd temp
git clone https://github.com/google/snappy.git
cd snappy
mkdir build
cd build
cmake -D CMAKE_INSTALL_PREFIX=/root_path/local ..
make -j4
make install
(7)lapack
cd temp
wget http://www.netlib.org/lapack/lapack-3.8.0.tar.gz
tar zxvf lapack-3.8.0.tar.gz
cd lapack-3.8.0
mkdir build
cd build
cmake -D CMAKE_INSTALL_PREFIX=/root_path/local ..
make -j4
make install
(8)openblas
cd temp
git clone https://github.com/xianyi/OpenBLAS.git
cd OpenBLAS
make
make install PREFIX=/root_path/local 
(9)opencv
cd temp
wget https://github.com/opencv/opencv/archive/3.2.0.zip
unzip 3.2.0
cd opencv-3.2.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/root_path/local ..
···
在cmake的过程中会出现需要下载ippicv_linux_20151201.tgz,
如果网络问题下载缓慢或者失败可以在我之前发的网盘地址中找到。
从网盘中下载后将文件放至opencv-3.2.0/3rdparty/ippicv/downloads/linux-*中并重新cmake
···
make 
make install

在这里如果你使用的CUDA版本为9.0或以上版本,会出现如下错误:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nppi_LIBRARY (ADVANCED)
    linked by target "opencv_cudev" in directory /home/yangxinwei/opencv-test/opencv-3.2.0/modules/cudev
    linked by target "opencv_cudev" in directory /home/yangxinwei/opencv-test/opencv-3.2.0/modules/cudev
    ......
    linked by target "opencv_version" in directory /home/yangxinwei/opencv-test/opencv-3.2.0/apps/version

此时需要对以下文件进行修改(建议将文件下载到本地进行修改):
1)在opencv-3.2.0/cmake中找到FindCUDA.cmake
将:

find_cuda_helper_libs(nppi)

替换为:

find_cuda_helper_libs(nppial)
find_cuda_helper_libs(nppicc)
find_cuda_helper_libs(nppicom)
find_cuda_helper_libs(nppidei)
find_cuda_helper_libs(nppif)
find_cuda_helper_libs(nppig)
find_cuda_helper_libs(nppim)
find_cuda_helper_libs(nppist)
find_cuda_helper_libs(nppisu)
find_cuda_helper_libs(nppitc)

将:

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")

替换为:

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")

将:

unset(CUDA_nppi_LIBRARY CACHE)

替换为:

unset(CUDA_nppial_LIBRARY CACHE)
unset(CUDA_nppicc_LIBRARY CACHE)
unset(CUDA_nppicom_LIBRARY CACHE)
unset(CUDA_nppidei_LIBRARY CACHE)
unset(CUDA_nppif_LIBRARY CACHE)
unset(CUDA_nppig_LIBRARY CACHE)
unset(CUDA_nppim_LIBRARY CACHE)
unset(CUDA_nppist_LIBRARY CACHE)
unset(CUDA_nppisu_LIBRARY CACHE)
unset(CUDA_nppitc_LIBRARY CACHE)

2)在同一个文件夹中找到OpenCVDetectCUDA.cmake
将:

  set(__cuda_arch_ptx "")
  if(CUDA_GENERATION STREQUAL "Fermi")
    set(__cuda_arch_bin "2.0")
  elseif(CUDA_GENERATION STREQUAL "Kepler")
    set(__cuda_arch_bin "3.0 3.5 3.7")
  ......

替换为:

  set(__cuda_arch_ptx "")
  if(CUDA_GENERATION STREQUAL "Kepler")
    set(__cuda_arch_bin "3.0 3.5 3.7")
  elseif(CUDA_GENERATION STREQUAL "Maxwell")
    set(__cuda_arch_bin "5.0 5.2")
  ......

3)在opencv-3.2.0/modules/cudev/include/opencv2中找到common.cpp
添加:

#include <cuda_fp16.h>

在解决了前一个错误后,可能还会出现:

nvcc fatal   : Unsupported gpu architecture 'compute_20'
CMake Error at cuda_compile_generated_gpu_mat.cu.o.cmake:208 (message):

这时使用下面语句从新cmake:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/root_path/local -D CUDA_GENERATION=Kepler ..
Tip2:

使用make 编译时可以使用 -jx来加速,在编译opencv时使用可能报错,如果是你在编译opencv时使用了此命令,且之前的安装配置都正确,可尝试使用只使用make

接上cmake之后再make和make install时报错:

LAPACKE_H_PATH-NOTFOUND/lapacke.h: No such file or directory

解决方法:将opencv-3.2.0/build/中的opencv_lapack.h中的:

#include "LAPACKE_H_PATH-NOTFOUND/lapacke.h"

修改为:

#include "/root_path/local/include/lapacke.h"

再重新编译即可成功安装。

(10)lmdb
cd temp
git clone https://gitorious.org/mdb/mdb.git
make
make prefix=/root_path/local install

至此,依赖部分已经安装完毕,但是在编译caffe中仍会出现许多问题,会在下篇文章中继续Caffe框架的编译过程,以及python接口的配置。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值