Java – How to add operations to your Tensorflow Android build

How to add operations to your Tensorflow Android build… here is a solution to the problem.

How to add operations to your Tensorflow Android build

I’m trying to run my Tensorflow model on Android, so I’m using nightly native build in here and follow Android demo where I have successfully run Tensorflow Android library and load the model with the following code.

inferenceInterface = new TensorFlowInferenceInterface(getAssets(), MODEL_FILE);

And the log shows good results.

I/TensorFlowInferenceInterface: Successfully loaded TensorFlow native methods (RunStats error may be ignored)
I/TensorFlowInferenceInterface: Model load took 1007ms, TensorFlow version: 1.2.0-rc0
I/TensorFlowInferenceInterface: Successfully loaded model from 'file:///android_asset/model.pb'

However, when I finish feeding all the input nodes

inferenceInterface.feed("input1", new int[]{1, 2, 3}, 1, 3);
inferenceInterface.feed("input2", new int[]{3}, 1);
inferenceInterface.feed("input3", new int[]{4}, 1);

Then call the run method

inferenceInterface.run(new String[]{"output"});

Tensorflow is broken saying that some kernels are not registered

E/TensorFlowInferenceInterface: Failed to run TensorFlow inference with inputs:[input1, input2, input3], outputs:[output]
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ... jnitest, PID: 16357
java.lang.RuntimeException: Unable to start activity ComponentInfo{... MainActivity}: java.lang.IllegalArgumentException: No OpKernel was registered to support Op 'LessEqual' with these attrs.  Registered devices: [CPU], Registered kernels:
<no registered kernels>

[[Node: .../LessEqual = LessEqual[T=DT_INT32](.../maximum_iterations, .../LessEqual/y)]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: No OpKernel was registered to support Op 'LessEqual' with these attrs.  Registered devices: [CPU], Registered kernels:
<no registered kernels>

[[Node: dynamic_seq2seq/decoder/decoder_1/LessEqual = LessEqual[T=DT_INT32](.../maximum_iterations, .../LessEqual/y)]]
at org.tensorflow.Session.run(Native Method)
at org.tensorflow.Session.access$100(Session.java:48)
at org.tensorflow.Session$Runner.runHelper(Session.java:295)
at org.tensorflow.Session$Runner.run(Session.java:245)
at org.tensorflow.contrib.android.TensorFlowInferenceInterface.run(TensorFlowInferenceInterface.java:142)
at org.tensorflow.contrib.android.TensorFlowInferenceInterface.run(TensorFlowInferenceInterface.java:111)
at ... jnitest. MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
.. 9 more

I think the “LessEqual” of int32 should be defined in Tensorflow, but should not be built with Tensorflow Android Lib.

So my question is how to include more kernels in the Android library build or solve this problem in any other way, we would appreciate it.

Solution

I recommend reading this free eBook: Building Mobile Applications with TensorFlow

It has an idea about what actions are available on mobile devices? (page 33) section, which explains how for those default operations the kernel adds implementation to the build that is stripped for mobile builds.

FYI, this eBook is written by Pete Warden (GitHub, blog), who works at Google and is one of the maintainers of TensorFlow.

Related Problems and Solutions