tfjs_layers_model 和 tfjs_graph_model 之间的区别

Difference between tfjs_layers_model and tfjs_graph_model

tensorflowjs converter 具有输出格式

tfjs_layers_model, tfjs_graph_model

两者有什么区别?有使用推荐吗?

似乎只有某些输入和输出格式对有效,namely

`keras` | `tfjs_layers_model` 
`keras_saved_model` | `tfjs_layers_model`
`tf_hub` | `tfjs_graph_model`
`tf_saved_model` | `tfjs_graph_model`

关于这个的信息似乎分散在下面链接的几个 repos 和网站上,所以我已经完成了侦探工作。


2 种 TFJS 格式

TensorFlow.js 层模型: JSON + 二进制权重文件,具有有限的 (Keras) 特征。在这种情况下,权重似乎是可选的。 而来自 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 图模型: JSON + 二进制权重文件,转换 to/ from SavedModel, but no training capability. About the graph model, the README 说:

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.

其中 JSON 文件包含:

  • 元数据(格式:'graph-model',convertedBy:“TensorFlow.js Converter v1.1.2”,generatedBy:“2.0.0-dev20190603”)
  • modelTopology:描述所有节点(Relu、Conv2D 偏差、Conv2D 权重)以及它们如何相互关联。
  • weightsManifest:权重文件可以分成多个文件(例如 group1-shard1of2.bingroup1-shard2of2.bin 或对于 ResNet,group1-shard9of12.bin

什么时候应该保存到图层模型?

从来没有!始终保存到 SavedModel,如果您需要 TFJS,则转换为图形模型。在 Tensorflow 2 中,所有内容都可以保存到 SavedModel 中,这些不能转换为层模型(只是不支持),只能转换为 图模型。您也更有可能在 Internet 上找到 SavedModel,而不是 keras_saved_model。 (这是 YouTube 上 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 的标准格式。

在 Keras 可以输出到 SavedModel 之前,我认为图层格式是 'go-to' 格式。现在,您只需保存到 SavedModel 并将模型转换为图形模型格式。 Google 发布的 tensorflow.js 模型似乎也都是图形模型格式。我找不到一层模型。

TensorFlow.js Layers currently only supports Keras models using standard Keras constructs. source, and example usage of layer models

不幸的是,这些图模型无法变回.tflite。如果有人知道怎么做,请告诉我!