TensorFlow and Go on Raspberry Pi
This is a guide which I ran through for building
libtensorflow on Raspberry Pi.
0. Used Hardwares and Softwares
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 (Stretch) / gcc 6.3.0
- Tensorflow 1.3.0
- Protobuf 3.1.0
- Bazel 0.5.1
Before the beginning, I had to install dependencies:
1. Install Protobuf
I cloned the protobuf’s repository:
and started building:
It took less than an hour to finish.
I could see the version of installed protobuf with:
2. Install Bazel
I got a zip file of bazel from here and unzipped it:
b. edit bootstrap files
In the unzipped directory, I opened the
searched for lines that looked like following:
-J-Xmx500M to the last line so that the whole lines would look like:
It was for enlarging the max heap size of Java.
After that, started building with:
It also took about an hour.
After the compilation had finished, I could find the compiled binary in
Copied it into
3. Build libtensorflow.so
(I referenced this document for following processes)
Got the tensorflow go code with:
b. edit files
In the downloaded directory, I checked out the latest tag and replaced
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 out in
If it is not commented out, bazel will build for mobile platforms like
Android, not Raspberry Pi.
To do this easily, just run:
Finally, it was time to configure and build tensorflow.
c. configure and build
I had to answer to some questions here.
Then I started building
My Pi became unresponsive many times during this process, 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
So I copied it into
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
I wanted to see a simple go program running, so I wrote this code:
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
So I had to work around this problem again by editing
and starting again from the beginning:
Then I could build it without further problems.
I hope it would be fixed on future releases.
Installing TensorFlow on Raspberry Pi is not easy yet. (There’s a kind project which makes it super easy though!)
libtensorflow.so is a lot more difficult, because it takes too much time to build it.
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,
You don’t have time to build it yourself, but still 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.