Pytorch on RaspberryPi
Finally, I successfully installed Pytorch on my RaspberryPi 3B. I followed the instructions on How to install PyTorch v0.3.1 on RaspberryPi 3B and a blog post (in Chinese) 在 RaspberryPi 上编译 PyTorch. I am running latest raspian image 2018-04-18-raspbian-stretch and a self-compiled Python 3.6.5. If you are interested in compile Python 3.6.5 or above on your own, check out Installing Python 3.6 on Raspbian. In this post, I will write down steps to my successful installation, and share my compiled wheel file at the end.
First of all, we need to set up SWAP. Edit file
/etc/dphys-swapfile, find the constant
CONF_SWAPSIZE and change its value to at least 2048, which means sparing 2G for swap file system. I set it to 4096 (4G), because I use a 32G card. To activate the swap file system, do the following commands:
sudo /etc/init.d/dphys-swapfile stop sudo /etc/init.d/dphys-swapfile start
Then, we need to install dependencies.
sudo apt-get install libopenblas-dev cython3 libatlas-dev \ m4 libblas-dev cmake pip3 install --user pyyaml numpy
We shall now be ready to compile pytorch. We get pytorch from github, and choose a version (v0.4.0) to build.
git clone --recursive https://github.com/pytorch/pytorch cd pytorch git checkout tags/v0.4.0 -b build git submodule update --init --recursive
Since Pi does not have a GPU that supports CUDA, we are going disable it. We also disable distributed computing.
export NO_CUDA=1 export NO_DISTRIBUTED=1
Suppose we are now in the pytorch folder, use the following command to build
python3 setup.py build
This process will be long! On my Pi, it took at least 4 hours (I forgot to time it). I would recommend running the building process in the background:
date && python3 setup.py build && date &> message-build &
The above command will run the building process in the background and output building messages to a file named message-build. Also,
date command is used to time the building process. When Pi is working hard to compile
pytorch, don't run any other complicated work on it. Indeed, I will suggest not to interrupt it at all, including not to peek the building message in message-build frequently. It is a good idea to run the building process before going to sleep.
After a long enough time, check your message-build file to see if you have a successful build. We can look at the last row of the file:
tail -1 message-build
You should get a message about time from command
date. If you do not get it, make sure to check running processes using
pstree to see whether the building process has ended. Say we have successfully built
pytorch. Since I wanted to get a wheel file for
pytorch and installed it to the local user, I did not install it directly using
sudo python3 setup.py install. Instead, I went to pytorch folder and did the following commands:
export NO_CUDA=1 export NO_DISTRIBUTED=1 pip3 install --user wheel python3 setup.py bdist_wheel cd dist pip3 install --user torch-0.4.0a0+3749c58-cp36-cp36m-linux_armv7l.whl
Pytorch on RaspberryPi got! I share
pytorch wheel file so that you can avoid the compilation. It should save you a lot of time. But make sure you are running Python 3.6 and have numpy installed. If so, use the following command to install
pip install --user https://wormtooth.com/files/pytorch/torch-0.4.0a0+3749c58-cp36-cp36m-linux_armv7l.whl