React Native, Tensorflow js, load model from local storage

685 views Asked by At

I'm trying to develop a mobile application that should be able to load a tensorflow js model and then provide predictions to the final user. Everything works as intended when the required files for the model to work (model.json, weights.bin) are bundled into the application as assets, the code I'm using to load such files looks as follows:

import { fetch, bundleResourceIO } from "@tensorflow/tfjs-react-native";
import * as tf from "@tensorflow/tfjs";
modelWeights = require("./assets/explain_model/weights.bin");
modelFile = require("./assets/explain_model/model.json");
    model = await tf.loadGraphModel(
      bundleResourceIO(modelFile, modelWeights)
    )
    console.log('model loaded...')

Later on, I aim to store both files externally and then download them i.e, both files weights.bin and model.json are uploaded to a web server and then downloaded to the application by using react-native's fetch-blob library when the application runs. Both files are getting stored in the DocumentDir path, for example /data/user/0/com.tensorflowexample/files/explain-weights.bin

So I would like to load the model from local storage, I saw 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?, if not, how could I use AsyncStorage to copy both files weights.bin and model.json from local storage to AsyncStorage in order to use asyncStorageIO

Thank you so much!

0

There are 0 answers