MPolaris / onnx2tflite

Tool for onnx->keras or onnx->tflite. Hope this tool can help you.
Apache License 2.0
526 stars 42 forks source link

yolov5s.onnx转换keras,tflite的验证测试 #24

Closed Alter-Y closed 1 year ago

Alter-Y commented 1 year ago

我使用yolov5的转换方式将yolov5s.pt转换成onnx,再通过onnx2tflite的转换命令转换成keras和tflite。 PS E:\pycharm_proj\yolov5_switf_test\onnx2tflite> python .\converter.py --weights ./yolov5s.onnx --formats tflite keras --int8 --imgroot .\datasets\coco128\images\train2017\ --int8mean 0 0 0 --int8std 255 255 255 Checking 0/1... [0] redundant input nodes are removed. nodes name : E:\yolov5-master\venv\lib\site-packages\tensorflow\lite\python\convert.py:746: UserWarning: Statistics for quantized inputs were expected, but not specified; cont inuing anyway. warnings.warn("Statistics for quantized inputs were expected, but not " Estimated count of arithmetic ops: 18.144 G ops, equivalently 9.072 G MACs fully_quantize: 0, inference_type: 6, input_inference_type: 3, output_inference_type: 3 Estimated count of arithmetic ops: 18.144 G ops, equivalently 9.072 G MACs WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. model.compile_metrics will be empty until you train or evaluate the model. WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. model.compile_metrics will be empty until you train or evaluate the model. 但首先我使用netron打不开h5,其次我使用yolov5的验证命令对tflite进行测试,先是shape的问题会报错,onnx2tflite输出的shape是[1, 85, 25200],而yolov5中输出是[1. 25200, 85],所以我修改了yolov5 val.py,line206 out = out.permute(0, 2, 1) # onnx2tflite: [1, 85, 25200] to [1, 25200, 85],再测试得到的输出显示为0是:

val: Scanning 'E:\datasets\coco128\labels\train2017.cache' images and labels... 128 found, 0 missing, 2 empty, 0 corrupt: 100%|██████████| 128/128 [00:00<?, ?it/s]
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|██████████| 128/128 [1:18:54<00:00, 36.99s/it]
                 all        128          0          0          0          0          0
Speed: 0.9ms pre-process, 36981.4ms inference, 0.6ms NMS per image at shape (1, 3, 640, 640)

请问,我的测试流程是否有问题?作者是用什么方式进行验证测试yolov5的?另外这里得到的tflite与yolov5导出的tflite相比,会多出许多transpose和gather操作,这是什么问题导致的? 附:onnx2tflite导出的tflite: yolov5s_onnx2tflite.zip yolov5的tflite: yolov5s-int8.zip

MPolaris commented 1 year ago

int8的tflite模型,需要对输入输出做量化和反量化处理,也许你是这一步没做对。 这个是依据onnx的模型结构来导出的,yolov5官方的tflite是通过代码来构建的。

Alter-Y commented 1 year ago

int8的tflite模型,需要对输入输出做量化和反量化处理,也许你是这一步没做对。 这个是依据onnx的模型结构来导出的,yolov5官方的tflite是通过代码来构建的。

你好,我是直接用yolov5的源码进行测试的,肯定有量化跟反量化处理,用的coco128标准数据集。作者是怎么验证yolo模型的呢?

MPolaris commented 1 year ago

YOLOV5的源码我看了,他没有写量化的这一部分代码。你可以先试试不量化的能不能推对,我以前是专门测试过的,但是代码找不到了,不过INT8的tflite模型在PC上推理非常慢 你可以参照这个写一下: https://www.tensorflow.org/lite/performance/post_training_quantization https://stackoverflow.com/questions/66767195/run-inference-with-quantized-tflite-model-int8-in-python

Alter-Y commented 1 year ago

好的,谢谢,我先尝试一下。

殇痕*秋雪 @.***

 

------------------ 原始邮件 ------------------ 发件人: "MPolaris/onnx2tflite" @.>; 发送时间: 2022年12月5日(星期一) 中午11:21 @.>; @.**@.>; 主题: Re: [MPolaris/onnx2tflite] yolov5s.onnx转换keras,tflite的验证测试 (Issue #24)

YOLOV5的源码我看了,他没有写量化的这一部分代码。你可以先试试不量化的能不能推对,我以前是专门测试过的,但是代码找不到了,不过INT8的tflite模型在PC上推理非常慢 你可以参照这个写一下: https://www.tensorflow.org/lite/performance/post_training_quantization https://stackoverflow.com/questions/66767195/run-inference-with-quantized-tflite-model-int8-in-python

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>