saved_model 的 tfjs 转换器问题 - 未保存权重并且 weights_manifest.json 只有输出节点

issue with tfjs converter for a saved_model - weights not saved and weights_manifest.json only has the output node

我正在努力弄清楚如何正确保存已保存为 saved_model 方法的 tensorflow 模型并将其转换为 tensorflow.js。当我目前转换它时,它最终得到一个 weights_manifest.json 只包含一组权重。

我要转换的模型来自这个jupyter notebook .RNN for Human Activity Recognition - 2D Pose Input.

可以在那个 jupyter notebook 中找到构建和训练图形的完整代码。

我可以成功运行代码:

save_path = "/content/gdrive/My Drive/saved_model/"

tf.saved_model.simple_save(sess,
  save_path,
  inputs={"x":x},
  outputs={"y": y})

通过查看图形构建代码,我不完全确定 output_node_names 应该在 tensorflowjs_converter 命令中,因为当我输入 y 时出现错误该节点不存在。我打印出所有图形节点并猜测它是 mul_1/y,因为这是图形构建代码中的内容:

def LSTM_RNN(_X, _weights, _biases):
    # model architecture based on "guillaume-chevalier" and "aymericdamien" under the MIT license.

    _X = tf.transpose(_X, [1, 0, 2])  # permute n_steps and batch_size
    # rest of graph building code left out for this post
    # Linear activation
    return tf.matmul(lstm_last_output, _weights['out']) + _biases['out']

当我运行命令时:

!tensorflowjs_converter \
    --input_format=tf_saved_model \
    --output_node_names="mul_1/y" \
    --saved_model_tags=serve \
    /content/gdrive/My\ Drive/saved_model/ \
    /content/gdrive/My\ Drive/web_model/

成功并显示此消息:

Using TensorFlow backend. 2019-02-23 01:46:53.475557: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA Writing weight file /content/gdrive/My Drive/web_model/tensorflowjs_model.pb...

然而,tensorflowjs_model.pb 文件只有 50 个字节,weights_manifest.json 中包含的全部内容是:

[{"paths": ["group1-shard1of1"], "weights": [{"name": "mul_1/y", "shape": [], "dtype": "int32"}]}]

我做错了什么?

output_node_names是你要return的节点名称。您可以为您的 tensorflow 操作命名,该名称将作为 output_name.

return tf.add(tf.matmul(lstm_last_output, _weights['out']), _biases['out'] , name="y")

然后您可以在转换器中使用 --output_node_names="y"

最后在 js 中执行您的代码

const model = await tf.loadFrozenModel(MODEL_URL, WEIGHTS_URL);

// define a tensor t
const y = model.execute({
    "x": t // x input_name of the tensorflow graph
  });
// it will return the output node y value