java - 如何在 Android 应用程序中使用 TensorflowInferenceInterface

标签 java android python tensorflow tensorflow-estimator

我在 Android 应用程序中使用经过训练的模型(卡住图),该应用程序利用 Tensorflow 的预制估计器 iris 示例,如以下链接所示:

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

我修改了 iris.py 以满足我的需求,并添加了一些语句来卡住图表,以便我有一个 .pb 文件可以放入我的 Android 应用程序的 Assets 文件夹中。

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

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)

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

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

最佳答案

我已经解决了我自己的问题。事实证明我还没有理解使用 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/

关于java - 如何在 Android 应用程序中使用 TensorflowInferenceInterface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52287664/

相关文章:

java - 如何为JNLP应用程序设置preferIPv4Stack属性?

Android UiAutomator - 检测应用程序是否处于 uiautomator 模式

android - ui-router 保持 Controller Activity

python - 使用 python 在 tkinter 中的图片上添加文本输入小部件

java - 将 EST 中的字符串转换为 PST 中的日期

java - 为什么我使用 try catch 时会出现错误

android - 在sqlite上获取插入错误代码

python - 从多个文件的行创建生成器

python - 如何在 pandas、python 上将多个 csv 文件合并到一个具有特定列的文件中?

java - Spring Boot - "Error creating bean with name ' entityManagerFactory'"- 开始