tensorflow: [tflite][java] has_rank_one_input_condition error when use runForMultipleInputsOutputs()

I obtain an error when use two inputs in runForMultipleInputsOutputs().

System information

  • Custom code

  • Android API 29

  • Android simulator

  • Used libraries: – org.tensorflow:tensorflow-lite:2.1.0 – org.tensorflow:tensorflow-lite-gpu:2.1.0 – org.tensorflow:tensorflow-lite-support:0.0.0-nightly

  • Used model information The model tolite file

[{‘name’: ‘input_1’, ‘index’: 0, ‘shape’: array([ 1, 10], dtype=int32), ‘dtype’: <class ‘numpy.float32’>, ‘quantization’: (0.0, 0)}, {‘name’: ‘input_2’, ‘index’: 1, ‘shape’: array([1, 2], dtype=int32), ‘dtype’: <class ‘numpy.float32’>, ‘quantization’: (0.0, 0)}]

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 10)]         0                                            
__________________________________________________________________________________________________
embedding_1 (Embedding)         (None, 10, 256)      15616       input_1[0][0]                    
__________________________________________________________________________________________________
input_2 (InputLayer)            [(None, 2)]          0                                            
__________________________________________________________________________________________________
lstm_1 (LSTM)                   (None, 256)          525312      embedding_1[0][0]                
__________________________________________________________________________________________________
concatenate (Concatenate)       (None, 258)          0           input_2[0][0]                    
                                                                 lstm_1[0][0]                     
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 61)           15799       concatenate[0][0]                
==================================================================================================

The error

java.lang.IllegalArgumentException: Internal error: Failed to run on the given Interpreter: tensorflow/lite/kernels/select.cc:80 data->has_rank_one_input_condition was not true.
Node number 23 (SELECT) failed to prepare.
Node number 19 (WHILE) failed to invoke.

The code

MappedByteBuffer tfliteModelCaption = FileUtil.loadMappedFile(activity, "classifier_caption.tflite");
Interpreter.Options options = new Interpreter.Options();
Interpreter tflite = new Interpreter(tfliteModelCaption, options);
float[][] inputString = new float[1][10];
inputString[0][9] = 4;
float[][] inputData = new float[1][2];
inputData[0][0] = 0;
inputData[0][1] = 1;
Object[] inputArray = {inputString, inputData};
Map<Integer, Object> outputMap = new HashMap<>();
outputMap.put(0, new float[1][61]);
tflite.runForMultipleInputsOutputs(inputArray, outputMap);
tflite.close();
float[] result = ((float[][]) outputMap.get(0))[0];
textViewAge.setText("Test: " + Arrays.toString(result));

The error seems to means the shape of the inputs does not fit the model input, but I checked it many time and for me it looks the same. I create a similar model with only one input (no input_2) and I can use it without error.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 43 (21 by maintainers)

Most upvoted comments

Finally, I found how to use concrete_func, I put the code below maybe it can help someone.

import tensorflow
from tensorflow.keras.models import Model
from tensorflow.keras.layers import LSTM, Embedding
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.layers import concatenate
import tensorflow.compat.v2 as tf
tf.enable_v2_behavior()

vocab_size = 10
max_length = 10
def create_model():
    input_str = Input(shape=(max_length,))
    x = Embedding(vocab_size, 256, mask_zero=True)(input_str)
    x = LSTM(256)(x)
    input_int = Input(shape=(2))
    y = input_int
    z = concatenate([y, x])
    outputs = Dense(vocab_size, activation='softmax')(z)
    return Model([input_int, input_str], outputs, name='lstm')

model = create_model()
model.summary()
model.save("model_lstm2")
run_model = tf.function(lambda x, y: model([x, y]))
BATCH_SIZE = 1
STEPS = 1
INPUT_SIZE = 2
concrete_func = run_model.get_concrete_function(tf.TensorSpec((BATCH_SIZE, 2), model.inputs[0].dtype),tf.TensorSpec((BATCH_SIZE, 10), model.inputs[0].dtype))
model.save("model_lstm2", save_format="tf", signatures=concrete_func)

I checked with my full model. All seems to work well now. Thank you.