【问题标题】:How to use TensorflowInferenceInterface in an Android App如何在 Android 应用中使用 TensorflowInferenceInterface
【发布时间】:2018-09-12 04:49:09
【问题描述】:

我在我的 Android 应用程序中使用了一个训练模型(冻结图),该应用程序利用了 Tensorflow 的预制虹膜估计器示例,如以下链接所示:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/learn/iris.py

我已经修改了 iris.py 以适应我的需要,并添加了一些语句来冻结图形,以便我将一个 .pb 文件放入我的 Android 应用程序的资产文件夹中。

为了在我的 Android 应用程序中使用 Tensorflow,我在我的 build.gradle (Module: app) 文件中添加了以下行(依赖项块中的最后一条语句)。

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 Studio的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)

我一直在寻找类似的问题和问题,但似乎找不到解决此问题的可行方法。

如果我需要添加任何信息以简化在此处获得帮助的过程,请告诉我。提前致谢。

【问题讨论】:

    标签: java android python tensorflow tensorflow-estimator


    【解决方案1】:

    我已经解决了我自己的问题。原来我还没有理解使用 Tensorflow 正确冻结模型的概念。 这个问题的简短回答是:

    1. 按照适当的步骤使用 freezeGraph.py 冻结模型(在本例中为 DNN 分类器)生成的图。
    2. 将冻结的 protobuf 文件包含在 Android 项目的“assets”文件夹中。
    3. 在此处阅读有关在 Android 应用中实施 Tensorflow 的指南: http://aqibsaeed.github.io/2017-05-02-deploying-tensorflow-model-andorid-device-human-activity-recognition/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      • 2014-05-24
      • 2011-06-07
      • 1970-01-01
      相关资源
      最近更新 更多