TensorFlow and Go on Raspberry Pi (Outdated)
Note: This post is outdated. If you’re looking for a guide for Raspbian Stretch or Tensorflow 1.3+, check this post.
Updated on 2017-06-19, for Tensorflow 1.2.0
TensorFlow 1.0 now supports Golang, so I gave it a try on Raspberry Pi:
0. Used Hardware and Software Versions
All steps were taken on my Raspberry Pi 3 B model with:
- Minimum GPU memory allocated (16MB)
- 1GB of swap memory
- External USB HDD (as root partition)
and software versions were:
- raspbian (jessie)
- tensorflow 1.2.0
- protobuf 3.1.0
- bazel 0.5.1
Before the beginning, I had to install dependencies:
for python
for protobuf
for bazel
for compiler optimization and avoiding possible errors
It is said that both protobuf and tensorflow should be built with gcc-4.8, so… :
then gcc-4.8 will be used instead.
It can be changed back later with:
1. Install Protobuf
I cloned the repository:
and built it:
The build took less than 1 hour to finish.
I could see the version of installed protobuf like:
2. Install Bazel
a. download
I got the latest release from here, and unzipped it:
b. edit bootstrap files
In the unzipped directory, I opened the scripts/bootstrap/compile.sh
file:
searched for lines that looked like following:
and appended -J-Xmx500M
to the last line so that the whole lines would look like:
It was for enlarging the max heap size of Java.
c. compile
After that, started building with:
This compilation took about an hour.
d. install
After the compilation had finished, I could find the compiled binary in output
directory.
Copied it into /usr/local/bin
directory:
3. Build libtensorflow.so
(I referenced this document for following processes)
a. download
Got the tensorflow go code with:
b. edit files
In the downloaded directory, I checked out the latest tag and replaced lib64
to lib
in the files with:
Raspberry Pi still runs on 32bit OS, so they had to be changed like this.
After that, I commented #define IS_MOBILE_PLATFORM
in tensorflow/core/platform/platform.h
:
If it is not commented out, bazel will build for mobile platforms like iOS
or Android
, not Raspberry Pi.
To do this easily, just run:
Finally, it was time to build tensorflow.
c. build and install
Started building libtensorflow.so
with:
I could tweak the –local_resources option as this bazel manual,
but if set too agressively, bazel could freeze or even crash with error messages like:
If this happens, just restart the build. It will resume from the point where it crashed.
My Pi became unresponsive many times, but I kept it going on.
…
After a long time of struggle, (it took nearly 7 hours for me!)
I finally got libtensorflow.so
compiled in bazel-bin/tensorflow/
.
So I copied it into /usr/local/lib/
:
All done. Time to test!
4. Go Test
I ran a test for validating the installation:
then I could see:
Ok, it works!
Edit: As this instruction says, I had to regenerate operations before the test:
5. Further Test
Wanted to see a simple go program running, so I wrote:
and ran it with go run sample.go
:
See the result?
From now on, I can write tensorflow applications in go, on Raspberry Pi! :-)
98. Trouble shooting
Build failure due to a problem with Eigen
With Tensorflow 1.2.0, I encountered this issue while building.
To work around this problem, I edited tensorflow/workspace.bzl
from:
to:
then I could build it without further problems.
I hope it would be fixed on upcoming releases.
99. Wrap-up
Installing TensorFlow on Raspberry Pi is not easy yet. (There’s a kind project which makes it super easy though!)
Building libtensorflow.so is a lot more difficult, because it takes too much time.
But it is worth trying; managing TensorFlow graphs in golang will be handy for people who don’t love python - just like me.
999. If you need one,
Do you need the compiled file? Good, take it here.
I cannot promise, but will try keeping it up-to-date whenever a newer version of tensorflow comes out.