0

Error: Caused by: java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite tensor with type FLOAT32 and a Java object of type java.lang.String (which is compatible with the TensorFlowLite type STRING).

I have built a neural network from my dataset and have 2 layers then I have saved the model as h5 and then converted it into tflite using tf.keras model and conversions but when I deploy it in the application it gives me the above error

I have tried inputting with a lot of type of arrays and array lists

Error: Caused by: java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite tensor with type FLOAT32 and a Java object of type java.lang.String (which is compatible with the TensorFlowLite type STRING).

model.add(layers.Dense(500, input_dim=3, activation='relu'))
model.add(layers.Dense(1, activation= "relu"))
model.summary() #Print model Summary
model.compile(loss='mean_squared_error',optimizer='adam')
model.fit(X_train,Y_train,epochs=1000,validation_split=0.3)

How I convert:-

from tensorflow.contrib import lite
converter = lite.TFLiteConverter.from_keras_model_file( 'Model.h5')
tfmodel = converter.convert()
open ("model.tflite" , "wb") .write(tfmodel)

Implementation to android

ArrayList<String> list = new ArrayList<>();
list.add("-0.5698444");
list.add("-0.57369368");
list.add("-1.31490297");






try (Interpreter interpreter = new Interpreter(mappedByteBuffer)) {
    interpreter.run(list, "output");
}


private MappedByteBuffer loadModelFile() throws IOException {
    AssetFileDescriptor fileDescriptor = getAssets().openFd("model.tflite");
    FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
    FileChannel fileChannel = inputStream.getChannel();
    long startOffset = fileDescriptor.getStartOffset();
    long declaredLength = fileDescriptor.getDeclaredLength();
    return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
2
  • It will be helpful if you post the code used to load the data in the TFLite model in Java. Commented May 27, 2019 at 13:06
  • Hi, I have added that part Commented May 29, 2019 at 5:22

1 Answer 1

0

Found the mistake. In the line,

try (Interpreter interpreter = new Interpreter(mappedByteBuffer)) {
    interpreter.run(list, "output");
 }

The 2nd argument for the interpreter.run() has to be a float[] and not "output". The float[] will be populated with the class probabilities when the model is run.

The correct method to provide inputs and outputs to the interpreter.run() method:

Interpreter interpreter = new Interpreter(mappedByteBuffer)

float[][] inputs = new float[1][num_features]
// populate the inputs float array above
float[][] outputs = new float[1][num_classes]

interpreter.run( inputs , outputs )

float[] classProb = outputs[0]
Sign up to request clarification or add additional context in comments.

1 Comment

Hi I tried using my shape of tensor it is saying is (0,1) while my java's is (1,1)

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.