Original article can be found here (source): Deep Learning on Medium
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.
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
- 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 –
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.
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 :
sudo apt update && sudo apt upgrade
python3 -m venv env
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
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.