Tensorflow
Tensorflow was developed by researches & engineers from ‘Google Brain Team’ in Google’s AI organization.
It is an open source software library for high-performance numerical computation & supports machine learning, deep learning processes.
Its flexible architecture allows easy deployment of computation across a variety of platform (CPUs, GPUs, TPUs), desktops, clusters of server and Mobile device. The flexible numerical computation core is used across many scientific domains.
Process for Installing Tensorflow for C++:
Using these steps, install Tensorflow & its C++ API working as a dependency in your own DLL on windows (with GPU support also).
Applicable for: Tensorflow V 1.3 & above.
1.Prerequisite
- Use the 64bit Tools from Visual Studio 2015.
- VS2015 is necessary to build DLL, but instead of building through the application, press your windows key and type in “Native”. You should be able to open the “VS2015 x 64 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.
- No need of SWIG.
- Should have correct CUDA & cuDNN installed.
For V1.3 choose CUDA 8 in combination with cuDNN 5.1 Git. ‘cmake’ is also needed.
2.Acquire Tensorflow source code
- Dwonload Tensorflow available on Github at this path:
https://github.com/tensorflow/tensorflow/tree/r1.3/
or
https://github.com/hhzrz/tensorflow-cpp
- From command line navigate to “tensorflow/contrib/cmake” subfolder of the source code.
- Create a directory with “mkdir build” & then “cd build”.
3.Create a build section
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 & 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.
If everything goes right, you should be able to build the tensorflow.dll by issuing the following command:
MSBuild /p:Configuration=RelWithDebInfo tensorflow.vcxpro
4.Add Tensorflow DLL 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.
Create two environment variables, for the root of the tensorflow source & for the build directory.
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($(TENSORRFLOW_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 And also target the library as the additional linking target:
link_directories(${TENSORFLOW_BUILD_DIR}/RelWithDebInfo) target_link_libraries(${PROJECT_NAME} ${TENSORFLOW_BUILD_DIR}/RelWithDebtInfo/tensorflow.dll)
5.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.
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 warning, 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
Completion
After this you should be able to compile your codebase while linking to Tensorflow and use the C++ API, doing some deep learning magic with CUDA under windows.