Comments (4)
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.
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.
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.
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)
- Anyone implemented "Movnet lightning/thunder" with Metal Delegate (Experiencing memory issues) HOT 1
- Error (Xcode): Undefined symbol: _TfLiteInterpreterGetSignatureCount HOT 1
- Pub-Cache folder location Mac HOT 4
- Please make support for hexagon delegate HOT 2
- This repo is very great! HOT 1
- ImageEmbedder API HOT 1
- Bad state: failed precondition HOT 4
- Error (Xcode): Framework not found TensorFlowLiteC even I did exactly the instruction for iOS HOT 1
- ffi is not update, Please help
- Feature Request: Enabled Batch Processing
- not able to call the function HOT 1
- I have error when I build object detection desktop app HOT 2
- Null check operator used on a null value in the method interpreter.run([input], [output]); HOT 3
- Invalid argument(s): Failed to lookup symbol 'TFLGpuDelegateCreate' : undefined symbol: TFLGpuDelegateCreate
- Error in model prediction
- I/flutter (16401): Failed to run model: Bad state: failed precondition
- Invalid argument(s): Size of bounding box dimBouBoxension 1 is not 4. Got 25 in shape [1, 25] HOT 1
- Failed to load dynamic library HOT 1
- android Gradle plugin's Kotlin Gradle plugin version and tflite_flutter_helper's version isn't matched HOT 1
- How to build tensorflowlite c++ library for iOS using bazel? (TensorFlowLiteC++_framework)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tflite_flutter_plugin.