Reader small image

You're reading from  TinyML Cookbook - Second Edition

Product typeBook
Published inNov 2023
PublisherPackt
ISBN-139781837637362
Edition2nd Edition
Right arrow
Author (1)
Gian Marco Iodice
Gian Marco Iodice
author image
Gian Marco Iodice

Gian Marco Iodice is team and tech lead in the Machine Learning Group at Arm, who co-created the Arm Compute Library in 2017. The Arm Compute Library is currently the most performant library for ML on Arm, and it's deployed on billions of devices worldwide – from servers to smartphones. Gian Marco holds an MSc degree, with honors, in electronic engineering from the University of Pisa (Italy) and has several years of experience developing ML and computer vision algorithms on edge devices. Now, he's leading the ML performance optimization on Arm Mali GPUs. In 2020, Gian Marco cofounded the TinyML UK meetup group to encourage knowledge-sharing, educate, and inspire the next generation of ML developers on tiny and power-efficient devices.
Read more about Gian Marco Iodice

Right arrow

Building a Weather Station with TensorFlow Lite for Microcontrollers

Nowadays, we can quickly obtain information about the weather thanks to the convenience of smartphones, laptops, and tablets connected to the internet. However, have you ever considered how you would forecast the weather in remote regions without internet access?

This chapter will teach us how to implement a simple weather station with machine learning (ML) to predict the occurrence of snowfall based on the temperature and humidity of the last three hours.

In this chapter, we will focus on dataset preparation and show how to acquire historical weather data from WorldWeatherOnline. After preparing the dataset, we will show how to train a neural network with TensorFlow and quantize the model to 8-bit with TensorFlow Lite. In the last part, we will deploy the model on the Arduino Nano and Raspberry Pi Pico with TensorFlow Lite for Microcontrollers to build an application capable...

Technical requirements

To complete all the practical recipes of this chapter, we will need the following:

  • An Arduino Nano 33 BLE Sense
  • A Raspberry Pi Pico
  • A SparkFun RedBoard Artemis Nano (optional)
  • A micro-USB data cable
  • A USB-C data cable (optional)
  • 1 x half-size solderless breadboard (Raspberry Pi Pico only)
  • 1 x AM2302 module with the DHT22 sensor (Raspberry Pi Pico only)
  • 3 x jumper wires (Raspberry Pi Pico only)
  • A laptop/PC with either Linux, macOS, or Windows

The source code and additional material are available in the Chapter03 folder in the GitHub repository: https://github.com/PacktPublishing/TinyML-Cookbook_2E/tree/main/Chapter03

Importing weather data from WorldWeatherOnline

The effectiveness of ML algorithms depends heavily on the data used for training. Thus, as we commonly say, the ML model is only as good as the dataset. The essential requirement for a good dataset is that the data must represent the problem we want to solve.

Therefore, we should consider factors such as temperature and humidity in our specific situation, as they directly impact snow formation.

In this recipe, we will show how to gather historical hourly temperature, humidity, and snowfall data to build a dataset for forecasting snow.

Getting ready

On the internet, there are various sources from which we can gather hourly weather data, but most of them are not free or have limited usage.

