一竟痰、安裝順序
Cuda9.0(先安裝顯卡驅(qū)動(dòng)) cuDNN7.2.1 OpenCV3.4.2 Tensorflow1.10
首先Ubuntu要安裝一些依賴庫(kù)
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-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
二签钩、更新一下顯卡驅(qū)動(dòng),GeForce1060顯卡坏快,最新驅(qū)動(dòng)390版本(2018.8.12)
2.1 NVIDIA網(wǎng)站铅檩,查找下載.run文件
https://www.nvidia.com/Download/index.aspx?lang=en-us
2.2 卸載老驅(qū)動(dòng)
sudo apt-get autoremove --purge nvidia-*
2.3 安裝新驅(qū)動(dòng)
關(guān)閉圖形界面,安裝.run驅(qū)動(dòng)文件莽鸿,打開(kāi)圖形界面
sudo service lightdm stop #(關(guān)閉圖形界面) (實(shí)測(cè)重裝驅(qū)動(dòng)需要此步驟)
./NVIDIA-390.run #(運(yùn)行驅(qū)動(dòng)安裝文件昧旨,若無(wú)權(quán)限,使用chmod +x NVIDIA-390.run)
sudo service lightdm start #(打開(kāi)圖形界面)
nvidia-smi #(查看顯卡及驅(qū)動(dòng)狀態(tài),smi--System Management Interface)
三臼予、安裝CUDA9.0+cuDNN7.2.1
下載CUDA9.0
https://developer.nvidia.com/cuda-toolkit-archive
上述網(wǎng)站選擇適合當(dāng)前系統(tǒng)的CUDA版本鸣戴,下載.run文件
測(cè)試方法
終端輸入如下指令查詢cuda版本信息
nvcc --version
把CUDA路徑添加到用戶環(huán)境變量里
sudo gedit ~/.bashrc
打開(kāi)文件,把下面兩條添加到文件末尾
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
執(zhí)行
source ~/.bashrc
使配置文件生效
3.2 安裝cuDNN
下載地址
https://developer.nvidia.com/rdp/cudnn-download
[cuDNN v7.2.1 Library for Linux]
下載得到tgz文件
tar xvf cudnn-xxx
進(jìn)入cudnn7.2.1解壓之后的include目錄粘拾,在命令行進(jìn)行如下操作:
sudo cp cudnn.h /usr/local/cuda/include/ #復(fù)制頭文件
再進(jìn)入lib64目錄下的動(dòng)態(tài)文件進(jìn)行復(fù)制和鏈接:
sudo cp lib* /usr/local/cuda/lib64/ #復(fù)制動(dòng)態(tài)鏈接庫(kù)
cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.7 #刪除原有動(dòng)態(tài)文件
sudo ln -s libcudnn.so.7.2.1 libcudnn.so.7 #生成軟銜接
sudo ln -s libcudnn.so.7 libcudnn.so #生成軟鏈接
四窄锅、安裝OpenCV3.4.2
4.1 OpenCV依賴包
GCC 4.4.x or later
CMake 2.6 or higher
Git
GTK+2.x or higher, including headers (libgtk2.0-dev) # 控制opencv GUI
pkg-config
Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, - - python-numpy)
ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, - libswscale-dev
[optional] libtbb2 libtbb-dev
[optional] libdc1394 2.x
[optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
安裝指令
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 #優(yōu)化opencv功能
sudo apt-get install ffmpeg
4.2 OpenCV3.4.2源碼下載配置
創(chuàng)建文件夾并打開(kāi)
mkdir OpenCV
cd OpenCV
官方下載地址
https://opencv.org/releases.html
注:官網(wǎng)下載速度太慢,在CSDN上下載了一個(gè).7z的壓縮源碼
7z解壓方法
sudo apt install p7zip-full #安裝解壓軟件
7z x opencv3.4.2+contrib.7z -r -o /home/xx # x ---- 解壓縮缰雇;-r ---- 表示遞歸所有的子文件夾入偷;-o 是指定解壓到的目錄
unzip opencv-3.4.2.zip
unzip opencv_contrib-3.4.2.zip # 解壓官方源碼壓縮包
配置編譯opencv (NVIDIA CUDA版本)
cd opencv-3.4.2
mkdir build && cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/OpenCV/opencv_contrib-3.4.2/modules \
-D PYTHON_EXCUTABLE=/usr/bin/python \
-D WITH_CUDA=ON \ # 使用CUDA
-D WITH_CUBLAS=ON \
-D DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" \
-D CUDA_ARCH_BIN="6.1" \ # 這個(gè)需要去官網(wǎng)確認(rèn)使用的GPU所對(duì)應(yīng)的版本(https://developer.nvidia.com/cuda-gpus)
-D CUDA_ARCH_PTX="" \
-D CUDA_FAST_MATH=ON \ # 計(jì)算速度更快但是相對(duì)不精確
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \ # 如果qt未安裝可以刪去此行;若因?yàn)槲凑_安裝qt導(dǎo)致的Qt5Gui報(bào)錯(cuò),可將build內(nèi)文件全部刪除后重新cmake械哟,具體可以參考[這里](http://stackoverflow.com/questions/17420739/opencv-2-4-5-and-qt5-error-s)
-D WITH_GTK=ON \
-D WITH_OPENGL=ON \
-D BUILD_EXAMPLES=ON ..
(上面指令報(bào)錯(cuò)找不到目錄疏之,把-D后面空格刪除后,可行暇咆,不知道什么原理)
編譯完成之后安裝
sudo make -j6
sudo make install
make時(shí)出錯(cuò)锋爪,錯(cuò)誤信息:
[ 88%] Built target example_gpu_optical_flow
Scanning dependencies of target example_gpu_video_reader
[ 88%] Building CXX object samples/gpu/CMakeFiles/example_gpu_video_reader.dir/video_reader.cpp.o
/usr/bin/ld: CMakeFiles/example_gpu_multi.dir/multi.cpp.o: undefined reference to symbol '_ZN3tbb18task_group_contextD1Ev'
//usr/lib/x86_64-linux-gnu/libtbb.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
samples/gpu/CMakeFiles/example_gpu_multi.dir/build.make:120: recipe for target 'bin/example_gpu_multi' failed
make[2]: *** [bin/example_gpu_multi] Error 1
CMakeFiles/Makefile2:46979: recipe for target 'samples/gpu/CMakeFiles/example_gpu_multi.dir/all' failed
make[1]: *** [samples/gpu/CMakeFiles/example_gpu_multi.dir/all] Error 2
make[1]: *** 正在等待未完成的任務(wù)....
[ 88%] Built target example_gpu_farneback_optical_flow
[ 88%] Linking CXX executable ../../bin/example_gpu_generalized_hough
[ 88%] Built target opencv_perf_superres
[ 88%] Linking CXX executable ../../bin/example_gpu_video_reader
[ 88%] Built target example_gpu_generalized_hough
[ 88%] Linking CXX executable ../../bin/example_gpu_pyrlk_optical_flow
[ 88%] Linking CXX executable ../../bin/example_gpu_stereo_multi
[ 88%] Linking CXX executable ../../bin/example_gpu_opticalflow_nvidia_api
[ 88%] Built target example_gpu_video_reader
[ 88%] Built target example_gpu_pyrlk_optical_flow
[ 88%] Built target example_gpu_opticalflow_nvidia_api
[ 88%] Built target example_gpu_stereo_multi
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2
錯(cuò)誤原因猜測(cè)為關(guān)于multi gpu的example例程出問(wèn)題了,所以更改cmake指令參數(shù)-DBUILD_EXAMPLES=OFF爸业,并添加-DBUILD_TESTS=OFF(此句應(yīng)該不添加也沒(méi)問(wèn)題)
將make生成清除再重新cmake其骄、make
$ make clean
$ cmake -DCMAKE_BUILD_TYPE=RELEASE \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DINSTALL_PYTHON_EXAMPLES=ON \
-DINSTALL_C_EXAMPLES=OFF \
-DOPENCV_EXTRA_MODULES_PATH=~/OpenCV/opencv_contrib-3.4.2/modules \
-DBUILD_opencv_python3=ON -DBUILD_opencv_python2=OFF \ ##這里做了些更改,生成與Anaconda+Python3匹配的共享庫(kù)(2018.08.15)
-DPYTHON_EXCUTABLE=/home/guo/anaconda3/bin/python3 \
-DPYTHON3_INCLUDE_DIR=/home/guo/anaconda3/include/python3.6m \
-DPYTHON3_LIBRARY=/home/guo/anaconda3/lib/libpython3.6m.so.1.0 \
-DPYTHON_NUMPY_PATH=/home/guo/anaconda3/lib/python3.6/site-packages \
-DWITH_CUDA=ON \
-DWITH_CUBLAS=ON \
-DDCUDA_NVCC_FLAGS="-D_FORCE_INLINES" \
-DCUDA_ARCH_BIN="6.1" \
-DCUDA_ARCH_PTX="" \
-DCUDA_FAST_MATH=ON \
-DWITH_TBB=ON \
-DWITH_V4L=ON \
-DWITH_GTK=ON \
-DWITH_OPENGL=ON \
-DBUILD_EXAMPLES=OFF \
-DWITH_OPENMP=ON \
-DBUILD_TESTS=OFF ..
##將生成的Anaconda+Python3匹配的共享庫(kù)復(fù)制到anaconda lib python3.6的目錄下(2018.08.15)
$ cp /home/guo/OpenCV/opencv-3.4.2/build/lib/python3/* ~/anaconda3/lib/python3.6/site-packages/
$ sudo make -j6
$ sudo make install
用python測(cè)試
import cv2
cv2.__version__
4.2 OpenCV環(huán)境配置
環(huán)境配置添加庫(kù)路徑
sudo gedit /etc/ld.so.conf.d/opencv.conf
#打開(kāi)后可能是空文件扯旷,在文件內(nèi)容最后添加
/usr/local/lib
更新系統(tǒng)庫(kù)
sudo ldconfig
配置bash拯爽,執(zhí)行如下命令
sudo gedit /etc/bash.bashrc
#在末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
保存退出,然后執(zhí)行如下命令使得配置生效
source /etc/bash.bashrc
# 激活配置然后更新database
sudo updatedb
4.3 在CLion中測(cè)試
創(chuàng)建CMakeLists.txt文件
cmake_minimum_required(VERSION 3.10)
project(demo)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)
add_executable(demo main.cpp)
target_link_libraries( demo ${OpenCV_LIBS} )
創(chuàng)建Main.cpp文件
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image;
# 必須使用絕對(duì)路徑
image = imread("/home/nic/20171125154428073.png");
if ( image.empty() )
{
cout<<"No image data!"<< endl;
return -1;
}
namedWindow("Display Image");
imshow("Display Image", image);
waitKey(0);
return 0;
}
進(jìn)行測(cè)試
五钧忽、Tensorflow安裝
根據(jù)Tensorflow官網(wǎng)的安裝教程 https://www.tensorflow.org/install/install_linux
首先添加環(huán)境變量
sudo gedit ~/.bashrc
打開(kāi)配置文件毯炮,添加
export CUDA_HOME=/usr/local/cuda ## 打開(kāi)目錄,發(fā)現(xiàn)/usr/local下有一個(gè)cuda-9.0的快捷方式
然后耸黑,運(yùn)行指令
source ~/.bashrc
更新配置
Tensorflow官網(wǎng)安裝教程里桃煎,需要安裝libcupti-dev庫(kù),安裝教程崎坊,運(yùn)行下面代碼
sudo apt-get install cuda-command-line-tools
提示無(wú)法定位軟件备禀,經(jīng)過(guò)百度,google查到奈揍,在安裝CUDA9.0時(shí)已經(jīng)安裝了此庫(kù)曲尸,在如下位置
/usr/local/cuda/extras/CUPTI/lib64
然后,只要添加一下環(huán)境變量就可以了男翰,添加方法跟前面一樣另患,在 ~/.bashrc最后添加,記得執(zhí)行 source ~/.bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/extras/CUPTI/lib64
繼續(xù)跟隨官網(wǎng)教程蛾绎,有一個(gè)可選的安裝選項(xiàng)昆箕,Nvidia TensorRT3.0鸦列,查了一下,可以加速模型預(yù)測(cè)速度鹏倘,在Nvidia官網(wǎng)下載了一個(gè)3.0.4版本(Ubuntu14.04薯嗤,Tensorflow教程中要求下載此版本)的tar文件,根據(jù)Nvidia教程解壓纤泵,添加環(huán)境變量骆姐,后續(xù)沒(méi)有嘗試其他,以后用到的話再說(shuō)
最后捏题,根據(jù)Tensorflow官網(wǎng)教程使用 Virtualenv 安裝 TensorFlow:
1.發(fā)出下列其中一條命令來(lái)安裝 pip 和 Virtualenv:
$ sudo apt-get install python-pip python-dev python-virtualenv # for Python 2.7
$ sudo apt-get install python3-pip python3-dev python-virtualenv # for Python 3.n
2.發(fā)出下列其中一條命令來(lái)創(chuàng)建 Virtualenv 環(huán)境:
$ virtualenv --system-site-packages targetDirectory # for Python 2.7
$ virtualenv --system-site-packages -p python3 targetDirectory # for Python 3.n
targetDirectory 用于指定 Virtualenv 樹(shù)的頂層目錄玻褪。我們的指令假定 targetDirectory 為 ~/tensorflow,但您可以選擇任何目錄公荧。
3.通過(guò)發(fā)出下列其中一條命令激活 Virtualenv 環(huán)境:
$ source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh
$ source ~/tensorflow/bin/activate.csh # csh or tcsh
$ . ~/tensorflow/bin/activate.fish # fish
4.確保安裝了 pip 8.1 或更高版本:
(tensorflow)$ easy_install -U pip
5.發(fā)出下列其中一條命令以在活動(dòng) Virtualenv 環(huán)境中安裝 TensorFlow:
(tensorflow)$ pip install --upgrade tensorflow # for Python 2.7
(tensorflow)$ pip3 install --upgrade tensorflow # for Python 3.n
(tensorflow)$ pip install --upgrade tensorflow-gpu # for Python 2.7 and GPU
(tensorflow)$ pip3 install --upgrade tensorflow-gpu # for Python 3.n and GPU
等待下載安裝完成带射,根據(jù)提示如缺失依賴庫(kù)就用pip指令安裝相應(yīng)庫(kù)即可,本人電腦上沒(méi)有出現(xiàn)問(wèn)題循狰。
6.測(cè)試窟社,需要在tensorflow環(huán)境激活狀態(tài)下,python指令進(jìn)入python終端晤揣,執(zhí)行下面代碼
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
若輸出
b'Hello, TensorFlow!'
說(shuō)明安裝沒(méi)有問(wèn)題~
7.在jupyter-notebook中使用tensorflow virtualenv的問(wèn)題
因?yàn)橐婚_(kāi)始是在virtualenv下安裝的虛擬環(huán)境桥爽,所里打開(kāi)jupyter-notebook后,看不到相關(guān)的kernel昧识,查詢資料,首先安裝了nb_conda軟件包盗扒,使用conda install
conda install nb_conda
安裝完成后跪楞,發(fā)現(xiàn)可以看到conda下的虛擬環(huán)境,還是看不到virtualenv下的tensorflow環(huán)境侣灶,繼續(xù)查資料甸祭,使用ipykernel軟件包,通過(guò)pip安裝
sudo pip install ipykernel
安裝完成后褥影,使用下述命令建立tensorflow虛擬環(huán)境的Kernel池户,注意在tensorflow環(huán)境激活狀態(tài)下
python -m ipykernel install --user --name=tensorflow
這樣tensorflow Kernel就建立好了,嘗試了一下凡怎,不必在tensorflow環(huán)境激活狀態(tài)下啟動(dòng)jupyter-notebook校焦,也可以使用tensorflow Kernel!