XYZCODE123
XYZCODE123

Reputation: 372

Loading tensorfow model from native file system using TensorflowJS 2.0.1

Am trying to load a pre-trained Keras model to my small react-App. Since with the 2.0 version of TensorFlow, few things were added and changed. I would like to know how one should load the model from the native file system.

  1. First I import tensorflowJS
import * as tf from "@tensorflow/tfjs";
  1. My directory structure model.json and all .bin files required are located in the same directory as my App.js from which am referring to them.

  2. Load the pre-trained model - docs

    model = await tf.loadLayersModel("file://model.json");

Unfortunately am getting failed to fetch error. Can anyone explain me what am I missing?

Upvotes: 1

Views: 1097

Answers (2)

saadi
saadi

Reputation: 1

expo projects doesnt support such structure.

  1. save your model folder in assets
  2. import liabrary "expo-asset" from expo documentatio
  3. then call your code as

const modelJson = Asset.fromModule(require('./assets/model/model.json'))

const modelWeights = Asset.fromModule(require('./assets/model/weights.bin'))

Upvotes: 0

yudhiesh
yudhiesh

Reputation: 6799

You have to set the path to the model weights and JSON using require()

const modelJSON = require("../model/model.json");

const modelWeights = require("../model/group1-shard1of1.bin");

const model = await tf.loadLayersModel(bundleResourceIO(modelJSON, modelWeights))

Edit in the case of you having multiple bin files you can only use one bin file with one model.json

The code below converts your keras model to a single bin file and model.json

tensorflowjs_converter --input_format keras --weight_shard_size_bytes 60000000 path_to_model.h5 path_to_save

Upvotes: 3

Related Questions