It is possible to use Tensorflow C++ API in QT project? - c++

I trained a Tensorflow model for image classification using the Python API, then i saved the model checkpoint.
Now I want to load this model in a QT(C++) project.
After looking into documentations, I found that to use Tensorflow C++ API you need to create a project inside a clone of the TensorFlow github repository, then build it with Bazel, who is not possible in my case.
Is there a way to include tensorflow library in my QT project ?

You need bazel (or CMake) to build the tensorflow library, but once it is compiled you are free to use whatever tool you like and treat it like an external library.
Believe me, once you have compiled tensorflow, the last thing you want to do is add it to your project and rebuild it accidentally. This baby takes time.

Yes, there is. Compile tensorflow project as a library (dll) and include it into qt project.

It is also possible to use make instead of bazel to build the TensorFlow library for your target platforms.🙂
You can find more information on building TensorFlow with a Makefile at the TensorFlow GitHub project.
You can also have a look at this guide and open-source example project for Image Classification and Object Recognition with Qt and TensorFlow. It describes how to build TensorFlow using make and integrates it to your Qt project using the QMAKE build system.

Related

Build OpenCV using qmake

I make part of a 'big' project based on QT-Creator.
It is an image adquisition/processing project.
I am working in a particular library where we need to use OpenCV functions.
I found out how to build, install and include OpenCV using CMAKE thanks to answers like this one.
My problem is that we need that each person on the team can get the OpenCV source and build/install/include automatically (something we normally do with our own libraries) AND this is 100% based on qmake then I should not add CMAKE to our project.
The question is does someone know how to build OpenCV directly with qmake in a project on QT-Creator?

Build Tensorflow r1.0 c++ API

I trying to build Tensorflow r1.0 for c++
I was able to compile the libtensorflow.so but not all the headers generated.
All the ops headers missing except from standard_ops.h and const_ops.h which include in them all the ops headers that missing.
Im using Ubuntu 16.04.
Code for most ops is generated during the build. When using bazel, you should fine the generated header files in: bazel-genfiles/tensorflow/cc/ops, e.g., bazel-genfiles/tensorflow/cc/ops/math_ops.h.
Hope that helps.
If you don't mind using CMake, you can use tensorflow_cc project. It builds and installs TF C++ API for you, along with convenient CMake targets you can link against.

How to build static libraries and linkthem to a c++ project in linux platform?

