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