主流推理框架在ubuntu上的速度对比实验

Torch TorchScript Onnx TensorRT 推理速度对比实验

本博文主要在ubuntu上进行主流推理框架在ubuntu上的速度对比实验,代码来源于pytorch-classifier,是博主自己整理的一个基于pytorch图像分类框架,这是详细的介绍基于pytorch实现的图像分类代码,这是代码的实践案例使用pytorch实现花朵分类,本博文也是pytorch-classifier v1.2版本更新日志.

pytorch-classifier v1.2 更新日志

  1. 新增export.py,支持导出(onnx, torchscript, tensorrt)模型.

  2. metrice.py支持onnx,torchscript,tensorrt的推理.

     此处在predict.py中暂不支持onnx,torchscript,tensorrt的推理的推理,原因是因为predict.py中的热力图可视化没办法在onnx、torchscript、tensorrt中实现,后续单独推理部分会额外写一部分代码.
     在metrice.py中,onnx和torchscript和tensorrt的推理也不支持tsne的可视化,那么我在metrice.py中添加onnx,torchscript,tensorrt的推理的目的是为了测试fps和精度.
     所以简单来说,使用metrice.py最好还是直接用torch模型,torchscript和onnx和tensorrt的推理的推理模型后续会写一个单独的推理代码.
    
  3. main.py,metrice.py,predict.py,export.py中增加–device参数,可以指定设备.

  4. 优化程序和修复一些bug.


训练命令:
python main.py --model_name efficientnet_v2_s --config config/config.py --batch_size 128 --Augment AutoAugment --save_path runs/efficientnet_v2_s --device 0 \
--pretrained --amp --warmup --ema --imagenet_meanstd
GPU 推理速度测试 sh脚本:
batch_size=1 # 1 2 4 8 16 32 64
python metrice.py --task fps --save_path runs/efficientnet_v2_s --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --half --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --model_type torchscript --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --half --model_type torchscript --batch_size $batch_size
python export.py --save_path runs/efficientnet_v2_s --export onnx --simplify --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --model_type onnx --batch_size $batch_size
python export.py --save_path runs/efficientnet_v2_s --export onnx --simplify --half --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --model_type onnx --batch_size $batch_size
python export.py --save_path runs/efficientnet_v2_s --export tensorrt --simplify --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --model_type tensorrt --batch_size $batch_size
python export.py --save_path runs/efficientnet_v2_s --export tensorrt --simplify --half --batch_size $batch_size 
python metrice.py --task fps --save_path runs/efficientnet_v2_s --model_type tensorrt --half --batch_size $batch_size
CPU 推理速度测试 sh脚本:
python export.py --save_path runs/efficientnet_v2_s --export onnx --simplify --dynamic --device cpu
batch_size=1
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --model_type torchscript --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --model_type onnx --batch_size $batch_size
batch_size=2
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --model_type torchscript --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --model_type onnx --batch_size $batch_size
batch_size=4
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --model_type torchscript --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --model_type onnx --batch_size $batch_size
batch_size=8
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --model_type torchscript --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --model_type onnx --batch_size $batch_size
batch_size=16
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --model_type torchscript --batch_size $batch_size
python metrice.py --task fps --save_path runs/efficientnet_v2_s --device cpu --model_type onnx --batch_size $batch_size

各导出模型在cpu和gpu上的fps实验:

实验环境:

SystemCPUGPURAMModel
Ubuntu20.04i7-12700KFRTX-309032G DDR5 6400efficientnet_v2_s
GPU
modelTorch FP32 FPSTorch FP16 FPSTorchScript FP32 FPSTorchScript FP16 FPSONNX FP32 FPSONNX FP16 FPSTensorRT FP32 FPSTensorRT FP16 FPS
batch-size 193.77105.65233.21260.07177.41308.52311.60789.19
batch-size 294.32108.35208.53253.83166.23258.98275.93713.71
batch-size 495.98108.31171.99255.05130.43190.03212.75573.88
batch-size 894.0385.76118.79210.5887.65122.31147.36416.71
batch-size 1661.9376.2575.45125.0550.3369.0187.25260.94
batch-size 3234.5658.1141.9372.2926.9134.4648.54151.35
batch-size 6418.6431.5723.1538.9012.6715.9026.1985.47
CPU
modelTorch FP32 FPSTorch FP16 FPSTorchScript FP32 FPSTorchScript FP16 FPSONNX FP32 FPSONNX FP16 FPSTensorRT FP32 FPSTensorRT FP16 FPS
batch-size 127.91Not Support46.10Not Support79.27Not SupportNot SupportNot Support
batch-size 225.26Not Support24.98Not Support45.62Not SupportNot SupportNot Support
batch-size 414.02Not Support13.84Not Support23.90Not SupportNot SupportNot Support
batch-size 87.53Not Support7.35Not Support12.01Not SupportNot SupportNot Support
batch-size 163.07Not Support3.64Not Support5.72Not SupportNot SupportNot Support

总结

我们可以看到在GPU上,tensorrt的速度是无须质疑的,在fp16的加持下,fps在batch_size为1的时候达到恐怖的789.19,就算在batch_size为64的时候,fps也可以达到85.47,比其他高出几倍之多.在CPU上,我们可以看到选用ONNX推理是比较合适的。

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
项目目的 一个算法模型的落地需要经历从算法任务确立,到方法调研、模型选型和优化、数据采集标定、模型训练、部署验证等一整个pipeline,其中对于绝大多数的算法工程师,模型的训练和输出是没有问题的,但是要快速地进行模型在移动设备上的效果验证,则需要移动端开发人员和配合才能完成。另一方面,考虑到团队内CV算法研究方向很多,如果每个模型都单独在移动端开发一套验证APP的话显然费时费力。 为了解决模型移动端部署验证困难,以及每个模型都单独在移动端开发一套验证APP带来的重复工作的问题,本项目实现了CV算法快速验证框架项目,旨在提供一套通用的CV算法验证框架框架经过一年多的开发和维护,目前已经完成绝大部分API的开发,实现包括实时视频流模块、单帧图像处理模块、3D场景模块、云端推理模块等众多功能。 研究方法 构建包含推理的应用程序所涉及的不仅仅是运行深度学习推理模型,开发者还需要做到以下几点: 利用各种设备的功能 平衡设备资源使用和推理结果的质量 通过流水线并行运行多个操作 确保时间序列数据同步正确 本框架解决了这些挑战,将上述软件框架解耦为数据流控制层、nn推理引擎层,以及UI层进行框架实现,把数据流处理管道构建为模块化组件,包括推理处理模型和媒体处理功能等。 其中数据流控制层包含三个大的模块 – 视频流模块、图片和编辑模块、3D场景模块,每个模块提供可供配置的数据流参数接口,同时提供了一些常用的工具包如OpenCV、QVision等用于作为模型的数据输入和预处理。 nn推理引擎层则集成了一些移动端常用的推理框架比如SNPE、TensorFlow Lite等,并提供统一模板便于后续持续维护扩展其他推理框架。 UI层则封装好了图像渲染模块,以及各种调试控件。在API方面,该算法验证框架提供了Native/JAVA/Script三个层次的API,前两者可以在Android工程中进行快速模型集成,Script API则不需要编写任何APP 代码,通过文本脚本解析的形式配置模型推理选项。 通过以上功能使开发者可以专注于算法或模型开发,并使用本框架作为迭代改进其应用程序的环境,其结果可在不同的设备和平台上重现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔鬼面具

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值