TensorflowSharp & libtensorflow update
Hello,
It is great that TensorflowSharp is updated to 1.7. However, there are some problem regarding the udpate.
- When I am trying to build on Android, some new operations that are added to Tensorflowsharp 1.7 are not available, but those are availble on windows PC, such as TFGraph.Assign() and TFGraph.SigmoidCrossEntropyWithLogits().
I would like to build it myself but i dont know how to build TnesorflowSharp for android..
- The windows libtensorflow.dll might not be the latest verison, since I notice that TFGraph.AddGradients() has some bugs on Conv operations, which it gives wrong dilate parameters. Not sure if it is the case for Mac or other platform.
Hi @tcmxx, we took the TensorFlowSharp.Android.dll file from this repo --https://github.com/gmlwns2000/TensorFlowSharp, and we built the libtensorflow.so for armeabi-v7a and x86 by following this blog -- http://teitoku-window.hatenablog.com/entry/2017/12/24/230717 and this readme https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/README.md. We have an internal documentation on the procedure of building for android, but that's currently a little messy so we probably won't put it into our documentation for now.
Regarding adding operators, we have modify the tensorflow/tensorflow/core/kernels/BUILD before building it, we have added the multinomial_op and stateless_random_ops for our own use case.
Regarding your first question, do you know what is the missing operator for the TFGraph.Assign() and TFGraph.SigmoidCrossEntropyWithLogits()?
Regarding your second question, do you know which version of tensorflow has the bug you mentioned, and what version fixed it? If so we can upgrade accordingly.
Hello @xiaomaogy For the first question, the operation is not missing from libtensorflow.so, but the TensorFlowSharp.Android.dll I think. TFGraph.Assign() and TFGraph.SigmoidCrossEntropyWithLogits() are availble in TensorflowSharp 1.7 but not some of the older versions. It looks like the the repo https://github.com/gmlwns2000/TensorFlowSharp is pretty old, maybe that is the reason TensorFlowSharp.Android.dll is not the latest version.
For the second, I dont know which version has the bug, but at least the latest libtensorflow.dll is good on windows. If you build libtensorflow.dll from the latest tensorflow repo it should be fine. (I think the one you get from TensorflowSharp nuget might be good as well)
@tcmxx For the first question, in that case we might not be able to help at this point. If you know some repo with the updated TensorFlowSharp.Android.dll or you could contribute to https://github.com/gmlwns2000/TensorFlowSharp , that would be really helpful.
For the second question, yes the libtensorflow.dll we use is not the latest version. I can post the steps to build it here if you are interested in building it yourself.
Build TensorFlow C Library(libtensorflow.so file) with added operator for Android
This method references this blog http://teitoku-window.hatenablog.com/entry/2017/12/24/230717 , which you might need to do google translation to understand. Prepare for TensorFlow and setup NDK
Following what the method suggested, I cloned the tensorflow repo from https://github.com/tensorflow/tensorflow, then setup the ndk following this link https://developer.android.com/ndk/guides/#download-ndk. When I was upgrading, I used Android NDK version = 14.1.3816874 (TensorFlow doesn’t support higher version), and Android SDK Build Tools 28-rc1(Highest version when I was doing this). The recommended SDK and NDK is explained here, reading recommended. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/README.md
Modify the symbols that is outputed to make it work for C#
Modify the version_scripts.lds under tensorflow/contrib/android/jni/ as shown in the blog.
VERS_1.0 {
# Export JNI symbols.
global:
Java_*;
JNI_OnLoad;
JNI_OnUnload;
+ *TF_*;
+ *TFE_*;
# Hide everything else.
local:
*;
};
Edit WORKSPACE
Install the bazel following the guide here: https://docs.bazel.build/versions/master/install-os-x.html
You can run ./configure under the tensorflow repo, answer ‘no’ or choose default for all of the choices until you reach:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:
For NDK choose /Users/<your_username>/library/Android/Sdk/ndk-bundle For SDK choose /Users/<your_username>/library/Android/Sdk/. For API Level choose 27, which is the highest level when I am writing this guide.
Edit BUILD file
Add "multinomial_op.cc", "multinomial_op.h", “stateless_random_ops.cc”, “stateless_random_ops.h” to tensorflow/tensorflow/core/kernels/BUILD in the file group android_core_ops. If your GridWorld environment doesn’t work but your 3DBall environment works, this should be the issue.
Build the libtensorflow.so library
Run
bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so
--crosstool_top=//external:android/crosstool
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain
--verbose_failures --local_resources 4096,4.0,1.0 -j 1
--cpu=armeabi-v7a
For armeabi-v7a architecture
bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so
--crosstool_top=//external:android/crosstool
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain
--verbose_failures --local_resources 4096,4.0,1.0 -j 1
--cpu=x86
For x86 architecture
The build will take 20+ mins for each. After the build finishes, you can find tensorflow_inference.so in bazel-bin/tensorflow/contrib/android. Copy libtensorflow_inference.so into other place. After building whole binary for each platform, rename libtensorflow_inference.so to libtensorflow.so. And then copy those files into correct place (ex: TensorflowSharp/GitIgnoredData/android/armeabi-v7a/)
@xiaomaogy Thanks a lot! I will see if I can get the Tensorflowsharp for Android updated myself!
I rebuilt the TensorflowSharp.Android.dll, TensorflowSharp.dll and libensorflow.dll(for windows) with latest version of Tensorflow and TensorflowSharp. Here is the link if anyone is interesting https://onedrive.live.com/?authkey=%21AAtl2iqF19WiW8U&id=D192F8A519FA380%21863022&cid=0D192F8A519FA380
Thanks @tcmxx!
@tcmxx Thank you very much for the contribution! Just wondering which version of the tensorflow did you targeted and what steps did you take to build it (In addition to the steps I mentioned above)? This would help us build the stuff based on the work you've done here. Thanks again!
@xiaomaogy I dont remember which tensorflow version, it should be the one that is use in TensorfowSharp 1.7. I also did not build tensorflow library for Android, i only rebuilt the Windows dll and TensorflowSharp dlls
The way I built TensorflowSharp dlls(TensorflowSharp.Android.dll, TensorflowSharp.dll), is to use the repo from the link you provided:
https://github.com/gmlwns2000/TensorFlowSharp
And merge it manually with the latest TensorflowSharp, then build it.
@tcmxx I see, thanks for the help. I will use your advice when we also work on this.
Since we've switched to barracuda and deprecated TensorFlowSharp, this issue is no longer valid. I will close it for now. Feel free to open if you want to discuss more.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.