【實戰】yolov8 tensorrt模型加速部署
TensorRT-Alpha基於tensorrt+cuda c++實現模型end2end的gpu加速,支援win10、linux,在2023年已經更新模型:YOLOv8, YOLOv7, YOLOv6, YOLOv5, YOLOv4, YOLOv3, YOLOX, YOLOR,pphumanseg,u2net,EfficientDet。
Windows10教程正在製作,可以關注倉庫:https://github.com/FeiYull/TensorRT-Alpha
一、加速結果展示
1.1 效能速覽
?快速看看yolov8n 在移動端RTX2070m(8G)的新能表現:
model | video resolution | model input size | GPU Memory-Usage | GPU-Util |
---|---|---|---|---|
yolov8n | 1920x1080 | 8x3x640x640 | 1093MiB/7982MiB | 14% |
下圖是yolov8n的執行時間開銷,單位是ms:
更多TensorRT-Alpha測試錄影在B站影片:
B站:YOLOv8n
B站:YOLOv8s
1.2精度對齊
下面是左邊是python框架推理結果,右邊是TensorRT-Alpha推理結果。
YOLOv4 YOLOv3 YOLOR YOLOX略。
二、Ubuntu18.04環境配置
如果您對tensorrt不是很熟悉,請務必保持下面庫版本一致。
2.1 安裝工具鏈和opencv
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install git
sudo apt-get install gdb
sudo apt-get install cmake
sudo apt-get install libopencv-dev
# pkg-config --modversion opencv
2. 安裝Nvidia相關庫
注:Nvidia相關網站需要註冊賬號。
2.1 安裝Nvidia顯示卡驅動
ubuntu-drivers devices
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt install nvidia-driver-470-server # for ubuntu18.04
nvidia-smi
2.2 安裝 cuda11.3
- 進入連結: https://developer.nvidia.com/cuda-toolkit-archive
- 選擇:CUDA Toolkit 11.3.0(April 2021)
- 選擇:[Linux] -> [x86_64] -> [Ubuntu] -> [18.04] -> [runfile(local)]
在網頁你能看到下面安裝命令,我這裡已經複製下來:
wget https://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda_11.3.0_465.19.01_linux.run
sudo sh cuda_11.3.0_465.19.01_linux.run
cuda的安裝過程中,需要你在bash視窗手動作一些選擇,這裡選擇如下:
- select:[continue] -> [accept] -> 接著按下Enter鍵取消Driver和465.19.01這個選項,如下圖(it is important!) -> [Install]
bash視窗提示如下表示安裝完成
#===========
#= Summary =
#===========
#Driver: Not Selected
#Toolkit: Installed in /usr/local/cuda-11.3/
#......
把cuda新增到環境變數:
vim ~/.bashrc
把下面複製到 .bashrc裡面
# cuda v11.3
export PATH=/usr/local/cuda-11.3/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_HOME=/usr/local/cuda-11.3
重新整理環境變數和驗證
source ~/.bashrc
nvcc -V
bash視窗列印如下資訊表示cuda11.3安裝正常
nvcc: NVIDIA (R) Cuda compiler driver<br>
Copyright (c) 2005-2021 NVIDIA Corporation<br>
Built on Sun_Mar_21_19:15:46_PDT_2021<br>
Cuda compilation tools, release 11.3, V11.3.58<br>
Build cuda_11.3.r11.3/compiler.29745058_0<br>
2.3 安裝 cudnn8.2
- 進入網站:https://developer.nvidia.com/rdp/cudnn-archive
- 選擇: Download cuDNN v8.2.0 (April 23rd, 2021), for CUDA 11.x
- 選擇: cuDNN Library for Linux (x86_64)
- 你將會下載這個壓縮包: "cudnn-11.3-linux-x64-v8.2.0.53.tgz"
# 解壓
tar -zxvf cudnn-11.3-linux-x64-v8.2.0.53.tgz
將cudnn的標頭檔案和lib複製到cuda11.3的安裝目錄下:
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
2.4 下載 tensorrt8.4.2.4
本教程中,tensorrt只需要下載\、解壓即可,不需要安裝。
- 進入網站: https://developer.nvidia.cn/nvidia-tensorrt-8x-download
- 把這個打勾: I Agree To the Terms of the NVIDIA TensorRT License Agreement
- 選擇: TensorRT 8.4 GA Update 1
- 選擇: TensorRT 8.4 GA Update 1 for Linux x86_64 and CUDA 11.0, 11.1, 11.2, 11.3, 11.4, 11.5, 11.6 and 11.7 TAR Package
- 你將會下載這個壓縮包: "TensorRT-8.4.2.4.Linux.x86_64-gnu.cuda-11.6.cudnn8.4.tar.gz"
# 解壓
tar -zxvf TensorRT-8.4.2.4.Linux.x86_64-gnu.cuda-11.6.cudnn8.4.tar.gz
# 快速驗證一下tensorrt+cuda+cudnn是否安裝正常
cd TensorRT-8.4.2.4/samples/sampleMNIST
make
cd ../../bin/
匯出tensorrt環境變數(it is important!),注:將LD_LIBRARY_PATH:後面的路徑換成你自己的!後續編譯onnx模型的時候也需要執行下面第一行命令
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/xxx/temp/TensorRT-8.4.2.4/lib
./sample_mnist
bash視窗列印類似如下圖的手寫數字識別表明cuda+cudnn+tensorrt安裝正常
三、YOLOv8模型部署
3.1 下載倉庫TensorRT-Alpha
git clone https://github.com/FeiYull/tensorrt-alpha
3.2 獲取onnx檔案
直接在網盤下載 weiyun or google driver 或者使用如下命令匯出onnx:
# ? yolov8 官方倉庫: https://github.com/ultralytics/ultralytics
# ? yolov8 官方教程: https://docs.ultralytics.com/quickstart/
# ?TensorRT-Alpha will be updated synchronously as soon as possible!
# 安裝 yolov8
conda create -n yolov8 python==3.8 -y
conda activate yolov8
pip install ultralytics==8.0.5
pip install onnx
# 下載官方權重(".pt" file)
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8s.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8m.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8l.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x6.pt
匯出 onnx:
# 640
yolo mode=export model=yolov8n.pt format=onnx dynamic=True #simplify=True
yolo mode=export model=yolov8s.pt format=onnx dynamic=True #simplify=True
yolo mode=export model=yolov8m.pt format=onnx dynamic=True #simplify=True
yolo mode=export model=yolov8l.pt format=onnx dynamic=True #simplify=True
yolo mode=export model=yolov8x.pt format=onnx dynamic=True #simplify=True
# 1280
yolo mode=export model=yolov8x6.pt format=onnx dynamic=True #simplify=True
3.3 編譯 onnx
# 把你的onnx檔案放到這個路徑:tensorrt-alpha/data/yolov8
cd tensorrt-alpha/data/yolov8
# 請把LD_LIBRARY_PATH:換成您自己的路徑。
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/TensorRT-8.4.2.4/lib
# 640
../../../../TensorRT-8.4.2.4/bin/trtexec --onnx=yolov8n.onnx --saveEngine=yolov8n.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
../../../../TensorRT-8.4.2.4/bin/trtexec --onnx=yolov8s.onnx --saveEngine=yolov8s.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
../../../../TensorRT-8.4.2.4/bin/trtexec --onnx=yolov8m.onnx --saveEngine=yolov8m.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
../../../../TensorRT-8.4.2.4/bin/trtexec --onnx=yolov8l.onnx --saveEngine=yolov8l.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
../../../../TensorRT-8.4.2.4/bin/trtexec --onnx=yolov8x.onnx --saveEngine=yolov8x.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
# 1280
../../../../TensorRT-8.4.2.4/bin/trtexec --onnx=yolov8x6.onnx --saveEngine=yolov8x6.trt --buildOnly --minShapes=images:1x3x1280x1280 --optShapes=images:4x3x1280x1280 --maxShapes=images:8x3x1280x1280
你將會的到例如:yolov8n.trt、yolov8s.trt、yolov8m.trt等檔案。
3.4 編譯執行
git clone https://github.com/FeiYull/tensorrt-alpha
cd tensorrt-alpha/yolov8
mkdir build
cd build
cmake ..
make -j10
# 注: 效果圖預設儲存在路徑 tensorrt-alpha/yolov8/build
# 下面引數解釋
# --show 表示視覺化結果
# --savePath 表示儲存,預設儲存在build目錄
# --savePath=../ 儲存在上一級目錄
## 640
# 推理圖片
./app_yolov8 --model=../../data/yolov8/yolov8n.trt --size=640 --batch_size=1 --img=../../data/6406407.jpg --show --savePath
./app_yolov8 --model=../../data/yolov8/yolov8n.trt --size=640 --batch_size=8 --video=../../data/people.mp4 --show --savePath
# 推理影片
./app_yolov8 --model=../../data/yolov8/yolov8n.trt --size=640 --batch_size=8 --video=../../data/people.mp4 --show --savePath=../
# 線上推理相機影片
./app_yolov8 --model=../../data/yolov8/yolov8n.trt --size=640 --batch_size=2 --cam_id=0 --show
## 1280
# infer camera
./app_yolov8 --model=../../data/yolov8/yolov8x6.trt --size=1280 --batch_size=2 --cam_id=0 --show