Android-TensorFlow-Lite-Example icon indicating copy to clipboard operation
Android-TensorFlow-Lite-Example copied to clipboard

Reading model from internal storage

Open sai-pher opened this issue 5 years ago • 4 comments

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?

sai-pher avatar Apr 28 '19 02:04 sai-pher

I have the same problem

liyoung1992 avatar May 16 '19 08:05 liyoung1992

Hi, I have a similar issue. Has anyone figured out how to solve it?

susielau avatar Jul 09 '19 19:07 susielau

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.

mehedi-shafi avatar Sep 24 '19 13:09 mehedi-shafi

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

GyanendroKh avatar Dec 23 '19 04:12 GyanendroKh