Search code examples
javascripttensorflowdeploymenttensorflow.js

Why loadGraphModel function from tensorflow.js not working?


I am working on deploy an ML that I trained using tensorflow (in Python). The model is saved as an .h5 file. After converting the model using the tensorflowjs_converter --input_format=keras ./model/myFile.h5 /JS_model/ command.

I imported the tensorflow library using the following: <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>

After this, I ws able to load the model using the loadLayersModel() function. However, when using the loadGraphModel, it does not work. It outputs this error on the browser: 'enter image description here'

This is my code for the outputed error

I also tried using the tf.models.save_model.save() function in python which it outputs the variables and assets folders, as well as the .pb file. However, an error still occurs. Using the code above, changing only the path to 'THE_classifier' (which is the name of the folder where asset, variables and the .pb is located), the output is: enter image description here

I want to work with the loadGraphModel() function because according to various sources, it provides a faster inference time.


Solution

  • layers models and graph models have different internal layout, they are not compatible and interchangable. if its a layers model, it must be loaded with tf.loadLayersModel and if its a graph model, it must be loaded with tf.loadGraphModel

    graph models are frozen models - so if you want to convert keras model to graph, you need to freeze it first, else it can only be converted to layers model

    (and thats where difference in inference time comes from - its faster to evaluate a frozen model than one that is still using variables)