ubuntu16.04安裝cuda版本的opencv3.3.0
1. 驗證NVIDIA驅動
1.1 查看是否已經安裝NVIDIA驅動
安裝glxinfo:
sudo apt-get install mesa-utils
查看:
glxinfo | grep rendering
如果輸出:
說明已經安裝了nvidia驅動。
或者用命令nvidia-smi
,出現下面界面,說明安裝成功。
1.2 安裝NVIDIA驅動
- 查看gpu對應的NVIDIA驅動版本,進入網站:NVIDIA,選擇自己的gpu信息,如下:
- 點選擇,然後DOWNLOAD。
- 卸載原有的nvidia驅動,
sudo apt-get purge nvidia*
- 禁用nouveau:
- 終端:
$ sudo gedit /etc/modprobe.d/blacklist-nouveau.conf
,在後面添加如下內容後保存:
blacklist nouveau
options nouveau modeset=0
- 更新,
sudo update-initramfs -u
- 重啓電腦,
lsmod | grep nouveau
,如果沒有輸出,說明禁用成功。 - 如果仍有沒禁用的nouveau文件,比如:nouveau.ko,nouveau.ko.org,則用命令禁用:
sudo rm -rf nouveau.ko
sudo rm -rf nouveau.ko.org
- 再更新
sudo update-initramfs -u
,重啓電腦。
- 安裝驅動
- ctrl+alt+F1進入命令行模式;
- 關閉圖像界面,
sudo service lightdm stop
- 進入NVIDIA下載的目錄,然後
sudo chmod a+x 'file'/NVIDIA-Linux-x86_64--390.87.run
sudo ./NVIDIA-Linux-x86_64--390.87.run --no-opengl-files --no-x-check --no-nouveau-check
後面幾個命令,可以有效防止循環啓動的問題。
- 安裝過程中,一直accept,或者yes就行了,除了configure X選擇否,這裏默認也是否,所以基本一路回車即可。
- 重啓圖像界面,
sudo service lightdm start
- 測試nvidia,輸入
nvidia-smi
,可以看到安裝成功。
2. 安裝cuda
我的顯卡是GTX1650,安裝的是cuda-10.2,建議用run方式安裝,不要用deb,具體大家要根據自己顯卡支持的cuda版本進行安裝。
2.1 獲取計算機對應版本cuda
進入網站https://developer.nvidia.com/cuda-gpus找到自己顯卡對應的cuda版本,然後下載。
另外,查看自己的NVIDIA驅動支持的cuda版本。
下載好對應的版本的runfile文件,比如我的:
2.2 安裝cuda
① ctrl+alt+F1進入命令行模式;
② 關閉圖像界面,sudo service lightdm stop
③ 進入目錄,安裝:sudo sh ./cuda_10.2.89_440.33.01_linux.run
,稍等片刻,出現(圖像來自網絡,版本不準):
這時,將鼠標移動至Driver那行,按空格取消選擇,因爲之前安裝過nvidia驅動了,這裏不再安裝,然後選擇Install,回車。
④ 安裝完後,啓動圖像界面,sudo service lightdm start
⑤ 配置環境,sudo gedit ~/.bashrc
,在末尾添加:
export PATH=/usr/local/cuda-10.2/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64:$LD_LIBRARY_PATH
⑥ source ~/.bashrc
,查看cuda是否安裝成功:nvcc - V
,出現:
‘
2.3 卸載cuda
cd /usr/local/cuda/bin
sudo ./cuda-uninstaller
cd /usr/local/cuda-10.2/bin
sudo ./cuda-uninstaller
sudo rm -rf /usr/local/cuda /usr/local/cuda-10.2
3. 安裝opencv3.3.0-cuda
文件下載和環境變了的配置請參考我的另一篇博客:ubuntu16.04(14.04)(重)安裝OpenCV3.3.0與opencv_contrib3.3.0及常見問題解決方案(附文件下載鏈接)
和普通opencv的安裝區別就是:cmake中告訴編譯器,需要安裝cuda版本opencv。
sudo cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local/opencv3.3.0 -DWITH_CUDA=ON -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.2 -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.3.0/modules/ -DCUDA_GENERATION=Kepler -DOPENCV_ENABLE_NONFREE=True ..
這裏需要注意的地方有:
-DCMAKE_INSTALL_PREFIX=/usr/local/opencv3.3.0
,由於我裝了多個版本opencv,所以設置安裝目錄爲這個,如果你是第一次安裝,也可以設置爲/usr/local
。-D CUDA_NVCC_FLAGS="-std=c++11 --expt-relaxed-constexpr"
,這句話是告訴cuda,也使用c++11來編譯,否則會報錯:“opencv cuda error: identifier “nullptr” is undefined”。-DOPENCV_EXTRA_MODULES_PATH=/home/jaf/package/opencv-3.3.0/opencv_contrib-3.3.0/modules
,這裏需要自己指定位置。
然後其他就和我上面提到的博客的安裝方式一致了。
這裏很可能報錯,因爲CUDA9.0以上不支持2.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
解決辦法如下:
- 在opencv-3.3.0/cmake文件夾下找到FindCUDA.cmake文件,或者用命令查找
sudo updatedb
locate FindCUDA.cmake
會發現有一個在opencv目錄下的文件,用gedit打開。
- 找到
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)
- 找到文件 OpenCVDetectCUDA.cmake,同樣可以用命令查找。
找到:
if(CUDA_GENERATION STREQUAL "Fermi")
set(__cuda_arch_bin "2.0")
將下一行的elseif,改爲if。
3. 找到文件opencv/modules/cudev/include/opencv2/cudev/common.hpp,添加頭文件
#include <cuda_fp16.h>
不支持的GPU arch_20問題
報錯如下:
nvcc fatal : Unsupported gpu architecture 'compute_20'
解決辦法,在cmake中加入一句(這個在我的裏面一句加了):
-D CUDA_GENERATION=Kepler