banner tensorflow machine learning

TensorFlow and AI on Raspberry Pi: A Beginner’s Guide

If you click our links and make a purchase, we may earn an affiliate commission. Learn more

Artificial Intelligence is a powerful tool, with examples such as ChatGPT and image recognition software. Training artificial intelligence might seem challenging at first. However, using the TensorFlow library, you can train your first AI Model on a Raspberry Pi.

TensorFlow can be installed on Raspberry Pi using the pip command and then used within the Python IDE. However, before installing TensorFlow, a few dependencies are configured.

In this tutorial, I will guide you through installing TensorFlow on Raspberry Pi and configuring the dependencies correctly. I will then show you how to train your first AI algorithm to recognize handwritten numerals using TensorFlow and visualize it using TensorBoard.

If you’re like me and sometimes mix up syntax between programming languages, I’ve got just the thing for you. I’ve put together a Python cheat sheet with all the essential syntax in one place, so you can keep it handy and avoid any confusion. Download it here for free!

An Introduction To TensorFlow

Let’s start by setting the scene and introducing the main concepts you need to understand before we discuss the more technical stuff in the next section.

What is Artificial Intelligence

You’ve heard a lot about Artificial intelligence (AI) lately, as it has become increasingly popular in recent years. But do you know what it means?

AI has numerous applications, from large language models like ChatGPT to small features integrated into your iPhone camera.

In essence, AI is software designed to mimic the human brain’s functionality. Similar to humans, AI requires training using examples and data.

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now

An AI is composed of neurons arranged in multiple layers. Each neuron has its own mathematical properties, such as gain and activation functions. These layers of neurons are interconnected to mimic the structure of the human brain.

Creating an AI from scratch requires significant effort, but libraries like TensorFlow can simplify the process.

Prefer reading without ads and popups?
Members get an ad-free version of every guide, plus exclusive project support.
Join the Community | Sign In

TensorFlow

TensorFlow is an open-source library developed by Google for training and evaluating AI algorithms.

It is compatible with many programming languages, such as Python, C++, and JavaScript.

There are other similar tools, such as PyTorch and Keras. However, TensorFlow offers the following key advantages over its competition:

  • Scalability: TensorFlow is designed for production use, enabling seamless deployment across multiple platforms, including mobile, web, and cloud.
  • Ecosystem: TensorFlow has a rich ecosystem that includes tools like TensorBoard for visualization, TensorFlow Lite for mobile and embedded devices, and TensorFlow Serving for production deployment.
  • Flexibility: With the introduction of TensorFlow 2, the framework has become more user-friendly and flexible. It integrates seamlessly with Keras for high-level model building while allowing low-level control when needed.
  • Community and Support: TensorFlow has a large and active community with extensive documentation and tutorials.
  • Interoperability: TensorFlow supports multiple programming languages (Python, JavaScript, Java, and C++) and can be integrated with other libraries and frameworks, enhancing its versatility in different tech stacks.
  • Pre-Trained Models: TensorFlow Hub provides a repository of pre-trained models that can be easily integrated into applications, saving time and resources in development.

TensorFlow Light

TensorFlow Light is a lighter version of the original TensorFlow. It is designed for use on mobile computing platforms and embedded devices.

TensorFlow Light optimizes the trained Model utilizing quantization techniques like pruning to reduce the required memory usage and computing requirements.

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now

One key difference between the original TensorFlow and TensorFlow Light is that TensorFlow Light can only perform predictions on an already trained model. On the other hand, TensorFlow can be used to build and train ML algorithms.

Installing TensorFlow

Now that the concepts are clear, let’s get down to business and learn how to begin with TensorFlow on your Raspberry Pi.

Prerequisites/ Dependencies

