Saturday, November 2, 2024

A Guide to STMicroelectronics' Edge AI Suite Tools

STMicroelectronics has plenty of Edge AI Tools that are useful in a variety of applications.  They collectively form the ST Edge AI Suite - STMicroelectronics. All of these tools can be used free of charge with a myST user account. To create an account, click on the avatar in the top right corner of the STMicroelectronics Website. You are now ready to access the ST Edge AI Suite!

1. NanoEdge AI Studio

The first tool is NanoEdge AI Studio, it is a good place to get started with Machine Learning. 

It is also compatible with Arduino Devices.


For getting started, the following video is a good example - Anomaly Detection Demo Using Edge AI.  It uses the Accelerometer on the STEVAL-STWINKT1B board.  In a future blog post I will try this demo using some other ST Micro boards that have an accelerometer.  Additionally, the Nano Edge AI libraries can be used with any type of sensor.


To use the Datalogger Feature in NanoEdge AI Studio, a Nucleo-F411RE, ST-EVAL or similar type of board is necessary.  Here are all the Board options shown:



ST Edge AI Developer Cloud is a tool that allows users to do on remotely stored devices. You can import your own ML model or choose from ST Edge AI Model Zoo for optimization and testing. 


The following video is a getting started guide for STM32Cube.AI Developer Cloud, the predecessor of ST Edge AI Developer Cloud. While there are some differences between the two, this getting started video does have some helpful steps.  I'm also working on a blog post about getting started with the updated ST Edge AI Developer Cloud.



3. MEMS-Studio

MEMS-Studio is a software solution for using AI in embedded systems development. It can be downloaded from the linked webpage (MEMS-Studio).  Once installed an opened, you will have access to Getting Started with MEMS-Studio - User manual and Introduction-to-MEMS-studio Video.


4. STEdgeAI-Core

STEdgeAI-Core is used for compiling edge AI models on various ST MCUs MPUs, and Smart Sensors - all in one tool.  The following video gives an overview.




5. ST Edge AI Model Zoo

The ST Edge Ai Model Zoo is a collection of AI models that can be used for running on the other tools in the ST Edge AI Suite. It is comprised of three GitHub repositories: AI Model Zoo for STM32 devicesGitHub - STMicroelectronics/STMems_Machine_Learning_Core, and GitHub - STMicroelectronics/st-mems-ispu: Examples, tutorials, and other development resources for the ISPU.

6. X-CUBE-AI - AI expansion pack for STM32CubeMX 

The AI expansion pack for STM32CubeMX  is a tool used for optimizing and profiling Neural Networks and Machine Learning models for STM32.  To install this tool, open the STM32CubeIDE, and go to Help > Manage Embedded Software Packages > STMicroelectronics, then scroll down to X-CUBE-AI and click on the newest available version (9.1.0 in this case), then click "Install."  You may need to log-in on the pop up that appears for the expansion to install successfully.  Here is a screenshot of the installation screen.






7. ST High Speed Datalog

    One of the newer tools in the suite, ST High Speed Datalog is a data acquisition and visualization toolkit. It is specifically designed for applications in embedded systems and data science. The following Datalog Quick Start Guide is designed to get started with the Datalog along with the STEVAL-STWINBX1 .

8. StellarStudioAI 

    The next tool, StellarStudioAI is an AI tool for StellarStudio - development environment for Stellar automotive MCUs.  StellarStudioAI generates neural networks and converts them into C libraries for use by MCUS.

9. X-Linux-AI

    X-Linux-AI is an expansion for the STM32 MPU OpenSTLinux Distribution.  X-LINUX-AI  provides LINUX AI frameworks and application examples for use with STM32 MPUs.

10. ST ToF Hand Posture

    The final tool, ST ToF Hand Posture, is used for detecting hand gestures on STM32 MCUs using a Time-of-Flight sensor. It requires a free login to MyST to access.


This completes the summary of the ST Edge AI Tools. The first four tools listed are the best place to get started when doing AI projects for ST MCUs and MPUs.  Helpful example videos are also available for NanoEdgeAI Studio, and MEMS Studio.  For example, NanoEdge AI Studio V3 - Anomaly Detection demo and Introduction To MEMS Studio.




 










Monday, August 26, 2024

Introduction to Machine Learning on Arduino - Micro Speech - Part 2

 

Part 2: Changing Words

