赞
踩
基于 TensorRT 8.2.4 版本,具体环境见下面的环境构建部分
目标:
对比 pytorch、onnx runtime、tensorrt C++(包括onnxparser、原生api)、tensorrt python(包括onnxparser、原生api)等不同框架的推理速度
流程:
.pth模型文件;.pth -> .onnx -> .plan;.pth -> .npz-> .plan;.pth -> .onnx -> .plan;.pth -> .wts -> .plan;以下是基于 x86_64 Linux 的结果,对应本文的环境构建部分
| PyTorch | ONNX | Python trt onnxparser | Python trt api | C++ trt onnxparser | C++ trt api | |
|---|---|---|---|---|---|---|
| VGG16 | 93 ms | 74 ms | 9 ms | 9 ms | 5 ms | 5 ms |
| ResNet50 | 96 ms | 75 ms | 9 ms | 9 ms | 5 ms | 5 ms |
| UNet | 181 ms | 152 ms | 26 ms | 26 ms | 24 ms | 19 ms |
| Deeplabv3+ | 208 ms | 158 ms | 30 ms | 28 ms | 26 ms | 22 ms |
| YOLOv5-v5.0 | 85 ms | – | – | – | – | 12 ms |
备注:
| Python trt onnxparser | Python trt api | C++ trt onnxparser | C++ trt api | |
|---|---|---|---|---|
| ResNet50 | 6 ms | 6 ms | 3 ms | 2 ms |
| Deeplabv3+ | 15 ms | 15 ms | 11 ms | 10 ms |
补充:int8量化前后精度对比
| before quantization | after quantization | |
|---|---|---|
| ResNet50(precision) | 95.08% | 95.08% |
| Deeplabv3+(mIOU) | 61.99% | 60.96% |
补充内容
展示由 CUDA 做图像预处理,速度进一步提升的结果
x86_64 Linux ,具体环境见下方环境构建| FP32 | FP16 | INT8 | |
|---|---|---|---|
| Deeplabv3+, cpp-preprocess | 22 ms | 12 ms | 10 ms |
| Deeplabv3+, cuda-preprocess | 15 ms | 5 ms | 3 ms |
| YOLOv5-v5.0, cpp-preprocess | 12 ms | 8 ms | 6 ms |
| YOLOv5-v5.0, cuda-preprocess | 6 ms | 3 ms | 3 ms |
数据来源:作者的另外 2 个项目 CUDA加速图像处理 ,TensorRT-YOLOv5-v5.0
Jetson Xavier NX硬件 和 Jetpack 4.6.1 系统| FP32 | FP16 | |
|---|---|---|
| Deeplabv3+, cpp-preprocess | 152 ms | – |
| Deeplabv3+, cuda-preprocess | 143 ms | 54 ms |
| YOLOv5-v5.0, cpp-preprocess | 45 ms | – |
| YOLOv5-v5.0, cuda-preprocess | 38 ms | 28 ms |
docker pull nvcr.io/nvidia/tensorrt:22.04-py3
| CUDA | cuDNN | TensorRT | python |
|---|---|---|---|
| 11.6.2 | 8.4.0.27 | 8.2.4.2 | 3.8.10 |
创建 docker 容器
docker run -it --gpus device=0 --shm-size 32G -v /home:/workspace nvcr.io/nvidia/tensorrt:22.04-py3 bash
其中-v /home:/workspace将宿主机的/home目录挂载到容器中,方便一些文件的交互,也可以选择其他目录
cd /etc/apt
rm sources.list
vim sources.list
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
apt update
安装 OpenCV-4.5.0
/home目录下,即容器的/workspace目录下https://github.com/opencv/opencv
# 安装依赖
apt install build-essential
apt install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
apt install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
# 开始安装 OpenCV
cd /workspace/opencv-4.5.0
mkdir build
cd build
cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_ENABLE_NONFREE=True ..
make -j6
make install
torch-1.12.0进入链接 https://download.pytorch.org/whl/torch/
找到 torch-1.12.0+cu116-cp38-cp38-linux_x86_64.whl
下载后放到 /workspace 目录下
pip install torch-1.12.0+cu116-cp38-cp38-linux_x86_64.whl
torchvision-0.13.0进入链接 https://download.pytorch.org/whl/torchvision/
找到 torchvision-0.13.0+cu116-cp38-cp38-linux_x86_64.whl
下载后放到 /workspace 目录下
pip install torchvision-0.13.0+cu116-cp38-cp38-linux_x86_64.whl
安装其他 python 库
pip install opencv-python==3.4.16.59
pip install opencv-python-headless==3.4.16.59
pip install tensorboard
pip install cuda-python
pip install onnxruntime-gpu==1.10
pip install scipy
pip install matplotlib
pip install tqdm
下载速度慢或超时的话,在后面加上-i https://pypi.tuna.tsinghua.edu.cn/simple/
project dir
├── flower_classify_dataset # 分类项目所用到的数据集,5种花的分类,完整数据集可通过下方链接获取
│ ├── train
│ ├── val
│ └── test
├── Camvid_segment_dataset # 语义分割项目所用到的数据集,完整数据集可通过下方链接获取
│ ├── images # 原始图像目录
| │ ├── train
| │ ├── val
| │ └── test
│ ├── labels # mask标签目录,类别index所构成
│ ├── train.lst # 原始图像路径\t标签路径
│ ├── val.lst # 原始图像路径\t标签路径
│ └── labels.txt # 标签index和其所对应的颜色及类别
├── VGG16
│ ├── PyTorch
│ │ ├── dataset.py
│ │ ├── model # 运行train.py时自动生成,pth模型会保存到其中
│ │ ├── pytorch_inference.py # 使用pytorch推理
│ │ ├── train.py # 启动训练文件
│ │ └── vgg.py # 模型文件
│ └── TensorRT
│ ├── C++
│ │ ├── api_model
│ │ │ ├── calibrator.cpp # int8量化
│ │ │ ├── calibrator.h
│ │ │ ├── Makefile
│ │ │ ├── pth2wts.py # pth模型文件转wts文件
│ │ │ ├── public.h
│ │ │ ├── trt_infer.cpp # 构建 tensorrt engine 及使用 engine 推理
│ │ │ └── vgg.py
│ │ └── onnx_parser
│ │ ├── calibrator.cpp
│ │ ├── calibrator.h
│ │ ├── Makefile
│ │ ├── onnx_infer.py # pth模型文件转onnx文件 并使用 onnx runtime 推理
│ │ ├── public.h
│ │ ├── trt_infer.cpp # onnx 转 tensorrt engine 及使用 engine 推理
│ │ └── vgg.py
│ └── python
│ ├── api_model
│ │ ├── calibrator.py # int8量化
│ │ ├── pth2npz.py # pth模型文件转 numpy 文件
│ │ ├── trt_inference.py # 构建 tensorrt engine 及使用 engine 推理
│ │ └── vgg.py
│ └── onnx_parser
│ ├── calibrator.py
│ ├── onnx_infer.py # pth模型文件转onnx文件 并使用 onnx runtime 推理
│ ├── trt_infer.py # onnx 转 tensorrt engine 及使用 engine 推理
│ └── vgg.py
├── ResNet50 # 文件结构基本同VGG16
│ ├── PyTorch
│ └── TensorRT
│ ├── C++
│ │ ├── api_model
│ │ └── onnx_parser
│ └── python
│ ├── api_model
│ └── onnx_parser
├── UNet # 文件结构基本同VGG16
│ ├── PyTorch
│ └── TensorRT
│ ├── C++
│ │ ├── api_model
│ │ └── onnx_parser
│ └── python
│ ├── api_model
│ └── onnx_parser
└── Deeplabv3+ # 文件结构基本同VGG16
├── PyTorch
└── TensorRT
├── C++
│ ├── api_model
│ └── onnx_parser
└── python
├── api_model
└── onnx_parser
至此,可运行项目中全部程序。
感谢阅毕,欢迎点赞收藏。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。