If you are anything like me, then you too couldn't resist the Raspberry Pi 4 4GB goodness. I wanted to improve the inference performance of my favourite Machine Learning project - Creating an image recognition solution with Azure IoT Edge and Azure Cognitive Services.
As a rough guide, the inference performance of my Azure Custom Vision model was as follows:
- Raspberry Pi 4 4GB - 720 Milliseconds
- Raspberry Pi 3B Plus - 1.2 Seconds
- Intel Core i5-8250U (Surface Laptop 2) - 230 Milliseconds
With the release of the Raspberry Pi 4 the Raspberry Pi Foundation has moved from Raspbian Stretch to Buster (Debian 10) as the default Linux distribution for all Raspberry Pis. For now, at least, it's busted a few things. So here are some tips and tricks for running Azure IoT Edge on a Raspberry Pi running Raspbian Buster.
This article is not intended as a guide to getting started with Raspberry Pi or Azure IoT Edge. For more information on those topics then read the following articles.
My Creating an image recognition solution with Azure IoT Edge and Azure Cognitive Services project is a great place to get started with Azure IoT Edge.
I typically run Raspbian Lite (Headless). Check out this guide "HEADLESS RASPBERRY PI 3 B+ SSH WIFI SETUP (MAC + WINDOWS)".
The Raspberry Pi 4 runs hot and you will likely need to provide some active cooling to prevent thermal throttling.
I like the Pimoroni case and Fan SHIM, it is a nice compact solution. Note, I have no affiliation with Pimoroni, I'm just a fan (pun intended, groan). There are other cool solutions for Raspberry Pi 4 too. Check out the Raspberry Pi 4 Thermals and Fan Shim article.
Check out the Getting Started with Fan SHIM article. In summary, install git and pip3 support, clone the Fan SHIM GitHub repo, install the dependencies, and then set up the automatic temperature monitor service that turns the fan on as required.
sudo apt install -y git sudo python3-pip && \ git clone https://github.com/pimoroni/fanshim-python && \ cd fanshim-python && \ sudo ./install.sh && \ cd examples && \ sudo ./install-service.sh --on-threshold 65 --off-threshold 55 --delay 2
The benefit of booting from USB 3 depends on how disk IO intensive your Azure IoT Edge Solution is. The Raspberry Pi 4 introduces vastly improved USB support and disk IO performance is impressive when combined with a USB 3 SSD drive. Check out the USB 3 Flash and SSD Disk Performance numbers I recorded.
You can't directly boot from USB 3 (yet), but you can boot from sd-card and then load the Operating System from USB.
In summary, create the boot sd-card as usual, then copy the OS files to the USB 3 drive, tweak the /boot/cmdline.txt to load the Operating System from the USB 3 drive. For step by step instructions follow the notes at How to Run Raspberry Pi 4 or 3 Off an SSD or Flash Drive
- Plug in your USB 3 drive, then list your drives. If you only plugged in one USB drive then it's highly likely your drive with be /dev/sda.
sudo fdisk -l
- Delete existing partitions and create a new primary partition on the USB drive.
sudo fdisk /dev/sda
**fdisk commands** - p = print partitions - d = delete a partition - n = new partition - create a primary partition - w = write the partition information to disk
- Format the newly created partition
sudo mkfs.ext4 /dev/sda1
- Create a mount point, mount the USB 3 drive, copy the Operating System files to the USB drive, and amend the cmdline.txt to enable booting from the USB 3 drive
sudo mkdir /media/usbdrive && \ sudo mount /dev/sda1 /media/usbdrive && \ sudo rsync -avx / /media/usbdrive && \ sudo sed -i '$s/$/ root=\/dev\/sda1 rootfstype=ext4 rootwait/' /boot/cmdline.txt
- Reboot the Raspberry Pi
curl -sSL get.docker.com | sh && sudo usermod pi -aG docker && sudo reboot
Buster does not ship with the require libssl1.0.2 library. As an interim workaround install this library before installing IoT Edge.
sudo apt-get install libssl1.0.2
curl https://packages.microsoft.com/config/debian/stretch/multiarch/prod.list > ./microsoft-prod.list && \ sudo cp ./microsoft-prod.list /etc/apt/sources.list.d/ && \ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg && \ sudo cp ./microsoft.gpg /etc/apt/trusted.gpg.d/ && \ sudo apt-get update && \ sudo apt-get -y install iotedge
sudo nano /etc/iotedge/config.yaml
sudo systemctl restart iotedge
Setting up public/private keys for SSH login and authentication is very handy (and secure).
The following creates a new SSH key, copies the public key to the Raspberry Pi. Take the default options.
Use the built-in Windows 10 (1809+) OpenSSH client. First install the OpenSSH Client for Windows (one time only operation).
From PowerShell as Administrator.
Add-WindowsCapability -Online -Name OpenSSH.Client
- From PowerShell, create a key pair.
ssh-keygen -t rsa
- From PowerShell, copy the public key to your Raspberry Pi
cat ~/.ssh/id_rsa.pub | ssh firstname.lastname@example.org "mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys"
- Create your key. Typically a one time operation.
ssh-keygen -t rsa
- Copy the public key to your Raspberry Pi. From Linux and macOS.
Torrent Tool for Windows (fastest way to download Raspbian Images)
- qBittorrent Windows Torrent Utility for fast download of Raspbian Images
- Burn Raspbian to sd card with Balena Etcher
- Raspberry Pi Storage Benchmarks 2019
For reference, these are performance stats I recorded for various drives using the performance tool found at Raspberry Pi Storage Benchmarks 2019
Category Test Result HDParm Disk Read 40.22 MB/s HDParm Cached Disk Read 39.47 MB/s DD Disk Write 17.8 MB/s FIO 4k random read 2614 IOPS (10457 KB/s) FIO 4k random write 296 IOPS (1186 KB/s) IOZone 4k read 8669 KB/s IOZone 4k write 2808 KB/s IOZone 4k random read 8609 KB/s IOZone 4k random write 1480 KB/s Score: 923
Category Test Result HDParm Disk Read 98.99 MB/s HDParm Cached Disk Read 70.08 MB/s DD Disk Write 21.6 MB/s FIO 4k random read 2238 IOPS (8953 KB/s) FIO 4k random write 116 IOPS (464 KB/s) IOZone 4k read 16067 KB/s IOZone 4k write 2204 KB/s IOZone 4k random read 7747 KB/s IOZone 4k random write 461 KB/s Score: 860
Category Test Result HDParm Disk Read 551.09 MB/s HDParm Cached Disk Read 77.30 MB/s DD Disk Write 55.0 MB/s FIO 4k random read 994 IOPS (3976 KB/s) FIO 4k random write 312 IOPS (1250 KB/s) IOZone 4k read 17479 KB/s IOZone 4k write 1616 KB/s IOZone 4k random read 4052 KB/s IOZone 4k random write 1005 KB/s Score: 1087
Category Test Result HDParm Disk Read 297.42 MB/s HDParm Cached Disk Read 264.49 MB/s DD Disk Write 69.0 MB/s FIO 4k random read 15019 IOPS (60076 KB/s) FIO 4k random write 8239 IOPS (32957 KB/s) IOZone 4k read 36059 KB/s IOZone 4k write 27480 KB/s IOZone 4k random read 20925 KB/s IOZone 4k random write 33354 KB/s Score: 6939