Can you guide on how to load model (model.json and group1-shard1of1.bin files) which are downloaded to Documents Directory in iOS and internal app storage folder in Android ?
I have these files model.json and group1-shard1of1.bin files) which are downloaded from server to Documents directory in iOS and downloads directory/internal app storage in android. I want to load these models. Can you point out to material/resources ?
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 ?
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.
This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.
This issue was closed due to lack of activity after being marked stale for past 7 days.