本文旨在總結深度學習基礎環境搭建中,一些基礎工具軟件包安裝。因爲在工作過程中,搭建環境不是天天遇到,但總是偶爾遇到,工具包安裝不可謂不重要;由於每次搭建基礎環境都要去找多個的博客,挺麻煩的,所以總結於此,方便自己也方便他人。
本文將總結適應於Tensorflow,Pytorch,Caffe等深度學習框架通用基礎環境搭建。
1 登陸方式
1.1 Ubuntu本機安裝
跳過該步
1.2 Ubuntu登陸另一臺Ubuntu
ssh -X [email protected]
注意加上 -X :支持圖像界面顯示(當然也要登陸的Ubuntu有圖像顯示能力)
1.3 Windows登陸另一臺Ubuntu
個人推薦Xshell工具遠程登陸
下載XshellPlus6和Xmanager6兩個工具包安裝即可,會出現3個圖標:
Xshell是遠程登陸工具,Xftp是文件傳輸工具,Xmanager是圖像界面顯示工具;有了這3個工具將簡單方便得多(若有用Xshell5的童鞋,建議更新,Xshell6比5好用多了,更加穩定,更加方便)
2 基礎工具
2.1 Python
大部分ubuntu初始化都是有python的;若不存在,建議安裝2.7 和 3.5兩個版本,因爲我們遇到的dl_demo可能兩個版本都有
//python2.7
sudo apt-get install python2.7 python2.7-dev
//python3.5
sudo apt-get install python3.5 python3.5-dev
//很多pip安裝的包都需要libssl和libevent編譯環境
sudo apt-get install build-essential libssl-dev libevent-dev libjpeg-dev libxml2-dev libxslt-dev
//安裝pip
sudo apt-get install python-pip
sudo apt-get install python3-pip
參考鏈接:ubuntu下Python的安裝和使用
2.2 Cmake
sudo apt-get install git cmake build-essential
//cmake-gui 有界面
sudo apt-get install cmake-qt-gui
安裝好可以測試
cmake-gui
看是否有cmake的界面出現(前提是環境要支持界面顯示)
3 部分依賴包
值得注意:如果讀者不用caffe,大可直接使用anaconda,來安裝環境,簡單快捷;但本人遇到過anaconda與caffe編譯在protobuf上的衝突,折騰了3天沒能解決,最終還是幹掉了anaconda;讀者可根據自己需求選擇。
//這裏大部分依賴包是caffe需要的
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
注:第一行中大多參考博客中都還有 libopencv-dev,我這裏把它幹掉了,因爲後面會單獨安裝opencv;個人覺得一個系統中一個opencv版本就夠了,以免混亂衝突
4 CUDA
4.1 查看顯卡驅動和型號
//驅動
lsmod | grep -i nvidia
//型號
lspci | grep -i vga
lspci | grep -i nvidia
默認情況下,是有驅動的,但可能不是CUDA需要的,可以用nvidia-smi查看下,若沒有則需要安裝驅動
4.2 安裝驅動
4.2.1 禁用第3方驅動nouveau(若存在)
lsmod | grep nouveau
沒有輸出則跳過,若有則:
//1創建文件(若不存在)
cd /etc/modprobe.d
sudo touch blacklist-nouveau.conf
sudo gedit blacklist-nouveau.conf
//2輸入禁用信息
//文件中輸入 並保存退出
blacklist nouveau
options nouveau modeset=0
//3更新 並重啓ubuntu
sudo update-initramfs -u
reboot
//4再次查看(此時應該沒有輸出了)
lsmod | grep nouveau
4.2.2 安裝驅動
//幹掉之前的驅動(卸載顯卡驅動)
sudo apt-get remove --purge nvidia-*
到官網查看驅動型號
點擊搜索:
可以下載離線安裝,也可一鍵安裝,如下:
//安裝(小編的是1080ti 選擇384,當然也可以是最新的410)
sudo apt-get install nvidia-384
//一路按住Enter 到100%即可
//驗證
nvidia-smi
出現下面的界面 就對了(若沒有 重啓試試)
4.2.3 安裝CUDA
到CUDA官網下載需要的CUDA;根據自己的環境選擇對應版本下載
安裝CUDA(小編下載的是CUDA8)
sudo sh cuda_8.0.61_375.26_linux.run
//單擊回車,一路往下運行,直到提示“是否爲NVIDIA安裝驅動nvidia-375?”,選擇否 因爲已經安裝好驅動程序了
//其他的全都是默認,需要選擇y/n時 輸入 y 即可
//不過要記住安裝位置,默認是安裝在/usr/local/cuda文件夾下
配置環境變量,運行如下命令打開 ~/.bashrc文件
sudo gedit ~/.bashrc
//打開文件後在文件末尾添加路徑,也就是安裝目錄,命令如下:
export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:$CUDA_HOME/bin
export LD_LIBRARY_PATH=/usr/local/cuda/lib64
//保存,然後重啓電腦(非必需)
reboot
測試CUDA的例子
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery
如果顯示的是關於GPU的信息,則說明安裝成功了。
4.3 cuDNN安裝
如果要使用GPU來對DL網絡進行加速,除了安裝CUDA以外,cuDNN也需要安裝的(也可不安裝);據本人測試,大部分DL網絡在有cuDNN加速下時可以提升速度的,尤其是在訓練中,能夠節約時間;但也有少部分DL網絡,cuDNN加速不明顯;但要注意若沒有安裝cuDNN,在編譯caffe時要關閉cudnn編譯選項。
跟CUDA一樣,去NVIDIA的官網下載cuDNN的安裝包。不過這次沒法直接下載,需要先註冊,然後還要做個調查問卷什麼的,稍微有點麻煩。我下的是cudnn-8.0-linux-x64-v6.0.tgz壓縮包
//解壓
tar -xvf cudnn-8.0-linux-x64-v6.0.tgz
文件如下:
安裝
//安裝(就是拷貝)
sudo cp lib64/lib* /usr/local/cuda/lib64/
sudo cp include/cudnn.h /usr/local/cuda/include/
//更新軟連接
cd /usr/local/cuda/lib64/
//先幹掉libcudnn.so libcudnn.so.6
(小心點 根目錄下rm的時候 一定要小心,不要幹錯了)
sudo rm -r libcudnn.so libcudnn.so.6
//重建軟連接
sudo ln -sf libcudnn.so.6.0.21 libcudnn.so.6
sudo ln -sf libcudnn.so.6 libcudnn.so
//查看
ll libcudnn*
輸出:
安裝
//更新
sudo ldconfig
5 Matlab
下載(密碼 4gj3)Linux版本的Matlab,小編下載的是R2015b_Linux
1 掛載
sudo mkdir /mnt/temp
//path :是指自己存放matlab安裝包的路徑
sudo mount -o loop path/R2015b_glnxa64.iso /mnt/temp
2 安裝
cd /mnt/temp/
sudo ./install
根據提示安裝
輸入密鑰
在文件夾Matlab 2015b Linux64 Crack下的readme.txt中
1) standalone:
- Install choosing the option "Use a File Installation Key" and supply the following FIK
09806-07443-53955-64350-21751-41297
複製到安裝界面裏
默認安裝路徑:/usr/local/MATLAB/R2015b
等待安裝。。。
3 卸載掛載
sudo umount /mnt/temp
4 激活
cd /usr/local/MATLAB/R2015b/bin/
sudo ./matlab
找到Matlab 2015b Linux64 Crack下的license_standalone.lic (這裏注意文件夾名最好不要有空格和漢字)
若出現licenses寫入錯誤,則需新建licenses文件夾並更改權限:
cd ..
sudo mkdir licenses
sudo chmod 777 licenses
5 破解
//進入Matlab 2015b Linux64 Crack\R2015b\bin\glnxa64文件夾將libmwservices.so複製到/usr/local/MATLAB/R2015b/bin/glnxa64中:
sudo cp libmwservices.so /usr/local/MATLAB/R2015b/bin/glnxa64/
6 運行
cd /usr/local/MATLAB/R2015b/bin/
./matlab
7 軟連接
MATLAB啓動程序默認會安裝到/usr/local/MATLAB/R2015b/bin目錄中,由於系統默認不會將該目錄加入PATH環境變量,因此,直接 在終端輸入matlab命令將不會啓動,解決方法非常簡單: 一種是將該路徑加入到PATH中,另一種就是在/usr/loca/bin裏建一個符號鏈接。 這裏選擇後者:
sudo ln -s /usr/local/MATLAB/R2015b/bin/matlab /usr/local/bin/matlab
之後在命令行中直接輸入matlab即可啓動Matlab應用。
8 權限問題
安裝好後,若發現用sudo matlab 表現正常;而直接matlab啓動出現權限問題:
可以查看
cd ~/.matlab/
ll
看到R2015b屬於root賬戶
更改所屬賬戶
sudo chown -R $USER: ~/.matlab
ll
就可以看到屬於當前賬戶了 ;然後正常了
matlab
9 其他問題
若在安裝過程出現激活錯誤操作等,先不要着急重新安裝,只需要刪除與 /usr/local/MATLAB/R2015b下的licenses文件夾,再激活即可
6 Opencv
Opencv的編譯安裝比較重要,Python,C/C++等多種語言都會用到Opencv;當然本文只先關注Python和C/C++的需求。
若只在Python中調用Opencv的朋友,可用pip安裝即可(忽略後文Opencv安裝)
//python2
sudo pip install opencv_python
//python3
sudo pip3 install opencv_python
回到正題,事實上我們遇到的有些DL_Demo是需要C/C++支持Opencv的,繼續
1 Opencv依賴包
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev # 處理視頻所需的包
sudo apt-get install libatlas-base-dev gfortran # 優化opencv功能
sudo apt-get install ffmpeg
當然上面個別包已經安裝過了,如cmake,git等,可跳過
2 Python依賴包
//python2
sudo pip install numpy scipy matplotlib scikit-image scikit-learn ipython
//python3
sudo pip3 install numpy scipy matplotlib scikit-image scikit-learn ipython
3 Opencv安裝包
Opencv安裝包包括opencv包和contrib包兩個,可以提前下載,也可git (本文用的方式1)
(1)下載鏈接,選擇sources版本,解壓下載下來的zip包,也可wget
mkdir opencv34
cd opencv34
wget https://github.com/opencv/opencv/archive/3.4.0.zip
unzip 3.4.0.zip
mkdir contrib
cd contrib
wget -c https://github.com/opencv/opencv_contrib/archive/3.4.0.zip
unzip 3.4.0.zip
完成後目錄結構:
(2)git方式
//根據自己喜好創建目錄
mkdir opencv
cd opencv
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.4.0
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.4.0
4 Opencv預編譯
cd opencv-3.4.0
mkdir build //編譯文件存放位置
cd build
cmake-gui
(1)選擇好source路徑和build路徑,點擊Generate按鈕,並勾選上Grouped和Advanced(也可不選,只是顯示結構而已
(2)在第1步後,找到OPENCV組下OPENCV_EXTRA_MODULES_PATH
添加存放contrib的路徑,目錄直到modules級
(3)不着急Configure,先檢測
a)檢測其他關聯是否正確,如Matlab,CUDA等一般不會出問題;但尤其要注意看python2和python3是否都正常,正常如下:
b)生成欄是否存在Error或個別Warning,若存在可根據提示酌情處理,處理後再次Generate;多次處理後,感覺OK了,下一步
(4)點擊Configure
沒有錯誤,就可以關閉Cmake-gui了,回到終端,你就發現build下有很多東西了
5 Opencv編譯
make -j8
-j8表示多核編譯,若CPU不是多核直接make就可以了,只是編譯速度問題;接下來就是30分鐘左右的等待時間。。。。
編譯好,opencv_python3和opencv_python2都編譯好,如下:
6 install
在上述操作後,opencv本地編譯好了,但系統還不認識opencv
sudo make install
大功告成
7 測試
//python2
python
import cv2
cv2.__version__
//python3
python3
import cv2
cv2.__version__
OK
7 Caffe
從github上git一個caffe工程試試環境
git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config
vim Makefile.config
//修改 cudnn opencv matlab hdf5
## Refer to http://caffe.berkeleyvision.org/installation.html
#Contributions simplifying and improving our build system are welcome!
#cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1 #cuDNN測試
#CPU-only switch (uncomment to build without GPU support).
#CPU_ONLY := 1
#uncomment to disable IO dependencies and corresponding data layers
#USE_OPENCV := 0
#USE_LEVELDB := 0
#USE_LMDB := 0
#This code is taken from https://github.com/sh1r0/caffe-android-lib
#USE_HDF5 := 0
#uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#You should not set this flag if you will be reading LMDBs with any
#possibility of simultaneous read and write
#ALLOW_LMDB_NOLOCK := 1
#Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3 #opencv3測試
#To customize your choice of compiler, uncomment and set the following.
#N.B. the default for Linux is g++ and the default for OSX is clang++
#CUSTOM_CXX := g++
#CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
#On Ubuntu 14.04, if cuda tools are installed via
#"sudo apt-get install nvidia-cuda-toolkit" then use this instead:
#CUDA_DIR := /usr
#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.
#For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH = -gencode arch=compute_30,code=sm_30 \ #刪除20 21兩行 警告一大堆
-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
#BLAS choice:
#atlas for ATLAS (default)
#mkl for MKL
#open for OpenBlas
BLAS := atlas
#Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
#Leave commented to accept the defaults for your choice of BLAS
#(which should work)!
#BLAS_INCLUDE := /path/to/your/blas
#BLAS_LIB := /path/to/your/blas
#Homebrew puts openblas in a directory that is not on the standard search path
#BLAS_INCLUDE := $(shell brew --prefix openblas)/include
#BLAS_LIB := $(shell brew --prefix openblas)/lib
#This is required only if you will compile the matlab interface.
#MATLAB directory should contain the mex binary in /bin.
MATLAB_DIR := /usr/local/MATLAB/R2015b #MATLAB測試
#MATLAB_DIR := /Applications/MATLAB_R2012b.app
#NOTE: this is required only if you will compile the python interface.
#We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include
#Anaconda Python distribution is quite popular. Include path:
#Verify anaconda location, sometimes it's in root.
#ANACONDA_HOME := $(HOME)/anaconda
#PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
# $(ANACONDA_HOME)/include/python2.7 \
# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
#Uncomment to use Python 3 (default is Python 2)
#PYTHON_LIBRARIES := boost_python3 python3.5m
#PYTHON_INCLUDE := /usr/include/python3.5m \
#/usr/lib/python3.5/dist-packages/numpy/core/include
#We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /usr/lib
#PYTHON_LIB := $(ANACONDA_HOME)/lib
#Homebrew installs numpy in a non standard path (keg only)
#PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
#PYTHON_LIB += $(shell brew --prefix numpy)/lib
#Uncomment to support layers written in Python (will link against Python libs)
#WITH_PYTHON_LAYER := 1
#Whatever else you find you need goes here. #額外添加hdf5路徑
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 /usr/lib/x86_64-linux-gnu/hdf5/serial
#If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
#INCLUDE_DIRS += $(shell brew --prefix)/include
#LIBRARY_DIRS += $(shell brew --prefix)/lib
#NCCL acceleration switch (uncomment to build with NCCL)
#https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
#USE_NCCL := 1
#Uncomment to use `pkg-config` to specify OpenCV library paths.
#(Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
#USE_PKG_CONFIG := 1
#N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
#Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
#DEBUG := 1
#The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0
#enable pretty build (comment to see full commands)
Q ?= @
在修改後,保存,編譯,表現正常
make -j8
make matcaffe
make pycaffe
8 Tensorflow
Tensorflow安裝是基於python的,最好指定版本,默認最新可能不支持CUDA8(根據自己CUDA版本選擇)
//python2
sudo pip install tensorflow-gpu==1.4
//python3
sudo pip3 install tensorflow-gpu==1.4
9 Pytorch
Pytorch安裝也是基於python,進入Pytroch官網根據個人情況選擇,然後複製命令行安裝即可(記得加sudo)
注意torch和torchvision都要安裝
測試
10 其他
至此基礎環境就安裝好了,caffe,tensorflow,pytorch等深度學習框架隨便切換;當然在有些demo下可能要求一些特殊包,按照提示安裝即可。