Get Informed out of Data

Full width home advertisement

Raspbian

DataScience

Post Page Advertisement [Top]

Install PyTorch on Raspberry Pi 4

Install PyTorch on Raspberry Pi 4

PyTorch is a crucial library to enable deep learning models to process in Raspberry Pi.

Procedure for Installing PyTorch and PyTorch Vision on Raspberry Pi 4B

1. First, update the Raspbian OS:

  • sudo apt-get update
  • sudo apt-get dist-upgrade

2. Install dependencies:

  • 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

3. You can install PyTorch and PyTorch Vision using the pre-compiled Python wheel files for saving your time for compiling:

4. Or, if you would like to get the different version by building PyTorch from the source yourself, which may take 6~10 hours (otherwise skip this step):

  • For preventing OOM memory error, add swap memory:

    • sudo dphys-swapfile swapoff
    • sudo fallocate -l 2G /swapfile
    • sudo chmod 600 /swapfile
    • sudo mkswap /swapfile
    • suso swapon /swapfile
    • sudo swapon /swapfile
    • sudo nano /etc/fstab
    • sudo swapon --show
    • sudo free -h
  • and for preventing blocked for more than 120 seconds. stack, run:

    • sudo sysctl -w vm.dirty_ratio=10
    • sudo sysctl -w vm.dirty_background_ratio=5

    Then commit your changes:

    • sudo sysctl -p

Compile Pytorch and Torchvision

1. Download source code and compile

  • mkdir pytorch_installation
  • cd pytorch_installation
  • git clone --recursive https://github.com/pytorch/pytorch
  • cd pytorch
  • git checkout v1.6.0 # choose the version
  • git submodule sync
  • git submodule update --init --recursive
  • git submodule update --remote third_party/protobuf

2. Edit CMakeLists.txt to set two flags:

  • option(USE_PYTORCH_QNNPACK "Use ATen/QNNPACK (quantized 8-bit operators)" OFF) # By dedault is ON
  • option(USE_SYSTEM_SLEEF "Use system-provided sleef." ON) # By default is OFF

3. Compile Pytorch

  • export BUILD_TEST=0
  • export USE_SYSTEM_NCCL=0
  • export USE_CUDA=0
  • export USE_MKLDNN=0
  • export USE_NNPACK=0
  • export USE_QNNPACK=0
  • export USE_NUMPY=1
  • export USE_DISTRIBUTED=0
  • export NO_CUDA=1
  • export NO_DISTRIBUTED=1
  • export NO_MKLDNN=1
  • export NO_NNPACK=1
  • export NO_QNNPACK=1
  • export ONNX_ML=1
  • python3 setup.py build
  • sudo -E python3 setup.py install
  • python setup.py bdist_wheel # build whl file. The wheel will be in the dist/ folder.

4. Compile Torchvision

  • cd ..
  • mkdir pytorch_vision && cd pytorch_vision
  • git clone --recursive https://github.com/pytorch/vision
  • cd vision
  • sudo -E python3 setup.py install
  • python setup.py bdist_wheel # build whl file. The wheel will be in the dist/ folder.

5. Check the installation by running a simple test and confirming that Python outputs a tensor

  • cd
  • python3
  • from __future__ import print_function
  • import torch
  • a = torch.rand(5,3)
  • print (a)

No comments:

Post a Comment

Bottom Ad [Post Page]