How to program with C++ API library on Windows using Bazel? - c++

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 is somebody already successfully linking built *.so and having solved the problems he has encountered.
There is a repo doing the what I want to do on Ubuntu and Arch Linux. I have contacted with the maintainer and he told me that they have no plan for supporting Windows now.
A related issue: Building a .dll on Windows.
A related issue: Packaged TensorFlow C++ library for bazel-independent use.
A related issue: Feature request: provide a means to configure, build, and install that includes cc.
A related question: How to build and use Google TensorFlow C++ api. The scope of this question is a little larger without 'using bazel' and 'on Windows' restrictions.
A related pull request: C++ API
There must be someone struggling with similar problems like me. I hope this question can build a reservoir of ways to solve the problem.

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
Hint: Microsoft is investing in the ONNX format
you can convert Tensorflow to ONNX, or Keras to ONNX
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.

Related

How to build and use the C++ API for Tensorflow on windows

Has anyone been successful in building/using the c++ API for TensorFlow on windows (withing Visual Studio)? The tutorials I found online or on TensorFlow's website have only shown building from source for python, or are outdated (3+ years old or for TensorFlow 1.x)
I see that there are docs for the functions of the built C++ API on TensorFlow's website, but there's no mention of where to get it or how to build it, the build from source section builds the python package that can be installed using pip, not anything that can be used with C++.
I've looked at the C api, but that seems to be different from what is mentioned in the C++ API section of the docs, and I was unable to get the C API working within VS due an error in mixing C++ and C.
If it helps, the end goal is to run inferences for pre-trained models (built using the python API) using C++.
Any help is appreciated.

How to build and use Google tensorflow C++ API on ARM processor

This is a follow-on to "how-to-build-and-use-google-tensorflow-c-api" : can any one explain how to build a Tensorflow C++ program on an ARM processor? I'm thinking specifically of Nvidia's Jetson family of GPU devices. Nvidia has lots and lots of documentation for these, but it all seems to be for Python (like this), for toy examples, and nothing for anyone who wants to write a C++ program using the full tensorflow API (if one even exists) for their own machine learning models. I'd like to be able to build programs like this one, which is a deep learning inference and exactly what the Jetson is supposedly made for.
I've found Web sites that offer links to installers too, but they all seem to be for the x86 architecture instead of ARM.
I have the same question about Bazel. I gather from all the unsatisfactory documentation I've been looking at that Bazel is mandatory for anyone who wants to build tensorflow programs using a GPU, but all of the installation instructions I can find are either incomplete or for a different architecture such as x86 (for example https://www.osetc.com/en/how-to-install-bazel-on-ubuntu-14-04-16-04-18-04-linux.html
I'll add that any link or github repository that dumps a load of code in my lap without making clear the prerequisites (since my little Jetson may not have the stuff installed that you assume) or the commands needed to actually build it (especially if it includes a project file for a compiler I never heard of) isn't very much help.

How to deploy a Tensorflow trained model for inference for a Windows standalone application

I would like to use a model trained with Tensorflow in a Windows standalone desktop application. I only need to perform predictions, I can train the model with Tensorflow Python API. What is the recommended approach?
I know there is a C++ API, but it is really hard to compile it, especially on Windows. Can I find any prebuilt C++ Tensorflow binaries for Windows?
Is there an easy way to distribute Python with Tensorflow as a Windows installer prerequisite?
Can I import the Tensorflow model in another technology and use it for inference? OpenCv DNN module has a function which imports data from Tensorflow, but I understood it has many limitations, and I was not able to import and use a model with OpenCv.
Thanks for help!
I was challenging the same issues as you.
You should at least try to compile it (try CMake, it might be easier)
If you still having trouble:
Compiler is out of Heap Space
Standalone Windows Lib
Basic Tensorflow Handling with C++
I asked a similar question and eventually found my own way to the answer. In the end, I found the Tensorflow instructions were actually pretty good (it was my reading them that was bad!). I have not tried using Bazel for Windows, but building Tensorflow using CMake ended up working fine.
The main issue was the compiler heap space issue. This always seems to occur in some random place if you are using the MS Visual Studio 32-bit compiler (default). The key is to make sure you run vcvarsall.bat or vcvars64.bat or whatever it takes to invoke the 64-bit compiler (in Task Manager, it should show up as cl.exe, not cl.exe *32) I found it hard (read: impossible) to get Visual Studio to use the 64-bit compiler, but using the MSBuild tool to compile on the command line worked fine.
Once you can build the example program, you have an example of an application that links to a static tensorflow library to do its stuff. You can just make your own application link to this library for what you want.

