前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow学习笔记(四十):tensorflow语音识别 及 python音频处理库

tensorflow学习笔记(四十):tensorflow语音识别 及 python音频处理库

作者头像
ke1th
发布2018-01-02 11:45:14
3.6K0
发布2018-01-02 11:45:14
举报

tensorflow 语音识别

最近在做语音识别的项目,现在项目告一段落,就把最近碰到的东西做一个总结。

python中关于语音处理的库

  • scipy.io.wavfile
  • python_speech_features
  • 读取wav文件
代码语言:javascript
复制
import scipy.io.wavfile as wav
fs, audio = wav.read(file_name)
  • 对读取的音频信息求MFCC(Mel频率倒谱系数)
代码语言:javascript
复制
from python_speech_features import mfcc
from python_speech_features import delta
#求MFCC
processed_audio = mfcc(audio, samplerate=fs)
#求差分(一阶,二阶)
delta1 = delta(processed_audio, 1)
delta2 = delta(processed_audio, 2)

pydub

github 项目地址 有了这个库,做音频的数据增强就容易多了.关于使用方法可以阅读 github上的文档,这里只对raw_data做一些说明.

代码语言:javascript
复制
raw_audio_data = sound.raw_data 

raw_audio_data 中包含的是 音频数据的bytestring,但是如果我们想对音频数据做MFCC,那么我们应该怎么办呢?

代码语言:javascript
复制
audio = np.fromstring(raw_audio_data, dtype=np.int16)
#此时audio是一个一维的ndarray,如果音频是双声道,
#我们只需要对其进行reshape就可以了
audio = np.reshape(audio, [-1, 2]) 

# 然后就可以使用python_speech_features做进一步操作了

tensorflow中做语音识别会碰到的API

这个部分包括了SparseTensor, sparse_tensor_to_dense,edit_distance

SparseTensor(indices, values, dense_shape)

  • indices: 一个2D的 int64 Tensor,shape为(N, ndims),指定了sparse tensor中的索引, 例如: indices=[[1,3], [2,4]]说明,dense tensor中对应索引为[1,3], [2,4]位置的元素的值不为0.
  • values: 一个1D tensor,shape(N)用来指定索引处的值. For example, given indices=[[1,3], [2,4]], the parameter values=[18, 3.6] specifies that element [1,3] of the sparse tensor has a value of 18, and element [2,4] of the tensor has a value of 3.6.
  • dense_shape: 一个1D的int64 tensor,形状为ndims,指定dense tensor的形状.

相对应的有一个tf.sparse_placeholder,如果给这个sparse_placeholder喂数据呢?

代码语言:javascript
复制
sp = tf.sparse_placeholder(tf.int32)

with tf.Session() as sess:
  #就这么喂就可以了
  feed_dict = {sp:(indices, values, dense_shape)}

tensorflow中目前没有API提供denseTensor->SparseTensor转换

tf.sparse_tensor_to_dense(sp_input, default_value=0, validate_indices=True, name=None)

把一个SparseTensor转化为DenseTensor.

  • sp_input: 一个SparceTensor.
  • default_value:没有指定索引的对应的默认值.默认为0.
  • validate_indices: 布尔值.如果为True的话,将会检查sp_inputindiceslexicographic order和是否有重复.
  • name: 返回tensor的名字前缀.可选.

tf.edit_distance(hypothesis, truth, normalize=True, name=’edit_distance’)

计算序列之间的Levenshtein 距离

  • hypothesis: SparseTensor,包含序列的假设.
  • truth: SparseTensor, 包含真实序列.
  • normalize: 布尔值,如果值True的话,求出来的Levenshtein距离除以真实序列的长度. 默认为True
  • name: operation 的名字,可选.

返回值: 返回值是一个R-1维的DenseTensor.包含着每个SequenceLevenshtein 距离.

SparseTensor所对应的DenseTensor是一个多维的Tensor,最后一维看作序列.

CTCloss

现在用深度学习做语音识别,基本都会在最后一层用CTCloss,这个loss自己实现起来还是有点费劲,不过,幸运的是,tensorflow中已经有现成的API了,我们只需调用即可。

