├── accuracy-vs-model-size.txt -- contains a list of all the data used for the accuracy vs model size & params chart
├── bluetooth.py -- this is a python implementation of the MCU bluetooth protocol.
├── build_and_test.sh -- this builds a new neural network, exports C headers. This is for neural network training
├── build.sh -- this is for automatically building main.c (MCU code), converting it to MSBL and running the bootloader
├── clean.sh -- this cleans the training directory of all extra files
├── decoder.c -- this takes exported MCU data (from data.txt) and exports inference_records.json which is a decoded version of the inference records
├── float32_neural_network.h -- this is the library for loading the neural_network_weights.h and running them and inferencing like a functioning neural network
├── main.c -- this is the main MCU code that collects data, stores it, broadcasts it, and runs inferences
├── Makefile -- unchanged
├── max32660.ld -- unchanged
├── modified_i2c_bootloader.py -- unchanged
├── msblGenWin32.exe -- unchanged
├── project.mk -- unchanged
├── README.md
├── training
│ ├── 4channeldata -- unchanged
│ ├── data -- unchanged
│ ├── extract_float32_weights.py -- extracts c headers and c arrays from a saved tensorflow model
│ ├── generate_validation_data.py -- converts a section of data from ./data and ./4channeldata into C arrays to be tested on the MCU or with the native C libraries
│ ├── simple_parameter_sweep.py -- runs train_float32_model and extracts all the data into C and runs validate_float32_model.c for multiple parameter configurations
│ └── train_float32_model.py -- tensorflow code for training an activity classification neural network
└── validate_float32_model.c -- simulates a bunch of validation data using our native C neural network libraries (float32_neural_network.h)
Make sure that you have bleak installed.
To change the target bluetooth device, change TARGET_NAME on line 8 to the address of your desired bluetooth device.
It will collect data, and then display two graphs. The first graph is the data coming from the accelerometer an temperature sensors of the MCU, the second graph is a graph of a random sample of the inferred class (for visual comparison)
When you export data (using the android phone with the ifm mobile all), put it in the same directory as decoder.c with tha name data.txt
Just compile with gcc and optionally use the -v if you want a verbose decoding of the inference
Dependent that there is a file ./training/neural_network_weights.h, generated from training a model and running ./training/extract_float32_weights.py.
The primary function of this library is run_neural_network_inference(const float*, float* output_probabilites).
The output probability with the highest probability is the inference
Generates inference_records which are:
typedef struct __attribute__((packed)) {
uint32_t timestamp; // RTC timestamp in milliseconds
uint32_t inference_id; // Sequential inference ID
int8_t predicted_class; // Predicted activity class (0-5)
float confidence; // Confidence score (0.0-1.0)
float sensor_data[NN_INPUT_SIZE]; // Complete sensor buffer used for inference
} inference_record_t;These are the things that get saved in memory and decoded by decoder.c
The primary functions of this file are:
Collect_Sensor_Data(void): reads accelerometer values with an avg delay of 30ms and stores the magnitude sqrt(x^2 + y^2 + z^2) * 4. Stores all of this data insensor_buffer. The last 3 values of this buffer are temperatures gotten fromRead_TempRun_Neural_Network_Inference(void): runs an inference based onsensor_bufferStore_Infernece_To_SPI(int, float, float*): generates aninference_recordand stores it in flash memory.DEBUG_BLE_BUFFER: debugs the whole buffer to bluetooth. Note: 254 signals the start of the blue buffer and 253 signals the end of it.DEBUG_BLE_FLOAT: sends a float value to the temperature characteristic in accordance with IEEE 754I2C_Readout_NEXTGENandChip_Eraseare unchanged
NOTE: The count (number of inferences) is stored at 0x008000. This is seperately stored from all the other inferneces
- On line 30, change
scale_factorsto affect how big you want all of the different models to be. The base config is on line 139, and by default it is[122, 61, 30] - This will save data results to stdout as well as
parameter_sweep_results.jsonandparameter_sweep_results.csv - Because running
train_float32_modelrequires a lot of environment variables and lots of extra steps to verify the model, its recommended to runsimple_parameter_sweep.pywith ascale_factors=[x]to get the desired model. This will automatically generate headers, verify that it works in C, and give simulated MCu accuracy.
- Simple converts
layer_x_weightsandlayer_x_biasesas well as standard scaler parameters into a format usable byfloat32_neural_network.h
- Reads
holdout_test_X.npyandholdout_test_y.npy(generated intrain_float32_model) and converts these into c headers thatvalidate_modelcan use
- Reads
validation_data.hand runs accuracy tests and generates a confusion matrix. Dependent on there being valid neural network weights in the training directory
- Swift implementation of
bluetooth.py