本文爲原創做品,未經本人贊成,禁止轉載,禁止用於商業用途!本人對博客使用擁有最終解釋權 html
歡迎關注個人博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing python
本文主要是介紹在ubuntu16.04下,怎麼配置當下流行的深度學習框架,cuda8.0+cudnn+caffe+theano+tensorflow linux
首先去官網上查看適合你GPU的驅動 c++
(http://www.nvidia.com/Download/index.aspx?lang=en-us) git
sudo add-apt-repository ppa:graphics-drivers/ppa github
sudo apt-get update 算法
sudo apt-get install nvidia-375(375是你查到的版本號) spring
sudo apt-get install mesa-common-dev ubuntu
sudo apt-get install freeglut3-dev api
執行完上述後,重啓(reboot)。
重啓後輸入
nvidia-smi
若是出現了你的GPU列表,則說明驅動安裝成功了。另外也能夠經過,或者輸入
nvidia-settings
出現
https://developer.nvidia.com/cuda-downloads
在cuda所在目錄打開terminal依次輸入如下指令:
sudo dpkg -i cuda-repo-ubuntu1604-8-0-rc_8.0.27-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda
ubuntu的gcc編譯器是5.4.0,然而cuda8.0不支持5.0以上的編譯器,所以須要降級,把編譯器版本降到4.9:
在terminal中執行:
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++
配置cuda8.0以後主要加上的一個環境變量聲明,在文件~/.bashrc以後加上
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
保存以後,建立連接文件
sudo gedit /etc/ld.so.conf.d/cuda.conf
在打開的文件中添加以下語句:
/usr/local/cuda/lib64
保存退出執行命令行:
sudo ldconfig
使連接當即生效。
命令行輸入(注意cuda-8.0是要相對應本身的cuda版本)
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
make
sudo ./deviceQuery
返回GPU的信息則表示配置成功
上官網下載對應的cudnn
https://developer.nvidia.com/cudnn
下載完cudnn後,命令行輸入文件所在的文件夾 (ubuntu爲本機用戶名)
cd home/ubuntu/Downloads/
tar zxvf cudnn-8.0-linux-x64-v5.1.tgz #解壓文件
cd進入cudnn5.1解壓以後的include目錄,在命令行進行以下操做:
sudo cp cudnn.h /usr/local/cuda/include/ #複製頭文件
再cd進入lib64目錄下的動態文件進行復制和連接:(5.1.5爲對應版本具體可修改)
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 #生成軟連接
從官網上下載opencv3.1.0
http://opencv.org/downloads.html
並將其解壓到你要安裝的位置,(下載的位置仍是在home/ubuntu、Downloads文件夾下)
首先安裝Ubuntu系統須要的依賴項,雖然我也不知道有些依賴項是幹啥的,可是隻管裝就行,也不會佔據不少空間的。
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
而後安裝OpenCV須要的一些依賴項,一些文件編碼解碼之類的東東。
sudo apt-get install build-essential cmake git
sudo apt-get install ffmpeg libopencv-dev libgtk-3-dev python-numpy python3-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libv4l-dev libtbb-dev qtbase5-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip
在終端中cd到opencv文件夾下(解壓的那個文件夾),而後
mkdir build #新建一個build文件夾,編譯的工程都在這個文件夾裏
cd build/
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..(後面兩點不要忘記)
cmake成功後,會出現以下結果,提示配置和生成成功:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ise/software/opencv-3.1.0/build
因爲CUDA 8.0不支持OpenCV的 GraphCut 算法,可能出現如下錯誤:
/home/usrname/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:120:54: error: 'NppiGraphcutState' has not been declared
typedef NppStatus (*init_func_t)(NppiSize oSize, NppiGraphcutState** ppStat
^
/home/usrname/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:135:18: error: 'NppiGraphcutState' does not name a type
operator NppiGraphcutState*()
^
/home/usrname/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:141:9: error: 'NppiGraphcutState' does not name a type
NppiGraphcutState* pState;
.......
進入opencv-3.1.0/modules/cudalegacy/src/目錄,修改graphcuts.cpp文件,將:
#include "precomp.hpp"
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
改成
#include "precomp.hpp"
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)
而後make編譯就能夠了
make -j8
上面是將opencv編譯成功,可是並無安裝到咱們的系統中,有不少的設置都沒有寫入到系統中,所以還要進行install。
sudo make install
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
重啓系統,重啓系統後cd到build文件夾下:
sudo apt-get install checkinstall
sudo checkinstall
而後按照提示安裝就能夠了。
使用checkinstall的目的是爲了更好的管理我安裝的opencv,由於opencv的安裝很麻煩,卸載更麻煩,其安裝的時候修改了一大堆的文件,當我想使用別的版本的opencv時,將當前版本的opencv卸載就是一件頭疼的事情,所以須要使用checkinstall來管理個人安裝。
執行了checkinstall後,會在build文件下生成一個以backup開頭的.tgz的備份文件和一個以build開頭的.deb安裝文件,當你想卸載當前的opencv時,直接執行dpkg -r build便可。
切換編譯器
選擇g++ 5.0以上的對應編號
sudo update-alternatives --config g++
sudo update-alternatives --config gcc
安裝依賴庫
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
編譯 Caffe,cd到要安裝caffe的位置
git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config
修改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
在問件裏面添加文本因爲hdf5庫目錄更改,因此須要單獨添加:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/aarch64-linux-gnu/hdf5/serial/
打開makefile文件
gedit Makefile
將
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替換
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
編輯/usr/local/cuda/include/host_config.h,將其中的第115行註釋掉:
sudo gedit /usr/local/cuda/include/host_config.h
將
#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
改成
//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
以後編輯便可
make -j4 all
make -j4 runtest
爲了更好地使用pycaffe ,建議安裝:
sudo apt-get install python-numpy python-setuptools python-pip cython python-skimage python-protobuf
make pycaffe
cd python
python
import caffe #測試安裝成功
到這裏Caffe開發環境就配置好了!
能夠測試一下,輸出AlexNet的時間測試結果:
cd ~/caffe
./build/tools/caffe time --gpu 0 --model ./models/bvlc_alexnet/deploy.prototxt
一、直接輸入命令:
sudo pip install theano
二、配置參數文件:.theanorc
sudo gedit ~/.theanorc
對打開的文件進行編輯
[global]
floatX=float32
device=gpu
base_compiledir=~/external/.theano/
allow_gc=False
warn_float64=warn
[mode]=FAST_RUN
[nvcc]
fastmath=True
[cuda]
root=/usr/local/cuda
三、運行測試例子:
sudo Vim test.py
from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time
vlen = 10 * 30 * 768 # 10 x #cores x # threads per core
iters = 1000
rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
print('Used the cpu')
else:
print('Used the gpu')
能夠看到結果:
/usr/bin/python2.7 /home/hjimce/PycharmProjects/untitled/.idea/temp.py
Using gpu device 0: GeForce GTX 960 (CNMeM is disabled, cuDNN not available)
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.302778 seconds
Result is [ 1.23178029 1.61879349 1.52278066 ..., 2.20771813 2.29967761
1.62323296]
Used the gpu
說明安裝成功
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md
先安裝anaconda
https://repo.continuum.io/archive/Anaconda2-4.2.0-Windows-x86_64.exe
上面的地址下載 該包默認在downloads裏面
cd /home/username/Downloads
sudo bash Anaconda2-4.2.0-Linux-x86_64.sh
配置環境變量
gedit /etc/profile
末尾添上,我是一路yes下來,因此安在了root下,你能夠本身選路徑,這時候的環境變量要改
export PATH=/root/anaconda2/bin:$PATH
重啓
打開終端
python
安裝成功
二、建立conda環境 名字叫tensorflow
conda create -n tensorflow python=2.7
source activate tensorflow #使能該環境
#下面這句話只能下載給CPU用的tensorflow
conda install -c conda-forge tensorflow
利用pip來下載給GPU用的tensorflow
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.11.0-cp27-none-linux_x86_64.whl
下載安裝
pip install --ignore-installed --upgrade $TF_BINARY_URL
安裝IPython
conda install ipython
關掉該環境
source deactivate
測試安裝是否正確
source activate tensorflow
python
輸入
import tensorflow as tf
import numpy as np
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3
# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
# Before starting, initialize the variables. We will 'run' this first.
init = tf.initialize_all_variables()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
# Learns best fit is W: [0.1], b: [0.3]
OK
問題:找不到Python.h
解決:給anaconda添加環境變量
gedit ~/.banshrc
添加
export PATH=/root/anaconda2/bin:$PATH
export PYTHONPATH=/path/to/caffe/python:$PATH
修改Makefile.config
在終端輸入
locate Python.h
gedit Makefile.config
在INCLUDE_DIRS 和LIBRARY_DIRS後面添上
/root/anaconda2/include/python2.7
啓用
ANACONDA_HOME := $(HOME)/anaconda2
PYTHON_ INCLUDE =$(ANACONDA_HOME)/include\
,把前面的#去掉,那三行都去掉#,並在註釋上面,
註釋這兩句PYTHON_INCLUDE := /usr/include/python2.7\
/usr/lib/python2.7…………..
若是編譯的時候發現有錯,回來改完以後又得從新編譯一遍pycaffe,因而出現以下錯誤
make: Nothing to be done for 'pycaffe'
則在終端輸入:
sudo make clean
修改完後再
sudo make pycaffe
這裏要從make –j8 all那一步開始編譯
編譯完後,顯示
而後 cd python進入該目錄
python
import caffe
若此時提示錯誤:
Traceback (most recent call last)
File
ImportError: /home/../anaconda2/lib/python2.7/site-packages/zmq/backend/cython/../../../../.././libstdc++.so.6: versionGLIBCXX_3.4.21' not found
解決:
https://github.com/BVLC/caffe/issues/4953
https://gitter.im/BVLC/caffe/archives/2015/08/20
cd ..
pip install protobuf
sudo apt-get install python-protobuf
coda install libgcc