This post summarizes my successful attempt to execute PyTorch 1.8 in the Zybo-Z7 running Pynq 2.7.
The stages described in this post come from the following sources:
- Wheels available for ARMV7l architecture by Chiao-Wei Wang: GitHub - CW-B-W/PyTorch-and-Vision-for-Raspberry-Pi-4B
- PyTorch Notebooks from: Introduction to PyTorch - YouTube
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:
-
Testing PyTorch 1.8 on Zybo-Z7 running Pynq 2.7 2022/05/10 - YouTube
The execution time for each training mini batch is about 5 minutes. Hence, the video summarizes the complete process, which could take more than 1 hour .
- 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.
- $ cd /home/xilinx/
- $ wget http://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
- $ tar -xf Python-3.7.0
- $ ./configure
- $ make install
- 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
- 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β.
- 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.
- 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 , I hope you werenβt bored