New Tensorflow releases for Raspberry Pi

Last month, the Tensorflow team at Google announced official support for Raspberry Pi, by releasing pre-build binaries of v1.9.0 to piwheels.org. Since then, two new releases (v1.10.1 and v1.11.0) have been made and they are now available on piwheels.org.

To install the latest Tensorflow on Raspbian Stretch, first install libatlas, which is a depencency for the optimised version of numpy on piwheels, the  simply use pip to install tensorflow:

sudo apt install libatlas3-base
sudo pip3 install tensorflow

If you’re using Tensorflow in your Pi project, be sure to let us know by tweeting at @piwheels.

How to work out the missing dependencies for a Python package

When you install a compiled Python wheel, whether it’s from PyPI or piwheels, it will likely depend on some shared libraries, specifically certain .so files (shared object files) in order to be used.

If you’ve ever been in the situation where you’ve installed a library but importing it fails, it’s a pretty unpleasant experience:

The next step for most people is to Google the error message. But that’s a long and slow process that often doesn’t lead to a successful import.

The best way to resolve this issue requires a couple of command line tools: ldd and apt-file.

First, navigate to the location of the package installation. This is usually /usr/local/lib/python3.5/dist-packages/<package>/. Note the package directory will be named after the import line, which may differ from the name of the package as you installed it. For example, you pip install numpy and import numpy but you pip install opencv-python and import cv2.

Run ls in that directory and look for an .so file:

pi@raspberrypi:/usr/local/lib/python3.5/dist-packages/cv2 $ ls
cv2.cpython-35m-arm-linux-gnueabihf.so data __init__.py __pycache__

Run ldd on that file:

ldd cv2.cpython-35m-arm-linux-gnueabihf.so

(tab completion is your friend)

You’ll see a lot of .so files. Those are shared objects the Python library source code refers to. Some of them will be available on your system, and will show the location they can be found:

libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0x74fdb000)

Others will show “not found”. These are the ones you need to make available:

libhdf5_serial.so.100 => not found

You can use grep to filter out the found ones:

ldd cv2.cpython-35m-arm-linux-gnueabihf.so | grep "not found"

To find out which apt packages provide this .so, use apt-file. apt-file isn’t installed by default, so install it with apt:

sudo apt install apt-file

You’ll want to update apt-file‘s cache:

sudo apt-file update

Then use apt-file search on the missing .so file:

apt-file search libhdf5_serial.so.100

This will show a list of apt packages (including some duplicates):

libhdf5-100: /usr/lib/arm-linux-gnueabihf/libhdf5_serial.so.100
libhdf5-100: /usr/lib/arm-linux-gnueabihf/libhdf5_serial.so.100.0.1

This indicates that the package libhdf5 will provide the required .so file opencv refers to. So install it:

sudo apt install libhdf5-100

That’s it! Just rinse and repeat. Once you’ve made all the missing shared object files available, you’ll be able to import the module no problem:

Note that some shared objects can be provided by multiple packages. Sometimes it’s obvious which is the lighter option, i.e. libatlas3-base rather than libatlas-base-dev (avoid -dev packages if possible), or libpango-1.0-0 rather than the humongous wolfram-engine. You can see the file size with apt-cache show:

pi@raspberrypi:~ $ apt-file search libpango-1.0.so.0
libpango-1.0-0: /usr/lib/arm-linux-gnueabihf/libpango-1.0.so.0
libpango-1.0-0: /usr/lib/arm-linux-gnueabihf/libpango-1.0.so.0.4000.5
wolfram-engine: /opt/Wolfram/WolframEngine/11.3/SystemFiles/Libraries/Linux-ARM/libpango-1.0.so.0
pi@raspberrypi:~ $ apt show libpango-1.0 | grep Size

Installed-Size: 515 kB
Download-Size: 305 kB
pi@raspberrypi:~ $ apt show wolfram-engine | grep Size

Installed-Size: 829 MB
Download-Size: 306 MB

As you can see, the choice here is an easy one: 300 kB vs 300MB!

We’re planning to add project pages to piwheels.org, which will feature library dependencies so you don’t have to look them up manually. If you were looking for opencv requirements, see our blog post New opencv builds which includes the lists of dependencies for the opencv packages.

New opencv builds

The opencv maintainers don’t release source distribution for the packages, so Dave has been building Raspberry Pi wheels manually from source on GitHub. They have also chosen to split releases into four separate pacakges:

opencv-contrib includes all of opencv, plus additional modules (listed in the opencv docs). The -headless releases exclude any GUI functionality, which mean they require fewer dependencies, and are ideal if you’re using Raspbian Lite or simply not using any GUI features.

We now have Raspberry Pi wheels for versions 3.4.2.16, 3.4.2.17 and 3.4.3.18 of all four package variations on piwheels.org.

Dependencies

When you pip install an opencv package, you’ll need various apt packages installed to provide make it work. We’re planning to document this on piwheels.org for each package in future but for now, here are the full installation instructions for opencv:

opencv-python

sudo apt install libatlas3-base libwebp6 libtiff5 libjasper1 libilmbase12 libopenexr22 libilmbase12 libgstreamer1.0-0 libavcodec57 libavformat57 libavutil55 libswscale4 libqtgui4 libqt4-test libqtcore4
sudo pip3 install opencv-python

opencv-python-headless

sudo apt install libatlas3-base libwebp6 libtiff5 libjasper1 libilmbase12 libopenexr22 libilmbase12 libgstreamer1.0-0 libavcodec57 libavformat57 libavutil55 libswscale4 libgtk-3-0 libpangocairo-1.0-0 libpango-1.0-0 libatk1.0-0 libcairo-gobject2 libcairo2 libgdk-pixbuf2.0-0
sudo pip3 install opencv-python-headless

opencv-contrib-python

sudo apt install libatlas3-base libsz2 libharfbuzz0b libtiff5 libjasper1 libilmbase12 libopenexr22 libilmbase12 libgstreamer1.0-0 libavcodec57 libavformat57 libavutil55 libswscale4 libqtgui4 libqt4-test libqtcore4
sudo pip3 install opencv-contrib-python libwebp6

opencv-contrib-python-headless

sudo apt install libatlas3-base libhdf5-100 libharfbuzz0b libwebp6 libtiff5 libjasper1 libilmbase12 libopenexr22 libgstreamer1.0-0 libavcodec57 libavformat57 libavutil55 libswscale4 libgtk-3-0 libpangocairo-1.0-0 libpango-1.0-0 libatk1.0-0 libcairo-gobject2 libcairo2 libgdk-pixbuf2.0-0
sudo pip3 install opencv-contrib-python-headless

 

Official Tensorflow releases

We’re pleased to announce the immediate availability of Tensorflow 1.9 for Raspberry Pi, as officially supported by the Tensorflow team at Google.

Install Tensorflow now on your Raspberry Pi:

sudo apt install libatlas3-base
sudo pip3 install tensorflow

This news was originally announced by Pete Warden, software engineer at Google:

Tensorflow is available for Python 3.7, 3.4 and 3.5 and works on any Raspberry Pi model. Read more in Pete’s Medium post and see the official documentation.

We’re really excited to see what people do with Tensorflow on the Raspberry Pi now that an easy installation is available.

Unlike all other packages on piwheels at present, Tensorflow was not built by the piwheels team. It was built by Google and submitted directly to us for inclusion.

Happy Tensorflowing!