2

I'm trying to get a keras model I converted to tensorflowJS to work in react native but getting the error in predicting the image tensor.

I have trained a custom tensorflow keras model and converted the keras h5 model to JavaScript JSON model. In React Native project, I have used expo camera with tensorflow to capture the image to predict the damage on the captured image.

Here, I have included the versions use in my projects, commands used to convert and predict the model.

Versions used

  • React Native Project "expo": ">=45.0.0-0 <46.0.0", "expo-camera": "~12.2.0", "expo-gl": "~11.3.0", "expo-gl-cpp": "~11.3.0", "@tensorflow/tfjs": "^4.0.0", "@tensorflow/tfjs-react-native": "^0.8.0", "react-native-fs": "^2.20.0",

  • Python

    "tensorflow Version" : 2.10.0

Command used to convert Keras h5 to JS json :

tensorflowjs_converter --input_format=keras --weight_shard_size_bytes=419430400 --quantize_float16=* /path/to/model.h5 /path/to/output

Creating and using Layers model in RN

 // Load layers model using model json and weights file
 const models = await tf.loadLayersModel(bundleResourceIO(modelJSON, weights));

Logs of Capturing Image via expo camera and cameraWithTensors

 LOG  imageAsTensors:  {"kept":false,"isDisposedInternal":false,"shape":[224,224,3],"dtype":"int32","size":150528,"strides":[672,3],"dataId":{"id":670},"id":980,"rankType":"3"}
 LOG  imageTensorReshaped:  {"kept":false,"isDisposedInternal":false,"shape":[1,224,224,3],"dtype":"int32","size":150528,"strides":[150528,672,3],"dataId":{"id":670},"id":981,"rankType":"4","scopeId":408}

Predicting image tensor using model

try {
      // predict against the model
      const output = await models.predict(imageTensorReshaped, { batchSize: 1 });
      return output.dataSync();
    } catch (error) {
      console.log('Error predicting from tensor image', error);
    }

Receiving the below error in catch Error predicting from tensor image [TypeError: null is not an object (evaluating 'opHandler.clone')]

Expectations : Prediction result array

1 Answer 1

1

The issue is that the setOpHandler function is not being called in the tfjs-node files in the package. You can check out this issue on github here! for a temporary fix using the provided patch files

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.