Machine learning with Raspberry Pi 4 and Coral USB Accelerator

Introduction

This article summarizes the installation procedure of Raspberry Pi 4 and Coral USB Accelerator as a method of machine learning with edge devices.

** Raspberry Pi 4 ** is the latest generation of current models. The CPU and memory performance has been greatly improved, and it also supports USB3.0.

写真 2020-02-17 0 31 29.jpg

** Coral USB Accelerator ** is Google's dedicated ASIC developed by Google for machine learning on edge devices.

写真 2019-12-30 0 02 35.jpg

This dedicated ASIC is called the Edge TPU and can take advantage of TensorFlow Lite's flakework, which specializes in making inferences at the edge. In addition, we do not support other than the flake work of TensorFlow Lite. In addition, a USB 3.0 connection is required to maximize the performance of the Coral USB Accelerator. In summary, the Raspberry Pi 4 is a great way to get the most out of your Coral USB Accelerator.

Raspberry Pi4 Although not officially supported, Google has provided a pre-compiled Raspberry Pi image called ** Edge TPU Platforms ** for using Edge TPU.

It has a built-in trained image classification model, etc., which will be useful for machine learning on the Raspberry Pi.

I will explain the procedure to use the image of Raspberry Pi 4 from EdgeTPU Platforms.

Please note that the interface of the Raspberry Pi 4 has been changed to ** USB Type-C ** for the power supply and ** Micro HDMI ** for HDMI.

EdgeTPU Platforms

  1. Download the ** Raspberry Pi 4, Buster, Edgetpu 2.11.1 ** image from google-coral / edgetpu-platforms ..
  2. Write the downloaded image to the SD card. For how to write to the SD card, refer to Raspberry Pi Basics that I wrote earlier.
  3. After writing to the SD card, start Raspberry Pi and perform the initial setup.

** If you boot with the downloaded image, you need to extend the file system to fit the size of the SD card. ** **

File system extension

The system area (/) before file system expansion is in the following state.

pi@raspberrypi:~ $ df -h
File system size used Remaining used%Mount position
/dev/root        3.3G  3.0G  100M   97% /
devtmpfs         1.8G     0  1.8G    0% /dev
tmpfs            1.9G     0  1.9G    0% /dev/shm
tmpfs            1.9G  8.6M  1.9G    1% /run
tmpfs            5.0M  4.0K  5.0M    1% /run/lock
tmpfs            1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/mmcblk0p1   253M   41M  213M   16% /boot
tmpfs            386M     0  386M    0% /run/user/1000

Execute the following command to start raspi-config and operate interactively.

$ sudo raspi-config

スクリーンショット 2019-12-30 17.38.27.png

First, select ** "7 Advanced Options" **.

スクリーンショット 2019-12-30 17.38.38.png

Then select A1 Expand Filesystem.

スクリーンショット 2019-12-30 17.38.54.png

Click OK.

スクリーンショット 2019-12-30 17.39.42.png

Execute the following command to restart.

$ sudo systemctl reboot

After rebooting, you can see that the system area (/) has been expanded.

pi@raspberrypi:~ $ df -h
File system size used Remaining used%Mount position
/dev/root         29G  3.1G   24G   12% /
devtmpfs         1.8G     0  1.8G    0% /dev
tmpfs            1.9G     0  1.9G    0% /dev/shm
tmpfs            1.9G  8.6M  1.9G    1% /run
tmpfs            5.0M  4.0K  5.0M    1% /run/lock
tmpfs            1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/mmcblk0p1   253M   41M  213M   16% /boot
tmpfs            386M     0  386M    0% /run/user/1000

Coral USB Accelerator To use Coral USB Accelerator, you need to do the following:

-[x] Edge TPU runtime installation -[x] Install TensorFlow Lite Library

To communicate with the Edge TPU, install the Edge TPU runtime. In addition, a library etc. is required to use TensorFlow Lite with Python. In this article, we will install a simple tflite_runtime library to use the TensorFlow Lite model in Python.

preparation work

Open the box containing the Coral USB Accelerator.

写真 2019-12-30 17 08 16.jpg

Connect the Coral USB Accelerator to your Raspberry Pi 4. USB3.0 has a ** blue </ font> ** interface.

写真 2020-02-16 23 52 49.jpg

