Code Monkey home page Code Monkey logo

Comments (5)

skadambala avatar skadambala commented on June 18, 2024

Adding some more details, I am working on react native app.

I would like to load the model from local storage, I see that bundleResourceIO only works for assets that are bundled when compiling the application, and tensorflow js react native also has an alternative to use asyncStorageIO.

Is it possible to load the model from local storage without getting into AsyncStorage? How could I use AsyncStorage to copy both files weights.bin and model.json from local storage/document directory to AsyncStorage in order to use asyncStorageIO. Can you throw some insights on this problem ?

from tfjs.

gaikwadrahul8 avatar gaikwadrahul8 commented on June 18, 2024

Hi, @skadambala

I apologize for the delayed response and could you please give it try with below workaround and see is it working as expected or not ?

import React from 'react';
import {Button, View} from 'react-native';
import * as tf from '@tensorflow/tfjs';
import {bundleResourceIO} from '@tensorflow/tfjs-react-native';
import RNFS from 'react-native-fs';

      
const modelJson1 = await RNFS.readFile(
      `${RNFS.DocumentDirectoryPath}/model/model.json`,
    );

const modelLoad = await tf.loadGraphModel(modelJson, {
            weightUrlConverter : async (weightFileName) => {
                  return `file://${RNFS.DocumentDirectoryPath}/model/group1-shard1of1.bin`;
          }});

If you're looking to use asyncStorageIO then you'll have do something like below :

  • Import required Libraries ( if it is not installed please install all the required libraries using NPM or yarn
import * as tf from '@tensorflow/tfjs';
import AsyncStorage from '@react-native-async-storage/async-storage';
import RNFS from 'react-native-fs'; // Assuming you have react-native-fs installed
  • Define AsyncStorage Keys:
const MODEL_JSON_KEY = 'modelJson';
const MODEL_WEIGHTS_KEY = 'modelWeights';

async function loadModel() {
  try {
    // Check for model in AsyncStorage
    const modelJsonString = await AsyncStorage.getItem(MODEL_JSON_KEY);
    const modelWeightsBytes = await AsyncStorage.getItem(MODEL_WEIGHTS_KEY);

    if (modelJsonString && modelWeightsBytes) {
      // Model found in AsyncStorage, load directly
      const modelJson = JSON.parse(modelJsonString);
      const modelWeights = tf.util.decodeBase64(modelWeightsBytes);
      return await tf.loadLayersModel({ modelConfig: modelJson, weights: modelWeights }, asyncStorageIO);
    } else {
      console.error('Model not found in AsyncStorage!!!');
    }
  } catch (error) {
    console.error('Error loading model:', error);
    return null; // Or handle error appropriately
  }
}

To further investigate this issue, I'd appreciate it if you could try the workaround described above and confirm if it resolves the problem. If the issue persists, could you please share your GitHub repository? This would allow us to replicate the behavior and conduct a more thorough investigation.

Thank you for your cooperation and patience.

from tfjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.