ml-agents icon indicating copy to clipboard operation
ml-agents copied to clipboard

TensorflowSharp & libtensorflow update

Open tcmxx opened this issue 8 years ago • 12 comments

Hello,

It is great that TensorflowSharp is updated to 1.7. However, there are some problem regarding the udpate.

  1. 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..

  1. 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.

tcmxx avatar Jun 27 '18 15:06 tcmxx

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.

xiaomaogy avatar Jun 27 '18 22:06 xiaomaogy

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 avatar Jun 28 '18 09:06 tcmxx

@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.

xiaomaogy avatar Jun 29 '18 23:06 xiaomaogy

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 avatar Jun 29 '18 23:06 xiaomaogy

@xiaomaogy Thanks a lot! I will see if I can get the Tensorflowsharp for Android updated myself!

tcmxx avatar Jul 02 '18 07:07 tcmxx

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

tcmxx avatar Jul 16 '18 14:07 tcmxx

Thanks @tcmxx!

EstonianGiant avatar Aug 21 '18 03:08 EstonianGiant

@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 avatar Sep 06 '18 20:09 xiaomaogy

@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 avatar Sep 07 '18 09:09 tcmxx

@tcmxx I see, thanks for the help. I will use your advice when we also work on this.

xiaomaogy avatar Sep 27 '18 21:09 xiaomaogy

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.

xiaomaogy avatar Apr 03 '19 22:04 xiaomaogy

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.

github-actions[bot] avatar Jan 29 '21 06:01 github-actions[bot]