LSH9832 / edgeyolo

an edge-real-time anchor-free object detector with decent performance
Apache License 2.0
446 stars 59 forks source link

export to tflite uint8 got worng! #53

Closed LiaoZihZrong closed 5 months ago

LiaoZihZrong commented 6 months ago

Dear Author,

When I converted the ONNX model to a SavedModel and then to a TFLite model with both input and output as uint8, I encountered an issue. It seems that the first four values (x,y,w,h) among the 85 output values from the last layer are not in the range of 0 to 1. As a result, after quantization, the obj_conf value minus the zero point becomes 0, making it impossible to correctly perform post-processing.

Could you please advise on how to resolve this issue?

Thank you for your assistance.

LSH9832 commented 6 months ago

Unfortunately I've never used TFLite before and I just don't know how to solve it, and my code does not support converting model with UINT8 input, but maybe code in cpp/rknn/include/image_utils/rknn.h can help you a little bit?

ramonhollands commented 6 months ago

Hi @LiaoZihZrong,

I managed to solve it by dividing the xy and wh by image size in the forward pass (only when exporting the model): https://github.com/ramonhollands/edgeyolo/blob/1110ac7964ef2fe1b071740ddc95712c640d470b/edgeyolo/models/yolo.py#L144

Then using onnx2tf the int8 model works as expected. I did quantize it while leaving input and output to float32 format.

The speed of tflite quantize (and resulting file size) is about 4 times faster than float32 with very little loss on accuracy.

Best regards, Ramon

LiaoZihZrong commented 6 months ago

Hi @LiaoZihZrong,

I managed to solve it by dividing the xy and wh by image size in the forward pass (only when exporting the model): https://github.com/ramonhollands/edgeyolo/blob/1110ac7964ef2fe1b071740ddc95712c640d470b/edgeyolo/models/yolo.py#L144

Then using onnx2tf the int8 model works as expected. I did quantize it while leaving input and output to float32 format.

The speed of tflite quantize (and resulting file size) is about 4 times faster than float32 with very little loss on accuracy.

Best regards, Ramon

many thanks! your code help me a lot !

LiaoZihZrong commented 5 months ago

@ramonhollands

Hi @LiaoZihZrong,

I managed to solve it by dividing the xy and wh by image size in the forward pass (only when exporting the model): https://github.com/ramonhollands/edgeyolo/blob/1110ac7964ef2fe1b071740ddc95712c640d470b/edgeyolo/models/yolo.py#L144

Then using onnx2tf the int8 model works as expected. I did quantize it while leaving input and output to float32 format.

The speed of tflite quantize (and resulting file size) is about 4 times faster than float32 with very little loss on accuracy.

Best regards, Ramon

if i want train a new edgeyolo, and output xy, wh, is in range 0-1,and not use torch.exp (my machine not support), can you help me ?

ramonhollands commented 5 months ago

Hi @LiaoZihZrong,

What machine are you on? Maybe it's better to upgrade the tensorflow version. That would be the easiest option I think.

Regards, Ramon

LiaoZihZrong commented 5 months ago

Hi @LiaoZihZrong,

What machine are you on? Maybe it's better to upgrade the tensorflow version. That would be the easiest option I think.

Regards, Ramon

@ramonhollands My edge device can`t use exp op, So i want to train a new edgeyolo, Maybe set the xywh to sigmoid Or other op to replace exp But when i use sigmoid or tanh Its fail to train a new one Do you have any idea Thank you a lot❤️

By the way I see there is IAuxDetect head in yolo.py Can i change this head and train a new model to replace exp op?

LSH9832 commented 5 months ago

well just let cpu(instead of gpu or other accelerate device) do the exp opration by using math or other library.