AxisCommunications / onnx-to-keras

Convert onnx models exported from pytorch to tensorflow keras models with focus on performace and highleve compatibility.
MIT License
25 stars 13 forks source link

Artpec-8 #43

Open xsacha opened 2 years ago

xsacha commented 2 years ago

Describe the feature

Ability to create models that work on Artpec-8

Additional context

According to the example, Artpec-8 prefers a different method of quantisation that only works with Tensorflow 1. That makes conversions such as to keras redundant. In fact all modern onnx and other formats cannot be converted to Tensorflow 1.

Despite this. I attempted to use a Tensorflow Lite model I created from this repo and TF2 that works on Artpec-7. It runs and produces no errors on Artpec-8 but it gives back the wrong results and runs slower.

Is there any solution for framework conversion such as this that applies to the new chip?

95hali74 commented 2 years ago

Hi,

Unfortunately not that I am aware of. The limitation with A8 is that it is optimized for per tensor quantization. Which is only supported when using Quantization aware training (QAT) in TF1. So even if you can successfully convert a model to a TF1 model would be of little benefit as you would still need to rewrite the model in TF1 for QAT training and port all of your training code from torch to TF1.

However you mentioned that the model is both slower and the results is wrong when running on A8? The model should only be slower but the results should still be the same.

xsacha commented 2 years ago

The results are wrong because it is a TF2/Keras model (from this repo) I suppose. It's otherwise a valid Tensorflow Lite model that works on Artpec-7. For an example of how it gets different results: On Artpec-7, the face detector model gets locations of all faces in the image. On Artpec-8, the model occasionally finds the location of lights instead -- never faces. Other models that return a feature or metric also get the wrong numbers back.

TF1 is obsolete. I am confused why Axis would launch a new chip that requires training with TF1? I tried to find a pretrained model that was trained on TF1 with QAT, so I could at least see it working, but it is really hard to find one.

95hali74 commented 2 years ago

So the same tflite model results in different outputs on ARTPEC-7 and ARTPEC-8? There can be some small differences in the output but it shouldn't affect the detection results as much as you are describing. If you want, I can take a look at the model and see what is wrong.

xsacha commented 2 years ago

Here's the model: Expired link

My other models fail too but nothing visual to show the result. I've been using it successfully on ARTPEC-7 but cannot figure out ARTPEC-8 at all. There are no errors running it. Code-wise, the difference is LAROD_CHIP_TFLITE_ARTPEC8DLPU instead of LAROD_CHIP_TPU

95hali74 commented 2 years ago

Hi,

I have managed to reproduce the results. There where some errors then loading the model. I have created a internal bug report but unfortunately it could take some time to solve it.

xsacha commented 2 years ago

Thank you!

xsacha commented 1 year ago

Hi @95hali74 I was wondering if there has been any progress on this bug?

Thanks again

95hali74 commented 1 year ago

Hi,

The errors I saw doesn't seem to be related to the model accuracy. So I am not entirely sure where the problem is. Could you try to run the model using the "cpu-tflite" larod chip on both the a7 and a8 camera and see if the output appears correct or not.

xsacha commented 1 year ago

We have already sent back the camera but it seemed to fail no matter what I tried, only on the a8 camera. Running as CPU was working. What is the progress on the original issue?

95hali74 commented 1 year ago

No unfortunately not, currently you still have to use tf 1 for A8.