The sine wave demo is great to do initial experimentation with training new simple single input->output models.

Google TensorFlow has a great guide here

The detailed part of the tutorial is in this colab script. Colab is great because its fully hosted, runs in any web-browser without using your CPU to do the training!

Re-creating the Default Sine Wave Model

Visit the colab script here:

And run all the script!

It may take a few minutes. When its complete you'll get an array of data at the bottom:

Grab that text starting with

unsigned char sine_model_quantized_tflite[] = {

and ending with

unsigned int sine_model_quantized_tflite_len = 2640;

(the number may vary)

unsigned char sine_model_quantized_tflite[] = {
  0x18, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x00, 0x00, 0x0e, 0x00,
  0x18, 0x00, 0x04, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x10, 0x00, 0x14, 0x00,
  0x0e, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10, 0x0a, 0x00, 0x00,
  0xb8, 0x05, 0x00, 0x00, 0xa0, 0x05, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  ....more here....
  0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x07, 0x00, 0x00, 0x00, 0x08, 0x00,
  0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x00, 0x00
};
unsigned int sine_model_quantized_tflite_len = 2640;

Now visit the hello_world_arcada sketch and fine the sine_model_data tab:

And paste that output from the notebook, replacing the

unsigned char sine_model_quantized_tflite[] = {

down to

const int g_sine_model_data_len = 2640;

Before you can compile, don't forget to change the declaration of the array from 'unsigned char sine_model_quantized_tflite' to 'unsigned const char g_sine_model_data' and the length variable from 'unsigned int sine_model_quantized_tflite_len' to 'const int g_sine_model_data_len'

Recompile and upload to your badge or Circuit Playground Bluefruit, you should get the exact same sine wave demo!

OK maybe not so exciting. Lets try changing the model.

Creating a Cosine Wave Model

Let's get wild and crazy now, by making a cosine wave model. Find the line in the script where we create the y_values from the x_values

And change it to a cos function!

Run the colab script from this point down (or the whole thing) to get a brand new unsigned char sine_model_quantized_tflite array  and follow the steps you did before to replace the model array in the hello_world sketch with your new model. Re-upload to now get cosine wave output, which looks like the plot above

Loading Models From Internal Storage

The PyBadge/EdgeBadge/Circuit Playground Bluefruit have 2MB of internal storage. We can use that to store TensorFlow models, so that we don't have to go through the recompilation step above. Instead, the model is loaded from that storage flash, we can read/write to the flash over USB by dragging-and-dropping, just like a USB key!

Upload the hello_world_arcada sketch. If you have a PyBadge/EdgeBadge, this time make sure to select TinyUSB as the USB stack since that will activate mass storage support. You don't have to specifically select TinyUSB for nRF52 (e.g. Circuit Playground Bluefruit)

Now when you upload, on reset you'll get a new disk drive appearing on your computer, it'll probably be name CIRCUITPY but you can rename it if you like.

Download this models zip file and drag the two .tflite files to CIRCUITPY

Rename one of the files to model.tflite - when this file is available to read, this will tell our sketch to load the model from disk instead of from memory!

Once you've renamed the file, click the reset button, you should get an alert like this:

Once you've renamed the file, click the reset button, you should get an alert like this. Press the A button on the badge or Circuit Playground Bluefruit (its the left button) to begin the model inference run.

Once you've proved that you're running one of the files, try renaming the other model file to model.tflite and reset. That way you can prove that its running from the disk.

You can go back to the colab script you ran, and look in the Files tab to find the sine_model_quantized.tflite file that is converted in the last stage. You can download that file directly.

Try creating new tflite files that model different functions!

This guide was first published on Dec 15, 2019. It was last updated on Dec 15, 2019.

This page (Customized Wave Demo) was last updated on Oct 09, 2021.

Text editor powered by tinymce.