15

What I want to do

First of all, my goal is using Tensorflow C++ API as a library on Windows, which is part of my project, instead of building my project inside Tensorflow.

Background

I had achieved this by building Tensorflow with CMake. However, from Tensorflow 1.10, building with CMake was deprecated and Bazel is recommended instead. But the official way to use C++ API is building project inside Tensorflow with Bazel. Thus, this way is not good for me.

What I have done

To use a newer version of Tensorflow, I have been trying to build Tensorflow with Bazel as a standalone library.

Some maintainer denoted that it is possible by substituting //tensorflow/tools/pip_package:build_pip_package to //tensorflow:libtensorflow_cc.so in the official tutorial. But in fact I encountered some problems and solved them by reading this tutorial. Now I have successfully built libtensorflow_cc.so.

What the problem is

However, I have no idea what should be done next to use the built result. And it is exactly what my problem is. There is no documentation of course. Only some incomplete ideas on it I have found, and I will show all of them, trying to give you more information:

There must be someone struggling with similar problems like me. I hope this question can build a reservoir of ways to solve the problem.

Guikarist
  • 181
  • 7
  • Have a look at [`tensorflow_cc`](https://github.com/FloopCZ/tensorflow_cc) (from [List of headers to use Tensorflow C++ API using libtensorflow_cc.so](https://stackoverflow.com/q/42898577)). – jdehesa Dec 17 '18 at 15:17
  • 1
    @jdehesa Thanks for your comment. But I have denoted that the maintainer of this repo doesn't plan to support Windows. – Guikarist Dec 18 '18 at 05:26
  • Oh you're right, I didn't realize. Well in principle you should be able to find the dll and the lib files among the built objects, but what may be difficult is to collect all the necessary headers. My workaround for this was to make a simplified wrapper for the library with just a couple of headers, so I'd only have those and my own dll (cannot open source unfortunately). – jdehesa Dec 18 '18 at 09:55
  • The other path for integration is with the [TensorFlow library for C](https://www.tensorflow.org/install/lang_c). This is in principle only supported on Linux and Mac at the moment, although in [this URL](https://storage.googleapis.com/tensorflow) you can find builds of the library for Windows (e.g. [here](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.12.0.zip) is a Windows CPU-only build of version 1.12.0). However, these come without .lib files, although in theory you can Google how to generate them. You can also build the C library from source. – jdehesa Dec 18 '18 at 10:00
  • 1
    Now I know how to patch on C++ API, so I made [a script to build Tensorflow C++ API using Bazel on Windows](https://github.com/guikarist/tensorflow-windows-build-script). It made it easier to build Tensorflow on Windows (which also supports building pip packages or C API). But I believe there will be better solutions and I hope everyone with same trouble could join me! – Guikarist Jan 02 '19 at 07:44
  • 1
    I have posted a [feature request for complete support for building C++ API on windows using Bazel](https://github.com/tensorflow/tensorflow/issues/24885). – Guikarist Jan 16 '19 at 06:40
  • @Guikarist I am also stuck with the same problem and trying to find a solution. Have you tried anything along the lines of using only the created DLL file using LoadLibrary()? I am new to building cpp projects in windows and got a suggestion to try this way. – Suba Selvandran Feb 14 '19 at 06:06
  • @SubaSelvandran If you use my script mentioned above to build Tensorflow, you can use [this script](https://gist.github.com/guikarist/e10dff3a4e777856cf40e44713fc2cb7) to extract Tensorflow libraries you need. And I think the best way to use Tensorflow on Windows is now using Visual Studio. – Guikarist Feb 14 '19 at 13:07
  • Sure. I found that for LoadLibrary() we have to know all the APIs that are supported and call them. This could be tricky compared to the method that you are suggesting. – Suba Selvandran Feb 18 '19 at 14:21
  • Until this answer is written, no plausible ways are found. [My script](https://github.com/guikarist/tensorflow-windows-build-script) provides a way to build and use C++ library on Windows, which is originally made for easier building on Windows. – Guikarist Aug 10 '19 at 13:27

1 Answers1

3

It's over 2 years since this question was asked, and the news is not good: it seems there are insufficient people with Windows skills in a position to provide the support to integrate Tensorflow into Windows applications using the familiar headers + library model. And Tensorflow advances week by week, meaning that the Windows support falls further behind.

In my assessment, the path to building on Windows is currently blocked due to inadequate documentation. It's not so much that "There is no documentation of course" as the OP asserts, it's that the sparse documentation is distributed throughout dozens of separate posts, each of which dates rapidly with the continuing development of the Tensorflow along paths other than Windows C++.

I originally gave this answer to a similar question, but updated it with advice along the following lines yesterday:

  • Windows is a Microsoft product, so watch what Microsoft is doing
  • You can implement your (ONNX) model on Windows in C++ in at least 3 ways:
    • Windows ML (uses Onnx runtime)
    • Onnx runtime (supports DirectML as an execution provider)
    • DirectML (how Microsoft uses graphics cards to boost performance)

We don't have the latest or best hardware (e.g. we have Intel graphics cards), but have been able to get a solution based on Onnx runtime that classifies 224 x 224 RGB images in about 20 milliseconds for us. We found the Windows ML path much more difficult to work with legacy code, and also slower to run.

omatai
  • 3,448
  • 5
  • 47
  • 74