TensorFlow on a Raspberry Pi Zero is a Bad Idea

April 6, 2019

A recent question from the Something Awful Forums:

Can I run TensorFlow on a Raspberry Pi Zero?

The answer? You can, but it’s a bad idea.

The Raspberry Pi Zero is a single core ARMv6, with no NEON. Which means it’s slow:

SystemPackageInstall TimeTest Time
Raspberry Pi Zero WVirtualenv/Pip>1 hour40 seconds
Raspberry Pi 3 Model B+Virtualenv/Pip10 minutes10 seconds
AMD ThreadRipper 1950X (KVM VM, 8 cores)Docker image2 minutes1.8 seconds

Details

Below are the steps I took to install TensorFlow on a Raspberry Pi Zero W. Note: you have to use Virtualenv to install TensorFlow in Raspbian. If you try to install TensorFlow directly with Pip, the installation will bomb out with an error.

Raspberry Pi Installation Steps:

# install system pip, numpy dependencies, and virtualenv
sudo apt-get install python3-pip python3-dev libatlas-base-dev virtualenv

# at this point i tried to install tensorflow directly via pip, which does NOT work
# sudo pip3 install --upgrade tensorflow

# created virtualenv environment instead
virtualenv --system-site-packages -p python3 ./venv

# activate virtual environment "venv"
# note: after this command your shell prompt will be prefixed with "(venv) "
source ./venv/bin/activate

# install tensorflow (i also installed keras here, because I use it for other stuff)
# note: this step takes a comically long time (>1 hour)
pip install tensorflow keras

Test Results (Raspberry Pi Zero W):

(venv) pabs@zero:~> time python -c "import tensorflow as tf; 
  tf.enable_eager_execution();
  print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
...
tf.Tensor(1533.9042, shape=(), dtype=float32)

real  0m40.802s
user  0m38.283s
sys  0m1.150s

Test Results (Raspberry Pi 3 Model B+):

(venv) pabs@peach:~> time python -c "import tensorflow as tf;
  tf.enable_eager_execution();
  print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
...
tf.Tensor(800.62, shape=(), dtype=float32)

real  0m9.408s
user  0m9.227s
sys  0m0.360s

Test Results (AMD ThreadRipper 1950X, 8 core KVM VM, Docker image):

pabs@hive:~> time docker run --rm -it tensorflow/tensorflow:latest-py3 python3 -c \
	"import tensorflow as tf; tf.enable_eager_execution();
  print(tf.reduce_sum(tf.random_normal([1000, 1000])))" 
...
tf.Tensor(-173.73222, shape=(), dtype=float32)

real  0m1.745s
user  0m0.032s
sys  0m0.016s