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 we have created a TensorFlow or keras model, 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.

Fayrix Products

Big Data Platform
Fayrix Big Data Platform helps to utilize large enterprise arrays of data for building meaningful analytical reports and forecasting future events.

Learn more
Digital Education Platform
Fayrix Digital Education Platform is an advanced solution to manage the whole cycle of education process at schools. It allows to keep pace with the times and bring education process to a new digital level.

Learn more
Social Media Monitoring Platform
Fayrix Social Media Monitoring Platform processes data from over 300 key media sources, groups and publications on social networks. It allows to analyze brand performance and presence in media space, identify key topics etc.

Learn more
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.
Final Thoughts
All in all, you will save a lot of time and money if you do everything right, but small mistakes can lead to unacceptable consequences. Fortunately, we're here to help you. Contact Fayrix right now, and you will gain access to our large experience, a long list of qualified professionals and a variety of ready developed solutions like Fayrix big data platform that helps to utilize large enterprise arrays of data for building meaningful analytical reports and forecasting future events;
or services like BI & Big data consulting that will help you to apply big data for machine learning projects and get valuable insights on hidden people behaviour patterns - their digital footprints, which conventional data analysis can't even detect and utilize. These insights help companies systematically increase sales, mitigate risks and refine operations.

Fayrix Services

Virtual development teams
Fayrix can help you to form your own dedicated virtual development company, or to build a remote dedicated team of developers, or to expand an existing one. Using a network of over 30k verified contractors from CIS countries we are ready to complete the task in 1 week.

Learn more
BI & Big data consulting
Big data, machine learning and predictive analysis has already evolved from being just a new exotic product to an essential mainstream instrument for many verticals & market sectors. Fayrix's dedicated team of 50+ data scientists, data analysts and developers is ready to execute a complete project of any scale.

Learn more
Custom Software Development
With a sharp and mindful attitude towards our customer's business, Fayrix is ready effectively organize your software development outsourcing and build a dedicated team of IT professionals of any size with flexible terms for offshore and
custom computer vision development services

Learn more
Have any questions?
We are here to help you.