arduino / ArduinoTensorFlowLiteTutorials

260 stars 117 forks source link

Tiny ML on Arduino: Gesture recognition tutorial results in a blank model.h #11

Open jomoengineer opened 4 years ago

jomoengineer commented 4 years ago

I attempted to run through the Tiny ML on Arduino: Gesture recognition example and it resulted in a a model.h file that is just a model array declaration.

Ex:

const unsigned char model[] = {
};

This is the example that I was following: https://colab.research.google.com/github/arduino/ArduinoTensorFlowLiteTutorials/blob/master/GestureToEmoji/arduino_tinyml_workshop.ipynb#scrollTo=f92-4Hjy7kA8

The following Warnings were seen while running the example: NOTE: An issue was filed with the Colab project but I do not see it listed.

Also, a gesture_model.tflite file was created and has about 145K of data.

E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/v/vim/xxd_8.0.1453-1ubuntu1.1_amd64.deb  404  Not Found [IP: 91.189.88.152 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

- Convert the Trained Model to Tensor Flow Lite

WARNING:tensorflow:Entity <function Function._initialize_uninitialized_variables..initialize_variables at 0x7fe16a17d598> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10) and attach the full output. Cause: module 'gast' has no attribute 'Num' WARNING: Entity <function Function._initialize_uninitialized_variables..initialize_variables at 0x7fe16a17d598> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10) and attach the full output. Cause: module 'gast' has no attribute 'Num' Model is 147764 bytes

jomoengineer commented 4 years ago

I found a temp solution.
I copied the "Encode the Model in an Arduino Header File" code and the gesture_model.tflite file to a Linux box, created a content folder and made a shell and a python script to run the code. Ex:

echo "const unsigned char model[] = {" > content/model.h cat gesture_model.tflite | xxd -i >> content/model.h echo "};" >> content/model.h


- get_header_size.py

!/usr/bin/env python3

import os model_h_size = os.path.getsize("./content/model.h") print(f"Header file, model.h, is {model_h_size:,} bytes.") print("\nOpen the side panel (refresh if needed). Double click model.h to download the file.")



I then copied the resulting model.h code to the Arduino IDE project and was able to complete the Gesture recognition tutorial. 

This worked for the "TinyML Classify objects by color" tutorial as well. 
pra-dan commented 4 years ago

Please refer to this example on Google Collab. I hope it helps https://github.com/PrashantDandriyal/arduino_GSoC2020/blob/master/activity_tracker/activity_tracker_motionSense_gsoc20.ipynb

jomoengineer commented 4 years ago

Uh, the link you provided is a GitHub link. I'm not see how that address this issue.

pra-dan commented 4 years ago

It is actually a link to the iPython notebook I shared to demonstrate how I obtained the header file-model and thought it may help you get over your errors.

intratron commented 4 years ago

I got the same problem. The model.h file was blank. I managed to solve it in part, not in a very elegant way. But I hope it helps.

I was able to solve it. What I did was to verify the file using TextEdit on a Mac. I realized some of the lines/cells of the flex.csv and punch.csv files where either empty, mixed with another cell or a whole line of cells was out. Apparently when the data from the IMU is gathered to the Arduino's serial monitor it has some issues so when you copy it to a text file, those issues stays. See the line:

This is ok: -0.37 | 0.395 | 1.931 | 284.363 | -9.46 | -77.026 -0.648 | 0.578 | 1.737 | 795.166 | 36.743 | -190.247 -0.238 | 0.815 | 1.104 | 1028.137 | 40.283 | -246.948

This is not ok: -0.37 | 0.395 | 1.931 | 284.363-9.46 | (empty) | -77.026 -0.648 | 0.578 | 1.737 | 795.166 | 36.743 | -190.247 -0.238 | 0.815 | 1.104 | 1028.137 | 40.283 | -246.948

So I had to open the file in excel and manually fix every error in the cells, I took me like 5 minutes but that was the only way I managed to run the Tiny ML colab webpage and run the training, convert and encode functions. After doing this, the model.h file was generated with the data (911kb).

Not everything was ok though. I ran the sketch in Arduino IDE using the IMU_Classifier, adding a tab, pasting the model.h data in it. The Arduino serial monitor is responding but is giving me just a "nan" label instead of a float eg. 0.8987

So apparently I managed to solve the model.h problem but still I don't know whats the "nan" problem.

BTW I'm following this tutorial https://blog.arduino.cc/2019/10/15/get-started-with-machine-learning-on-arduino/

JatinPendharkar commented 3 years ago

@intratron im getting the same error. Did you find any solution for same ? The Arduino serial monitor is responding but is giving me just a "nan" label instead of a float eg. 0.8987