avatarMichał Marcińczuk, Ph.D.

Summary

The web content provides essential tips for working with Raspberry Pi Zero, focusing on hardware differences, memory management, and software compatibility.

Abstract

The article titled "Image classification on Raspberry Pi Zero (part 1) — Introduction" offers insights into the Raspberry Pi Zero's capabilities, emphasizing the importance of understanding its 32-bit and 64-bit variants for machine learning applications. It highlights that while the Raspberry Pi Zero 2 W is equipped with 512 MB SDRAM, only 419 MB is usable, with the operating system consuming a significant portion. The default SWAP size of 100 MB is often insufficient for intensive tasks, leading to potential system freezes, and the article suggests increasing it to 2 GB. Additionally, the article stresses the importance of being vigilant about module and library versions to avoid compatibility issues, recommending the tracking of specific versions that work well together.

Opinions

  • The author believes that the difference between the 32-bit and 64-bit versions of the Raspberry Pi Zero is significant, especially for machine learning tasks, as Torch can be installed on the 64-bit version but not on the 32-bit version without compiling from source.
  • The author points out that the actual RAM available on the Raspberry Pi Zero 2 W is less than the advertised 512 MB due to system reservations, which users should be aware of when planning memory-intensive applications.
  • The article suggests that the default SWAP size is inadequate and that increasing it can prevent system freezes during resource-demanding operations, implying that this is a common issue that users should proactively address.
  • There is an opinion that users should pay close attention to the versions of third-party modules and libraries they use, as relying on the latest releases can lead to unexpected errors, particularly highlighted with the example of PyTorch's release history and the issues encountered post-October 4, 2023.
  • The author advocates for documenting the exact versions of modules that are known to work together, emphasizing the importance of this practice in maintaining a stable development environment.

Image classification on Raspberry Pi Zero (part 1) — Introduction

Five lessons worth to know before you start working with Raspberry Pi Zero

{0} Table of Contents

{1} 32-bit vs 64-bit {2} Actual RAM size {3} Default SWAP is too small {4} Keep attention to module versions

{1}💡32-bit vs 64-bit

Raspberry Pi Zero has two variants: 32-bit (W) and 64-bit (2 W). The physical size is the same, but the difference is essential. Torch (a machine learning framework) can be easily installed on the 64-bit version but not on the other. There are no official builds of the torch for 32-bit ARM systems, and you would need to install it from the source.

{2}💡Actual RAM size

According to the official specification, Raspberry Pi Zero 2 W has 512 MB SDRAM memory. However, when you run htop you will see just 419 MB. Moreover, the OS Lite will already use 60 MB out of 419 MB. For OS with desktop, it will be even.

{3}💡Default SWAP is too small

The default SWAP size is 100 MB. In many applications, this might be insufficient. Even while installing Python libraries, you can encounter 100% SWAP usage, leading to a system freeze. You can easily increase the SWAP size up to 2 GB, which is a huge convenience.

# Stop Swap
sudo dphys-swapfile swapoff
# Modify the size of the swap
sudo vi /etc/dphys-swapfile
CONF_SWAPSIZE=2024
# Initialize Swap File
sudo dphys-swapfile setup
# Start Swap
sudo dphys-swapfile swapon

To check your current SWAP, you can use, for example, htop.

htop

View before any changes:

htop showing the SWAP size

And increasing the SWAP using dphys-swapfile:

htop showing the SWAP size

To recognize if your freezes are caused by the too small SWAP you can also use htop. Before running a script, open the htop in a new terminal. Then, run your script and observe the level of SWAP. When you reach the 99% SWAP usage and the terminal stops responding, you can be sure that you have to increase SWAP.

99% SWAP usage in htop

{4} 💡Keep attention to module versions

This universal rule applies to any scenario using third-party modules and libraries. It can be frustrating when, while setting up the environment, one day, everything works, and the next day, you get errors. For example, in the case of PyTorch, it is a convenient way to copy-paste the installation instructions from the home page.

PyTorch installation

This form always points to the latest release. Sometimes, the newest release might need some fixing. It might have some bug, or the dependent modules might not be compatible with the latest version. To see how event new versions are released, you can go to the history of releases: https://pypi.org/project/torch/#history

torch release history

As you can see in the above picture, until 4 October 2023, the default version of the torch was 2.0.1. Since 4th October, it has been 2.1.0. After that day, I encountered unexpected problems with the Segmentation fault caused by the latest version.

It is a good practice to track versions of your modules and not rely on the default/latest releases. Remember to write down exact versions when you find a combination of working modules. For Python, you can use pip:

pip freeze

Sample output:

accelerate==0.23.0
aiohttp==3.8.6
aiosignal==1.3.1
async-timeout==4.0.3
attrs==23.1.0
...
Raspberry Pi
Deep Learning
Machine Learning
Computer Vision
Microcontrollers
Recommended from ReadMedium