JSPM

tfjs-react-native-expo-fix

0.2.3
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 2
    • Score
      100M100P100Q34839F
    • License Apache-2.0

    TensorFlow.js platform implementation for React Native

    Package Exports

    • tfjs-react-native-expo-fix

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tfjs-react-native-expo-fix) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Platform Adapter for React Native

    This package provides a TensorFlow.js platform adapter for react native. It provides GPU accelerated execution of TensorFlow.js supporting all major modes of tfjs usage, include:

    • Support for both model inference and training
    • GPU support with WebGL via expo-gl.
    • Support for loading models pretrained models (tfjs-models) from the web.
    • IOHandlers to support loading models from asyncStorage and models that are compiled into the app bundle.

    Setting up a React Native app with tfjs-react-native

    These instructions assume that you are generally familiar with react native developement.

    Step 1. Create your react native app.

    You can use the React Native CLI or Expo. This library relies on a couple of dependencies from the Expo project so it may be convenient to use expo but is not mandatory.

    On macOS (to develop iOS applications) You will also need to use CocoaPods to install these dependencies.

    Depending on which workflow you used to set up your app you will need to install different dependencies.

    If you are in a managed expo application these libraries should be present and you should be able to skip this step.

    After this point, if you are using Xcode to build for ios, you should use a ‘.workspace’ file instead of the ‘.xcodeproj’

    Step 3: Configure Metro

    Edit your metro.config.js to look like the following. Changes are noted in the comments below.

    // Change 1 (import the blacklist utility)
    const blacklist = require('metro-config/src/defaults/blacklist');
    
    module.exports = {
      transformer: {
        getTransformOptions: async () => ({
          transform: {
            experimentalImportSupport: false,
            inlineRequires: false,
          },
        }),
      },
      resolver: {
        // Change 2 (add 'bin' to assetExts)
        assetExts: ['bin', 'txt', 'jpg'],
        sourceExts: ['js', 'json', 'ts', 'tsx', 'jsx'],
        // Change 3 (add platform_node to blacklist)
        blacklistRE: blacklist([/platform_node/])
      },
    };

    Step 4: Install TensorFlow.js and tfjs-react-native

    • Install @tensorflow/tfjs - npm install @tensorflow/tfjs
    • Install @tensorflow/tfjs-react-native - npm install @tensorflow/tfjs-react-native

    Step 5: Install and configure other peerDependencies

    Step 6: Test that it is working

    Before using tfjs in a react native app, you need to call tf.ready() and wait for it to complete. This is an async function so you might want to do this in a componentDidMount or before the app is rendered.

    The example below uses a flag in the App state to indicate that TensorFlow is ready.

    import * as tf from '@tensorflow/tfjs';
    import '@tensorflow/tfjs-react-native';
    
    export class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          isTfReady: 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,
        });
      }
    
    
      render() {
        //
      }
    }
    

    After gathering feedback in the alpha release we will add an example to the tensorflow/tfjs-examples repository.

    For now you can take a look at integration_rn59/App.tsx for an example of what using tfjs-react-native looks like. The Webcam demo folder has an example of a style transfer app.

    style transfer app initial screen style transfer app initial screen style transfer app initial screen style transfer app initial screen

    API Docs

    API docs are available here