shaqian / tflite-react-native

React Native library for TensorFlow Lite
https://www.npmjs.com/package/tflite-react-native
MIT License
291 stars 106 forks source link

could not invoke TfliteReactNative.loadModel is null models/mobilenet_v1_1.0_224.tflite #12

Open marikani opened 5 years ago

marikani commented 5 years ago

I am facing this problem.

hinhinvn1111111 commented 5 years ago

Hi guy. i already sovle this problem( see https://github.com/reneweb/react-native-tensorflow/issues/34).

  1. Run yarn add --dev metro metro-core
  2. Run yarn install
  3. Replace file metro.config.js module.exports = { resolver: { assetExts: ['pb', 'txt', 'jpg'] } }
Macilias commented 5 years ago

really? TFLite ending is .tflite not .pb so how could this one help?

Macilias commented 5 years ago

I have also the same issue:

Could not invoke TfliteREactNative.loadModels
null
/path/to/my/model.tflite

This is how I initialize the model:

    async init() {
        try {
            console.log( "started initialization of TENSOR FLOW LITE" );
            let tfLite = new Tflite();
            await tfLite.loadModel( {
                model: this.props.protobuf, // required
                labels: this.props.labels,  // required
                numThreads: 1,              // defaults to 1
            } );
            this.setState( {tfLite: tfLite} );
        } catch ( err ) {
            if ( err.description === "cancelled" ) {
                console.log( "cancelled by user" );
            }
            console.error( "Got error on init(): ", err );
        }
    }

I'm passing the path to model file and labels as props. This approach is working exactly the same way for react-native-tensorflow but with a .pb file of caurse instead of an .tflite file as in this case. I tried with putting file:// before the actual file, but didn't help.

By the way: First I had this one:

[15:20:16] E | ReactNativeJS ▶︎ 'Got error on init(): ', { [TypeError: null is not an object (evaluating 'TfliteReactNative.loadModel')]
                             │ line: 115254,
                             │ column: 26,
                             └ sourceURL: 'http://localhost:8081/index.bundle?platform=android&dev=true&minify=false' }

but it was due of missing steps from Android part, so I'm adding it here, just in case someone else ends up in the same situation and needs quick help.

ShaharyarMaroof commented 5 years ago

Any updates on how to fix this? I am also getting this same error. I used the example code provided in the repo and have placed the models and labels in the android/app/src/main/assets directory as well, and in a models directory in my react-native code src.

Macilias commented 5 years ago

nope and the project seams not to be updated as frequently as it should to become reliable. I suggest you check out the real deal: https://github.com/tensorflow/tfjs/tree/master/tfjs-react-native just compare the stars

ShaharyarMaroof commented 5 years ago

Hey @Macilias, thanks for the suggestion will look into it as well! I finally got it to work by the following steps:

  1. Create the assets directory in the app folder via Android Studio.
    • Open the android project in Android Studio, then click on the app folder
    • The go to File > New > Assets Folder
    • Select main as the target source set.
  2. Create a models folder in the assets directory and placed all models and labels and labels which are available in the example code.
  3. Now, when I used the example code it worked correctly.
robsonpsdesv commented 5 years ago

@marikani @ShaharyarMaroof I couldn't solve it by following what you did. Help me =/

janithwanni commented 5 years ago

Hey @robsonpsdesv I was able to get the model up by following these steps which are similar to the ones @ShaharyarMaroof did

  1. Make a new folder called assets in app/src/main/
  2. Place the model file (tflite and txt) on to the folder
  3. Add to android/app/build.gradle the lines from the README (I normally put it towards the end of the file)
    aaptOptions {
        noCompress 'tflite'
    }
  4. And then call the loadModel function

Do let us know what your error might have been

robsonpsdesv commented 5 years ago

Yes @ShaharyarMaroof succecfuly, tks;)

kad1r commented 5 years ago

I have also the same issue:

Could not invoke TfliteREactNative.loadModels
null
/path/to/my/model.tflite

This is how I initialize the model:

    async init() {
        try {
            console.log( "started initialization of TENSOR FLOW LITE" );
            let tfLite = new Tflite();
            await tfLite.loadModel( {
                model: this.props.protobuf, // required
                labels: this.props.labels,  // required
                numThreads: 1,              // defaults to 1
            } );
            this.setState( {tfLite: tfLite} );
        } catch ( err ) {
            if ( err.description === "cancelled" ) {
                console.log( "cancelled by user" );
            }
            console.error( "Got error on init(): ", err );
        }
    }

I'm passing the path to model file and labels as props. This approach is working exactly the same way for react-native-tensorflow but with a .pb file of caurse instead of an .tflite file as in this case. I tried with putting file:// before the actual file, but didn't help.

By the way: First I had this one:

[15:20:16] E | ReactNativeJS ▶︎ 'Got error on init(): ', { [TypeError: null is not an object (evaluating 'TfliteReactNative.loadModel')]
                             │ line: 115254,
                             │ column: 26,
                             └ sourceURL: 'http://localhost:8081/index.bundle?platform=android&dev=true&minify=false' }

but it was due of missing steps from Android part, so I'm adding it here, just in case someone else ends up in the same situation and needs quick help.

Hello, if possible, can you please share your working code? I'm getting same error and none of those methods are working.

kad1r commented 5 years ago

My solution is related with emulator. If you are using real device it works.

sauravjmedhi commented 3 years ago

Since the official docs do not tell us to create a 'models' directory inside the 'assets' directory, removing 'models' from the path should work.

tflite.loadModel({
  model: 'mobilenet_v1_1.0_224.tflite',// required
  labels: 'mobilenet_v1_1.0_224.txt',  // required
  numThreads: 1,                              // defaults to 1  
},