aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2019-04-06-tensorflow-on-a-raspberry-pi-zero-w-is-a-bad-idea.md
blob: a4d22697920410888d5a5230842c2d1e9cee78ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
date: "2019-04-06T10:22:40Z"
title: TensorFlow on a Raspberry Pi Zero is a Bad Idea
---

A recent question from the [Something Awful Forums][saf]: 

    Can I run TensorFlow on a Raspberry Pi Zero?

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

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

{{< table "tensorflow_on_pi" "times" >}}

**Details**

Below are the steps I took to install [TensorFlow][] on a
[Raspberry Pi Zero W][pizerow].  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][pizerow]):

```
(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+][pi3bp]):

```
(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][cpu], 8 core [KVM][] VM, [Docker image][tf-img]):

```
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
```

[pip]: https://pip.pypa.io/en/stable/installing/ "Python package manager"
[virtualenv]: https://virtualenv.pypa.io/en/stable/ "Tool to create isolated Python environments"
[pizerow]: https://www.raspberrypi.org/products/raspberry-pi-zero-w/ "Raspberry Pi Zero W"
[pi3bp]: https://www.raspberrypi.org/products/raspberry-pi-3-model-b-plus/ "Raspberry Pi 3 Model B+"
[cpu]: https://www.amd.com/en/products/cpu/amd-ryzen-threadripper-1950x "AMD Ryzen ThreadRipper 1950x"
[saf]: https://forums.somethingawful.com/ "Something Awful Forums"
[kvm]: https://www.linux-kvm.org/ "Linux Kernel Virtual Machine"
[docker]: https://www.docker.com/ "Docker"
[tensorflow]: https://www.tensorflow.org/ "TensorFlow machine learning framework"
[tf-img]: https://hub.docker.com/r/tensorflow/tensorflow/ "TensorFlow Docker image"
[tf-pip]: https://www.tensorflow.org/install/pip "Install TensorFlow using Virtualenv and Pip"
[simd]: https://en.wikipedia.org/wiki/SIMD "Single Instruction, Multiple Data"
[raspbian]: https://www.raspberrypi.org/downloads/raspbian/ "Raspbian"
[armv6]: https://en.wikipedia.org/wiki/ARM_architecture
[neon]: https://en.wikipedia.org/wiki/ARM_architecture#Advanced_SIMD_(NEON) "ARM SIMD instruction set"