Load only weights for NodeJS

I converted a model from Keras to be used in NodeJS. Currently, this model sets modelTopology: null, presumably because the file is the weights only.

I supposed it is possible to just write the network myself and load the weights only, using LayersModel.loadWeights (this is missing from the documentation afaik but the autocompletion shows it, and the source code as well.) however the weights themselves (currently binary) must be JSON, and I don’t see much information how to convert the binary weights to JSON so that they can be loaded.

Any help on this please?

Hi @Mah_Neh ,

To load binary weights into a Node.js LayersModel instance, you need to:

  • Read the binary weights file using tf.io.readFromFile.
  • Decode the binary data into a tensor using tf.io.decodeRaw.
  • Convert the tensor to a Base64 string using tf.io.encodeBase64.
  • Create a JSON object containing the Base64 string.
  • Load the weights into the LayersModel instance using model.loadWeights.

Here is demo gist you can refer you can adjust the parameters as per your use case ,

Hope this helps ,

Thank you