The following examples demonstrate to train Deep Learning models using PyTorch and then deploy on a Raspberry Pi 3 to get predictions.
The PyTorch installation on Raspberry Pi 3 uses this docker image: lisaong/armv71-torch-py3.6
- Train model on desktop system
- Transfer to RaspberryPi to evaluate
- Basic PyTorch training and deployment
- RNN to generate multi-sine wave
- RNN on sensor data to predict equipment failure
Reference: https://pytorch.org/get-started
Install Cuda 9.2: https://developer.nvidia.com/cuda-92-download-archive
(both the Base installer and Patch 1)
conda create -n torch python=3.6
conda activate torch
conda install pytorch cuda92 -c pytorch
pip install torchvision
pip install tensorboardX
# test gpu install
python -c "import torch; print(torch.rand(5, 3).cuda())"
conda create -n torch python=3.6
conda activate torch
conda install pytorch torchvision -c pytorch
pip install tensorboardX
These steps have been tested on a Model 3B.
- Download and flash a recent Raspbian Stretch Lite image onto a 8GB or larger micro SD card. Note that the Raspbian Stretch Lite image does not contain a desktop.
- Boot up the Raspberry Pi
sudo apt-get install git
, thengit clone https://github.com/lisaong/stackup-workshops
- Bootstrap git-lfs
# install git-lfs
cd stackup-workshops/ai-edge/bootstrap
sh ./install_git_lfs_rpi.sh
# go to the stackup-workshops folder and run git lfs pull
cd ../..
git lfs pull
- Reboot Raspberry Pi
- Install docker
curl -sSL get.docker.com | sh
sudo usermod -aG docker pi
# IMPORTANT: log out, then log back in again for changes to take effect, then run the next line
sudo systemctl start docker
- Launch the PyTorch docker image (lisaong/armv71-torch-py3.6)
cd stackup-workshops/pi-pytorch/docker
sh launch_docker.sh
- From the docker image, run the following test script to load and evalute a neural network. You should see output like below (actual values will differ because of random seed).
root@xxxxx:/code# cd tutorials/train
root@xxxxx:/code/tutorials/train# python3 nn_eval.py
result tensor([[-1.3430, 0.3558, -1.3451, 0.6432, -0.6340, -0.1667, -0.5551,
0.3526, -0.1512, -0.1724]])
- (Optional) MQTT IP publishing. Refer to this location for a setup script that will automatically publish the Wifi network's IP.