The tensorflowjs converter
has the output formats
tfjs_layers_model
,tfjs_graph_model
What is the difference between the two? Is there a use recommendation?
The tensorflowjs converter
has the output formats
tfjs_layers_model
,tfjs_graph_model
What is the difference between the two? Is there a use recommendation?
It seems like information about this is fragmented all over a few repos and websites linked below, so I have done the detective work 🙂.
TensorFlow.js Layers model: JSON + binary weight file(s), with limited (Keras) features. The weights seem to be optional in this case. And from the Tensorflow JS docs,
This mode is not applicable to TensorFlow SavedModels or their converted forms. For those models, use tf.loadGraphModel(). The loaded model supports the full inference and training (e.g., transfer learning) features of the original keras or tf.keras model.
TensorFlow.js graph model: JSON + binary weight file(s), with conversion to/ from SavedModel, but no training capability. About the graph model, the README says:
The loaded model supports only inference, but the speed of inference is generally faster than that of a tfjs_layers_model (see above row) thanks to the graph optimization performed by TensorFlow. Another limitation of this conversion route is that it does not support some layer types (e.g., recurrent layers such as LSTM) yet.
where the JSON file contains:
group1-shard1of2.bin
, group1-shard2of2.bin
or for ResNet, group1-shard9of12.bin
Never! Always save to a SavedModel, and convert into a graph model if you need it for TFJS. In Tensorflow 2, everything can be saved into a SavedModel, and these can't be converted to a layers model (its just not supported), but only a graph model. You're also more likely to find a SavedModel on the internet, as opposed to a keras_saved_model
. (it is the standard format for TFHub). Keep it simple, save to SavedModel, and convert to graph model if needed in TFJS, The TensorFlow team seem to recommend using SavedModel too, as per this slide on YouTube.
I would think the layers format was the 'go-to' format before Keras could output to SavedModel. Now, you can just save to a SavedModel and convert the model to the graph model format. It also seems like tensorflow.js models published by Google are all in the graph model format. I was not able to find one layers model.
TensorFlow.js Layers currently only supports Keras models using standard Keras constructs. source, and example usage of layer models
The unfortunate thing is, these graph models cannot be turned back into .tflite
. If someone knows how, let me know!
There seem to be only certain pairs of input and output formats that work, namely
`keras` | `tfjs_layers_model`
`keras_saved_model` | `tfjs_layers_model`
`tf_hub` | `tfjs_graph_model`
`tf_saved_model` | `tfjs_graph_model`