1
votes

I'm loading a TensorFlow Lite model into my Android app, but the application keeps crashing (without any error printed out) on:

tflite = new Interpreter(loadModelFile(activity));

It is my understanding, from some issues on the tensorflow repo on github, that this is due to a bad tflite model; indeed, if I load a demo model from the repository instead, it works fine.

How do I check the correctness of a .tflite model? I created it from a tensorflow model file .pb converted with toco from the repo, which gave me neither errors nor positive feedback. The tensorflow model comes from a convertion of a keras model.

1
Can you look at the logcat and grab the stack trace around the crash? What kind of model are you using? Can you provide code snippet of your Android app? - Pannag Sanketi
Also you can try testing on python using the python interpreter if that is simpler for you: tensorflow.org/api_docs/python/tf/contrib/lite/Interpreter - suharshs

1 Answers

0
votes

You could use it as follows,[assuming your model is a file in the assets folder]

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


private MappedByteBuffer loadModelFile() throws IOException {
    AssetFileDescriptor fileDescriptor = this.getAssets().openFd("modelname.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);
}