最近在做语音识别的项目,现在项目告一段落,就把最近碰到的东西做一个总结。
import scipy.io.wavfile as wav
fs, audio = wav.read(file_name)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)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做进一步操作了这个部分包括了SparseTensor, sparse_tensor_to_dense,edit_distance。
int64 Tensor,shape为(N, ndims),指定了sparse tensor中的索引, 例如: indices=[[1,3], [2,4]]说明,dense tensor中对应索引为[1,3], [2,4]位置的元素的值不为0.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.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转换
把一个SparseTensor转化为DenseTensor.
SparceTensor.True的话,将会检查sp_input的indices的lexicographic order和是否有重复.计算序列之间的Levenshtein 距离
SparseTensor,包含序列的假设.SparseTensor, 包含真实序列.True的话,求出来的Levenshtein距离除以真实序列的长度. 默认为Trueoperation 的名字,可选.返回值:
返回值是一个R-1维的DenseTensor.包含着每个Sequence的Levenshtein 距离.
SparseTensor所对应的DenseTensor是一个多维的Tensor,最后一维看作序列.
现在用深度学习做语音识别,基本都会在最后一层用CTCloss,这个loss自己实现起来还是有点费劲,不过,幸运的是,tensorflow中已经有现成的API了,我们只需调用即可。
此函数用来计算ctc loss.
int32的SparseTensor, labels.indices[i, :] == [b, t] 表示 labels.values[i] 保存着(batch b, time t)的 id.3D Tensor (max_time * batch_size * num_classes).保存着 logits.(通常是RNN接上一个线性神经元的输出)size为 [batch_size]. 序列的长度.此 sequence_length 和用在dynamic_rnn中的sequence_length是一致的,使用来表示rnn的哪些输出不是pad的.True的话,tensorflow会对输入的labels进行预处理,连续重复的会被合成一个.返回值:
一个 1-D float Tensor, size 为 [batch], 包含着负的 logp\text{log}p.加起来即为batch loss.
上面的函数是用在训练过程中,专注与计算loss,此函数是用于inference过程中,用于解码.
3D Tensor (max_time * batch_size * num_classes).保存着 logits.(通常是RNN接上一个线性神经元的输出)size为 [batch_size]. 序列的长度.此 sequence_length 和用在dynamic_rnn中的sequence_length是一致的,使用来表示rnn的哪些输出不是pad的.返回值:
一个tuple (decoded, log_probabilities)
list. decoded[0]是一个SparseTensor,保存着解码的结果.(total_decoded_outputs * 2),每行中保存着[batch, time ].size为 (total_decoded_outputs).向量中保存的是解码的类别.Tensor的shape, size为(2).shape的值为[batch_size, max_decoded_length].(batch_size*1)包含着序列的log 概率.另一种寻路策略。和上面那个差不多。
知道这些,就可以使用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_