# cuda-image-preprocess **Repository Path**: gfb1986144783/cuda-image-preprocess ## Basic Information - **Project Name**: cuda-image-preprocess - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-01-09 - **Last Updated**: 2025-01-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Cuda编程加速图像预处理 ## 项目简介 - 基于 `cuda` 和 `opencv` 环境 - **目标:** - 单独使用,以加速图像处理操作; - **结合 TensorRT 使用,进一步加快推理速度** ## 加速效果 - 这里对比 `Deeplabv3+ ` 使用 `cuda` 预处理前后的 tensorrt 推理速度 - 未使用cuda图像预处理的代码,可参考作者的另一个 [tensorrt](https://github.com/emptysoal/tensorrt-experiment) 的项目: | Deeplabv3+ | FP32 | FP16 | INT8 | | :-----------: | :------------: | :------------: | :------------: | | C++图像预处理 | 22 ms | 12 ms | 10 ms | | CUDA图像预处理 | 15 ms | 5 ms | 3 ms | - 对比 `YOLOv5-v5.0` 使用 `cuda` 预处理前后的 tensorrt 推理速度 | YOLOv5-v5.0 | FP32 | FP16 | INT8 | | :-----------: | :------------: | :------------: | :------------: | | C++图像预处理 | 12 ms | 8 ms | 6 ms | | CUDA图像预处理 | 6 ms | 3 ms | 3 ms | YOLOv5 TensorRT 推理代码源自作者其他的项目 [C++预处理](https://github.com/emptysoal/TensorRT-v8-YOLOv5-v5.0/tree/main) [CUDA预处理](https://github.com/emptysoal/TensorRT-v8-YOLOv5-v5.0/tree/cpp-preproc) ## 文件说明 ```bash project dir ├── bgr2rgb # 实现BGR转RGB的cuda加速 | ├── Makefile | └── bgr2rgb.cu ├── bilinear # 实现双线性插值的cuda加速 | ├── Makefile | └── resize.cu ├── hwc2chw # 实现通道维度前置的cuda加速 | ├── Makefile | └── transpose.cu ├── normalize # 实现归一化的cuda加速 | ├── Makefile | └── normal.cu ├── preprocess # 汇总以上的图像处理(不是简单的拼接),实现常用的图像预处理,之后输入到网络当中 | ├── Makefile | └── preprocess.cu ├── union_tensorrt # 将上述的图像预处理,结合TensorRT一起使用,对比推理加速效果 | ├── Makefile | ├── preprocess.cu | ├── preprocess.h | └── trt_infer.cpp # 用于模型推理 └── lena.jpg # 用于测试的图片 ``` ## 使用说明 ### 图像加速单一操作: - 对于目录:bgr2rgb、bilinear、hwc2chw、normalize,实现单一功能上的图像操作加速 - 使用测试: ```bash cd make ./ example: cd bgr2rgb make ./bgr2rgb ../lena.jpg ``` 备注:如果 cuda 或 opencv 安装目录与 Makefile 中的不同,记得切换成自己的 ### 常规图像预处理 - 在推理之前,图像通常需经过 Resize、BGR to RGB、HWC to CHW、Normalize - 使用测试: ```bash cd preprocess make ./preprocess ../lena.jpg # 即可对图像完成上述全部操作 ``` ### 结合 TensorRT 使用 使用方式: 1)根据作者的另一个 [tensorrt](https://github.com/emptysoal/tensorrt-experiment) 的项目,构建好环境,下载分割数据集,并训练Deeplabv3+网络 2)进入到目录:`Deeplabv3+/TensorRT/C++/api_model/` 3)将本项目的`union_tensorrt`目录下的文件放入上述目录中(或替换原文件) 4)依次执行以下命令来使用TensorRT推理 ```bash python pth2wts.py make ./trt_infer ``` 5)得到以下结果,则说明运行成功,同目录下会生成分割结果图像 ```bash Loading weights: ./para.wts Succeeded building backbone! Succeeded building aspp! Succeeded building decoder! Succeeded building total network! Succeeded building serialized engine! Succeeded building engine! Succeeded saving .plan file! Total image num is: 8 inference total cost is: 105ms average cost is: 19ms ```