1. Overview
Artificial intelligence and neural networks have been hot topics in the field of computer science in recent years. Today, we will deploy an open-source image classification system on a Raspberry Pi. We will first train a convolutional neural network, and then you can input different images for it to classify. This tutorial will be very simple and easy to use, even if you don’t understand neural networks; you can modify it according to the tutorial to classify any image. We hope this video will help everyone feel the charm of neural networks. The main content of this tutorial can be divided into two parts: the installation of the latest TensorFlow on the official Raspberry Pi system, and the deployment and training of the neural network. For beginners, you can follow our tutorial and modify the necessary parts to run image classification, while advanced users can use TensorFlow and OpenCV installed in this tutorial for their own projects. Let’s get started!
Materials needed (Raspberry Pi system, dataset, network code, installation package) can be obtained by sending the keyword "image classification" to the WeChat public account [Raspberry Pi Enthusiasts Base] to receive the download link.
Repository address: Gitee: https://gitee.com/yangkun_monster/pi_cnn/tree/master Github: https://github.com/yangkun5330/pi_cnn/tree/main
2. Tutorial Content
1. Dataset Introduction
The dataset used this time is the Fashion-MNIST dataset, which is very commonly used for classification tasks in neural networks. This dataset contains 70,000 different product images in 10 categories. Among them, there are 60,000 training images and 10,000 test images. The image format is 28×28 grayscale images, as shown below.
The classification of the dataset is as follows:
2. System Introduction
Here we use the official Buster version of the Raspberry Pi system. Please do not use the latest official system (because the latest official Bullseye version has a built-in Python version of 3.9, which is incompatible with the TensorFlow package). The Raspberry Pi system has been provided in the materials.
3. Changing Raspberry Pi Source
sudo nano /etc/apt/sources.list
Comment out the original one with # and replace it with the following
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
sudo nano /etc/apt/sources.list.d/raspi.list
Comment out the original one with # and replace it with the following
deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui
sudo apt-get update sudo apt-get upgrade
4. Installing TensorFlow
TensorFlow is a deep learning framework released by Google in 2015 and is currently one of the mainstream deep learning frameworks. Here, we will use TensorFlow to implement convolutional neural networks.
Download the TensorFlow installation package from the materials (there are versions 2.3 and 2.4 in the materials; if you have TensorFlow on your computer, choose the same version as your computer, otherwise, you can choose any version), and then **copy it to the Raspberry Pi’s pi directory via a USB drive**.
If you want to download other versions yourself, follow the rules and methods below:
https://github.com/lhelontra/tensorflow-on-arm/releases
When downloading, pay attention to download the corresponding version:
(1) The middle CPXX represents the Python version. If you select the wrong one, installation will fail. For example, for Python 3.5, choose CP35; for Python 2.7, choose CP27.
(2) For Raspberry Pi 2/3/4, choose the one ending with armv7l.whl.
(3) It is not suitable to train models on Raspberry Pi, so first train the model on your computer, and then directly use the trained model on Raspberry Pi. Note that the TensorFlow versions on Raspberry Pi and your computer must be consistent, or there will be errors.
5. Installing TensorFlow
sudo pip3 install tensorflow-2.3.0-cp37-none-linux_armv7l.whl
During the installation process, some dependencies may fail to install. In that case, copy the download address, download it on your computer, and then copy it to the PI directory and use the command below to install it. (Replace XXXXXXXXXXXXXXX with the name of the downloaded file)
sudo pip3 install XXXXXXXXXXXXXXX
If you still cannot install successfully using the above method, you can use the following command to install:
sudo apt-get install python3-package_name e.g.: sudo apt-get install python3-opencv
6. Neural Network Deployment
Note: The code is open source, and the author’s information is retained in the code. We have made modifications to make it more suitable for Raspberry Pi.
Code Introduction: The code is open source, and the author’s information is retained in the code.
data_split.py: Dataset partitioning code. If you use the dataset in this article, you do not need to execute this code.
train_cnn.py: Code for building and training the network.
test_model.py: Code for testing the network’s recognition accuracy, which can be skipped.
window.py: Code for the graphical interface.
First, download some libraries
sudo apt-get install python3-opencv sudo apt-get install python3-pyqt5
Open THONNY, open the training network code (train_cnn.py), and run it. Then open the graphical interface code (window.py) to start testing.
Note: If you want to change to another dataset, you can download it online, and then use the dataset partitioning code (data_split.py) to split the dataset into training and testing sets. Then modify the training network code (train_cnn.py) and the graphical interface code (window.py) according to the comments in the code.