tf.nn.ctc_loss(labels, inputs, sequence_length, preprocess_collapse_repeated=False, ctc_merge_repeated=True)

此函数用来计算ctc loss.

  • labels:是一个int32SparseTensor, labels.indices[i, :] == [b, t] 表示 labels.values[i] 保存着(batch b, time t)id.
  • inputs:一个3D Tensor (max_time * batch_size * num_classes).保存着 logits.(通常是RNN接上一个线性神经元的输出)
  • sequence_length: 1-D int32 向量, size[batch_size]. 序列的长度.此 sequence_length 和用在dynamic_rnn中的sequence_length是一致的,使用来表示rnn的哪些输出不是pad的.
  • preprocess_collapse_repeated:设置为True的话,tensorflow会对输入的labels进行预处理,连续重复的会被合成一个.
  • ctc_merge_repeated: 连续重复的是否被合成一个

返回值: 一个 1-D float Tensor, size[batch], 包含着负的 logp\text{log}p.加起来即为batch loss.

tf.nn.ctc_greedy_decoder(inputs, sequence_length, merge_repeated=True)

上面的函数是用在训练过程中,专注与计算loss,此函数是用于inference过程中,用于解码.

  • inputs:一个3D Tensor (max_time * batch_size * num_classes).保存着 logits.(通常是RNN接上一个线性神经元的输出)
  • sequence_length: 1-D int32 向量, size[batch_size]. 序列的长度.此 sequence_length 和用在dynamic_rnn中的sequence_length是一致的,使用来表示rnn的哪些输出不是pad的.

返回值: 一个tuple (decoded, log_probabilities)

  • decoded: 一个只有一个元素的哦list. decoded[0]是一个SparseTensor,保存着解码的结果.
    • decoded[0].indices: 索引矩阵,size为(total_decoded_outputs * 2),每行中保存着[batch, time ].
    • decoded[0].values: 值向量,size(total_decoded_outputs).向量中保存的是解码的类别.
    • decoded[0].shape: 稠密Tensorshape, size为(2).shape的值为[batch_size, max_decoded_length].
  • log_probability: 一个浮点型矩阵(batch_size*1)包含着序列的log 概率.

tf.nn.ctc_beam_search_decoder(inputs, sequence_length, beam_width=100, top_paths=1,merge_repeated=True)

另一种寻路策略。和上面那个差不多。

知道这些,就可以使用tensorflow搭建一个简单的语音识别应用了。

参考资料

https://www.tensorflow.org/api_docs/python/tf/nn/ctc_loss https://www.tensorflow.org/api_docs/python/tf/nn/ctc_greedy_decoder https://www.tensorflow.org/api_docs/python/tf/nn/ctc_beam_search_decoder http://stackoverflow.com/questions/38059247/using-tensorflows-connectionist-temporal-classification-ctc-implementation https://www.tensorflow.org/versions/r0.10/api_docs/python/nn/conectionist_temporal_classification__ctc_

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • tensorflow 语音识别
    • python中关于语音处理的库
      • pydub
        • tensorflow中做语音识别会碰到的API
          • SparseTensor(indices, values, dense_shape)
          • tf.sparse_tensor_to_dense(sp_input, default_value=0, validate_indices=True, name=None)
          • tf.edit_distance(hypothesis, truth, normalize=True, name=’edit_distance’)
        • CTCloss
          • tf.nn.ctc_loss(labels, inputs, sequence_length, preprocess_collapse_repeated=False, ctc_merge_repeated=True)
          • tf.nn.ctc_greedy_decoder(inputs, sequence_length, merge_repeated=True)
          • tf.nn.ctc_beam_search_decoder(inputs, sequence_length, beam_width=100, top_paths=1,merge_repeated=True)
        • 参考资料
        相关产品与服务
        语音识别
        腾讯云语音识别(Automatic Speech Recognition,ASR)是将语音转化成文字的PaaS产品,为企业提供精准而极具性价比的识别服务。被微信、王者荣耀、腾讯视频等大量业务使用,适用于录音质检、会议实时转写、语音输入法等多个场景。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档