Testing PyTorch 1.8 in Zybo-Z7 running Pynq 2.7

This post summarizes my successful attempt to execute PyTorch 1.8 in the Zybo-Z7 running Pynq 2.7. :grin:
The stages described in this post come from the following sources:

In addition, the post uses the Pynq 2-7 image for Zybo-Z7 proposed in Pynq 2.7 for Zybo-Z7, which can be downloaded from Zybo-Z7-2.7.0.img - Google Drive .

Lastly, a demonstration video of the PyTorch training a CNN to classify CIFAR10 can be found in:

  1. Installing Python3.7 (50 min approx.)
    The Pynq 2.7 release comes with Python3.8, but the latest PyTorch wheel found for the armv7l architecture is compiled for Python3.7. For this reason, the former python version must be installed in the Pynq.
  1. Create a virtual environment for Python3.7 (5 min approx.)
    To avoid unresolved dependencies between packages and be able to create an Ipython kernel in the future, it is necessary to create a virtual environment.
  • $ cd /home/xilinx/
  • $ python3.7 -m pip install virtualenv
  • $ python3.7 -m virtualenv pytorch_env
  • $ source pytorch_env/bin/activate
  1. Install PyTorch in the virtual environment (> 2h approx.)
    Warning: This step can take a lot of time.
    PyTorch requieres two wheels: torch-1.8 and torchvision-0.9.
  • $ git clone GitHub - CW-B-W/PyTorch-and-Vision-for-Raspberry-Pi-4B
  • $ cd PyTorch-and-Vision-for-Raspberry-Pi-4B/
  • $ pip3.7 install torch-1.8.0a0+56b43f4-cp37-cp37m-linux_armv7l.whl
  • $ pip3.7 install torchvision-0.9.0a0+8fb5838-cp37-cp37m-linux_armv7l.whl
  • $ python3.7 -m pip install matplotlib
  • $ apt install libopenblas-dev
  • $ exec $SHELL
  • $ cd /home/xilinx
  • $ source pytorch_env/bin/activate
  • $ python3.7
  • >>> import torch
  • >>> print(torch.__version__)
    The last command should return β€˜1.8.0a0+56b43f4’.
  1. Create the Ipython Kernel (1h approx.)
    To use PyTorch in a Jupyter notebook a kernel must be created.
  • (pytorch_env) $ python3.7 -m pip install ipykernel # 1h approx.
  • (pytorch_env) $ python3.7 -m ipykernel install --user --name=pytorch_kernel
    After this step the kernel for PyTorch has been created.
  1. Testing PyTorch
    The Notebooks used for testing PyTorch can be found in Introduction to PyTorch - YouTube, I have put a copy of the wheels and notebooks in https://gitlab.com/dorfell/fer_sys_dev/-/tree/master/01_hw/Pynq_Zybo-Z7, just in case.

Observations

  • The installation time could take several hours, so be patient.
  • The CNN training time over CIFAR10 was about 1 hour. In addition, the SoC memory is around 495 MB, and the training uses up to 85% of it.
  • Model accuracy and loss computed on the Zybo-Z7 is similar to the one reported in the notebook.

Thanks for reading the whole post :sweat_smile:, I hope you weren’t bored :stuck_out_tongue:

6 Likes

Please use the updated link to the Pynq 2.7 image: https://drive.google.com/file/d/17mufz0rYCEqsAM5xVtrHPzSmV1oAXh-7/view?usp=sharing