google-coral / pycoral

Python API for ML inferencing and transfer-learning on Coral devices
https://coral.ai
Apache License 2.0
347 stars 144 forks source link

In Pycoral python API, what's the function that replaces the "classify_with_image()" in deprecated EdgeTPU API #92

Open hillyuyichu opened 1 year ago

hillyuyichu commented 1 year ago

Description

I'm running Pycoral python API on my Raspberry Pi 4 with Coral USB Accelerator.

My program basically captures images from camera and upload them to the google cloud. Before going to the cloud, the images go through the image classification with pre-trained tflite model (used Google's vertex AI service).

My problem is my prediction results are completely off after I switched from the old EdgeTPU API to Pycoral python API. There isn't a classify_with_image() in Pycoral API. The closest thing I found is get_classes(). Please tell me if there is some caveat that I have to know for doing this.

Here is my main code for classification: pycoral_classification.py EdgeTPU_classification.py config.py (needed in classification.py)

PS. I have no problem with the inference speed, just the prediction results.

Click to expand! ### Issue Type Performance ### Operating System Linux ### Coral Device _No response_ ### Other Devices Rapsberry Pi 4 ### Programming Language Python 3.9 ### Relevant Log Output _No response_
hjonnala commented 1 year ago

Hello @hillyuyichu seems to be you are creating multiple interpreters for different classifiers on single TPU at: https://github.com/hillyuyichu/Pycoral-python-API/blob/main/pycoral_classification.py#L163. If you want to run/load multiple models on same TPU either you have compile the models with co comiplation, recommended only if all the model sizes combined are less than 8MB, or you have to create the interpreter each time when switching form one model to another model.

hillyuyichu commented 1 year ago

Thank you for the reply @hjonnala !

I'm loading 5 models into a library but only one is set as active and being run each time. config.json

Here is how my program looks like on front end UI screenshot

Here is how the program works in the main classify.start()

All my models are under 8MB and exported as Edge TPU TF lite.

hillyuyichu commented 1 year ago

Is there an updated version for downloading EdgeTPU compiler?

This seems to be deprecated:

curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list

sudo apt-get update

sudo apt-get install edgetpu-compiler
hjonnala commented 1 year ago

can you try creating the interpreter inside the for loop at: https://github.com/hillyuyichu/Pycoral-python-API/blob/main/pycoral_classification.py#L70 instead pre loading the models.

We don't have any updated version of EdgeTPU compiler. Fell free to use the colab tutorial if you are having any issues.

hillyuyichu commented 1 year ago

I've moved the interpreter inside the for loop. It doesn't seem to change anything.

I have also done the co-compilation for two of my models. The performance has improved a little bit. Before, it was way off. However, it still doesn't give the correct predictions. For example, when I put an empty pan in front of the camera, I got a prediction result of 0.80 for empty_pan label on your Pycoral's example code classfiy_image.py. But when I run it on mine classification.py, I get a prediction result of 0.11 for empty_pan label. My classification.py is bascially the same as the Pycoral's example except there's an dictionary added.