For this recipe, WorldWeatherOnline (https://www.worldweatheronline.com/developer/) has been our choice, which has a free trial period of 30 days and provides the following:

  • A simple API through HTTP requests...

Preparing the dataset

The dataset preparation is a crucial phase in any ML project because it has implications for the effectiveness of the resulting trained model.

In this recipe, we will create a dataset from the physical quantities acquired earlier by adopting two techniques to make it suitable for training an accurate model. These two techniques will allow us to obtain a balanced dataset and bring its values into the same numerical range.

Getting ready

The dataset required for our task must be prepared to train a binary classification model, meaning the output can only belong to two classes: Yes, it snows or No, it does not snow. Therefore, mapping the snowfall in cm into these two classes is the first thing we must do. In this project, we have considered the snow formation only when the snowfall in cm is above 5 cm.

The next step for building the dataset concerns the selection of input features to predict the snow. Considering our goal of forecasting...

Training the model with TensorFlow

The model designed for forecasting the snow is a simple binary classifier based on a neural network, and it is illustrated in the following diagram:

Figure 3.3: Neural network model for forecasting snow

The network consists of the following layers:

  1. 1 x fully connected layer with 12 neurons followed by a ReLU activation function
  2. 1 x dropout layer with a 20% rate (0.2) to prevent overfitting
  3. 1 x fully connected layer with one output neuron followed by a sigmoid activation function

In this recipe, we will train the preceding model with TensorFlow.

Getting ready

As you will remember from physics class, the likelihood of snow formation is high when the temperature falls below 0°C (freezing condition) and there is sufficient humidity.

Therefore, given this relationship, we can infer that a basic feedforward neural network, like the...

Evaluating the model’s effectiveness

Accuracy and loss are not enough to judge the model’s effectiveness. In general, accuracy is a good performance indicator if the dataset is balanced, but it does not tell us the strengths and weaknesses of our model. For instance, what classes do we recognize with high confidence? What frequent mistakes does the model make?

This recipe will judge the model’s effectiveness by visualizing the confusion matrix and evaluating the recallprecision, and F1-score performance metrics.

Getting ready

To complete this recipe, we must familiarize ourselves with the confusion matrix and the alternative performance metrics crucial for evaluating the model’s effectiveness. Let’s start by learning the confusion matrix in the following subsection.

Evaluating the performance with the confusion matrix

A confusion matrix is an NxN matrix reporting the number...

Quantizing the model with the TensorFlow Lite converter

The TensorFlow model produced in the previous recipe is well suited for sharing or resuming training sessions. However, the model cannot be used for a microcontroller deployment because of its high memory requirements, which are mainly due to the following reasons:

  • The weights are stored in floating-point format
  • It keeps information that’s not required for the inference

Since our target device has computational and memory constraints, it is crucial to transform the trained model into something more compact.

This recipe will teach you how to convert the trained model into a lightweight format with the help of TensorFlow Lite and post-training integer 8-bit quantization.

Getting ready

TensorFlow Lite and post-training integer 8-bit quantization are the main ingredients that make the trained model suitable for inference on devices with reduced memory computational capabilities...

Reading temperature and humidity data with the Arduino Nano

The Arduino Nano and Raspberry Pi Pico have unique hardware features that make them ideal for tackling different development scenarios. For example, the Arduino Nano has a built-in temperature and humidity sensor. As a result, we do not need external components for our project with this board.

In this recipe, we will show how to read a single temperature and humidity sensor data using the Arduino Nano.

Getting ready

Since the temperature and humidity sensor is integrated into the board, no external components need to be connected, and the software library required to interface with the sensor is already integrated into the Arduino Web Editor.

If you use the local Arduino IDE, this library can be installed easily through the Arduino Library Manager. The instructions to install the library have been reported in the following How to do it… section.

As a result, to accomplish...

Reading temperature and humidity with the DHT22 sensor and the Raspberry Pi Pico

Unlike the Arduino Nano, the Raspberry Pi Pico requires an external sensor and an additional software library to measure the temperature and humidity.

In this recipe, we will show how to use the DHT22 sensor with a Raspberry Pico to get temperature and humidity measurements.

Getting ready

The temperature and humidity sensor module considered for the Raspberry Pi Pico is the low-cost AM2302.

As shown in the following diagram, the AM2302 module is a through-hole component with three pins that integrates the DHT22 temperature and humidity sensor:

Figure 3.17: The AM2302 module with the DHT22 sensor

Figure 3.18 summarizes the key characteristics of the DHT22 sensor:

Figure 3.18: Key characteristics of the DHT22 temperature and humidity sensor

DHT11 is another popular temperature and humidity sensor from the DHT family. However, we cannot...

Preparing the input features for the model inference

As we know, the model’s input features are the scaled and quantized temperature and humidity of the last three hours.

In this recipe, we will see how to prepare this data on the microcontroller. In particular, this recipe will teach us how to acquire, scale, and quantize the sensor measurements and keep them in temporal order using a circular buffer.

Getting ready

Our application will acquire the temperature and humidity every hour to get the necessary input features for the model. However, how can we keep the last three measurements in temporal order to feed the network the correct input?

In this recipe, we will use a circular buffer, a fixed-sized data structure that implementsFirst-In-First-Out (FIFO) buffer.

This data structure is well suited to buffering data streams and can be implemented with an array and a pointer that indicates the location in memory where...

On-device inference with TensorFlow Lite for Microcontrollers

Here we are, ready to dive into our first ML application on microcontrollers.

This recipe will guide us through deploying the trained model using TensorFlow Lite for Microcontrollers (tflite-micro) on the Arduino Nano and Raspberry Pi Pico.

Getting ready

Tflite-micro is a component of TensorFlow Lite designed explicitly by Google and the open-source community to run ML models on microcontrollers and other devices with only a few kilobytes of memory.

Theoretically, nothing prevents you from using tflite-micro to run ML models on your laptop. However, it may not perform well since tflite-micro is optimized for low-resource devices such as microcontrollers.

Running a model with TensorFlow Lite or tflite-micro typically consists of the following:

  1. Loading the model: We load the weights and network architecture stored in the TensorFlow Lite model.
  2. Preparing the input data: We convert the...

Summary

The recipes presented in this chapter demonstrated how to deploy a model trained with TensorFlow using tflite-micro on Arduino-compatible platforms, such as the Arduino Nano and Raspberry Pi Pico.

Initially, we learned how to build a dataset to forecast snow using the temperature and humidity over the last three hours. In this part, we focused on the importance of feature scaling and proposed the Z-score function to bring the input features to a similar numerical range.

Afterward, we delved into the model training phase. Here, we trained the network with TensorFlow and learned how to make the model compact with TensorFlow Lite using 8-bit quantization.

Then, we discovered how to acquire temperature and humidity measurements with the Arduino Nano and Raspberry Pi Pico. In particular, we learned how to use the built-in sensor on the Arduino Nano and connect the DHT22 sensor to the Raspberry Pi Pico.

Finally, we deployed the trained model on the microcontrollers...

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt.link/tiny

lock icon
The rest of the chapter is locked
You have been reading a chapter from
TinyML Cookbook - Second Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781837637362
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gian Marco Iodice

Gian Marco Iodice is team and tech lead in the Machine Learning Group at Arm, who co-created the Arm Compute Library in 2017. The Arm Compute Library is currently the most performant library for ML on Arm, and it's deployed on billions of devices worldwide – from servers to smartphones. Gian Marco holds an MSc degree, with honors, in electronic engineering from the University of Pisa (Italy) and has several years of experience developing ML and computer vision algorithms on edge devices. Now, he's leading the ML performance optimization on Arm Mali GPUs. In 2020, Gian Marco cofounded the TinyML UK meetup group to encourage knowledge-sharing, educate, and inspire the next generation of ML developers on tiny and power-efficient devices.
Read more about Gian Marco Iodice