Install Edge TPU runtime

--Add repository $ echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list $ curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - $ sudo apt-get update

--Installation $ sudo apt-get install libedgetpu1-std

Install the TensorFlow Lite library

--Download library $ wget https://dl.google.com/coral/python/tflite_runtime-1.14.0-cp37-cp37m-linux_armv7l.whl

--Installing the library $ pip3 install tflite_runtime-1.14.0-cp37-cp37m-linux_armv7l.whl

Executing the TensorFlow Lite API

Image classification with reference to Get started with the USB Accelerator Let's run the machine.

$ python3 classify_image.py --model models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite --labels models/inat_bird_labels.txt --input images/parrot.jpg

INFO: Initialized TensorFlow Lite runtime.
----INFERENCE TIME----
Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory.
18.6ms
4.6ms
4.6ms
4.6ms
4.6ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.76562

I used TensorFlow Lite to perform inference on the Edge TPU. By the way, the output result when executed with USB 2.0 is as follows. You can see that it is slower than USB3.0.

INFO: Initialized TensorFlow Lite runtime.
----INFERENCE TIME----
Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory.
120.5ms
11.5ms
11.5ms
11.7ms
11.6ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.76562

in conclusion

Now you can machine learn on edge devices.

Next, create a model with TensorFlow and convert the model to TensorFlow Lite.

reference

Recommended Posts

Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Pet monitoring with Rekognition and Raspberry pi
Try Object detection with Raspberry Pi 4 + Coral
MQTT RC car with Arduino and Raspberry Pi
Get temperature and humidity with DHT11 and Raspberry Pi
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Easy IoT to start with Raspberry Pi and MESH
Easy machine learning with scikit-learn and flask ✕ Web app
Detect mask wearing status with OpenCV and Raspberry Pi
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
USB boot with Raspberry Pi 4 Model B (3) LVM edition
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
Practical machine learning with Scikit-Learn and TensorFlow-TensorFlow gave up-
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Display USB camera video with Python OpenCV with Raspberry Pi
Machine learning learned with Pokemon
Mutter plants with Raspberry Pi
Machine learning with Python! Preparation
Machine learning Minesweeper with PyTorch
Machine learning and mathematical optimization
Beginning with Python machine learning
Try machine learning with Kaggle
[Machine learning] Start Spark with iPython Notebook and try MLlib
Easy introduction to home hack with Raspberry Pi and discord.py
Build a machine learning scikit-learn environment with VirtualBox and Ubuntu
Create a web surveillance camera with Raspberry Pi and OpenCV
Python beginner opens and closes interlocking camera with Raspberry Pi
Create an LCD (16x2) game with Raspberry Pi and Python
I tried connecting Raspberry Pi and conect + with Web API
Production of temperature control system with Raspberry Pi and ESP32 (1)
Measure and compare temperature with Raspberry Pi and automatically generate graph
Machine learning to learn with Nogizaka46 and Keyakizaka46 Part 1 Introduction
Raspberry Pi + python + IoT device, environment construction procedure to start image processing and machine learning
[Raspberry Pi] Stepping motor control with Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
USB over ethernet using Raspberry pi
MQTT on Raspberry Pi and Mac
Significance of machine learning and mini-batch learning
[Machine learning] Try running Spark MLlib with Python and make recommendations
Serial communication with Raspberry Pi + PySerial
I tried machine learning with liblinear
Machine learning with python (1) Overall classification
Christmas classic (?) Lighting a Christmas tree with Raspberry Pi and Philips Hue
OS setup with Raspberry Pi Imager
Classification and regression in machine learning
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Try machine learning with scikit-learn SVM
Try L Chika with raspberry pi
Make a Kanji display compass with Raspberry Pi and Sense Hat
Organize machine learning and deep learning platforms
VPN server construction with Raspberry Pi
Graph display of household power consumption with 3GPI and Raspberry Pi
[Reading Notes] Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow Chapter 1
Try moving 3 servos with Raspberry Pi
"Gaussian process and machine learning" Gaussian process regression implemented only with Python numpy
Quantum-inspired machine learning with tensor networks
Building a Raspberry Pi for studying Python and machine learning (RaspberryPi4 & Buster version (RaspberryPi3 is also possible))
Get started with machine learning with SageMaker