|
| 1 | +# Cuda编程加速图像预处理 |
| 2 | + |
| 3 | +## 项目简介 |
| 4 | + |
| 5 | +- 基于 `cuda` 和 `opencv` 环境 |
| 6 | + |
| 7 | +- **目标:** |
| 8 | + - 单独使用,以加速图像处理操作; |
| 9 | + - **结合 TensorRT 使用,进一步加快推理速度** |
| 10 | + |
| 11 | +## 加速效果 |
| 12 | + |
| 13 | +- 这里对比 `Deeplabv3+ ` 使用 `cuda` 预处理前后的 tensorrt 推理速度 |
| 14 | +- 未使用cuda图像预处理的代码,可参考作者的另一个 [tensorrt](https://github.com/emptysoal/tensorrt-experiment) 的项目: |
| 15 | + |
| 16 | +FP32精度: |
| 17 | + |
| 18 | +| C++图像预处理 | cuda图像预处理 | |
| 19 | +| :-----------: | :------------: | |
| 20 | +| 25 ms | 19 ms | |
| 21 | + |
| 22 | +Int8量化后: |
| 23 | + |
| 24 | +| C++图像预处理 | cuda图像预处理 | |
| 25 | +| :-----------: | :------------: | |
| 26 | +| 10 ms | **3 ms** | |
| 27 | + |
| 28 | +## 文件说明 |
| 29 | + |
| 30 | +```bash |
| 31 | +project dir |
| 32 | + ├── bgr2rgb # 实现BGR转RGB的cuda加速 |
| 33 | + | ├── Makefile |
| 34 | + | └── bgr2rgb.cu |
| 35 | + ├── bilinear # 实现双线性插值的cuda加速 |
| 36 | + | ├── Makefile |
| 37 | + | └── resize.cu |
| 38 | + ├── hwc2chw # 实现相当于transpose((2, 0, 1))的cuda加速 |
| 39 | + | ├── Makefile |
| 40 | + | └── transpose.cu |
| 41 | + ├── normalize # 实现归一化的cuda加速 |
| 42 | + | ├── Makefile |
| 43 | + | └── normal.cu |
| 44 | + ├── preprocess # 汇总以上的图像处理(不是简单的拼接),实现常用的图像预处理,之后输入到网络当中 |
| 45 | + | ├── Makefile |
| 46 | + | └── preprocess.cu |
| 47 | + ├── union_tensorrt # 将上述的图像预处理,结合TensorRT一起使用,对比推理加速效果 |
| 48 | + | ├── Makefile |
| 49 | + | ├── preprocess.cu |
| 50 | + | ├── preprocess.h |
| 51 | + | └── trt_infer.cpp # 用于模型推理 |
| 52 | + └── lena.jpg # 用于测试的图片 |
| 53 | +``` |
| 54 | + |
| 55 | +## 使用说明 |
| 56 | + |
| 57 | +### 图像加速单一操作: |
| 58 | + |
| 59 | +- 对于目录:bgr2rgb、bilinear、hwc2chw、normalize,实现单一功能上的图像操作加速 |
| 60 | +- 使用测试: |
| 61 | + |
| 62 | +```bash |
| 63 | +cd <dir name> |
| 64 | +make |
| 65 | +./<bin file> <image path> |
| 66 | + |
| 67 | +example: |
| 68 | +cd bgr2rgb |
| 69 | +make |
| 70 | +./bgr2rgb ../lena.jpg |
| 71 | +``` |
| 72 | + |
| 73 | +备注:如果 cuda 或 opencv 安装目录与 Makefile 中的不同,记得切换成自己的 |
| 74 | + |
| 75 | +### 常规图像预处理 |
| 76 | + |
| 77 | +- 在推理之前,图像通常需经过 Resize、BGR to RGB、HWC to CHW、Normalize |
| 78 | +- 使用测试: |
| 79 | + |
| 80 | +```bash |
| 81 | +cd preprocess |
| 82 | +make |
| 83 | +./preprocess ../lena.jpg # 即可对图像完成上述全部操作 |
| 84 | +``` |
| 85 | + |
| 86 | +### 结合 TensorRT 使用 |
| 87 | + |
| 88 | +使用方式: |
| 89 | + |
| 90 | +1)根据作者另一个 [tensorrt](https://github.com/emptysoal/tensorrt-experiment) 的项目,构建好环境,下载分割数据集,并训练Deeplabv3+网络 |
| 91 | + |
| 92 | +2)进入到目录:`Deeplabv3+/TensorRT/C++/api_model/` |
| 93 | + |
| 94 | +3)将本项目的`union_tensorrt`目录下的文件放入上述目录中(或替换原文件) |
| 95 | + |
| 96 | +4)依次执行以下命令来使用TensorRT推理 |
| 97 | + |
| 98 | +```bash |
| 99 | +python pth2wts.py |
| 100 | +make |
| 101 | +./trt_infer |
| 102 | +``` |
| 103 | + |
| 104 | +5)得到以下结果,说明运行成功,同目录下会生成分割结果图像 |
| 105 | + |
| 106 | +```bash |
| 107 | +Loading weights: ./para.wts |
| 108 | +Succeeded building backbone! |
| 109 | +Succeeded building aspp! |
| 110 | +Succeeded building decoder! |
| 111 | +Succeeded building total network! |
| 112 | +Succeeded building serialized engine! |
| 113 | +Succeeded building engine! |
| 114 | +Succeeded saving .plan file! |
| 115 | +Total image num is: 8 inference total cost is: 105ms average cost is: 19ms |
| 116 | +``` |
| 117 | + |
0 commit comments