Tag Archives: c++

Link Tensorflow into your Windows DLL with GPU support and cmake [V1.3]

Getting Tensorflow and its C++ API working as a third party dependency in your own DLL on Windows and with GPU support can be an adventure. Here are the necessary steps to get it working, which might help. This tutorial is written for Tensorflow V 1.3, some information might be invalid for other versions.

1. Acquire the source code.

This one is easy, tensorflow is available on Github and can be found at https://github.com/tensorflow/tensorflow/tree/r1.3/

2. Have all dependencies installed.

Since we do not use the python bindings, there is no need for SWIG, but you should have
the correct CUDA version and cuDNN installed on your machine. For V1.3 this is CUDA 8 in combination with cuDNN 5.1. Git and cmake are also needed. More info here.

3. Use the 64bit Tools from Visual Studio 2015

VS2015 is necessary to build the DLL, but instead of building through the application,
press your windows key and type in “Native”. You should be able to open the “VS2015 x64 Native Tools Command Prompt”. This is needed so VS uses the 64 bit toolset, otherwise your build will just fail because it is not able to leverage enough memory.

4. Navigate to the source files

Navigate in the commandline to the “tensorflow/contrib/cmake” subfolder of the source code and create a directory with “mkdir build”. Afterwards navigate to the fresh build folder with “cd build”.

5. Create a build solution

Use the following build command:

cmake .. -A x64 -DCMAKE_BUILD_TYPE=RelWithDebInfo --Dtensorflow_BUILD_PYTHON_BINDINGS=OFF -Dtensorflow_BUILD_CC_EXAMPLE=OFF -Dtensorflow_ENABLE_GRPC_SUPPORT=OFF -Dtensorflow_BUILD_CC_TESTS=OFF -Dtensorflow_BUILD_PYTHON_TESTS=OFF -Dtensorflow_ENABLE_GPU=ON -DCUDNN_HOME="[YOUR_CUDA_LOCATION]" -Dtensorflow_WIN_CPU_SIMD_OPTIONS=/arch:AVX -Dtensorflow_BUILD_SHARED_LIB=ON

Make sure to replace “YOUR_CUDA_LOCATION” with the location of your cuda installation.
This setup does not need SWIG since we do not build the python bindings, which makes the build more lightweight and still contains the whole C++ API to load and run graphs at runtime.
The WIN_CPU_SIMD_OPTIONS flag can of course only be used when your machine has this
instruction set.

When everything went right you should be able to build the tensorflow.dll  by issuing the following command:

MSBuild /p:Configuration=RelWithDebInfo tensorflow.vcxproj

6. Add the tensorflow DLL as dependency

After the build is done (this can take 3-4 hours on an average desktop computer) adopt your cmake setup to link to Tensorflow correctly. I created two environment variables, for the root of the tensorflow source and for the build directory, you can do this as you feel like. With TENSORFLOW_SRC_DIR as source directory and TENSORFLOW_BUILD_DIR as the build directory, make sure you add the following directories as includes:

include_directories(${TENSORFLOW_SRC_DIR})
include_directories(${TENSORFLOW_BUILD_DIR})
include_directories(${TENSORFLOW_BUILD_DIR}/external/eigen_archive)
include_directories(${TENSORFLOW_SRC_DIR}/third_party/eigen3)
include_directories(${TENSORFLOW_BUILD_DIR}/protobuf/src/protobuf/src)

Make sure you are adding the folder with the DLL as linker dependency:

link_directories(${TENSORFLOW_BUILD_DIR}/RelWithDebInfo)

And also target the library as additional linking target:

target_link_libraries(${PROJECT_NAME} ${TENSORFLOW_BUILD_DIR}/RelWithDebInfo/tensorflow.dll)

7. Debug in Release

Since tensorflow only builds in release with build symbols, we need to do the same for our own project. Otherwise the STL containers that are handed between our DLL and the C++ API of tensorflow will make it explode. Therefore follow the given instructions here, to adopt your own visual studio solution so that it generates debug symbols in the release build mode. Exercise for advanced users: Set the given settings in the linked webpage directly from cmake.

8. Additional Defines

Also make sure that you add the following three defines to your own code, to be able to link to tensorflow correctly. Tensorflow generates quite some warnings, so you can of course add a #pragma warning( disable : 4049) for every warning that annoys you, but this is up to you.

#define PROTOBUF_USE_DLLS 
 #define NOMINMAX 
 #define COMPILER_MSVC

8. Done

After this you should be able to compile your code base while linking to Tensorflow and use the C++ API, doing some deep learning magic with CUDA under windows. The C++ API.

Have fun!

 

 

Collaborative Research S3D

Collaborative Research S3D

“Development of systems and methods for effective creation and processing of stereoscopic content” was the description of a 3 year joint research project. I was able to participate in that project as part of my internship at the research & development group for six months at filmakademie bw. I was developing tools in c++ based framework frapper, relying on Qt as the GUI API and Ogre as the 3D render engine.

Project Homepage on the webserver of R&D @ Filmakademie BW

Kinect XAML Prototype

Kinect Prototype

Implementation of a Application Prototype for the advertising degree program of my university. Used for watching content analysis of the several advertising channels of a company. Based on the WPF and XAML technologies and using Microsoft Kinect for interactive control with gestures.

contentDo