user3138464
user3138464

Reputation: 41

How to test Tensorflowlite model with multiple inputs?

I created a simple MLP Regression Keras model with 4 inputs and one output. I converted this model to TFlite now I'm just trying to find out how to test it on android studio. How can I input multiple 4D objects to test in Java? The following gives an error when trying to run the model:

try{
            tflite = new Interpreter(loadModelFile());
        }
        catch(Exception ex){
            ex.printStackTrace();
        }   

double[][] inp= new double[1][4];
    inp[0][1]= 0;
    inp[0][0] = 0;
    inp[0][2]= 0;
    inp[0][3]=-2.01616982303105;

    double[] output = new double[100];

    tflite.run(inp,output);

EDIT: Here is the model I originally created:

# create model
model = Sequential()
model.add(Dense(50, activation="tanh", input_dim=4, 
kernel_initializer="random_uniform", name="input_tensor"))
model.add(Dense(50, activation="tanh", 
kernel_initializer="random_uniform"))
model.add(Dense(1, activation="linear", 
kernel_initializer='random_uniform', name="output_tensor"))

Upvotes: 2

Views: 3689

Answers (2)

Amarnath
Amarnath

Reputation: 51

This is my code:

Object[] inputArray = {iArray[0],iArray[1]};
tflite.runForMultipleInputsOutputs(inputArray,outputMap);

The 1st object works fine. But the 2nd object in the function fails in Tensor getInputTensor(int index) on this condition: if(index >= 0 && index < this.inputTensors.length)

But the index is 1. Is there any problem with my code?

Upvotes: 1

jdduke
jdduke

Reputation: 159

If your inputs are actually 4 separate tensors, then you should use the Interpreter.runForMultipleInputsAndOutputs API which allows multiple separate inputs. See also this example from the TensorFlow Lite repository. For example:

double[] input0 = {...};
double[] input1 = {...};
Object[] inputs = {input0, input1};
double[] output = new double[100];
Map<Integer, Object> outputs = new HashMap<>();
outputs.put(0, output);
interpreter.runForMultipleInputsOutputs(inputs, outputs);

Upvotes: 2

Related Questions