Jaime Cuellar
Jaime Cuellar

Reputation: 474

How to load tensorflow lite model on Xamarin Android app

I want to load a .tflite model on my Xamarin Android app.

I've tried loading it with TensorFlow Interpreter

var file = new Java.IO.File("C:\\Users\\Jaime\\source\\repos\\IdentificadorImagenesSolution\\IdentificadorImagenes\\IdentificadorImagenes.Android\\Assets\\detect.tflite");
var model = new Xamarin.TensorFlow.Lite.Interpreter(file);

and I get this Exception

The model is not a valid Flatbuffer file

But i cant find FlatBuffer class inside the bindings of tflite for Xamarin, would be helpful if someone knows how could i load my model.

Upvotes: 2

Views: 1704

Answers (1)

Jaime Cuellar
Jaime Cuellar

Reputation: 474

Got help from this question

{
    var assets = Application.Context.Assets;
    AssetFileDescriptor fileDescriptor = assets.OpenFd("detect.tflite");
    FileInputStream inputStream = new FileInputStream(fileDescriptor.FileDescriptor);
    FileChannel fileChannel = inputStream.Channel;
    long startOffset = fileDescriptor.StartOffset;
    long declaredLength = fileDescriptor.DeclaredLength;
    var asd = fileChannel.Map(FileChannel.MapMode.ReadOnly, startOffset, declaredLength);
    var model = new Xamarin.TensorFlow.Lite.Interpreter(asd);
}
catch(Exception e)
{
    System.Diagnostics.Debug.WriteLine(e);
}

make sure you go to Project->Project Properties-> Android Options->Leave resource extensions with out compress and add .tflite to it.

Upvotes: 1

Related Questions