I am working on a text-classification project, which is big and doesn't use bazel as its build tool. I want to integrate tensorflow into my project, but I find it is hard to change my build tool to bazel. So I wish to build static libraries on tensorflow and link them into my project.
Does anyone know how to build standalone static libraries on tensorflow source and link them in the existing c++ project? Thanks a lot.
The TensorFlow repository has some Makefiles you can use to build a static library (see tensorflow/contrib/Makefile).
Alternatively, you could use bazel to build the TensorFlow C++ shared library and then load and use the shared library in your application (bazel build -c opt //tensorflow:libtensorflow_cc.so). Unfortunately, bazel can't yet produce a static library (#1920).
Hope that helps.

Is there way to package Tensorflow for c++ api?

I've been developing c++ project using a Tensorflow c++ api. it just execute created tensorflow's graph from Python. I build it using bazel with Tensorflow code now. But I think it's inefficient way.
I want just Tensorflow library and header files, and Just compile my project only using Cmake.
I know how to build shared library.
bazel build -c opt --config=cuda //tensorflow:libtensorflow.so but this command just make a libtensorflow.so file. I can't find header files for build my project.
Is there way to package tensorflow library for c++? such as mvn package command.
As far as I know, there is no official distributable C++ API package. There is, however, tensorflow_cc project that builds and installs TF C++ API for you, along with convenient CMake targets you can link against. According to your description, that may be just what you need.
If your operating system is Debian or Ubuntu, you can download unofficial prebuilt packages with the Tensorflow C/C++ libraries. This distribution can be used for C/C++ inference with CPU, GPU support is not included:
https://github.com/kecsap/tensorflow_cpp_packaging/releases
There are instructions written how to freeze a checkpoint in Tensorflow (TFLearn) and load this model for inference with the C/C++ API:
https://github.com/kecsap/tensorflow_cpp_packaging/blob/master/README.md
Beware: I am the developer of this Github project.
As Floop already mentioned, his tensorflow_cc project is also a good alternative without packaging, especially if you want GPU support for the inference.
You can build tensorflow with CMake. This also creates a TensorflowConfig.cmake, which you can integrate in your project
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/cmake
Little hint: You have to build the shared lib, even if you do not need it.
You have two option: static linking and dynamic linking.
If you want to dynamic link your c++ project to TensorFlow, all you need is a --whole-archive linker flag. The necessary header files are provided by a pip install.
Generating the library is basically
bazel build -c opt --copt=-mfpmath=both --config=cuda //tensorflow:libtensorflow.so
bazel build -c opt --copt=-mfpmath=both --config=cuda //tensorflow:libtensorflow_cc.so
Having everything in place it is easy to run a TensorFlow graph in C, C++, Go (GitHub project). See the linked project for these working examples in C, C++, Go.
When building against the shared library, the headers I use are in $PROJECT_HOME/bazel-genfiles.
Adding $PROJECT_HOME/bazel-genfiles to the linker header list should be enough.

C++ how to manage dependencies (use libraries from github for example)

I'm very new to C++ world, so please, sorry for such a dummy question. I googled a little, but wasn't able to find a proper answer.
My question is fairly simple - how should I use libraries in C++ world. For example in Java - there is maven and gradle for this task. In Python - I use pip. In javascript npm and bower do all the stuff. In C# you use nuget or just adding DLL lib to your project. But looks like in C++ things isn't such easy.
I found a tool, called conan but amount of libraries they have is pretty small and does not include any what I'm looking for.
So, for example - I want to use nlp lib meta but it seems like they don't provide any installer files. So I assume I need to get sources from Github. Should I compile them and then try to add the compiled files to my project or do I need to have a lib folder in my project, and put meta's sources in those folder and after operate with meta's sources as they are in my project?
My question isn't about how to install specific meta lib, but more from the source management point of view. If I use Visual Studio on Windows for example, but my colleague will be coding Clion under Linux. And I don't know the proper way of managing dependencies in C++ world.
C++ doesn't have anything like pip or npm/bower. I don't know if maven or gradle can be persuaded to handle C++ libraries.
In general, you are going to have to end up with
Header files in a directory somewhere
library files (either static libraries, or DLLs/shared objects). If the library is a header-only library like some of the boost libraries, then you won't need this.
You get hold of the library files, either by building them on your machine (typical for open source projects, and projects aimed at Linux platforms), or by downloading the pre-compiled binaries (typical for Windows libraries, particularly paid-for).
Hopefully, the instructions for building the library will be included on the library website. As noted in the comments, 'meta' seems to be quite good at that.
When you try to compile with the library, you may need a command line option (eg -I) to specify the directory containing the header files, and you may need a linker option (eg -l) to tell the linker to link against your library.
Cget will install any package that uses standard cmake, and works for linux and windows. It has shorten syntax for getting packages directly from github(such as cget install google/googletest).
In addition, dependencies can be automatically downloaded as well by listing them in a requirements.txt file.
There is also recipes for installing non-cmake packages and the repository here has over 300 libraries(and growing). So you can install curl with just cget install pfultz2/cget-recipes curl.
C++ sadly has no package manager for libraries. Some are out there and try to be one which are still small and scattered though (like conan).
In linux you have some "-dev" packages you can install but they are also not "all".
You most likely end up downloading them yourself. Next though is you have the problem of integrating those libraries. You have different build systems per operating system so you have to see how you build c++ files.
Like in windows with Visual studio you have to get a visual studio project or a nmake compatible makefile to build the libraries and then add them to your project. Same with linux makefiles.
There are several build frameworks who are higher level like cmake. The example you have in your post also works with CMake. So integrating that one into a cmake build environment would be easier but this only applies for other libraries also trying to use/integrate cmake build environments to it (e.g. boost / qt is doing this).
Yeah these are some thoughts to this. Sadly there won't be an easy/definitive answer to this because there is no real central c++ packet repository which is also integrated into a build system.
It appears to me that the Crascit/DownloadProject could be of help in your situation. It provides CMake plugins for downloading projects from a git repository by specifying tags, etc. Then you can use add_custom_target to run commands you need to have the project built.
There are a number of popular C++ released via nuget packages.
You can search on the gallery for them, usually using the native or c++ tags. Obviously you need a nuget manager for your OS, and I'm pretty sure that the C++ nuget packages rely on MSBuild for a lot of the grunt work, so you may have trouble getting a non-Visual Studio oriented setup to work nicely.
Also Gradle actually does have some support for native dependencies as well. I had a look at little while ago but the work on it was curtailed because the support for VS 2015 was lacking.
I recommend vcpkg for cross platform development. It has a number of IDE integrations. GitHub project is here.
I do cross platform development using tools like CMake, Visual Studio, WSL. vcpkg was incredibly helpful.
I started new project... in cureent time it's just "source package manager" you can provide some source code on github and then it will be just copy to you project (based on cmake + auto generating cmake files)
So links here:
https://github.com/wsjcpp/wsjcpp