Skip to content

Error in loading tf model in react expo app #8539

Open
@aissahm

Description

@aissahm

Hello,

I have the following error when trying to load a very simple tf model in my react expo app:

Error loading model: Error: modelWeightsID must be a number or number array.
Have you wrapped your asset paths in a require() statements?

Here is the code snippet in my component.tsx:

import * as tf from "@tensorflow/tfjs";
import {bundleResourceIO, decodeJpeg} from "@tensorflow/tfjs-react-native";
import * as FileSystem from 'expo-file-system';
import * as DocumentPicker from 'expo-document-picker';

export default function App() {

const loadingtest = async ()=>{
console.log(loading);
await tf.ready();
console.log(tf ready);

const modelJson = await require('@/assets/model/newtest/model.json');
const weightAssets = await require('@/assets/model/newtest/group1-shard1of1.bin');

console.log(`modeljsobn`, modelJson);
console.log(`modelweigts`, weightAssets);

try {
  const model = await tf.loadLayersModel(bundleResourceIO(modelJson, weightAssets));
  console.log(`model loaded`);
  // You can now use the 'model' object for predictions
} catch (error) {
  console.error("Error loading model:", error);
}

}

loadingtest();

return (
<Animated.View style={[styles.container ]}>
</Animated.View>
);
}

System information
I created the model from GoogleColab, then installed Tensorflowjs to convert the model from .keras and .h5 to .json and .bin files for expo app.
The version for tensorflowconverter:
installed ml_dtypes-0.5.1 packaging-23.2 tensorboard-2.19.0 tensorflow-2.19.0 tensorflow-decision-forests-1.12.0 tensorflowjs-4.22.0 tf-keras-2.19.0 wurlitzer-3.1.1 ydf-0.11.0

Describe the current behavior
in the console log on web browser, I get:

modeljsobn {format: 'layers-model', generatedBy: 'keras v3.8.0', convertedBy: 'TensorFlow.js Converter v4.22.0', modelTopology: {…}, weightsManifest: Array(1)}
rankview.tsx:29 modelweigts /assets/?unstable_path=.%2Fassets%2Fmodel%2Fnewtest/group1-shard1of1.bin

when doing console.log(modeljsobn, modelJson) and console.log(modelweigts, weightAssets); in the code snippet above.

then, the error: rankview.tsx:36 Error loading model: Error: modelWeightsID must be a number or number array.
Have you wrapped your asset paths in a require() statements?

Describe the expected behavior
the model should be loading as per the code snippet.

Standalone code to reproduce the issue
I am providing the two files .json and .bin to load the model in expo app as a zipped file archive.zip

Archive.zip

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions