繁体   English   中英

Tensorflow:如何在java中使用在python中训练的语音识别模型

[英]Tensorflow : How to use speech recognition model trained in python in java

我按照这篇文章在 python 中训练了一个 tensorflow 模型 训练后,我生成了冻结图。 现在我需要使用这个图并在基于JAVA的应用程序上生成识别。 为此,我正在查看以下示例 但是我不明白是如何收集我的输出。 我知道我需要为图表提供 3 个输入。

从官方教程中给出的示例中,我阅读了基于 python 的代码。

def run_graph(wav_data, labels, input_layer_name, output_layer_name,
              num_top_predictions):
  """Runs the audio data through the graph and prints predictions."""
  with tf.Session() as sess:
    # Feed the audio data as input to the graph.
    #   predictions  will contain a two-dimensional array, where one
    #   dimension represents the input image count, and the other has
    #   predictions per class
    softmax_tensor = sess.graph.get_tensor_by_name(output_layer_name)
    predictions, = sess.run(softmax_tensor, {input_layer_name: wav_data})

    # Sort to show labels in order of confidence
    top_k = predictions.argsort()[-num_top_predictions:][::-1]
    for node_id in top_k:
      human_string = labels[node_id]
      score = predictions[node_id]
      print('%s (score = %.5f)' % (human_string, score))

    return 0

有人可以帮我理解tensorflow java api吗?

您上面列出的 Python 代码的字面翻译将是这样的:

public static float[][] getPredictions(Session sess, byte[] wavData, String inputLayerName, String outputLayerName) {
  try (Tensor<String> wavDataTensor = Tensors.create(wavData);
       Tensor<Float> predictionsTensor = sess.runner()
                    .feed(inputLayerName, wavDataTensor)
                    .fetch(outputLayerName)
                    .run()
                    .get(0)
                    .expect(Float.class)) {
    float[][] predictions = new float[(int)predictionsTensor.shape(0)][(int)predictionsTensor.shape(1)];
    predictionsTensor.copyTo(predictions);
    return predictions;
  }
}

返回的predictions数组将具有每个预测的“置信度”值,您必须运行逻辑来计算其上的“前 K”,类似于 Python 代码使用 numpy ( .argsort() )的方式对sess.run()返回的内容执行此操作。

从对教程页面和代码的粗略阅读来看, predictions似乎有 1 行和 12 列(每个热门词一个)。 我从以下 Python 代码中得到了这个:

import tensorflow as tf

graph_def = tf.GraphDef()
with open('/tmp/my_frozen_graph.pb', 'rb') as f:
  graph_def.ParseFromString(f.read())

output_layer_name = 'labels_softmax:0'

tf.import_graph_def(graph_def, name='')
print(tf.get_default_graph().get_tensor_by_name(output_layer_name).shape)

希望有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM