主流推理框架在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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

魔鬼面具

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

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

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

打赏作者

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

抵扣说明:

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

余额充值