将子类语音识别模型转换为 Tensorflow.js

Convert Subclassed Speech Recognition Model to Tensorflow.js

我有一个子类语音识别模型 (link),我想用它在我的 node.js 服务器上进行推理。我正在尝试使用 tfjs 转换它,但因为它是一个子类模型,所以我收到以下错误:

NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable. Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using `save_weights`.

我正在关注official tutorial, which doesn't count this scenario in. And surprisingly I couldn't find much info on the web apart from a closed issue

关于如何将子类模型转换为 tensorflowjs 的任何想法?

您应该使用另一个选项 model.save("my_model_dir"),而不是像教程中那样使用 model.save_weights(),您可以在此处查看 confirmation

保存模型目录后,您需要使用 Tensorflowjs 转换器对其进行转换

$ tensorflowjs_converter \
    --input_format=tf_saved_model \
    my_model_dir \ # input_path 
    converted_model # output_path

好的,所以我专门尝试转换语音识别模型(上面的 link),但目前 tfjs (including mozilla's deepspeech 似乎不支持大多数此类模型)。

它会专门抛出这个错误:

ValueError: Unsupported Ops in the model before optimization
AudioSpectrogram

本例中使用的命令是:

tensorflowjs_converter path/to/qnet15/ path/to/qnet15/converted/ --input_format=tf_saved_model --output_format=tfjs_graph_model

但是,可以通过在上述命令中添加 --skip_op_check 标志来消除此错误。在一系列警告后,它将生成预期的 model.json 及其相应的权重二进制文件。

但是,如果您尝试推断@node,则会发生相同的错误:

Promise {
  <rejected> TypeError: Unknown op 'AudioSpectrogram'. File an issue at https://github.com/tensorflow/tfjs/issues so we can add it, or register a custom execution with tf.registerOp()

模型基本没用。几年前就有了此功能的 open issue