Giter Site home page Giter Site logo

Comments (4)

am15h avatar am15h commented on July 18, 2024

Strange, the interpreter generally works on all tflite models, (not tested for custom ops till now). Please share your flutter doctor -v as well.

These kinds of issues are mostly a result of input/output format mismatch. I recommend using https://pub.dev/packages/tflite_flutter_helper, to eliminate such errors.

from tflite_flutter_plugin.

MojtabaYEG avatar MojtabaYEG commented on July 18, 2024

Here is my flutter doctor -v:

[√] Flutter (Channel stable, v1.17.5, on Microsoft Windows, locale en-CA)
• Flutter version 1.17.5 at C:\Path
• Framework revision 8af6b2f038 (7 weeks ago), 2020-06-30 12:53:55 -0700
• Engine revision ee76268252
• Dart version 2.8.4

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at C:\Path
• Platform android-29, build-tools 29.0.3
• Java binary at: C:\Path
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
• All Android licenses accepted.

[√] Android Studio (version 3.6)
• Android Studio at C:\Path
• Flutter plugin version 47.1.1
• Dart plugin version 192.8052
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)

[√] Connected device (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)

• No issues found!

I also tried tflite_flutter_helper. here is my code:

var interpreterOptions = InterpreterOptions()..addDelegate(NnApiDelegate());
final interpreter = await Interpreter.fromAsset('path',options: interpreterOptions);
var imageBytes = (await rootBundle.load('path')).buffer;
imageLib.Image oriImage = imageLib.decodePng(imageBytes.asUint8List());
imageLib.Image copyImage = imageLib.copyCrop(oriImage, 60, 0, 60, 30);
TensorImage tensorImage = TensorImage.fromImage(copyImage);
TensorBuffer probabilityBuffer = TensorBuffer.createFixedSize([1,1], TfLiteType.float32);
interpreter.run(tensorImage.buffer, probabilityBuffer.buffer);

which doesn't work and gives me the following error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Bad state: failed precondition

the error referes to this line of code:
interpreter.run(tensorImage.buffer, probabilityBuffer.buffer);

The problem goes away when I use the ImageProcessorBuilder. Then my code is:

var interpreterOptions = InterpreterOptions()..addDelegate(NnApiDelegate());
final interpreter = await Interpreter.fromAsset('path',options: interpreterOptions);
var imageBytes = (await rootBundle.load('path')).buffer;
imageLib.Image oriImage = imageLib.decodePng(imageBytes.asUint8List());
imageLib.Image copyImage = imageLib.copyCrop(oriImage, 60, 0, 60, 30);
TensorImage tensorImage = TensorImage.fromImage(copyImage);
ImageProcessor imageProcessor = ImageProcessorBuilder().add(DequantizeOp(0, 1 / 255.0)).build();
tensorImage = imageProcessor.process(tensorImage);
TensorBuffer probabilityBuffer = TensorBuffer.createFixedSize([1,1], TfLiteType.float32);
interpreter.run(tensorImage.buffer, probabilityBuffer.buffer);

But all the input and output stuff will be incorrect. the input shape should be 13060*3 = 5400 while after using the imageProcessor it's 21600 ( which is 5400 * 4). also the ouput is [35, 204, 170, 74], and all of them are incorrect. Also outputs are integer instead of float. The output should be just 1 float number ( 60.5)

from tflite_flutter_plugin.

MojtabaYEG avatar MojtabaYEG commented on July 18, 2024

I also changed the model from a regression to a classifier by making 118 output neurons instead of 1 neuron. I trained the new classifier CNN. The outputs of the classifier is also different from what I'm getting in python from h5 and tflite model.

@am15h Did you find a chance to make some test experiments? Is it possible to make an example that classifies MNIST with the package?

from tflite_flutter_plugin.

MojtabaYEG avatar MojtabaYEG commented on July 18, 2024

Finally working with tflite_flutter_helper the problem is gone using the following code:

var interpreterOptions = InterpreterOptions()..addDelegate(NnApiDelegate());
final interpreter = await Interpreter.fromAsset('ModelPath',options: interpreterOptions);
var imageBytes = (await rootBundle.load('ImagePath')).buffer;
imageLib.Image oriImage = imageLib.decodePng(imageBytes.asUint8List());
imageLib.Image copyImage = imageLib.copyCrop(oriImage, 60, 0, 60, 30);
TensorImage tensorImage = TensorImage.fromImage(copyImage);
ImageProcessor imageProcessor = ImageProcessorBuilder().add(NormalizeOp(0, 255)).build();
tensorImage = imageProcessor.process(tensorImage);
TensorBuffer probabilityBuffer = TensorBuffer.createFixedSize(<int>[1,1], TfLiteType.float32);
interpreter.run(tensorImage.buffer, probabilityBuffer.buffer);
print(probabilityBuffer.buffer.asFloat32List());

from tflite_flutter_plugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.