Home

Awesome

tensorflow_ros_cpp

Build Status

A Catkin-friendly package for utilizing the C++ API of Tensorflow.

Get Tensorflow C++ API into ROS as easy as

find_package(catkin REQUIRED COMPONENTS
  ... your other packages ...
  tensorflow_ros_cpp
)

See the usage example at [https://github.com/tradr-project/tensorflow_ros_test].

New: Support for Noetic and TensorFlow 2.x!

Supported Tensorflow installations

You can choose either one of the following options to install Tensorflow.

See below for more detail about each of the installation types and how to set them up.

Note for rosdep users

If you're managing dependencies via rosdep, it is likely that you do not want it to try to install the optional dependencies (currently python-tensorflow-pip and tensorflow_catkin). In such case, add the following to the rosdep call:

rosdep install ... --skip-keys=tensorflow_catkin --skip-keys=python-tensorflow-pip

Tested compatible versions

If you successfully used this package on an untested configuration (marked with ?), please, tell us.

Ubuntu 14.04 64bits, Python 2.7.6, ROS Indigo

TFCUDACUDNNpip tensorflowpip tensorflow-gpubazel (CPU)bazel (GPU)tensorflow_catkin (CPU)tensorflow_catkin (GPU)
0.12.1nono<span style="color:green"></span><span style="color:green"></span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.0.0nono<span style="color:green"></span><span style="color:green"></span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.1.085<span style="color:green"></span><span style="color:green"></span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.2.085<span style="color:green"></span><span style="color:green"></span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.3.086<span style="color:green"></span><span style="color:green"></span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.4.086<span style="color:green"></span><span style="color:red">N/A (wants CUDA 9)</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.5.086<span style="color:green"></span><span style="color:red">N/A (wants CUDA 9)</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.6.086<span style="color:green"></span><span style="color:red">N/A (wants CUDA 9)</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.7.086<span style="color:green"></span><span style="color:red">N/A (wants CUDA 9)</span><span style="color:green"></span><span style="color:green"></span><span style="color:green"></span><span style="color:green"></span>
1.8.086<span style="color:green"></span><span style="color:red">N/A (wants CUDA 9)</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.14.086<span style="color:green"></span><span style="color:red">N/A (wants CUDA 10)</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.15.086<span style="color:red">X (link error)</span><span style="color:red">N/A (wants CUDA 10)</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>

Ubuntu 14.04 64bits, Python 3.4, ROS Indigo

Had to set TF_PYTHON_LIBRARY manually since CMake was only finding Python 2.7 libraries.

TFCUDApip3 tensorflowpip3 tensorflow-gpu
1.8.08<span style="color:green"></span><span style="color:red">N/A (wants CUDA 9)

Ubuntu 16.04 64bits, Python 2.7.6, ROS Kinetic

TFCUDACUDNNpip tensorflowpip tensorflow-gpubazel (CPU)bazel (GPU)tensorflow_catkin (CPU)tensorflow_catkin (GPU)
0.12.18.0no<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.0.08.05.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.1.08.05.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.2.08.05.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.3.08.06.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.4.08.06.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.5.09.07.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.6.09.07.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.7.09.07.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span><span style="color:green"></span><span style="color:green"></span><span style="color:green"></span><span style="color:green"></span>
1.8.09.07.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span><span style="color:green"></span><span style="color:green"></span><span style="color:red">N/A</span><span style="color:red">N/A</span>
1.14.09.07.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:red">N/A (wants CUDA 10)</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.15.09.07.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:red">N/A (wants CUDA 10)</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>

Ubuntu 16.04 64bits, Python 3.5, ROS Kinetic

Had to set TF_PYTHON_LIBRARY manually since CMake was only finding Python 2.7 libraries.

TFCUDACUDNNpip3 tensorflowpip3 tensorflow-gpu
1.8.09.07.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>

Ubuntu 18.04 64bits, Python 2.7.6, ROS Melodic

TFCUDACUDNNpip tensorflowpip tensorflow-gpubazel (CPU)bazel (GPU)tensorflow_catkin (CPU)tensorflow_catkin (GPU)
1.0.08.05.0<span style="color:orange">✓, see ABI difference problems</span>???<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.1.08.05.0<span style="color:orange">✓, see ABI difference problems</span>???<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.2.08.05.0<span style="color:orange">✓, see ABI difference problems</span>???<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.3.08.06.0<span style="color:orange">✓, see ABI difference problems</span>???<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.4.08.06.0<span style="color:orange">✓, see ABI difference problems</span>???<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.5.09.07.1<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.6.09.07.1<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:red">N/A</span><span style="color:red">N/A</span>
1.7.09.07.1<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>??<span style="color:green"></span><span style="color:green"></span>
1.7.09.17.1<span style="color:red">N/A</span><span style="color:red">N/A</span>??<span style="color:green"></span><span style="color:green"></span>
1.8.09.07.1<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span><span style="color:green"></span><span style="color:green"></span><span style="color:red">N/A</span><span style="color:red">N/A</span>
1.8.09.17.1<span style="color:red">N/A</span><span style="color:red">N/A</span><span style="color:green"></span><span style="color:green"></span><span style="color:red">N/A</span><span style="color:red">N/A</span>
1.14.010.07.4<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span><span style="color:green"></span>???
1.14.010.07.4<span style="color:orange">✓, see ABI difference problems</span>?????

Ubuntu 18.04 64bits, Python 3.6, ROS Melodic

Had to set TF_PYTHON_LIBRARY manually since CMake was only finding Python 2.7 libraries.

TFCUDACUDNNpip3 tensorflowpip3 tensorflow-gpu
1.8.09.07.0<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>
1.14.010.07.4<span style="color:orange">✓, see ABI difference problems</span><span style="color:orange">✓, see ABI difference problems</span>

Ubuntu 20.04 64bits, Python 3.8, ROS Noetic

TF | CUDA | CUDNN | pip3 tensorflow --- | --- | --- | --- | --- 2.9.1 | no | no | <span style="color:green"></span> | <span style="color:green"></span>

Debian Jessie 64bits, Python 2.7.6, ROS Indigo

TFCUDACUDNNpip tensorflowpip tensorflow-gpubazel (CPU)bazel (GPU)tensorflow_catkin (CPU)tensorflow_catkin (GPU)
1.3.08.06<span style="color:green"></span><span style="color:green"></span>????

Debian Stretch 64bits, Python 2.7.6, ROS Indigo (compiled from source)

TFCUDACUDNNpip tensorflowpip tensorflow-gpubazel (CPU)bazel (GPU)tensorflow_catkin (CPU)tensorflow_catkin (GPU)
1.4.08.06<span style="color:orange">C++ ABI problems</span><span style="color:orange">C++ ABI problems</span><span style="color:green"></span>???

Exported CMake variables

Except for the standard catkin variables (tensorflow_ros_cpp_INCLUDE_DIRS, tensorflow_ros_cpp_LIBRARIES, tensorflow_ros_cpp_DEPENDS and tensorflow_ros_cpp_CATKIN_DEPENDS), the following variables can be used from packages that find_package this package:

Configuration-time CMake variables

These variables are read when configuring the package:

Pip installation

Prerequisites

Assumes tensorflow or tensorflow-gpu pip package is installed. Ideally via pip, but custom installs are also supported (if they're either on PYTHON_PACKAGE_PATH or if you manually specify environment variable TF_PIP_PATH).

Install either using rosdep, or by calling

sudo pip install tensorflow
# or
pip install --user tensorflow
# or in a virtualenv
pip install tensorflow

Note

This package uses a "hack" to link against the library that's installed by pip. I have no idea if this is currently an officially supported way of accessing the C++ API, but it seems to work.

CMake variables:

You can change these variables either in the CMake cache file, or from commandline passing e.g. -DTF_PIP_PATH=/my/path

C++ ABI difference problems

Ubuntu systems starting with 16.04 (Xenial) are using the new C++11 ABI for all system libraries. Until pip bug 1707002 gets resolved (if ever!), the pip-distributed Tensorflow is built againts an older C++ ABI, which is incompatible with the C++11 ABI. This means linking to the Tensorflow library will fail on such systems and there's no way around it. This only affects TF 1.x, because TF 2.x is always built with C++11 ABI.

The only "workaround" is to wrap all tensorflow code into a library that exposes its functions using C API (so no std::strings, std::vectors or Eigen types) and which does not link to any system library with a C++ API. Such library can then be built separately with -D_GLIBCXX_USE_CXX11_ABI=0. Then you can freely link the rest of your code using ROS, Eigen and so on to this library.

An example of this approach can be found at kinetic-devel branch of tensorflow_ros_test.

A library created this way is "backwards compatible" with older systems, so it can be used everywhere. However, separating all Tensorflow code can be a pain in the ***, so we only suggest it as a last resort option. This is why searching for the pip-installed Tensorflow is disabled by default on systems using the new C++11 ABI (you can force it using -DFORCE_TF_PIP_SEARCH=ON).

tensorflow_catkin installation

Prerequisites

Add package tensorflow_catkin to your Catkin workspace. Read its readme to correctly set the required CMake variables (no setup needed for the CPU version).

Be prepared that the compilation will eat up a lot of RAM and take a long time. On my system with 8 concurrent build jobs, it ate around 20 GBs in the peak. You can add BUILD_COMMAND make -j2 and INSTALL_COMMAND make -j2 install to its CMakeLists.txt/ExternalProject_Add to limit the number of concurrent build jobs, which also decreases memory requierements.

CMake variables

Custom compilation of Tensorflow using Bazel

Prerequisites

Follow the Installing TensorFlow from Sources guide up to "Configure the installation" (including), and build the C++ library with the following command:

bazel build --config=opt --define framework_shared_object=false tensorflow:libtensorflow_cc.so

You don't need to continue with the guide building or installing the pip package (but you might be interested, because a custom-built tensorflow can provide you with higher performance even in Python).

If you encounter memory problems during the build, you can limit the used resources using the --local_resources bazel option.

CMake variables

Possible problems

If you encounter crashes when running the TF-using application, or it errors out with Not found: No session factory registered for the given session options, try the change to tensorflow/BUILD suggested in https://github.com/tradr-project/tensorflow_ros_cpp/issues/9#issuecomment-604692643, clean the bazel workspace, and build it again.

Tensorflow 1.15+ is not compatible with ROS Indigo. You will get linking errors when using the library. Please use TF 1.14 or older on Indigo.