Before installing TensorFlow on Raspberry Pi, there are a few Prerequisites/ Dependencies that you need to install. Follow these steps to get your system ready for the installation of TensorFlow:

  • Install Raspberry Pi OS on your Raspberry Pi using the Raspberry Pi Imager.
  • Ensure you have internet connectivity and SSH enabled during the installation using the configuration settings shown in the screenshots below:

    Alternatively, you can follow this tutorial to connect to the internet and enable SSH.
  • SSH into your Raspberry Pi using the command:
    ssh <username>@<hostname>
  • Make sure your device is up-to-date using the following command:
    sudo apt update && sudo apt full-upgrade
  • Make sure your Python version is between 3.9 and 3.12 using the command:
    python --version
  • Set up a Virtual Environment (venv) using the command:
    python3 -m venv tf
  • Activate the Virtual Environment using the command:
    source tf/bin/activate
  • Update your pip version to 19.0 or higher using the command:
    pip install --upgrade pip

That is it: Our Raspberry Pi is now ready to install TensorFlow.

Optional/Recommended Dependencies

With the current setup, you can run Tensorflow on your Raspberry Pi. However, to use better visualization aids for your models, I recommend installing the following additional dependencies (Optional/ Not Necessary):

  • Pydot: To install Pydot, run the following command inside the Virtual Env of your Tensorflow setup.
    pip install pydot
  • GraphViz: To install GraphViz, run the following command in your standard shell.
    sudo apt install graphviz

Installation

Having everything configured correctly, installation of TensorFlow is as simple as running a single command:

  • SSH into your Raspberry Pi using the command:
    ssh <username>@<hostname>
  • Activate the Virtual Environment using the command:
    source tf/bin/activate
  • Install TensorFlow using the command:
    pip install tensorflow
  • The installation might take some time, depending on your network connectivity speed. Once you’re finished, you’ll be returned to your SSH prompt.
Lost in the terminal? Grab My Pi Cheat-Sheet!
Download the free PDF, keep it open, and stop wasting time on Google.
Download now

As you can see, TensorFlow can be installed using a single command and automatically installs all missing dependencies.

Verifying Installation

You can enter the following command to verify that TensorFlow has been successfully installed on your Raspberry Pi:
pip show tensorflow

Alternatively, you can enter the Python IDE and import TensorFlow to check if it is integrated with your Python. If it has been correctly installed and configured, as shown below, you can import it successfully.

However, If it is not configured correctly with your Python, attempting to import it will give an error, as shown below.

How to train your first Neural Network using TensorFlow

After successfully installing TensorFlow on your Raspberry Pi, you can start training your first Machine Learning Algorithm. This tutorial will follow the official quick start guide on the TensorFlow documentation.

In this tutorial, we shall create and train a neural network on the MNIST database, a large dataset of handwritten numerals. It has a training set of 60,000 examples and a test set of 10,000 examples. A network trained on this network will be able to identify handwritten numerals such as shown below:

To make your first neural network and train it, follow these steps:

  • Launch the Python IDE.
  • Import TensorFlow into your program with the command:
    import tensorflow as tf
  • Verify that the correct version of TensorFlow has been loaded by using the command:
    print("TensorFlow Version:", tf.__version__)
  • The MNIST dataset is available through the Keras library. Many such datasets are available through the Keras database, which can be directly loaded and does not require you to download anything extra. Use the command to load the MNIST dataset:
    mnist = tf.keras.datasets.mnist
  • The dataset we loaded contains the Train data (used for training the network) and Test data (used for testing/ validating the network once it has been trained). However, we must first separate them and save them in arrays. Load train and test data from the dataset using the command:
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
  • The original MNIST dataset contains data on Pixel Values ranging from 0 to 255. However, we must rescale our neural network between 0 and 1. To do that, run the following command:
    x_train, y_train = x_train/255.0, y_train/255.0
  • Now, we need to define the different layers, their structure/ size, and activation functions. We can do this using the command:
    model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10)])
  • To train an AI model, we need to define a loss function. A loss function is a mathematical formula the training algorithm uses to calculate our model’s errors in predicting the answer. TensorFlow has many predefined loss functions. We can define a simple cross-entropy loss function using the command:
    loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
  • Before we start training the network, it’s crucial to decide which optimizer to use. An optimizer is an algorithm that utilizes the loss function to guide the model training efficiently. Just like the loss function, TensorFlow offers several pre-programmed optimizers to choose from. For our project, we will use the basic ‘Adam’ optimizer and compile our model using the command:
    model.compile(optimizer='adam', loss=loss_fn, metrics=['accuracy'])
  • Finally, we are ready to train our neural network. The following command can do this using the model.fit method:
    model.fit(x_train, y_train, epochs=5)
  • Wait for the training to be completed.
  • We can use the model.evaluate method to check if our model has been trained correctly and accurately:
    model.evaluate(x_test, y_test, verbose=2)
  • The model has been trained to predict the handwritten numbers of our test dataset with an accuracy of 98%.