In this part, we will use the words ""On" and "Off" to control the LED and turn it on and off.

To do this, we can use the Google Commands dataset which has data for many short words. The following video by Digi-Key includes instructions for Downloading the dataset. Although it is not labeled as machine learning in the title, it is similar to what is being done in this ML guide.



2.1 Downloading Python Script

The Dataset Curation Python Script is available to download. Here are the steps to extract it properly, as the video instructions are shown too quickly.

2.1.1 Install 7-Zip

For extracting the file
On the 7-Zip Download Page Select the download for your machine. (I'm using Windows 7 64-Bit)
Once downloaded, open the .exe file. (You may need to click "Allow Changes" in the pop-up window) Next, Install.



2.1.2

Click on the green "Code" box and select the "Download ZIP" option that appears

2.1.3


Left Click on the Downloaded zip file, then click on "Show more options".

2.1.4


Under "Show more options" Hover over "7-zip", then click on "Extract Here"

2.1.5

Once it's extracted, open File Explorer and search for "ei-keyword". Open the "ei-keyword-spotting-master" folder.

2.1.6


Copy and paste the "dataset-curation" and "utils" Python files into a directory where they can be accessed.

2.2 Downloading Speech Commands

As shown in the video description, here is a download link for the Google Speech Commands Dataset.  If a page appears when following this link, click on "Go to Site" to start the download.

2.2.1

Left Click on the Downloaded zip file of the Dataset, then click on "Extract All"



2.2.2 

Create a folder for housing the necessary files. In this example I named it "Speech_Rec".








2.2.3 

Cut and paste the _background_noise_ into the created folder. The background_noise_ must be stored seperately from the individual keywords.




2.2.4


Create a folder for housing keywords such as "on" and "off". (The video example includes using custom keywords as well)



2.2.5

Copy and paste the "on and "off" keywords into the newly created "keywords" folder



2.3 Using the Program

2.3.1

Python will need to be installed to run the speech Recognition Code Download Python  
Click on the yellow box to download the latest version.

2.3.2

If you already have Python installed, it may ask to upgrade to the latest version. Otherwise, there should be an option to install.



2.3.3

An option to close the installer will appear once setup is successful


2.3.4

Open Windows Powershell (Terminal)( and type the following command:  
pip install librosa numpy soundfile
This installs all the necessary Python libraries for the speech recognition.



2.3.5

While in the PowerShell, go to the folder that contains all the speech recognition files ("Speech_Rec"). 
To find the path click on the folder name in file explorer, then left click and select "Copy Address"


Use the cd command followed by the path to the directory, for example: cd C:\Users\maxcl\Downloads\Speech_Rec

2.3.6

Next, enter the following line into the terminal. It runs the dataset-curation.py script and sets all the necessary parameters.

python dataset-curation.py -t "on, off" -n 1500 -w 1.0 -g 0.1 -s 1.0 -r 16000 -e PCM_16 -b "..\Speech_Rec\_background_noise_" -o "Speech_Rec\keywords" "..\Speech_Rec\data_speech_commands_v0.02" "..\datasets\keywords"


The script may take a few minutes to run.


2.3.7

Once it's finished, a set of folders named after the input, output, and keywords can be found in the directory. (Note: In this case, the folder name "Speech_Rec" appeared twice so I changed the second one to "results")


2.3.8

Open the "on" folder and select one of the audio samples to play.


2.4 Edge Impulse

2.4.1

Next, we will go to Edge Impulse. Click on the "Login" Option in the top left corner of the home screen, as this will give you the option to either log into your account or create a new account.

2.4.2

Once you've created an account and/or logged in, click on the icon in the top left, then click on "Projects"



2.4.3

Click on "Create New Project"




2.4.4

Name your project then click on the green "Create New Project".  I chose to leave this one as public.





2.4.5

In the new project, Click on "Data Acquisition" 

2.4.6

Next, click on "Add Data"





2.4.7

Select "Upload Data"
 

2.4.8
Leave the default selections for category and label, and click on "Select Files"



2.4.9

Start by uploading all the files in the noise folder. (I selected all by clicking on the box next to "name"). Click "Open"

2.4.10

Afterwards, click on the purple "Upload Data" button
Edge Impulse will then process and add the files to the project.

2.4.11

Repeat steps 2.4.8 through 2.4.10 for the "on" "off" and "unknown" categories.

2.4.12

The pie charts at the top of the Dashboard should show that the data is split amongst the four categories, and 80% of the data was assigned to training and 20% was assigned to testing.



2.4.13

On the left side, Click on Impulse Design, then Create Impulse. Click on " Add Processing Block".


2.4.14

Add the Audio (MFCC) Processing Block

2.4.15

Select the "Add a Learning Block Option", then choose the default "Classification" Block



2.4.16

Click on the Green "Save Impulse"


2.4.17

From the MFCC on the left, Select the Generate Features tab, then click on "Generate Features"


2.4.18

Next, under Classifier on the left, choose Save & Train. 

2.4.19

Now that the model is trained, the loss and accuracy of the model will appear. The accuracy refers to the percentage of the data that was correctly classified. As for loss, it is based upon a predefined function, and a smaller loss value indicates better ML model accuracy.

2.4.20

To test the model, go to Model Testing on the left. Select "Classify All".

2.4.21

Once the testing is done, the results will be shown.


2.4.22

Next, we will deploy the model for Arduino. Click On the Search Bar and select the Arduino Options. (Note: There are other options that can be used for other Microcontrollers such as STM32 devices)


2.4.23

Select "Build" to create the Arduino Library, which will Download automatically.
Once it's done building, a dialogue will pop up showing the path for using the Arduino Library


2.5 Arduino

2.5.1

In the Arduino IDE, open a new Arduino Sketch (File - New Sketch)

2.5.2

Once you're in a new sketch, select Sketch-Include Library- Add .ZIP Library and open the library downloaded from Edge Impulse

2.5.3

Next, Go to File- Examples and scroll down to find the library you just installed. It should have the same name as the project in Edge Impulse. Go to "Speech_Rec_inferencing"(May differ based on your project name) - "nano_ble33_sense" - "nano_ble33_sense_microphone_continuous". Note: the Arduino IDE might need to be restarted for it to appear)



2.5.4

Open the File Explorer, and go to the newly installed Speech_Rec library and locate the Model variables file. The path will likely be: User > Arduino > libraries > Speech_Rec_Inferencing > src >model_parameters > model_variables 





2.5.5

Open the model_variables.h file in a code editor such as VS Code, and search for where the keywords are listed. 
The line will likely be:  
const char* ei_classifier_inferencing_categories[] = { "_noise", "_unknown", "off", "on" };

In this case "_noise" is in position 0, "_unknown" is in 1, "off" is 2, and "on" is 3.


2.5.6

Return to the Arduino IDE. In the nano_ble33_sense_continuous example, create a new line under 
line 59: static int print_results = -(EI_CLASSIFIER_SLICES_PER_MODEL_WINDOW);

The new line will read: static const int led_pin = LED_BUILTIN;

2.5.7

Next, go a few lines down to be within the void setup() { block of code.


Type the following line: pinMode(led_pin, OUTPUT);


2.5.8

Even further down in the code, above line 114: if (++print_results >= (EI_CLASSIFIER_SLICES_PER_MODEL_WINDOW)) {

Enter the following block of code:


    //turn on LED if "On" vlaue is above a threshold
    if (result.classification[3].value > 0.7) {
      digitalWrite(led_pin, HIGH);
    }
    //turn off LED if "Off" vlaue is above a threshold
    else if (result.classification[2].value > 0.7) {
      digitalWrite(led_pin, LOW);
    }
    else {
      digitalWrite(led_pin, LOW);
    }
   
This code controls whether the LED is "on or "off" based on the keyword recognized. the result.classification[2] and result.classification[3] values are consistent with the labels for "Off" and "On" found in the model_variables.h file.




2.5.9

Next, click the arrow to upload the code. It may take awhile to compile, as it is a lengthy code segment

2.5.10

Once the code is complied, speak the keywords within a couple inches of the board. The voice needs to be very close for the recognition to work. If the board recognizes a word with a confidence value of 0.7, the LED next to the USB will turn yellow.



2.5.11

To check the readings for the speech recognition values on the board, you can look at the serial monitor in the Arduino IDE. To do this click on Tools > Serial Monitor (or type Ctrl + Shift + M). The serial Monitor continually shows the speech rec values for all the keywords, as we are in the continuous example.



We now have a working Arduino program that uses machine learning to recognize the words "on" and "off". In the future, the code can be adjusted so that the LED stays on for longer, thus making the "off" command more effective.