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

tensorflow 语音识别

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

一些资料
https://medium.com/@ageitgey/machine-learning-is-fun-part-6-how-to-do-speech-recognition-with-deep-learning-28293c162f7a
https://distill.pub/2017/ctc/

python中关于语音处理的库

  • scipy.io.wavfile
  • python_speech_features
  • librosa : 读取各种格式的音频;特征提取

  • 读取wav文件

import scipy.io.wavfile as wav
fs, audio = wav.read(file_name)
  • 对读取的音频信息求MFCC(Mel频率倒谱系数)
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做一些说明.

raw_audio_data = sound.raw_data 

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

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喂数据呢?

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 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搭建一个简单的语音识别应用了。

一个小 DEMO

参考资料

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_

  • 16
    点赞
  • 122
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
语音识别是一种将音频转换为可读文本的技术。在TensorFlow中,可以使用一些方法来搭建语音识别系统。首先,需要将音频转换为特征矩阵,通常可以使用频谱图来表示音频信号。可以使用TensorFlow的信号处理tf.signal来进行短时傅里叶变换(STFT)来创建频谱图。具体的代码可以参考引用\[1\]中的示例。然后,可以将特征矩阵输入到神经网络中进行训练和识别。可以使用TensorFlow的神经网络模块来构建和训练语音识别模型。关于如何使用TensorFlow编写一个基本的端到端自动语音识别系统,可以参考引用\[3\]中的文章。在实际应用中,还可以使用一些公开的语料样本来进行训练和测试,比如清华大学公开的语料样本thchs30,可以从引用\[2\]中提供的下载地址获取相关数据集。通过学习和交流,可以进一步提升语音识别的技术水平。 #### 引用[.reference_title] - *1* *3* [语音识别第一课:基于Tensorflow的端到端语音识别技术](https://blog.csdn.net/duxinshuxiaobian/article/details/102299211)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [tensorflow学习笔记语音识别](https://blog.csdn.net/mingyue_ruhuai/article/details/89556968)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值