PyTorch is an open source machine learning library. It is widely used for applications such as computer vision and natural language processing.
First, update the Raspbian OS (on the terminal):
sudo apt-get update
sudo apt-get dist-upgrade
Install dependencies (on the terminal):
sudo apt install libopenblas-dev libblas-dev m4 cmake cython python3-dev python3-yaml python3-setuptools
sudo apt-get install libavutil-dev libavcodec-dev libavformat-dev libswscale-dev
You can install PyTorch and PyTorch Vision using the pre-compiled Python wheel files:
First, download wheel files from https://github.com/sungjuGit/Pytorch-and-Vision-for-Raspberry-Pi-4B
sudo pip3 install replace_with_name_for_pytorch_wheel.whl
sudo pip3 install replace_with_name_for_pytorchvision_wheel.whl
Or, if you would like to get the latest version by building PyTorch from the source yourself, which may take 8~10 hours (otherwise skip this step):
mkdir pytorch_install
cd pytorch_install
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
git submodule update --remote third_party/protobuf
export NO_CUDA=1
export NO_DISTRIBUTED=1
export NO_MKLDNN=1
export NO_NNPACK=1
export NO_QNNPACK=1
python3 setup.py build
sudo -E python3 setup.py install
cd ..
mkdir pytorch_vision_install && cd pytorch_vision_install
git clone --recursive https://github.com/pytorch/vision
cd vision
sudo -E python3 setup.py install
Check the installation by running a simple test and confirming that Python outputs a tensor (5x3 matrix) with random number elements:
cd
python3
(in the python shell)>>> from __future__ import print_function
(in the python shell)>>> import torch
(in the python shell)>>> a = torch.rand(5,3)
(in the python shell)>>> print (a)