Leave us a message. We'll get back to you within 1 business day.
Leave us a message. We'll get back to you within 1 business day.
Attachment

Building libraries of TensorFlow neuronets
in Windows environment

Fayrix Big Data team shares their expertice on creating TensorFlow neural networks
Сonvolutional neural networks (CNN) development and testing in the Python environment currently does not cause any difficulties. Many ready-made frameworks are available for implementing CNN of any kind of structure. Difficulties usually arise when you need to implement CNN outside of Python, for example in C ++, in some program to run on Windows. Prediction functionality typically needs to be implemented on a trained model.

Let's say wehavecreated a TensorFlow or kerasmodel, trained and saved it (usually in hdf5 format). Next we want to write the predict function in C ++ language in the Windows 7x64 or higher environment, load the model, some data and get the network response. There are a lot of similar examples in C ++, but you will need TensorFlow libraries to build any of them. This article describes the steps to build TensorFlow libraries, version 1.10.0 using CMake.
System
REQUIREMENTS

Windows 7 x64 or higher, 16Gb RAM, 128Gb SSD (HDD will also work, of course, but it will be slower)
INSTALLATION
Visual studio
Install Microsoft Visual Studio Community 2017, select only the "Desktop development with C++" optionand add the "VC ++ 2015.3 v14.00 (v140) toolset for desktop"component to it.

Python environment
Download and install the Anaconda package. This is a fully configured Python along with a pre-installed set of the most popular modules. During the installation process, do not forget to tick the box to add the package to the system variable PATH.

CMake, Git, SWIG
Download and install CMake for Windows. Add the installation path to the PATH.
Download and install Git for Windows. Add the installation path to the PATH.
Download and install SWIG. This package provides the ability to call functions written in onelanguages from code in other languages.

CUDA

Optional. We have test version 9.0
Run the environment preparation script C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat
TensorFlow
Create a build directory and download the TensorFlow repository.
mkdir C:\src
cd C:\src
git clone https://github.com/tensorflow/tensorflow.git
cd C:\src\tensorflow
git checkout r1.10
ren C:\src\tensorflow C:\src\tensorflow.1.10.0.cpu
cd C:\src\tensorflow.1.10.0.cpu\tensorflow\contrib\cmake
mkdir build
cd build


Create a configure.bat configuration script and put the following lines there.

cmake .. -A x64 -T host=x64 ^
-DCMAKE_BUILD_TYPE=Release ^
-DSWIG_EXECUTABLE=PATH\TO\SWIG\swig.exe ^
-DPYTHON_EXECUTABLE=PATH\TO\ANACONDA\python.exe ^
-DPYTHON_LIBRARIES=PATH\TO\ANACONDA\libs\python35.lib ^
-DPYTHON_INCLUDE_DIR=PATH\TO\ANACONDA\include ^
-Dtensorflow_BUILD_PYTHON_BINDINGS=OFF ^
-Dtensorflow_ENABLE_GRPC_SUPPORT=ON ^
-Dtensorflow_BUILD_SHARED_LIB=ON ^
-Dtensorflow_ENABLE_GPU=OFF ^
-Dtensorflow_WIN_CPU_SIMD_OPTIONS=/arch:AVX


If you need CUDA support, add:
-DCUDNN_HOME="C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0" ^-DCUDA_HOST_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe"
A FEW NOTES
on the configuration

  1. tensorflow_ENABLE_GRPC_SUPPORT, in fact, we don't need it, but without it, I didn't want to assemble a project.
  2. tensorflow_BUILD_SHARED_LIB needs to be enabled because our goal is to get the DLL library
  3. tensorflow_ENABLE_GPU - if enabled, then you need to install the CUDA Development Tools package (I compiled with version 9.0) and the project will be assembled twice as long.
  4. tensorflow_WIN_CPU_SIMD_OPTIONS - flag for using new sets of instructions. This flag must be set carefully. If you install AVX2, the assembly will not work on processors where this instruction set does not exist.
  5. There have been several attempts to use the Intel MKL libraries — these are optimized algorithms for convolutional networks. Oddly enough, the inclusion of these libraries slowed down the work of models by almost two times. It may well be that I missed something, but decided to remove them from the configuration.

After running this configuration file, all the files for the build will be created.
Building
SCRIPT

cd C: \ src \ tensorflow.1.10.0.cpu \ tensorflow \ contrib \ cmake \ build

Create the build.bat configuration script and put the following lines there:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin \MSBuild.exe" ^
/m:1 ^
/p:CL_MPCount=1 ^
/p:Configuration=Release ^
/p:Platform=x64 ^
/p:PreferredToolArchitecture=x64 ALL_BUILD.vcxproj ^
/filelogger

If you have a multicore processor, you can set the parameter / m: 3 and / p: CL_MPCount = 3 this will speed up the build process.
After running the script, the build process begins and you can safely go for a walk for a couple of hours. If you plan to build libraries using CUDA, you can walk for four hours.
After the build process is complete, the following directory will appear:
C: \ src \ tensorflow \ v1.10.0.cpu \ tensorflow \ contrib \ cmake \ build \ Release where you can find our compiled libraries tensorflow.dll, tensorflow.lib, tensorflow.def
In conclusion, please note that all actions described above, the TensorFlow developers consider to be unofficial and in the future, probably, the possibility of assembling via CMake will not be possible. However, for the time being, this is the only way to build your TensorFlow library.