Lost in the terminal? Grab My Pi Cheat-Sheet!
Download the free PDF, keep it open, and stop wasting time on Google.
Download now

As you can see, we have successfully built and trained our first neural network model using the TensorFlow library within the Python IDE. Our built model can predict the images of handwritten numerals with an accuracy of 98%.

Tip: Command lines can be a pain to memorize. I put the essential Linux commands on a printable cheat sheet so you don't have to keep googling them. You can grab the PDF here if you want to save some time.

Alternate Approach

If you are uncomfortable with the CLI, here is an alternate approach for running the same example. You can copy the following code, save it in a Python file, and execute it to achieve a similar result.

import tensorflow as tf
import datetime
import os

os.system('rm -rf ./logs/')

mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

def create_model():
  return tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28), name='layers_flatten'),
    tf.keras.layers.Dense(512, activation='relu', name='layers_dense'),
    tf.keras.layers.Dropout(0.2, name='layers_dropout'),
    tf.keras.layers.Dense(10, activation='softmax', name='layers_dense_2')
  ])

model = create_model()
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

dot_img_file = '/tmp/model_1.png'
tf.keras.utils.plot_model(model, 
	to_file=dot_img_file, 
	show_shapes=True, 
	show_dtype=True, 
	show_layer_names=True, 
	show_layer_activations=True)

model.fit(x=x_train,
          y=y_train,
          epochs=5,
          validation_data=(x_test, y_test),
          callbacks=[tensorboard_callback])

os.system('tensorboard --logdir logs/fit')

Note: You must install optional/recommended dependencies to run this example.

You can follow these steps to achieve this:

  • Copy the above code and paste it into a text editor.
  • Save this file at the location ~/myModel.py.
  • Open a Terminal and execute the following command:
    python myModel.py
  • Navigate to /temp and open the model_1.png file to visualize the layer structure of your model.
  • Additionally, you can open the web browser and go to http://localhost:6006/ for more detailed visualizations of the training process using the TensorBoard library.
  • TensorBoard can be used for different visualizations, including a Histogram and an Accuracy/ Loss versus Epoch graph.

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Prefer videos over reading? The RaspberryTips Community members get exclusive video lessons every month. Join now and watch them all right away. Get instant access.

Conclusion

Following this tutorial, you have successfully installed TensorFlow and learned how to use it on your Raspberry Pi. You have also successfully trained your very first Neural Network, which can identify handwritten numerals with an accuracy of 98%.

To learn more about TensorFlow’s capabilities, consult its official documentation. You can also follow further examples on this Tutorials page.

Whenever you’re ready, here are other ways I can help you:

Test Your Raspberry Pi Level (Free): Not sure why everything takes so long on your Raspberry Pi? Take this free 3-minute assessment and see what’s causing the problems.

The RaspberryTips Community: Need help or want to discuss your Raspberry Pi projects with others who actually get it? Join the RaspberryTips Community and get access to private forums, exclusive lessons, and direct help.

Master your Raspberry Pi in 30 days: If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.

Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.

You can also find all my recommendations for tools and hardware on this page.

Similar Posts