1
votes

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?

1

1 Answers

2
votes

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