It seems like information about this is fragmented all over a few repos and websites linked below, so I have done the detective work ??.
2 Formats for TFJS
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:
- metadata (format: 'graph-model', convertedBy: "TensorFlow.js Converter v1.1.2", generatedBy: "2.0.0-dev20190603")
- modelTopology: Describes all the nodes (Relu, Conv2D bias, Conv2D weights) and how they relate to each other.
- weightsManifest: Weight files can be broken up into multiple files (e.g.
group1-shard1of2.bin
, group1-shard2of2.bin
or for ResNet, group1-shard9of12.bin
When should you save to a layers model?
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!