This project implements Human Activity Recognition (HAR) using a Long Short-Term Memory (LSTM) neural network on an M5Stack Gray device. The model is trained on the UCI Human Activity Recognition dataset and deployed on the M5Stack for real-time inference using its built-in IMU sensor.
- Clone this repository:
git clone https://github.com/MaxRondelli/Human-Activity-Recognition-with-LSTM-on-M5Stack.git
- Install the required Python packages:
pip install -r requirements.txt
- Set up the Arduino IDE with M5Stack support following the official M5Stack documentation.
- Download and include the two libraries necessary to run inference on the IoT device.
#include <M5Stack.h>
#include <TensorFlowLite_ESP32.h>
TensorFlowLite for ESP32 is necessary to load and interpreter the LSTM model.
- Download the UCI HAR dataset.
cd data
python download_dataset.py
- Start the training.
python main.py
- The best model and all the statistics about the training will be saved in the folder results/that_specific_run/.
- Convert the
best.pth
model to TensorFlowLite format. Look at the given path for the variableBEST_MODEL
in the classpytorch2tflite.py
python pytorch2tflite.py
- Convert the
edge_model.tflite
to.h
format. It must be in the following format to be load on the M5Stack device.
xxd -i edge_model.tflite > edge_model.h
- Once you have the converted model in
.h
format, add the attributeconst
to the model the variables in the model. So change from:
unsigned char edge_model_tflite[] = { model_inside_the_brackets }
unsigned int edge_model_tflite_len = 70912;
to
const unsigned char edge_model_tflite[] = { model_inside_the_brackets }
const unsigned int edge_model_tflite_len = 70912;
- To run the inference on M5Stack, move the
edge_model.h
inside the same folder where the.ino
file is. Include the model inside the file and run the code.
Model's performance on the left and M5Stack inference on the right.