Caldarie / flutter_tflite_audio

Audio classification Tflite package for flutter (iOS & Android). Can support Google Teachable Machine models
MIT License
64 stars 26 forks source link

Tensorflow Lite errors when running in iOS devices #7

Closed cmalbuquerque closed 3 years ago

cmalbuquerque commented 3 years ago

Running my application in iOS device (iPhone 7 with iOS 14.4) it crashes when the model is processing the data. I believe that happens due to Tensorflow Lite Errors (see output) but I have no idea how to fix it:

carolinaalbuquerque ~/Documents/audio_recognition_app (*main) > flutter run
Launching lib/main.dart on iPhone de Carolina in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project: 5SSNTW7HP4
Running Xcode build...                                                  
 └─Compiling, linking and signing...                        19,5s
Xcode build done.                                           28,9s
(lldb) 2021-02-20 18:18:18.623713+0000 Runner[414:13363] Warning: Unable to create restoration in progress marker file
fopen failed for data file: errno = 2 (No such file or directory)       
Errors found! Invalidating cache...                                     
fopen failed for data file: errno = 2 (No such file or directory)       
Errors found! Invalidating cache...                                     
Installing and launching...                                        36,1s
Initialized TensorFlow Lite runtime.
TensorFlow Lite Error: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.
TensorFlow Lite Error: Node number 2 (FlexSize) failed to prepare.

Failed to create the interpreter with error: Failed to allocate memory for input tensors.
["0 Background Noise", "1 Bell", "2 Whistle", "3 Xylophone"]
Activating Dart DevTools...                                         5,9s
Syncing files to device iPhone de Carolina...                       176ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h Repeat this help message.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).
An Observatory debugger and profiler on iPhone de Carolina is available at: http://127.0.0.1:53066/z1fkaZhV7VE=/

Flutter DevTools, a Flutter debugger and profiler, on iPhone de Carolina is available at:
http://127.0.0.1:9101?uri=http%3A%2F%2F127.0.0.1%3A53066%2Fz1fkaZhV7VE%3D%2F

Running with unsound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety
requesting permission
start microphone
recordingBuffer length: 11008
recordingBuffer length: 22016
recordingBuffer length: 33024
recordingBuffer length: 44032
reached threshold
Running model
* thread #21, queue = 'conversionQueue', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00000001d6d0a128 libsystem_platform.dylib`_platform_memmove + 72
libsystem_platform.dylib`_platform_memmove:
->  0x1d6d0a128 <+72>: stnp   x12, x13, [x0]
    0x1d6d0a12c <+76>: stnp   x14, x15, [x0, #0x10]
    0x1d6d0a130 <+80>: subs   x2, x2, #0x40             ; =0x40 
    0x1d6d0a134 <+84>: b.ls   0x1d6d0a158               ; <+120>
Target 0: (Runner) stopped.
Lost connection to device.

I am using a Google Teachable Machine model and I followed these steps for iOS configuration.

I have tested already in Android device and it works perfectly but I need to guarantee iOS support!

Caldarie commented 3 years ago

Hi @cmalbuquerque

the error that sticks out the most is: TensorFlow Lite Error: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.

Usually, you will get this error if you run it on an emulator, but it seems like you are using a device.

The most likely possibility is that your select ops haven’t been configured correctly. Have you force load it? Instructions here

cmalbuquerque commented 3 years ago

@Caldarie yes, I forced load as mentioned on documentation:

Captura de ecrã 2021-02-20, às 22 50 34

And after specify the force load, I ran into iOS folder:

$ flutter pub get
$ pod install
$ flutter clean
Caldarie commented 3 years ago

To check whether it’s a bug or not, can you load your model and labels on the example project provided in this repository? Can you tell me if you receive the same error?

cmalbuquerque commented 3 years ago

Yes @Caldarie, I tested using the example project and I had the same error... Also, I checked your iOS configurations for GTM models and are the same than mine. 😞

Caldarie commented 3 years ago

If it’s ok with you, would you be willing to share your model with me? It’s possible that google may of changed something, or the people responsible for iOS tensorflow made a breaking change.

cmalbuquerque commented 3 years ago

Yes, of course! Maybe you're right and some update from them breaks something...
Here is my basic model: gtm_model.zip Thanks! 😄

Caldarie commented 3 years ago

Hi @cmalbuquerque,

I was able to run your model fine (as shown on the image below).

IMG_4584

There are few possibilities for your problem:

  1. It's possible that your editor (vscode or xcode) has failed to detect your device and you are running the project on an emulator. To detect the device, you need to run a few steps: Instructions here

  2. The other possibility is that your configuration may be incorrect. But i think that is least likely considering that you ran it with my example

  3. Another possibility is that you may have forgotten to add the following in your pubsec.yaml. However, i also think this is also very unlikely.

    - assets/soundclassifier.tflite
    - assets/labels.txt
cmalbuquerque commented 3 years ago

Thanks! Could you tell me what's your Xcode version?

I reviewed all steps of my configuration and I noticed that when I run flutter pub get into /ios directory, it changes automatically the ios/Flutter/Release.xcconfig file to:

#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"

And running pod install:

carolinaalbuquerque ~/Documents/audio_recognition_app/ios (*main) > pod install    
Analyzing dependencies
Downloading dependencies
Generating Pods project
Integrating client project
Pod installation complete! There are 3 dependencies from the Podfile and 5 total pods installed.

[!] CocoaPods did not set the base configuration of your project because your project already has 
a custom config set. In order for CocoaPods integration to work at all, please either set the base 
configurations of the target `Runner` to `Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig` 
or include the `Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig` in your build configuration
(`Flutter/Release.xcconfig`).

Also, if I replace my ios/Flutter/Release.xcconfig file with yours, the same warning appears. The suggestion given by warning has already in file, so I don't understand what might be misconfigured 😞 Here is my Xcode configurations:

Captura de ecrã 2021-02-21, às 15 36 28
Caldarie commented 3 years ago

Hmm, I’ll get back to you about this problem

As for Xcode version, I’m using 12.4

cmalbuquerque commented 3 years ago

@Caldarie thanks for your time! I tried to reinstall Xcode, update pods and finally it works! Sorry for your time and thanks again! 😁

Caldarie commented 3 years ago

Glad I could be of assistance!