tflite_flutter_helper icon indicating copy to clipboard operation
tflite_flutter_helper copied to clipboard

Unhandled Exception: Bad state: failed precondition

Open prakashssp077 opened this issue 4 years ago • 6 comments

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Bad state: failed precondition E/flutter ( 776): #0 checkState (package:quiver/check.dart:73:5) E/flutter ( 776): #1 Tensor.setTo (package:tflite_flutter/src/tensor.dart:150:5) E/flutter ( 776): #2 Interpreter.runForMultipleInputs (package:tflite_flutter/src/interpreter.dart:194:33) E/flutter ( 776): #3 Interpreter.run (package:tflite_flutter/src/interpreter.dart:165:5) E/flutter ( 776): #4 Classifier.predict (package:fluttertfliteapp/Classifier.dart:137:17) E/flutter ( 776): #5 _MyHomePageState._predict (package:fluttertfliteapp/main.dart:86:28) E/flutter ( 776): #6 _MyHomePageState.getImage. (package:fluttertfliteapp/main.dart:79:7) E/flutter ( 776): #7 State.setState (package:flutter/src/widgets/framework.dart:1267:30) E/flutter ( 776): #8 _MyHomePageState.getImage (package:fluttertfliteapp/main.dart:73:5) E/flutter ( 776): E/flutter ( 776): D/EGL_emulation( 776): eglMakeCurrent: 0xec5697e0: ver 2 0 (tinfo 0xd3994710) D/EGL_emulation( 776): eglMakeCurrent: 0xec569060: ver 2 0 (tinfo 0xec474b90)

prakashssp077 avatar Apr 08 '21 10:04 prakashssp077

Hi @prakashssp077, Can you please share some reproducible code.

am15h avatar Apr 17 '21 07:04 am15h

I had this when I tried to run PoseNet on an image, the precondition fails where inputTensor bytelength is 4x the input byte's length.

yaizudamashii avatar Apr 27 '21 00:04 yaizudamashii

It seems that when loading the model from asset, _tensor in interpreter.dart already has 4x the number of bytes and trying to resizeInputTensor fails

yaizudamashii avatar Apr 27 '21 02:04 yaizudamashii

@am15h Actually I am getting a similar error, even without doing anything.

I/tflite  ( 5512): Initialized TensorFlow Lite runtime.
I/flutter ( 5512): Interpreter Created Successfully
I/flutter ( 5512): Labels loaded successfully
I/flutter ( 5512): Time to load image: 51 ms
E/tflite  ( 5512): tensorflow/lite/kernels/reshape.cc:66 num_input_elements != num_output_elements (4004 != 1001)
E/tflite  ( 5512): Node number 29 (RESHAPE) failed to prepare.
E/flutter ( 5512): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Bad state: failed precondition
E/flutter ( 5512): #0      checkState (package:quiver/check.dart:74:5)
E/flutter ( 5512): #1      Interpreter.allocateTensors (package:tflite_flutter/src/interpreter.dart:142:5)
E/flutter ( 5512): #2      Interpreter.runForMultipleInputs (package:tflite_flutter/src/interpreter.dart:181:7)
E/flutter ( 5512): #3      Interpreter.run (package:tflite_flutter/src/interpreter.dart:157:5)
E/flutter ( 5512): #4      Classifier.predict (package:imageclassification/classifier.dart:97:17)
E/flutter ( 5512): #5      _MyHomePageState._predict (package:imageclassification/main.dart:67:28)
E/flutter ( 5512): #6      _MyHomePageState.getImage.<anonymous closure> (package:imageclassification/main.dart:61:7)
E/flutter ( 5512): #7      State.setState (package:flutter/src/widgets/framework.dart:1088:30)
E/flutter ( 5512): #8      _MyHomePageState.getImage (package:imageclassification/main.dart:57:5)
E/flutter ( 5512): <asynchronous suspension>
E/flutter ( 5512): 

Steps to reproduce:

  1. clone the repository
  2. run sh install.sh from example/image_classification
  3. build example/image_classification on Android emulator
  4. load any image from the emulator by tapping the bottom right button

yaizudamashii avatar Apr 27 '21 17:04 yaizudamashii

Ok false alarm, I had changed the package code and had to repair it using flutter pub cache repair

yaizudamashii avatar Apr 27 '21 19:04 yaizudamashii

@yaizudamashii were you able to solve the above error of failed precondition

Priyanshu078 avatar Sep 20 '23 11:09 Priyanshu078