如何在Tensorflow Android版本中添加操作

时间:2017-07-06 02:22:51

标签: java android build tensorflow

我正在尝试在Android上运行我的Tensorflow模型,因此我使用nightly native build in here并在Android demo之后,我已成功运行Tensorflow Android lib并在下面加载模型代码。

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

日志显示结果很好。

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'

然而,当我完成所有输入节点的输入

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

然后调用run方法

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

Tensorflow被打破,说某些内核未注册

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

我认为int32的“LessEqual”应该在Tensorflow中定义,但不是与Tensorflow Android Lib一起构建的。

所以我的问题是如何在Android lib版本中包含更多内核或以其他任何方式解决此问题,我们将非常感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

我建议阅读这本免费的电子书:Building Mobile Applications with TensorFlow

它有一个关于What Ops Are Available on Mobile?(第33页)的部分,它解释了如何Add the implementation to the build那些默认为移动构建被剥离的操作内核。

仅供参考,该电子书由Pete Warden(GitHubblog)撰写,他在Google工作,是TensorFlow的维护者之一。