tensorflow / tensorflow

An Open Source Machine Learning Framework for Everyone

Home Page:https://tensorflow.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tensorflow on Raspberry Pi

boje opened this issue · comments

Hi,
Is it possible to install Tensorflow on a Raspberry Pi? OS Ubuntu Mate, Python2.7

We have not tried -- if you (or someone else) gives it a shot, let us know!

Quick notes from a quick look...

  1. According to https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md a source build requires Bazel. There is currently a live thread on this in bazelbuild/bazel#606 /cc @vmayoral
  2. re other dependencies - os_setup.md mentions 'python-numpy swig python-dev'; of these, the scariest looking is numpy but http://raspberrypi.stackexchange.com/questions/8308/how-to-install-last-scipy-version-on-raspberry-pi and http://www.pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/ seems optimistic...

Anyone get any further?

Thanks @danbri for summarizing it. I'm currently trying different things but there seems to be serious issues with bazel and ARM.

It's great seeing several developers interested on the Pi and although the hardware has constrained resources, the interest seems relevant. Hope Google reacts here.

Nearby w.r.t. bazel, #326

bazelbuild/bazel#606 is looking more promising than last week...

Some progress at #445.

On the Bazel build part, http://cudamusing.blogspot.com.au/2015/11/building-tensorflow-for-jetson-tk1.html has instructions for an Arm32 build that may work on the Pi.

Would love to see this too. Is it possible to just run TensorFlow compiled things on Raspberry Pi 2?

Hi @nlothian, I follow this Jetson build step, use the whl file to install on RPi, but Tensorflow will include CUDA lib so file. So, it is not work.

commented

👍

@philipz I meant those instructions maybe helpful for getting Bazel building on the Pi as a necessary step towards getting TensorFlow working. If you got past that point it maybe worth looking at #445 where the specific problems around that were being tracked.

You shouldn't be linking against CUDA for a Pi build!

