【问题标题】:Tensorflow error on AndroidAndroid上的Tensorflow错误
【发布时间】:2017-11-14 05:28:37
【问题描述】:

我正在学习 Tensorflow,并按照教程制作了一个自定义模型以在 Android 应用程序中运行它,但我遇到了问题。我有以下代码:

    public void testModel(Context ctx) {
        String model_file = "file:///android_asset/model_graph.pb";
        int[] result = new int[2];
        float[] input = new float[]{0.0F, 1.0F, 0.0F, 1.0F, 1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 1.0F, 0.0F, 1.0F, 0.0F, 1.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 0.0F, 1.0F, 0.0F, 1.0F, 0.0F, 1.0F, 0.0F, 0.0F, 1.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 1.0F, 0.0F, 1.0F, 1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 1.0F, 0.0F, 1.0F, 1.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 1.0F, 0.0F, 1.0F, 0.0F, 1.0F, 0.0F};
        TensorFlowInferenceInterface inferenceInterface;
        inferenceInterface = new TensorFlowInferenceInterface(ctx.getAssets(), model_file);
        inferenceInterface.feed("input", input, 68);
        inferenceInterface.run(new String[]{"output"});
        inferenceInterface.fetch("output", result);
        Log.v(TAG, Arrays.toString(result));
    }

当应用尝试运行inferenceInterface.run(new String[]{"output"}) 方法时出现错误:

java.lang.IllegalArgumentException: In[0] is not a matrix
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_input_0_0, W1)]]

我不相信我创建的模型是问题所在,因为我能够在 Python 代码中使用它并获得肯定的结果。

【问题讨论】:

    标签: android machine-learning tensorflow


    【解决方案1】:

    从错误消息 (In[0] is not a matrix) 看来,您的模型要求输入是一个矩阵(即二维张量),而您正在输入一个包含 68 个元素的一维张量(向量)。

    特别是,TensorFlowInferenceInterface.feeddims 参数在该行中似乎不正确:

    inferenceInterface.feed("input", input, 68);
    

    相反,它应该是这样的:

    inferenceInterface.feed("input", input, 68, 1);
    

    如果您的模型需要 68x1 矩阵(或 34, 2 如果需要 34x2 矩阵,17, 4 需要 17x4 矩阵等)

    希望对您有所帮助。

    【讨论】:

    • 是的,问题在于模型期望的是矩阵而不是数组。我修改了模型一个操作tf.reshape,这样我可以设置一个数组的输入,然后模型将其更改为矩阵
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 2018-01-13
    • 2018-04-18
    相关资源
    最近更新 更多