Cannot load @tensorflow-models/speech-commands

Viewed 696

I am trying to load tensorflow.js model speech-commands in my react-native-app. But after trying a lot I am not able to load the model. I am getting this error

Error: Unable to resolve module `fs` from `node_modules\@tensorflow-models\speech-commands\dist\browser_fft_utils.js`: fs could not be found within the project.

I tried adding 'fs' module in the main react-native-app but got this error-:

Error: While trying to resolve module `fs` from file `...\node_modules\@tensorflow-models\speech-commands\dist\browser_fft_utils.js`, the package `...\node_modules\fs\package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (...\node_modules\fs\index.js`.

My package.json dependencies

"dependencies": {
  "@react-native-community/async-storage": "^1.12.0",
  "@tensorflow-models/speech-commands": "^0.4.2",
  "@tensorflow/tfjs": "^2.3.0",
  "@tensorflow/tfjs-react-native": "^0.3.0",
  "expo-camera": "^8.3.1",
  "expo-gl": "^8.4.0",
  "expo-gl-cpp": "^8.4.0",
  "fs": "0.0.1-security",
  "jpeg-js": "^0.4.2",
  "react": "16.13.1",
  "react-native": "0.63.2",
  "react-native-fs": "^2.16.6",
  "react-native-unimodules": "^0.10.1",
  "util": "^0.12.3"
},

My app.js

import React from 'react'
import {Text} from'react-naitve'
import * as tf from '@tensorflow/tfjs';
import '@tensorflow/tfjs-react-native';
import * as speechCommands from '@tensorflow-models/speech-commands';

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isTfReady: false,
      isModelReady: false
    };
  }

  async componentDidMount() {
    // Wait for tf to be ready.
    await tf.ready();
    // Signal to the app that tensorflow.js can now be used.
    this.setState({
      isTfReady: true,
    });
    this.model = await speechCommands.load()
    this.setState({ isModelReady: true })
  }

  render() {
    return(
      <Text>
        {this.state.isTfReady?<Text>ready</Text>:<Text>no loading///...</Text>}
        Model ready?{' '}
        {this.state.isModelReady ? <Text>Yes</Text> : <Text>Loading Model...</Text>}
      </Text>
    )
  }
}

I tried to find why I was getting this error, found a similar issue on GITHUB, but was unable to solve the issue.

Please could anyone tell me how I can get rid of this error? THANKS

1 Answers

My workaround is to add the following in package.json under node_modules/@tensorflow-models/speech-commands

"browser": { "fs": false, "node-fetch": false, "string_decoder": false, "crypto": false },

Ideally same solution in https://github.com/tensorflow/tfjs/issues/1274 should be applied.

Related