Quick setup instructions for installing PyTorch and fastai on Raspberry Pi 4

PyTorch and fastai on Raspberry Pi 4 Model B for doing deep learning tasks like image classification and object detection.

Sriramya Kannepalli
Analytics Vidhya
6 min readApr 4, 2020

--

source: https://www.raspberrypi.org/products/raspberry-pi-4-model-b/

Yes, like everyone who just started with Raspberry Pi to test their deep learning models, even I got too excited and curious to deploy my image classifier Baby_Vibes built with deep learning libraries PyTorch and fastai on my brand new Raspberry Pi 4 model B brought from amazon.com

Before I could jump into working with my model inference, there was no clear documentation readily available for setting up Raspberry Pi 4 with Pytorch and Fastai on Python 3.7. However, I found this discussion link from fastai forums very useful to get started.

Let’s quickly understand what are we dealing with —

What is PyTorch?

Source : https://pytorch.org/

PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing. It is primarily developed by Facebook’s AI Research lab. It is free and open-source software released under the Modified BSD license.

There are several PyTorch online tutorials and YouTube videos available now but still to highlight my favorites : the official PyTorch tutorials and fastai — Practical Deep Learning for Coders, v3

What is Fast.ai?

source: https://www.fast.ai/

fastai is a modern deep learning library, available from GitHub as open source under the Apache 2 license, which can be installed directly using the conda or pip package managers. It includes complete documentation and tutorials, and is the subject of the book Deep Learning for Coders with fastai and PyTorch: AI Applications Without a PhD (Howard and Gugger 2020).

And finally what is Raspberry Pi 4?

Your tiny, dual-display, desktop computer

…and robot brains, smart home hub, media centre, networked AI core, factory controller, and much more..

Raspberry Pi 4 Model B

Below are the libraries/packages we will be installing -

  1. Python 3.7
  2. PyTorch dependencies
  3. torch
  4. torchvision
  5. fast.ai
  6. fast.ai dependencies

Note: If this is the first time you are switching on your Raspberry Pi, please refer Raspberry Pi 4 Getting Started video and complete the initial boot up. Once your are done, open “Terminal” as shown below -

Raspberry Pi 4 Model B Terminal Screen

Run the below command to get hold of the ARM processor configuration of our Pi required for searching compatible PyTorch wheel -

If the output is armv7l GNU/Linux continue with the installation.

Wait !!! What is ARM processor and how do they differ from Intel processors present in most of our desktop PCs ??

ARM (Advanced RISC(Reduced Instruction Set Computing) Machines ) has been at the center of modern microprocessors and embedded design.

ARM processors are extensively used in consumer electronic devices such as smartphones, tablets, multimedia players and other mobile devices, such as wearables. Because of their reduced instruction set, they require fewer transistors, which enables a smaller die size for the integrated circuitry (IC).

Intel processors, on the other hand, fit into a family called CISC which stands for Complex Instruction Set Computing.

Unlike RISC computers, the instructions available on a CISC are more focused on performing complex tasks with large amounts of flexibility. Intel, for its part, has mainly produced processors aimed at high performance and high throughput environments, including desktop PCs, laptops, servers, and even supercomputers.

If you are interested to know more read this — Understanding the Differences Between ARM and x86 Processing Cores.

So that’s the reason why we cannot use a python package from our PC/Laptop/standard x86_64 machine directly into our Raspberry Pi since it will not be compatible with the processor architecture of Pi, instead python packages installed in Pi should be compiled for its specific architecture.

This being said there are two ways of installing PyTorch on Raspberry Pi:

  1. Building PyTorch from source: If you are interested in this , please refer to the amazing blog — Building PyTorch for the Raspberry Pi boards
  2. Using pre-built PyTorch wheels uploaded by some great people who did all the hard work for us.

We will be going with the easy one, option 2 and using the pre-built PyTorch wheels uploaded by others compatible with armv7l GNU/Linux.

So go ahead and download torch-1.3 and torchvision-0.4 wheel files and copy them to your pi with USB or directly download from the links using chromium browser in your Pi.

Python has two flavors, Python 2x and Python 3x. We will be working with Python3x for our installations.

In a terminal window, check for python 3.7 by typing :

If you get python version as 3.7, continue with the installation, if not then refer How to install Python 3.7 on Raspberry Pi. If you are new to virtual environments please refer this and move on.

Install PyTorch dependencies first:

Ensure torch and torchvision wheel files are saved in the project_folder and type in terminal:

To test if everything is installed correctly, log into your python terminal and run the commands:

If you get further errors while doing —

Create new text file in project_folder and copy below contents. Name it requirements.txt

Now type in terminal:

This should resolve your error and import torch, torchvision and fastai successfully for vision projects. We have skipped spacy dependency needed for the fastai text package, so for vision it should work. Refer fastai forums discussions.

Final Note : You may get warnings if you used a different version of PyTorch for training your model file ‘export.pkl’ and installed a different version of PyTorch on pi. If you feel these warnings can be ignored, python has a warnings module in sys-system specific parameters and functions library and this is how you can handle it -

Add this code snippet to your inference.py or app.py file.

If you could come along up to here. Pheww.. Yes we did it !! we are all set to test our deep learning PyTorch fastai image classification model inference on Raspberry pi 4 Model b. Hurray!!

If you want a sample code for testing, please clone Baby-Vibes github and start working.

Baby-Vibes uses Image classification to identify crying baby and pass a Voice command to Google Home to play their favorite cartoon for e.g. Tom and Jerry in this case, while we excuse us from every one and come to their rescue.

If you are interested in learning Pytorch fastai Azure Web service deployment you can refer this article.

Happy Coding !!!😊😊😊

References:

--

--

Sriramya Kannepalli
Analytics Vidhya

Startup Founder | Machine Learning Engineer | Tech Blogger