M.Pomeroy M.Pomeroy - 23 days ago 9
Android Question

Obscure TensorFlow error after building for Android with Bazel

I'm attempting to build a simple Android app based on the TensorFlow Android demo; however the model I'm using requires an kernel Op not included by default with the TensorFlow aar. This led me down the path of compiling the libraries from source.

I've gotten the apk to build through gradle, using bazel as the native build tool; however, whenever I try and load the model from a

.pb
file(any model in protobuff format, in fact) the app crashes with an error that claims the model is "Not a valid TensorFlow Graph serialization: Invalid GraphDef".

Has anyone experienced this issue? I'm guessing something hasn't compiled correctly, but I'm not even really sure where to start looking for the mistake.

Here is the Bazel build command I'm using (on multiple lines for convenience, pulled almost verbatim from the demo):

/usr/local/bin/bazel build -c opt
tensorflow/examples/android/app:tensorflow_native_libs
--crosstool_top=//external:android/crosstool --cpu=arm64-v8a
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain


Note: the
build.gradle
file, and Bazel BUILD files remain largely untouched; I altered some paths(I moved the android example into a subdirectory
app/
, to allow for a multi-project gradle setup), and included the additional required kernel Op.

Things I've tried:


  • After some research, I found an issue on the TensorFlowSharp github page that suggests this crash can be a problem with buffer sizes, and my model is sizeable (~93MB). However, with a much smaller network (1.6MB), I get the same error.

  • I've also tried turning off compression of
    .pb
    files as part of the gradle build, as that was recommended somewhere as well, no change either.

  • In a desperate attempt I tried replacing the existing
    .so
    files in the Tensorflow android aar available from JCenter, with the ones I had just built, but this results in the same error.

  • Compiling the
    .so
    files offline and manually placing them in the appropriate android project directory.


Answer Source

It turns out this was an issue with the TensorFlow commit I was working off. When I merged in the most recent changes from the upstream TensorFlow repo the error went away.

I can't imagine anyone else will run into this issue; but if you do, or you run into some similarly obscure error, updating to a newer version of TensorFlow might be your solution. Alternatively, you could choose to be smarter than me, and build off of a stable release version of the library.