如何在 Android 应用程序中使用 TensorflowInferenceInterface
How to use TensorflowInferenceInterface in an Android App
我在我的 Android 应用程序中使用经过训练的模型(冻结图),该应用程序利用 Tensorflow 的预制估算器虹膜示例,如 link 所示:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/learn/iris.py
我修改了 iris.py 以满足我的需要,并添加了一些语句来冻结图表,这样我就有一个 .pb 文件可以放入我的 Android 应用程序的资产文件夹中。
为了在我的 Android 应用程序中使用 Tensorflow,我已将以下行添加到我的 build.gradle(模块:应用程序)文件中(依赖项块中的最后一条语句)。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'no.nordicsemi.android.support.v18:scanner:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.2'
implementation 'org.tensorflow:tensorflow-android:+'
}
有了我的冻结图,我通过执行这些语句来测试 Tensorflow 是否正在运行我的应用程序:
//testing tensorflow feature
TensorFlowInferenceInterface tfInterface = new
TensorFlowInferenceInterface(
getAssets(), "estimator_frozen_graph.pb");
Graph graph = tfInterface.graph();
Toast.makeText(ScanActivity.this, "Tensorflow Graph Init Success",
Toast.LENGTH_SHORT).show();
int[] inputValues = {1, 1, 121, 800, 300};
long rowDim = 1;
long columnDim = 5;
tfInterface.feed("dnn/input_from_feature_columns/input_layer/concat:0",
inputValues, rowDim, columnDim);
String[] outputNames = {"dnn/logits/BiasAdd:0"};
boolean logstats = false;
tfInterface.run(outputNames, logstats);
float[] outputs = new float[6];
tfInterface.fetch("dnn/logits/BiasAdd:0", outputs);
for(int i = 0; i<= outputs.length; i++)
{
System.out.println(outputs[i]);
}
当程序到达行时:
tfInterface.run(outputNames, logstats);
Android工作室的logcat中出现了以下错误信息:
Caused by: java.lang.IllegalArgumentException: No OpKernel was registered to support Op 'Iterator' with these attrs. Registered devices: [CPU], Registered kernels:
<no registered kernels>
[[Node: Iterator = Iterator[container="", output_shapes=[[?], [?], [?], [?], [?], [?]], output_types=[DT_INT64, DT_INT64, DT_INT64, DT_INT64, DT_INT64, DT_INT64], shared_name=""]()]]
at org.tensorflow.Session.run(Native Method)
我一直在寻找类似的问题,但我似乎找不到解决这个问题的可行方法。
如果我需要添加任何信息以简化此处获得帮助的过程,请告诉我。提前致谢。
我已经解决了我自己的问题。原来我还没有理解使用 Tensorflow 正确冻结模型的概念。
这个问题的简短答案是:
- 按照适当的步骤使用 freezeGraph.py.
冻结模型(在本例中为 DNN 分类器)生成的图形
- 将冻结的 protobuf 文件包含在 Android 项目 "assets" 文件夹中。
- 在此处阅读有关在 Android 应用程序中实施 Tensorflow 的指南:
http://aqibsaeed.github.io/2017-05-02-deploying-tensorflow-model-andorid-device-human-activity-recognition/
我在我的 Android 应用程序中使用经过训练的模型(冻结图),该应用程序利用 Tensorflow 的预制估算器虹膜示例,如 link 所示:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/learn/iris.py
我修改了 iris.py 以满足我的需要,并添加了一些语句来冻结图表,这样我就有一个 .pb 文件可以放入我的 Android 应用程序的资产文件夹中。
为了在我的 Android 应用程序中使用 Tensorflow,我已将以下行添加到我的 build.gradle(模块:应用程序)文件中(依赖项块中的最后一条语句)。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'no.nordicsemi.android.support.v18:scanner:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.2'
implementation 'org.tensorflow:tensorflow-android:+'
}
有了我的冻结图,我通过执行这些语句来测试 Tensorflow 是否正在运行我的应用程序:
//testing tensorflow feature
TensorFlowInferenceInterface tfInterface = new
TensorFlowInferenceInterface(
getAssets(), "estimator_frozen_graph.pb");
Graph graph = tfInterface.graph();
Toast.makeText(ScanActivity.this, "Tensorflow Graph Init Success",
Toast.LENGTH_SHORT).show();
int[] inputValues = {1, 1, 121, 800, 300};
long rowDim = 1;
long columnDim = 5;
tfInterface.feed("dnn/input_from_feature_columns/input_layer/concat:0",
inputValues, rowDim, columnDim);
String[] outputNames = {"dnn/logits/BiasAdd:0"};
boolean logstats = false;
tfInterface.run(outputNames, logstats);
float[] outputs = new float[6];
tfInterface.fetch("dnn/logits/BiasAdd:0", outputs);
for(int i = 0; i<= outputs.length; i++)
{
System.out.println(outputs[i]);
}
当程序到达行时:
tfInterface.run(outputNames, logstats);
Android工作室的logcat中出现了以下错误信息:
Caused by: java.lang.IllegalArgumentException: No OpKernel was registered to support Op 'Iterator' with these attrs. Registered devices: [CPU], Registered kernels:
<no registered kernels>
[[Node: Iterator = Iterator[container="", output_shapes=[[?], [?], [?], [?], [?], [?]], output_types=[DT_INT64, DT_INT64, DT_INT64, DT_INT64, DT_INT64, DT_INT64], shared_name=""]()]]
at org.tensorflow.Session.run(Native Method)
我一直在寻找类似的问题,但我似乎找不到解决这个问题的可行方法。
如果我需要添加任何信息以简化此处获得帮助的过程,请告诉我。提前致谢。
我已经解决了我自己的问题。原来我还没有理解使用 Tensorflow 正确冻结模型的概念。 这个问题的简短答案是:
- 按照适当的步骤使用 freezeGraph.py. 冻结模型(在本例中为 DNN 分类器)生成的图形
- 将冻结的 protobuf 文件包含在 Android 项目 "assets" 文件夹中。
- 在此处阅读有关在 Android 应用程序中实施 Tensorflow 的指南: http://aqibsaeed.github.io/2017-05-02-deploying-tensorflow-model-andorid-device-human-activity-recognition/