Android-TensorFlow-Lite-Example
Android-TensorFlow-Lite-Example copied to clipboard
Reading model from internal storage
Hi there.
I wanted to know if it is possible to store and read the trained .tflite
model from the Android device's internal storage instead of the assets folder?
The issue I am having is with the startOffset
value in the loadModelFile(AssetManager assetManager, String modelPath)
function. currently, it comes from AssetFileDescriptor fileDescriptor = assetManager.openFd(modelPath)
.
This file descriptor is used to get the start offset and declared length. Is there another way to read it from internal memory instead and still get the start offset and declaredLength? If not, is there a way to calculate the startOffset of a new model and its declared length when reading the raw binary from internal storage?
I have the same problem
Hi, I have a similar issue. Has anyone figured out how to solve it?
File modelFile = new File(this.modelPath);
FileChannel channel = new FileInputStream(modelFile).getChannel();
interpreterInference = new Interpreter(channel.map(FileChannel.MapMode.READ_ONLY, 0, modelFile.length()), new Interpreter.Options());
This does load the model. Without any issue. But it arises a new problem which is
Caused by: java.lang.IllegalStateException: Internal error: Unexpected failure when preparing tensor allocations: tensorflow/lite/kernels/conv.cc:237 input->dims->size != 4 (1 != 4)Node number 0 (CONV_2D) failed to prepare.
at org.tensorflow.lite.NativeInterpreterWrapper.allocateTensors(Native Method)
Well I guess that's because of the input tensor size (shape) I have to reshape the image I suppose.
Well, I've been searching all over the place and finally I've figured it out. It's dead simple.
For some reason I thought that AssetFileDescriptor
's getStartOffset
is related to the actual tflite model
but it's not. I think the getStartOffset
gives the start
point of the file in the application's asset. And for the tflite model
the startOffset
should be 0
because that's where the file start as it is only one file.
So, the code should be
File file = new File('path_to_model');
FileInputStream is = new FileInputStream(file);
return is.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());