tradr-project / tensorflow_ros_cpp

Catkin-friendly C++ bindings for tensorflow.
BSD 3-Clause "New" or "Revised" License
59 stars 17 forks source link
catkin cpp ros tensorflow

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

TF CUDA CUDNN pip tensorflow pip tensorflow-gpu bazel (CPU) bazel (GPU) tensorflow_catkin (CPU) tensorflow_catkin (GPU)
0.12.1 no no ? ? N/A N/A
1.0.0 no no ? ? N/A N/A
1.1.0 8 5 ? ? N/A N/A
1.2.0 8 5 ? ? N/A N/A
1.3.0 8 6 ? ? N/A N/A
1.4.0 8 6 N/A (wants CUDA 9) ? ? N/A N/A
1.5.0 8 6 N/A (wants CUDA 9) ? ? N/A N/A
1.6.0 8 6 N/A (wants CUDA 9) ? ? N/A N/A
1.7.0 8 6 N/A (wants CUDA 9)
1.8.0 8 6 N/A (wants CUDA 9) ? ? N/A N/A
1.14.0 8 6 N/A (wants CUDA 10) ? ? N/A N/A
1.15.0 8 6 X (link error) N/A (wants CUDA 10) ? ? N/A N/A

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.

TF CUDA pip3 tensorflow pip3 tensorflow-gpu
1.8.0 8 N/A (wants CUDA 9)

Ubuntu 16.04 64bits, Python 2.7.6, ROS Kinetic

TF CUDA CUDNN pip tensorflow pip tensorflow-gpu bazel (CPU) bazel (GPU) tensorflow_catkin (CPU) tensorflow_catkin (GPU)
0.12.1 8.0 no ✓, see ABI difference problems ✓, see ABI difference problems ? ? N/A N/A
1.0.0 8.0 5.0 ✓, see ABI difference problems ✓, see ABI difference problems ? ? N/A N/A
1.1.0 8.0 5.0 ✓, see ABI difference problems ✓, see ABI difference problems ? ? N/A N/A
1.2.0 8.0 5.0 ✓, see ABI difference problems ✓, see ABI difference problems ? ? N/A N/A
1.3.0 8.0 6.0 ✓, see ABI difference problems ✓, see ABI difference problems ? ? N/A N/A
1.4.0 8.0 6.0 ✓, see ABI difference problems ✓, see ABI difference problems ? ? N/A N/A
1.5.0 9.0 7.0 ✓, see ABI difference problems ✓, see ABI difference problems ? ? N/A N/A
1.6.0 9.0 7.0 ✓, see ABI difference problems ✓, see ABI difference problems ? ? N/A N/A
1.7.0 9.0 7.0 ✓, see ABI difference problems ✓, see ABI difference problems
1.8.0 9.0 7.0 ✓, see ABI difference problems ✓, see ABI difference problems N/A N/A
1.14.0 9.0 7.0 ✓, see ABI difference problems N/A (wants CUDA 10) ? ? N/A N/A
1.15.0 9.0 7.0 ✓, see ABI difference problems N/A (wants CUDA 10) ? ? N/A N/A

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.

TF CUDA CUDNN pip3 tensorflow pip3 tensorflow-gpu
1.8.0 9.0 7.0 ✓, see ABI difference problems ✓, see ABI difference problems

Ubuntu 18.04 64bits, Python 2.7.6, ROS Melodic

TF CUDA CUDNN pip tensorflow pip tensorflow-gpu bazel (CPU) bazel (GPU) tensorflow_catkin (CPU) tensorflow_catkin (GPU)
1.0.0 8.0 5.0 ✓, see ABI difference problems ? ? ? N/A N/A
1.1.0 8.0 5.0 ✓, see ABI difference problems ? ? ? N/A N/A
1.2.0 8.0 5.0 ✓, see ABI difference problems ? ? ? N/A N/A
1.3.0 8.0 6.0 ✓, see ABI difference problems ? ? ? N/A N/A
1.4.0 8.0 6.0 ✓, see ABI difference problems ? ? ? N/A N/A
1.5.0 9.0 7.1 ✓, see ABI difference problems ✓, see ABI difference problems ? ? N/A N/A
1.6.0 9.0 7.1 ✓, see ABI difference problems ✓, see ABI difference problems ? ? N/A N/A
1.7.0 9.0 7.1 ✓, see ABI difference problems ✓, see ABI difference problems ? ?
1.7.0 9.1 7.1 N/A N/A ? ?
1.8.0 9.0 7.1 ✓, see ABI difference problems ✓, see ABI difference problems N/A N/A
1.8.0 9.1 7.1 N/A N/A N/A N/A
1.14.0 10.0 7.4 ✓, see ABI difference problems ✓, see ABI difference problems ? ? ?
1.14.0 10.0 7.4 ✓, see ABI difference problems ? ? ? ? ?

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.

TF CUDA CUDNN pip3 tensorflow pip3 tensorflow-gpu
1.8.0 9.0 7.0 ✓, see ABI difference problems ✓, see ABI difference problems
1.14.0 10.0 7.4 ✓, see ABI difference problems ✓, see ABI difference problems

Ubuntu 20.04 64bits, Python 3.8, ROS Noetic

TF CUDA CUDNN pip3 tensorflow
2.9.1 no no

Debian Jessie 64bits, Python 2.7.6, ROS Indigo

TF CUDA CUDNN pip tensorflow pip tensorflow-gpu bazel (CPU) bazel (GPU) tensorflow_catkin (CPU) tensorflow_catkin (GPU)
1.3.0 8.0 6 ? ? ? ?

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

TF CUDA CUDNN pip tensorflow pip tensorflow-gpu bazel (CPU) bazel (GPU) tensorflow_catkin (CPU) tensorflow_catkin (GPU)
1.4.0 8.0 6 C++ ABI problems C++ ABI problems ? ? ?

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.