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.
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?
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?
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..
Below are the libraries/packages we will be installing -
- Python 3.7
- PyTorch dependencies
- torch
- torchvision
- fast.ai
- 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 -
Run the below command to get hold of the ARM processor configuration of our Pi required for searching compatible PyTorch wheel -
uname -a
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:
- Building PyTorch from source: If you are interested in this , please refer to the amazing blog — Building PyTorch for the Raspberry Pi boards
- 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 :
python3 --version
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.
sudo apt update && sudo apt upgrade
mkdir project_folder
cd project_folder
python3 -m venv env
source env/bin/activate
Install PyTorch dependencies first:
sudo apt install libopenblas-dev libblas-dev m4 cmake cython python3-dev python3-yaml python3-setuptools python3-wheel python3-pillow python3-numpy
Ensure torch and torchvision wheel files are saved in the project_folder and type in terminal:
sudo apt install libatlas3-basesudo pip3 install numpypython3 -m pip install Pillow==6.1pip3 install torch-1.3.0a0+deadc27-cp37-cp37m-linux_armv7l.whlpip3 install torchvision-0.4.0a0+d31eafa-cp37-cp37m-linux_armv7l.whlpip3 install fastai --no-deps
To test if everything is installed correctly, log into your python terminal and run the commands:
$ python3.7>>> import torch>>> import torchvision>>> import fastai
If you get further errors while doing —
from fastai.vision import *
Create new text file in project_folder and copy below contents. Name it requirements.txt
beautifulsoup4
bottleneck
fastprogress>=0.2.1
matplotlib
numexpr
nvidia-ml-py3
packaging
pandas
pyyaml
requests
scipy
Now type in terminal:
pip3 install -r requirements.txt
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 -
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
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: