0

I am learning Tensorflow and following a tutorial I was able to make a custom model to run it in an Android App but I am having problems with it. I have the following code:

    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));
    }

I got an error when the app try to run the inferenceInterface.run(new String[]{"output"}) method:

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)]]

I don't believe the model I created is the problem because I was able to use it in a Python code with positive result.

1 Answer 1

1

From the error message (In[0] is not a matrix), it appears that your model requires the input to be a matrix (i.e., a two dimensional tensor), while you are feeding a one dimensional tensor (vector) with 68 elements.

In particular, the dims argument to TensorFlowInferenceInterface.feed seems incorrect in the line:

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

Instead, it should be something like:

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

if your model expects a 68x1 matrix (or 34, 2 if it expects a 34x2 matrix, 17, 4 for a 17x4 matrix etc.)

Hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the problem was that the model was expecting a matrix not an array. I modified the model a op tf.reshape,that way I could set an input of an array and the model changes it to a matrix later

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.