Ubuntu-Caffe 环境搭建

  1. NVIDIA 显卡
    1. 去官网(http://www.nvidia.com/Download/index.aspx?lang=en-us)查询合适的GPU驱动,如图

    1. 驱动安装
      1. 删除原来驱动:

sudo apt-get purge nvidia-*

      1. 添加第三方驱动源:

sudo apt-get install python-software-properties software-properties-common

sudo add-apt-repository ppa:graphics-drivers/ppa

      1. 更新源:

sudo apt-get update

      1. 查看可用的驱动版本:

sudo apt-cache search nvidia-*

      1. 安装驱动:

sudo apt-get install nvidia-375

      1. 安装第三方库:

sudo apt-get install mesa-common-dev

sudo apt-get install freeglut3-dev 

      1. 重启服务器,控制台输入 nvidia-smi

或输入nvidia-settings

    1. cuda 配置
      1. 下载: 官网(https://developer.nvidia.com/cuda-downloads)下载cuda8.0

      1. 安装:

sudo dpkg -i cuda-repo-ubuntu1604-8-0-rc_8.0.27-1_amd64.deb

sudo apt-get update

sudo apt-get install cuda

      1. gcc版本降级: ubuntu的gcc编译器是5.4.0,然而cuda8.0不支持5.0以上的编译器,因此需要降级,把编译器版本降到4.9;

sudo apt-get install gcc -4.9 gcc-5 g++-4.9 g++-5

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 10

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 10

sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30

sudo update-alternatives --set cc /usr/bin/gcc

sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30

sudo update-alternatives --set c++ /usr/bin/g++

      1. 添加环境变量(~/.bashrc, /etc/profile):(注意等号两边不能有空格

sudo gedit ~/.bashrc

添加: export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}

export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

sudo gedit /etc/profile

添加: export PATH=/usr/local/cuda/bin:$PATH

      1. 创建链接:

sudo gedit /etc/ld.so.conf.d/cuda.conf

添加: /usr/local/cuda/lib64

      1. 使链接生效:

sudo ldconfig

      1. 测试cuda的Samples:

命令行输入(注意cuda-8.0是要相对应自己的cuda版本)

cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery

make

sudo ./deviceQuery

返回GPU的信息则表示配置成功

 

    1. cudnn配置
      1. 下载: 官网(https://developer.nvidia.com/cudnn) 下载对应的cudnn

      1. 安装: (ubuntu为本机用户名)

cd home/ubuntu/Downloads/

tar zxvf cudnn-8.0-linux-x64-v5.1.tgz

cd cuda/include

sudo cp cudnn.h /usr/local/cuda/include/ #复制头文件

cd ../lib64

lib文件的复制和链接(注意:查看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.1.5 libcudnn.so.5 #生成软衔接

sudo ln -s libcudnn.so.5 libcudnn.so #生成软链接

sudo ldconfig

  1. Opencv3.3.0 编译

为了加快编译速度,下载ippicv_2017u2_lnx_intel64_20170418.tgz,命名为87cbdeb627415d8e4bc811156289fa3a-ippicv_2017u2_lnx_intel64_20170418.tgz放到opencv/.cache/ippicv/

    1. 安装编译环境

sudo apt-get install --assume-yes libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip ffmpeg libgtk-3-dev python3-numpy libgstreamer1.0-dev qtbase5-dev

    1. 编译

cd opencv #解压的那个文件夹

mkdir build

cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \

    -D CMAKE_INSTALL_PREFIX=/usr/local \ 

    -D BUILD_EXAMPLES=ON \

    -D BUILD_TIFF=ON\

    -D WITH_TBB=ON\

    -D WITH_V4L=ON\

    -D WITH_QT=ON\

    -D WITH_OPENGL=ON\

-D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" .. #最后两点不要漏掉

make –j8

sudo make install

sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'

sudo ldconfig

  1. Anaconda配置python环境
    1. 安装

bash Anaconda3-2.4.1-Linux-x86_64.sh

    1. 添加环境变量

sudo gedit ~/.bashrc

export LD_LIBRARY_PATH="/home/jials/anaconda2/lib:$LD_LIBRARY_PATH"

export LD_LIBRARY_PATH="/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH"

source ~/.bashrc

  1. 配置caffe环境
    1. 切换g++ 到5.0以上版本

sudo update-alternatives --config g++

sudo update-alternatives --config gcc

    1. 安装依赖库

sudo add-apt-repository universe

sudo apt-get update -y

sudo apt-get install cmake -y

# General Dependencies

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler -y

sudo apt-get install --no-install-recommends libboost-all-dev -y

# BLAS

sudo apt-get install libatlas-base-dev -y

# Remaining Dependencies

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev -y

sudo apt-get install python-dev python-numpy –y

sudo apt-get install -y python-pip

sudo apt-get install -y python-dev

sudo apt-get install -y python-numpy python-scipy

    1. 下载,编译caffe
      1. 下载

git clone --recursive https://github.com/BVLC/caffe.git

      1. 修改 Makefile.config

cd caffe

cp Makefile.config.example Makefile.config

gedit Makefile.config

# cuDNN acceleration switch (uncomment to build with cuDNN).

USE_CUDNN := 1

# Uncomment if you're using OpenCV 3 如果用的是opencv3版本

OPENCV_VERSION := 3

# Uncomment to support layers written in Python (will link against Python libs)

WITH_PYTHON_LAYER := 1

#python 2.7 或使用python3.5

ANACONDA_HOME := $(HOME)

 PYTHON_INCLUDE := $(ANACONDA_HOME)/include \

         $(ANACONDA_HOME)/include/python2.7 \

         $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

#python 3.5 查看/usr/lib/x86_64-linux-gnu/ boost_python3 修改为对应的版本

PYTHON_LIBRARIES := boost_python-py35 python3.5m

ANACONDA_HOME := $(HOME)/anaconda2/envs/py35

PYTHON_INCLUDE := $(ANACONDA_HOME)/include \           $(ANACONDA_HOME)/include/python3.5m/site-packages/numpy/core/include \

               $(ANACONDA_HOME)/include/python3.5m\

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

      1. 安装 python 接口依赖库

cd ~/caffe/python

pip install -r requirements.txt

      1. caffe 编译

make clean

make  all -j8

make test -j8

make runtest -j8

#Python 接口编译

 make pycaffe

#添加环境变量

sudo gedit ~/.bashrc

export PYTHONPATH="/home/jials/caffe/python:$PYTHONPATH"

      1. caffe 测试

cd ~/caffe

./build/tools/caffe time --gpu 0 --model ./models/bvlc_alexnet/deploy.prototxt

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值