§ 1.3 Open3D编译与安装 (Ubuntu20.04 - CUDA加速)

1. 开发环境

  • 操作系统:Ubuntu 20.04
  • 显卡型号:1080Ti

2. 安装GPU显卡驱动

参考博客: 输入ubuntu-drivers devices 不返回任何结果同时安装显卡驱动

添加源

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

更新

sudo apt-get update

安装

sudo apt-get install nvidia-dkms-470 nvidia-driver-470 nvidia-settings nvidia-prime

驱动完成之后,需要重启操作系统。

验证驱动是否安装成功:

nvidia-smi
kyle@deepsense:~/Project$ nvidia-smi
Thu Nov 25 14:06:42 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.86       Driver Version: 470.86       CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  Off  | 00000000:01:00.0  On |                  N/A |
| 32%   32C    P8    18W / 250W |    407MiB / 11175MiB |     10%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A       896      G   /usr/lib/xorg/Xorg                137MiB |
|    0   N/A  N/A      1188      G   /usr/bin/gnome-shell               96MiB |
|    0   N/A  N/A      2250      G   ...mviewer/tv_bin/TeamViewer       21MiB |
|    0   N/A  N/A      2835      G   ...AAAAAAAAA= --shared-files       28MiB |
|    0   N/A  N/A      2922      G   /usr/lib/firefox/firefox          116MiB |
+-----------------------------------------------------------------------------+

3. 安装CUDA

安装Cuda前,如果已经装过GPU驱动,在软件和更新里面,选择附件驱动。勾选 X.Org Xserver ,然后重启。

https://developer.nvidia.com/cuda-toolkit-archive

选择版本: cuda 11.2.0

Cuda版本型号需要跟GPU驱动型号匹配,有版本要求,确保你安装的GPU驱动版本号大于最小版本号要求。

https://developer.nvidia.com/cuda-11.2.0-download-archive?target_os=Linux⌖_arch=x86_64⌖_distro=Ubuntu⌖_version=2004⌖_type=runfilelocal

CUDA 11.2.0 Update 2 >=460.32.03

选择自己的型号设置

  • Linux
  • x86_64
  • Ubuntu
  • Ubuntu 20.04
  • runfile (local)

.run 里面也内置了一个显卡驱动,注意不要勾选安装GPU驱动的选项,不需要卸载原来的GPU。测试 文档 示例这些都可以不勾选。

安装成功日志:

$ sudo sh cuda_11.2.0_460.27.04_linux.run
[sudo] kyle 的密码: 
===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-11.2/
Samples:  Installed in /home/kyle/

Please make sure that
 -   PATH includes /usr/local/cuda-11.2/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-11.2/lib64, or, add /usr/local/cuda-11.2/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-11.2/bin
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 460.00 is required for CUDA 11.2 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run --silent --driver

Logfile is /var/log/cuda-installer.log

编辑 /etc/environment

sudo gedit /etc/environment

追加一行

注:以实际安装的cuda版本号为准

CUDACXX="/usr/local/cuda/bin/nvcc"

.bashrc 文件中追加

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

修改完成后需要重启。

4. 安装CuDNN

在英伟达官网找到cuda版本号对应的cudnn。

https://developer.nvidia.com/rdp/cudnn-archive

找到cuda 11.2.0对应的最新的兼容的cudnn版本是 Download cuDNN v8.2.1 (June 7th, 2021), for CUDA 11.x

下载 cuDNN Library for Linux (x86_64)

压缩包解压

tar zxf cudnn-11.3-linux-x64-v8.2.1.32.tgz 

拷贝文件

sudo cp cuda/include/* /usr/local/cuda/include/
sudo cp cuda/lib64/* /usr/local/cuda/lib64/

参考博客: NVIDIA Ubuntu 20.04 安装 nvidia-460 + cuda-11.2

5. 升级CMake

参考博客: Ubuntu 如何更新 Cmake 最新版

open3d依赖的cmake版本

cmake_minimum_required(VERSION 3.19.2)

安装依赖

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates gnupg software-properties-common wget

增加key

wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null

增加repository

sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ focal main'
sudo apt-get update

更新CMake的版本号

 sudo apt-get install cmake --upgrade

6. OpenCV

通过pip3安装opencv

sudo pip3 install opencv-python
sudo pip3 install opencv-contrib-python

7. 安装PyTorch

注:可选,非必须

通过pip3安装 torch

sudo pip3 install torch
sudo pip3 install torchvision

8. 从源码编译Open3D

官方文档: http://www.open3d.org/docs/latest/compilation.html

克隆Open3D代码仓库,截止到目前Open3D最新的版本号为 0.15

git clone https://github.com/isl-org/Open3D

克隆Open3D-ML模块

git clone https://github.com/isl-org/Open3D-ML.git

安装ubuntu下所需依赖

sudo util/install_deps_ubuntu.sh

创建build文件夹

mkdir build
cd build

cmake

只选择CUDA模块选项,其他的可以根据情况选择。

# In the build directory
cmake -DPython3_ROOT=/usr/bin/python3 \
	  -DBUILD_CUDA_MODULE=ON \
      -DGLIBCXX_USE_CXX11_ABI=OFF \
      -DBUILD_PYTORCH_OPS=OFF \
      -DBUILD_TENSORFLOW_OPS=OFF \
      -DBUNDLE_OPEN3D_ML=OFF \
      ..

带机器学习模块的版本

注:编译ML模块没成功,还存在一些版本不兼容不稳定的情况?以后再试。

# In the build directory
cmake -DPython3_ROOT=/usr/bin/python3 \
	  -DBUILD_CUDA_MODULE=ON \
   -DGLIBCXX_USE_CXX11_ABI=OFF \
   -DBUILD_PYTORCH_OPS=ON \
   -DBUILD_TENSORFLOW_OPS=OFF \
   -DBUNDLE_OPEN3D_ML=ON \
   -DOPEN3D_ML_ROOT=/home/kyle/Software/Open3D-ML\
   ..

安装Open3D的C++库

sudo make install

构建并安装pip安装包

sudo make install-pip-package

检验下Open3D有没有被正确安装

$ python3
Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import open3d
>>> open3d.__version__
'0.15.1+76b494ef5'
>>> 

9. 联系作者