语音命令识别android demo提供的tflite模型"conv_actions_tflite"是如何转换的?

How does the tflite model "conv_actions_tflite" , provided by speech command recognition android demo has been converted?

我使用精简版转换器在终端中将我的 pb format 模型转换为 tflite format,但效果不佳。

但是当我使用speech command android demo提供的tflite model时,效果很好。所以我想知道这个模型是怎么转换的?

https://github.com/tensorflow/docs/blob/master/site/en/r1/tutorials/sequences/audio_recognition.md

使用上面的 link 我用下面的命令训练了模型

(base) unizen@admin:~/tensorflow/tensorflow/examples/speech_commands$ python train.py

训练后保存模型时,我使用以下代码创建了冻结模型

(base) unizen@admin:~/tensorflow/tensorflow/examples/speech_commands$ python freeze.py \
--start_checkpoint=/tmp/speech_commands_train/conv.ckpt-18000 \
--output_file=/tmp/my_frozen_graph.pb

但是当我尝试将 .pb format 转换为 tflite format

(base) unizen@admin:~/tensorflow/tensorflow/examples/speech_commands$  tflite_convert \
--saved_model_dir  /home/unizen/Downloads/my_frozen_graph.pb \
--input_format TENSORFLOW_GRAPHDEF \
--input_arrays decoded_sample_data \
--input_shapes 16000,1 \
--output_arrays labels_softmax \
--output_format TFLITE \
--output_file /tmp/sprc.tflite \
--allow_custom_ops

错误是

(base) unizen@admin:~/tensorflow/tensorflow/examples/speech_commands$ python usage: tflite_convert [-h] --output_file OUTPUT_FILE
                      (--saved_model_dir SAVED_MODEL_DIR | --keras_model_file KERAS_MODEL_FILE)
tflite_convert: error: one of the arguments --saved_model_dir --keras_model_file is required.

请提供frozen模型转tflite模型的解决方案

这个

tflite_convert: error: one of the arguments --saved_model_dir --keras_model_file is required.

表示您正在使用 tensorflow >= 2.0.0.
自 2.0.0 以来不再使用冻结图 (.pb),开发人员应将其模型保存为 "saved models" 或 keras 模型,因此 tflite_convert 命令不再支持它。
但是如果你安装了 tensorflow 1.15 你应该能够像这样转换它:

tflite_convert
--output_file=/output.tflite
--graph_def_file /path/to/my_frozen_graph.pb \
--input_arrays decoded_sample_data,decoded_sample_data:1 \
--output_arrays labels_softmax \
--allow_custom_ops

或者,如果您不想安装 tensorflow 1.15,只需使用 python API 和 tf.compat.v1:

import tensorflow as tf

converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph("./conv_actions_frozen.pb", input_arrays=['decoded_sample_data', 'decoded_sample_data:1'], output_arrays=['labels_softmax'])
converter.allow_custom_ops=True
tflite_model = converter.convert()
open("output.tflite", "wb").write(model)