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!

 

 

Crescer3D – An adaption of agar.io

In the summer term 2016 a group of students, including myself, created a 3D adaption
of the classic web browser game agar.io. The game was created as part of the course”Computer Graphics” at Linköping University, Sweden. The game is a combination of
fun ideas, an use case to learn game development and the CG rendering pipeline.

The code is hosted on github: Crescer3D

Starshooter – a web based wheelchair trainer

As second project of my time at the KTH Stockholm we developed
a game that is built to enable people that recently started to use
a wheelchair to learn how to “pitch”, which refers to the vertical
lifting of the little front wheels that is necessary in a lot of everyday
life situations.

The game is hosted at: http://starshooter.kspri.se

The “about” section of the games homepage gives further information.

Padawan 101 – VR based Lightsaber Simulator

In the winter term 2015/2016 I was guest student of the computer science and communication department at KTH Stockholm. The most interesting project I was participated is a group work
of 5 computer science and media technology students. We were developing a game where people are encouraged to blend into the role of a young yedi aspirant, trying to learn the fight with lightsabers. The first prototype consisted of the Oculus Rift Headset and the Leap Motion controller and was presented to a public audience.
Eventually we were working on improving the game and presented it at Comic Con Gamex in Stockholm in October 2015. Here you can find a Facebook event for the presentation.
My work in the project:

  • First:
    • developing a finite state machine software structure
      to couple the different game elements.
    • integrate the leap motion sensor information
      correctly with our virtual lightsaber.
  • Second:
    • working on using gyro/accelerometer from a smartphone
      to get rotational information out of a smartphone and
      the Kinect SDK for positional tracking.
    • implementing a sensor fusion algorithm that smoothly
      integrates the positional information from Kinect,
      the rotation data from gyro and accelerometer
      into a smooth 6DOF tracking.

website of the course “Advanced Graphics and Interaction” within we were working.
Padawan101 was nominated for technical excellence at the C Awards 2016.

Stockholm Street Art

Stockholm Street Art – 13.09.2015

Compared to other big cities street art is very rarely seen.
This is possibly also due the high fees if you get caught.
At one area in the south of Stockholm, artists are allowed
to work on several walls of private property.

street art 01 street art 02 street art 03 street art 04 street art 05 street art 06 street art 07 street art 08