Download and build the sample application
Install Arm toolchain and Mbed CLI
- Download Arm cross compilation toolchain. Select the correct toolchain for the OS that your computer is running. For Windows users, if you have already set up the Linux virtual environment, install the toolchain there.
- To build and deploy the application, we will use the Mbed CLI. We recommend that you install Mbed CLI with our installer. If you need more customization, you can perform a manual install. Although this is not recommended.
If you do not already have Mbed CLI installed, download the installer:
- After Mbed CLI is installed, tell Mbed where to find the Arm embedded toolchain.
mbed config -G GCC_ARM_PATH <path_to_your_arm_toolchain>/binImportant: We recommend running the following commands from inside the Mbed CLI terminal that gets launched with the Mbed CLI Application. This is because it will be much quicker to set up, because it resolves all your environment dependencies automatically.
Build and compile micro speech example
Navigate to the directory where you keep code projects. Run the following command to download TensorFlow Lite source code.
git clone https://github.com/tensorflow/tensorflow.git
While you wait for the project to download, let’s explore the project files on GitHub and learn how this TensorFlow Lite for Microcontrollers example works.
The code samples audio from the microphone on the STM32F7. The audio is run through a Fast Fourier transform to create a spectrogram. The spectrogram is then fed into a pre-trained machine learning model. The model uses a convolutional neural network to identify whether the sample represents either the command “yes” or “no”, silence, or an unknown input. We will explore how this works in more detail later in the guide.
Here are descriptions of some interesting source files:
- disco_f746ng/audio_provider.cc captures audio from the microphone on the device.
- micro_features/micro_features_generator.cc: uses a Fast Fourier transform to create a spectrogram from audio.
- micro_features/tiny_conv_micro_features_model_data.cc. This file is the machine learning model itself, represented by a large array of unsigned char values.
- command_responder.cc is called every time a potential command has been identified.
- main.cc. This file is the entry point for the Mbed program, which runs the machine learning model using TensorFlow Lite for Microcontrollers.
After the project has downloaded, you can run the following commands to navigate into the project directory and build it:
make -f tensorflow/lite/micro/tools/make/Makefile TARGET=mbed TAGS="disco_f746ng" generate_micro_speech_mbed_project
This will create a folder in
tensorflow/lite/micro/tools/make/gen/mbed_cortex-m4/prj/micro_speech/mbed that contains the source and header files, Mbed driver files, and a README.
mbed config root .
TensorFlow requires C++ 11, so you will need to update your profiles to reflect this. Here is a short Python command that does that. Run it from the command line:
python -c 'import fileinput, glob;
for filename in glob.glob("mbed-os/tools/profiles/*.json"):
for line in fileinput.input(filename, inplace=True):
print line.replace("\"-std=gnu++98\"","\"-std=c++11\", \"-fpermissive\"")'
After that setting is updated, you can compile:
mbed compile -m DISCO_F746NG -t GCC_ARM