Cross-compiling C++ OpenCV from Visual Studio 2013 on Windows to Ubuntu

I've got a C++ VS2013 solution with 3 projects, using OpenCV 2.4.10, easylogging++ and Dlib libraries. The problem is that our client is now migrating to Linux (Ubuntu Server). I've looked into Cygwin and MinGW and a few questions here on stackoverflow, but I'm still lost as to where I should start. I need this done as soon as possible, so the simpler solution would be best. I really appreciate any help you can provide.
I recommend using cmake build system on ubuntu. Using Opencv and easylogging on ubuntu out of the box could be a matter of minutes. dlib's website also mentions a simple cmake building steps that works on ubuntu.
After you get your libraries up and running, build your code and see if there are any system-dependent functions then google how to standardize them across systems. If your code is already standardized it should run right then and there.
You can then ask about any specific issues you meet on the way.

Write applications in C or C++ for Android? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I'm trying to develop/port a game to Android, but it's in C, and Android supports Java, but I'm sure there must be a way to get a C app on there, anyone knows of a way to accomplish this?
For anyone coming to this via Google, note that starting from SDK 1.6 Android now has an official native SDK.
You can download the Android NDK (Native Development Kit) from here:
https://developer.android.com/ndk/downloads/index.html
Also there is an blog post about the NDK:
http://android-developers.blogspot.com/2009/06/introducing-android-15-ndk-release-1.html
The Android NDK is a toolset that lets you implement parts of your app in native code, using languages such as C and C++. For certain types of apps, this can help you reuse code libraries written in those languages.
For more info on how to get started with native development, follow this link.
Sample applications can be found here.
Normally, you have to:
Install Google Android NDK. It
contains libs, headers, makfile
examples and gcc toolchain
Build an executable from your C code
for ARM, optimize and link it with
provided libs if required
Connect to a phone using provided
adb interface and test your
executable
If you are looking to sell an app:
Build a library from your C code
Create simple Java code which will
use this library
Embed this library into application
package file
Test your app
Sell it or distribute it for free
Google has released a Native Development Kit (NDK) (according to http://www.youtube.com/watch?v=Z5whfaLH1-E at 00:07:30).
Hopefully the information will be updated on the google groups page (http://groups.google.com/group/android-ndk), as it says it hasn't been released yet.
I'm not sure where to get a simple download for it, but I've heard that you can get a copy of the NDK from Google's Git repository under the donut branch.
The official position seems to be that this isn't something you'd ever "want to do". See this thread on the Android Developers list. Google envisage android running on a variety of different devices (CPUs, displays, etc). The best way to enable development is therefore to use (portable) managed code that targets the Dalvik VM. For this reason, the Android SDK doesn't support C/C++.
BUT, take a look at this page:
Android includes a set of C/C++
libraries used by various components
of the Android system. These
capabilities are exposed to developers
through the Android application
framework.
The managed application framework appears to be layered on-top of these libraries. The page goes on to list the C/C++ libs: standard C library, media, 3D, SQL lite, and others.
So all you need is a compiler chain that will compile C/C++ to the appropriate CPU (ARM, in the case of the G1). Some brief instructions on how to do this are here.
What I don't know is where to find descriptions of the APIs that these libraries provide. I'd guess there may be header files buried in the SDK somewhere, but documentation may be sketchy/missing. But I think it can be done!
Hope thats useful. For the record, I haven't written any native android apps - just a few simple managed ones.
Andy
You can use nestedvm to translate C (or other GCC languages) into Java bytecode, and use that as the basis of your port. For example, see the Android port of Simon Tathams portable puzzle collection.
I expect this method is made obsolete by the NDK, but it might not be in if some networks or something don't allow people to upgrade their phones.
Google has already launched Google I/O 2011: Bringing C and C++ Games to Android session which is available at http://www.youtube.com/watch?v=5yorhsSPFG4
which is good to understand the use of NDK for writing application in c and c++ for android.
If you just want to cross compile any console based native game and run them on android then this Article has shown 3 methods for the same.
1: Static compilation using standalone toolchain
2: Cross compilation using Android NDK’s toolchain
3: Cross compilation using AOSP source code
Maybe you are looking for this?
http://www.mosync.com/
It is a middle layer for developing for several mobile platforms using c++.
Looking at this it seems it is possible:
http://openhandsetmagazine.com/2007/11/running-c-native-applications-on-android-the-final-point/ (now only available via the WayBack Machine)
"the fact is only Java language is supported doesn’t mean that you cannot develop applications in other languages. This have been proved by many developers, hackers and experts in application development for mobile. The guys at Elements Interactive B.V., the company behind Edgelib library, succeeded to run native C++ applications on the Android platform, even that at this time there is still many issues on display and sound … etc. This include the S-Tris2 game and a 3D animation demo of Edgelib."
Since 2009 there is a development on this matter.
Necessitas - Qt(C++ framework) for Android
Getting started video.
Take a look at google ndk group it looks promising, first version of the NDK will be available in 1H2009.
Update:
And it is released http://android-developers.blogspot.com/2009/06/introducing-android-15-ndk-release-1.html
I'm not sure the NDK provides full coverage of the official Java API.
From http://developer.android.com/sdk/ndk/index.html#overview :
Please note that the NDK does not
enable you to develop native-only
applications. Android's primary
runtime remains the Dalvik virtual
machine.
Google just released the NDK which allows exactly that.
http://feedproxy.google.com/~r/blogspot/hsDu/~3/2foWz7hwFtE/introducing-android-15-ndk-release-1.html
It can be found here:
http://developer.android.com/sdk/ndk/1.5_r1/index.html
This blog post may be a good start: http://benno.id.au/blog/2007/11/13/android-native-apps
Unfortunately, lots of the important stuff is "left as an exercise to the reader".
I do not know a tutorial but a good development tool: Airplay SDK from Ideaworks Labs. (Recently rebranded "Marmelade") Using C/C++ you can build apps for Windows Mobile, iPhones, Android. The only component I didn't like was the GUI composer - a buggy one, but you always can substitute it with the Notepad.
You can download c4droid and then install the GCC plugin and install to your SD. From the shell I just traverse to the directory where the GCC binary is and then call it to make an on board executable.
find / -name gcc
/mnt/sdcard/Android/data/com.n0n3m4.droidc/files/gcc/bin/arm-linux-androideabi-gcc
cat > test.c
#include<stdio.h>
int main(){
printf("hello arm!\n");
return 0;
}
./arm-linux-androideabi-gcc test.c -o test
./test
hello arm!
This three steps are good to have and store in this post.
1) How to port native c code on android
2) http://www.integratingstuff.com/2010/12/12/calling-native-c-code-through-jni-in-android-applications/
3) http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/
Native C/c++ Files libstdc++.* from your Ubuntu are x86 (or x86_64) binaries but Android devices and emulators are ARM. Of course, this will not work anyway, even if you'll set correct soname. This is very naive way which leads nowhere. Android has very limited support of C++ meaning there is no exceptions, standard C++ library (including STL) and RTTI. If you need such functionality, use my custom NDK distribution from
http://crystax.net/android/ndk.php - it support full C++ features listed above.
Why is there error: undefined reference to '__cxa_end_cleanup' link error. Android stlport
time. Because there is no link to libstdc + +. A. So wrong.
Because it uses some static library, it is necessary to link the full libstdc + +. A. Can
http://crystax.net/android/ndk.php here to download the package
sources \ cxx-stl \ gnu-libstdc + + \ libs \ armeabi directory.
Android on its own libstdc + + support is limited, it must be linked to a complete libstdc +
+. A the job.
Add file in Android.mk LOCAL_LDFLAGS = $ (LOCAL_PATH) / libs / libcurl.a \
$ (LOCAL_PATH) / libs / liblua.a \
`$ (LOCAL_PATH) / libs / libstdc + +. A`
And LOCAL_CPPFLAGS + =-lstdc + +-fexceptions can be compiled
There is a plan to allow C/C++ libraries in the next SDK version of Android (Codename Eclair?)To date, it's not possible through the Android Java SDK. However, you can grab the HUGE open source project, roll your own libraries, and then flash your own device...but anyone who wants to use your library will have to flash your custom build as well.
Short answer: You can't.
#Backslash17: Looking through the article and the following link with installation instructions, all the company got working is to launch a self compiled executable on the emulator, bypassing the android framework. Once you have the emulator you can telnet in and have a linux shell. Running a linux program there is trivial of course.
But that's not working for phones without jailbreak(aka root access) and not deliverable on the market.
Unless Google opens other opportunities or someone writes a custom compiler that compiles Language X into Dalvik bytecode (Dalvik is the VM that runs Android programs) you won't be able to write native code.