Just to spread some work that's been done in #445, we've managed to get TensorFlow compiled and running on the Raspberry Pi running Raspbian (although there hasn't been extensive testing yet). I've posted an unofficial pre-built Python wheel as well as step-by-step instructions on building from source yourself here.

commented

Thanks! I can confirm that it works with Ubuntu MATE on a Pi 3.

I'm also successfully running @samjabrahams 's build on a Pi 3 (Linux raspberrypi 4.1.19-v7+ #858 SMP Tue Mar 15 15:56:00 GMT 2016 armv7l GNU/Linux).

https://twitter.com/danbri/status/709889859029213184

Since #445 is now closed, I'm going to post progress and updates for running TensorFlow on Raspberry Pi here. I did some very basic benchmarking on the Inception-v3 model to explore whether the build is working properly. Interestingly, the Pi appears to have similar performance to a MacBook Pro when it comes to using a compiled C++ model, but much worse when using the Python version (granted, the C++ version also runs slower on the Mac, which raises a separate set of questions).

I haven't gotten a good look at how Python binds to C++ in the code, but I'll do my best to figure out what's going on. My hope is that there is something screwy on the Pi that can be fixed which would lead to an order-of-magnitude time increase. I don't have much experience with C++ and its compile settings, so any ideas would be much appreciated!

@petewarden - you mentioned here that you thought some slowdown could be due to the compiler not using NEON options. Do you think that's worth exploring here?

@danbri - Could you send over the pixel dimensions of the pictures captured and used by your Raspberry Pi camera? I want to try testing images around that size to see if I end up with similar run-times

All I had time to try so far was default output of raspiutil via the Python wrapper code. I started trying to build the C++ but didn't finish yet. For realtime camera access (staying within python) I believe http://picamera.readthedocs.org/en/latest/api_camera.html would be the way to go. The camera needs some warmup time to judge light balance, I read. And I didn't break down the python timings yet either into setup vs inference. Anyway:

tensorflow/tensorflow/models/image/imagenet $

time raspistill --timeout 3 --nopreview -o ~/Desktop/headless.jpg

real 0m0.765s
user 0m0.010s
sys 0m0.030s

time python classify_image.py --model_dir $HOME/imagenet/ --image_file ~/Desktop/headless.jpg --num_top_predictions 5 2>/dev/null
hourglass (score = 0.47746)
lampshade, lamp shade (score = 0.19448)
table lamp (score = 0.06217)
four-poster (score = 0.01556)
wall clock (score = 0.00718)

real 0m33.291s
user 0m53.120s
sys 0m2.420s

file ~/Desktop/headless.jpg
/home/pi/Desktop/headless.jpg: JPEG image data, Exif standard: [TIFF image data, big-endian, direntries=10, height=0, manufacturer=RaspberryPi, model=RP_OV5647, xresolution=156, yresolution=164, resolutionunit=2, datetime=2016:03:18 11:22:43, width=0], baseline, precision 8, 2592x1944, frames 3

FWIW here's the picture http://imgur.com/UbsYZYC (upside down and truncated salt/pepperpots, so 'hourglass' was close ;)

I'm also curious about the Python wrapper being slower than plain C++ since I thought Python offloaded all TF's heavy lifting to compiled code anyway. Is it possible it's running a different build of the TF core in one case?

Thanks @danbri- I'm definitely going to take a closer look at the Python and C++ Inception code to see if there are any obvious differences in implementation between the two. I ran your image through my RPi directly in Raspbian and got the same results as I did with smaller images. My gut tells me that the Pi can do better that the performance we're getting, but I'm not sure if I should be looking at compiler options first or dive into the code first.

I also checked out memory usage to cover more bases- doesn't appear to be an issue.

@samjabrahams ... sorry I've not had time to try your new binaries yet. Have you had any more insights into the speed issue?

@danbri - Not much yet: I've been low on time myself as well. My current goal is to get Bazel 2 running on a RPi, then put out new binaries for the 0.8 release once the candidate has gone gold. Afterward, hopefully I'll have some time to dig deeper into which operations are causing headaches on the Pi.

Hi,
I am trying to install tensorflow on my RPi 2 with ubuntu 14.04 image.
I installed @samjabrahams package (Thanks for the effort btw). I encountered no error.
However when I'm importing tensorflow in python I run to this error:

File "/usr/local/lib/python2.7/dist-packages/tensorflow/init.py", line 23, in
from tensorflow.python import *
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/init.py", line 45, in
from tensorflow.python import pywrap_tensorflow
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in
_pywrap_tensorflow = swig_import_helper()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
ImportError: /usr/lib/arm-linux-gnueabihf/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /usr/local/lib/python2.7/dist-packages/tensorflow/python/_pywrap_tensorflow.so

I do have "libstdc++.so.6" and "libstdc++.so.6.0.19" in /usr/lib/arm-linux-gnueabihf/ and I have gcc4.8 installed.
Have you tried this package on RPi 2?

Any advice is appreciated.
Thanks

thanks. I tried it but didn't help. Same error.
I did this.
export LD_LIBRARY_PATH=/usr/lib/gcc/arm-linux-gnueabihf/4.8/:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/lib/arm-linux-gnueabihf/:$LD_LIBRARY_PATH
sudo ldconfig
First two lines add the path to where an instance of libstdc++.so.6 exist.

I can confirm that @samjabrahams instructions for the RPi install also works for an Odroid Xu4 running Ubuntu 14.04, and while it did require building from source, it only took an hour or two to do so.

@danbri - I ran that test case on the Odroid and my laptop, there was no noticeable difference in evaluation time, definitely same order of magnitude. Looking at resource draw, it takes 4 cores at peak and ~500 MB of RAM when evaluating that imagenet model.

I'm building custom classifiers one basic perceptron type model and a CNN and I can say the odroid runs 5x slower with the perceptron case (which is still plenty fast actually) and the CNN actually runs at about the same rate on both machines (really slow). When the CNN does run on the Odroid it eats between 4 and 7 of the 8 available cores and consumes ~1.9 GB of the 2 GB of RAM. I still have the swap partition turned on from the install and from what I've seen that's probably a good thing to have when running big CNNs like that. All in all pleasantly surprised by the performance so far.

@austinsteamboat Thanks for the info! I just finished putting the 0.8.0 pip release out for the Raspberry Pi, so I'm hoping to have a few spare minutes over the next few weeks to check out some of the speed issues. From your info, it sounds like the problem could be at the operating system level, as Raspbian is still a 32-bit OS. The Odroid is certainly a more powerful machine than the Pi, but if the ImageNet model only takes 4 cores on both, I don't see the Pi lagging behind that badly.

Hopefully I (or someone) can find something a bit more conclusive and precise for this.

I confirm that with @samjabrahams whl I can get the "panda" and "uniform" result in 15s(use python example. not sure the cpp example). Thanks a lot! building tensorflow and bazel on raspberry is not a easy job......I tried but blocked at the javac report out of memory(I already change the swap file to 2GB but I found the javac even does not consider any swap space).

  1. is it possible to put the bazel whl on github also? we need to build our own code with tensorflow.
  2. is it possible to improve the performance to such as 1s with GPU? I'm trying to let it run for a robot with ROS.if one second it can report the object it see, then it can be fun. Too long time is not fun :-)

@NickQian Glad the TensorFlow binaries are working for you. Couple quick responses:

  1. At this time, I'm unable to distribute the Bazel binaries directly, mainly because I don't know the exact files that need to be packaged up (I believe there's more to it than just a single binary file). If someone could help me out with this, I'd be able to do so- I just don't have much (if any) time to dedicate to this at the moment.
    • To follow up: are you following the instructions I wrote exactly? Instead of modifying the swap file /etc/dphys-swapfile, the instructions call for mounting an additional USB drive. Theoretically, swap space should be swap space, but I imagine putting more bottlenecks on the SD card isn't helpful
    • Did you edit the file scripts/bootstrap/compile.sh inside of the Bazel repository? The javac command used by Bazel is in that script, and you may need to specify the maximum heap size there (as instructed in the guide)
  2. The question of speed is where the conversation is right now. For the base Inception model, my fear is that we're going to see slow performance until a 64-bit operating system is available on RPi. However, now that 8-bit quantization is possible, we may be able to prep some models to run more effectively on the Pi :)

Sam, thanks for your great work. Have you compiled & shared binaries for (raspberry pi)Tensorflow for Python3?

@indiangolfer It's on my to-do list! I've streamlined my compilation process a touch, so I'll try to get the Python 3 binaries out before the end of the weekend.

That would be great. Thanks

@indiangolfer Let me know if this binary works for you! I just updated the readme and guide to include Python 3 installation instructions as well.

Hi Sam. Perfect. It worked perfectly on both Pi 2 and 3. Many thanks.

Hi @samjabrahams. Thanks for your GREAT job. I use your binary file to make a Docker image for RPi. https://github.com/philipz/rpi-tensorflow

Great work @samjabrahams.
Hi @indiangolfer, could you please tell how did you install binary on RPi2B?

I follow below steps on RPi2B with Raspbian Wheezy:
For Python 2.7

sudo apt-get install python-pip python-dev

wget https://github.com/samjabrahams/tensorflow-on-raspberry-pi/raw/master/bin/tensorflow-0.8.0-cp27-none-linux_armv7l.whl

sudo pip2 install tensorflow-0.8.0-cp27-none-linux_armv7l.whl

tensorflow installed successfully

Installing collected packages: tensorflow
Successfully installed tensorflow-0.8.0

When I test it

python
>>> import tensorflow as tf

Got following error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 23, in <module>
    from tensorflow.python import *
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 45, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
ImportError: /usr/lib/arm-linux-gnueabihf/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /usr/local/lib/python2.7/dist-packages/tensorflow/python/_pywrap_tensorflow.so)

Well Tensorflow binaries works on RPi2B + Rasbian Jessie. Thank you @samjabrahams.

A couple recent changes are throwing a wrench into compiling full TensorFlow binaries on RPi. These lines cause IS_MOBILE_PLATFORM to be defined when compiling RPi, which in turn causes errors in some of the image processing files (such as core/platform/png.h and core/platform/gif.h). It also prevents non-32-bit operations from being defined in this file. This may not be a bad thing, considering Raspbian is still a 32-bit OS, but it causes quirks when people try to use certain 64-bit-only Operations.

I imagine that the current setup works when using the Makefile, so I don't want to screw anything up. That said, any ideas on not defining IS_MOBILE_PLATFORM when compiling on Raspberry Pi? The best thing I could find was this answer on Stack Overflow- it seems a little clunky, so I didn't want to jump to making a PR with it without asking first.

In the meantime, the current fix is to delete this line before doing the Bazel build on the Pi. If there's no clean way to fix this up here, it's no big deal to tweak that file for builds moving forward.

@samjabrahams perhaps this situation merits a new dedicated bug report, as it is a very specific matter?

@danbri You're right- I've opened up an issue for this #3469

HI, would you like to make version10 of tensorflow on Pi? Because i want to try object recognition.Thanks.

Hi @puchiko939393, I'm planning on building wheels for TensorFlow 0.10 as soon as it's officially released. There is only a release candidate at this time (0.10RC0), so I'm holding off until then. Feel free to use the guide to build it yourself if you simply can't wait!

Looks like https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/makefile wasn't shared here. Non-bazel, makefile-based compilation; there's a "Raspberry Pi" section in the readme, plus https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/pi_examples from @petewarden and @vrv has some nice examples. Main drawback seems to be lack of language bindings (esp. python). (and no GPU, but that's pretty much a given for Pi anyway)

edit:

  • #4680 helped me get the examples running

Hi,

Can I install TensorFlow on the Pi zero.

I tried "sudo pip2 install tensorflow-0.9.0-cp27-none-linux_armv7l.whl"

but it returned
tensorflow-0.9.0-cp27-none-linux_armv7l.whl is not a supported wheel on this platform.
Storing debug log for failure in /root/.pip/pip.log

I haven't try from Docker image yet. is it possible ?

thank you and regards,
Khoa

Time for a benchmark update- I posted the latest Inception-v3 benchmarks for version 0.11.0 here, and I'll update them again once 0.12.0 is official.

Highlights:

  • Inception-v3 average run time is now under 2 seconds (after a few warmup runs)
  • Benchmark script is slightly fancier, giving best/worst/average times as well as flags to modify the number of samples and warmup runs
  • It seems the majority of the time doing the classify_image.py script is spent importing tensorflow and building the graph from disk

I'm still compiling the Python 3 binaries for 0.11.0, and I need to update the instructions to make sure they work on a fresh copy of Raspbian. Hopefully get all that done by the end of the weekend.

commented

Not strictly related but new optimized cpu and gpu ARM kernels could be available. See ARM-software/ComputeLibrary#7

Dear samjabrahams
Thank you for all your effort.
Does the Raspberry Pi binaries apply to clone (particularily Orange Pi+ 2e - AllWinner H3 CPU + Mali 400 GPU)?
I'm trying to build according to your instruction but I'm stuck with compiling Bazel (java zero fails with Internal Error (os_linux_zero.cpp:254) unhandled signal 11 using OpenJDK Zero VM (25.121-b13) build 1.8.0_121-8u121-b130ubuntu1.16.04.2-b13). The OS is armbian.
In case above binaries do not apply, do you know if switching back to Java 1.7 may help?

Kind regards,

P.

@pczekalski - I'm moving your question to my repo in issue 90, as this thread probably isn't the right place to discuss non-RPi TensorFlow.

Closing since Raspberry Pi binaries exist.

@samjabrahams what are your plans w.r.t. the pi binaries? is the build process any easier than last year?

@danbri - The process is a little smoother than it was at the end of last year, but it's still pretty much the same (in terms of needing to compile on-device). We don't need to compile protoc from scratch anymore, and there's less finagling overall- there's basically only five changes you need to make to Bazel and TensorFlow, total.

I haven't had the time to put much effort into trying out cross-compilation options, unfortunately. In addition, I'm starting a new job which means that I'll need to talk to HR about continuing my efforts (I'm optimistic, but I don't make any assumptions).

On a brighter note, I'm excited to try out these MobileNets on-device!

@danbri @samjabrahams

This is, reply to @samjabrahams comment on Mar 17, 2016. Have you guys tested if the python and c++ versions have similar performance. When I ran object detection model using python, I can clearly see only one core being used and it takes 6 seconds to process 1 image(640x480 res). I tried configuring the intra_ops config parameter, but no luck. I think by default tensorflow uses all cpu cores. I am not sure if I am the only one facing performance issues..

@danbri . thanks for the reply. I hope they fix it in official release..

download the bazel binary for armv7l(pi 3) with this link below
https://drive.google.com/open?id=0B7NeIJaVzSxIZmZHUnVzZjBjVDg

whatt about Mali 400 and more and tensorflow