当前位置:   article > 正文

主流推理框架在ubuntu上的速度对比实验_魔傀面具 csdn

魔傀面具 csdn

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的推理的推理模型后续会写一个单独的推理代码.
    
    • 1
    • 2
    • 3
  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
  • 1
  • 2
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

各导出模型在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推理是比较合适的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/271226
推荐阅读
相关标签
  

闽ICP备14008679号