【问题标题】:How to extract all timestamps of badminton shot sound in an audio clip using Neural Networks?如何使用神经网络提取音频剪辑中羽毛球击球声音的所有时间戳?
【发布时间】:2022-12-12 06:36:43
【问题描述】:

我试图在从羽毛球比赛中获取的源音频文件中找到实例,其中一名球员击球。出于同样的目的,我用正面(命中声音)和负面(没有命中声音:评论/人群声音等)标签标记了时间戳,如下所示:

shot_timestamps = [0,6.5,8, 11, 18.5, 23, 27, 29, 32, 37, 43.5, 47.5, 52, 55.5, 63, 66, 68, 72, 75, 79, 94.5, 96, 99, 105, 122, 115, 118.5, 122, 126, 130.5, 134, 140, 144, 147, 154, 158, 164, 174.5, 183, 186, 190, 199, 238, 250, 253, 261, 267, 269, 270, 274] 
shot_labels = ['no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no','no','no', 'no', 'yes', 'yes', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no', 'no', 'no', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'yes', 'yes', 'no', 'no', 'yes', 'yes', 'no'] 

我一直在这些时间戳周围使用 1 秒窗口,如下所示:

rate, source = wavfile.read(source) 
def get_audio_snippets(shot_timestamps): 

    shot_snippets = []  # Collection of all audio snippets in the timestamps above 

    for timestamp in shot_timestamps: 
        start = math.ceil(timestamp*rate)
        end = math.ceil((timestamp + 1)*rate)
        if start >= source.shape[0]: 
            start = source.shape[0] - 1

        if end >= source.shape[0]: 
            end = source.shape[0] - 1  

        shot_snippets.append(source[start:end]) 
        
    return shot_snippets

并将其转换为模型的频谱图图像。该模型似乎没有学到任何准确率在 50% 左右的东西。我可以做些什么来改进模型?

编辑:

音频文件:Google Drive

时间戳标签:Google Drive

代码:Github

这些时间戳是最近制作的,并没有在上面的代码中使用,因为我不完全知道用于标记目的的窗口大小。上面的注释文件包含击球的所有时间戳。

PS:还按照推荐在 Data Science Stackexchange 上添加了这个:https://datascience.stackexchange.com/q/116629/98765

【问题讨论】:

  • 你是如何进行频谱图转换的?当您绘制是/否类别的频谱图(比如每个 10 个)时,数据看起来如何?
  • 模型看起来如何,训练完成了吗?
  • 你能提供与注释相匹配的音频文件吗?
  • @JonNordby 感谢您的宝贵时间。我已经用您在此处要求的大部分信息更新了问题。确切的代码可以在 Github 存储库中的 (3.1) 文件编号中找到。

标签: python machine-learning audio deep-learning librosa


【解决方案1】:

检测特定声音何时发生被称为声音事件检测(SED)。这个主题有多种方法,因为它已经被积极研究了几十年。

您现有的解决方案,在波形域中使用一些模板声音的相关性不太可能很好地完成这项任务。这是因为一场比赛中羽毛球击球声音之间的变化量可能非常高。

推荐的方法是收集一个小数据集,并使用监督学习来学习检测器。例如,从 20 场不同的比赛中获取数据(最好使用不同的记录设置等),然后根据时间段对每个短片进行注释,以从每场比赛中获得至少 50 次投篮。

使用深度学习的声音事件检测

可以在Sound Event Detection: A Tutorial 中找到对现代深度学习方法的描述。它描述了所需的部分:

  • 使用对数标度的梅尔频谱图进行音频预处理
  • 将频谱图拆分为固定长度的重叠窗口
  • 使用卷积循环神经网络 (CRNN) 的模型架构
  • 使用时间序列(事件激活)作为神经网络的输出/目标
  • 将连续事件激活后处理为离散事件
  • 使用基于事件的指标评估模型性能

可以在 this notebook 中找到一个完整的实现,使用音频和您注释的匹配项的标签。

我在这里复制了一些关键代码,以供后代使用。

SEDNet模型

def build_sednet(input_shape, filters=128, cnn_pooling=(5, 2, 2), rnn_units=(32, 32), dense_units=(32,), n_classes=1, dropout=0.5):
    """
    SEDnet type model
    Based https://github.com/sharathadavanne/sed-crnn/blob/master/sed.py
    """
    from tensorflow.keras import Model
    from tensorflow.keras.layers import Input, Bidirectional, Conv2D, BatchNormalization, Activation, 
            Dense, MaxPooling2D, Dropout, Permute, Reshape, GRU, TimeDistributed
    
    spec_start = Input(shape=(input_shape[-3], input_shape[-2], input_shape[-1]))
    spec_x = spec_start
    for i, pool in enumerate(cnn_pooling):
        spec_x = Conv2D(filters=filters, kernel_size=(3, 3), padding='same')(spec_x)
        spec_x = BatchNormalization(axis=1)(spec_x)
        spec_x = Activation('relu')(spec_x)
        spec_x = MaxPooling2D(pool_size=(1, pool))(spec_x)
        spec_x = Dropout(dropout)(spec_x)
    spec_x = Permute((2, 1, 3))(spec_x)
    spec_x = Reshape((input_shape[-3], -1))(spec_x)

    for units in rnn_units:
        spec_x = Bidirectional(
            GRU(units, activation='tanh', dropout=dropout, recurrent_dropout=dropout, return_sequences=True),
            merge_mode='mul')(spec_x)

    for units in dense_units:
        spec_x = TimeDistributed(Dense(units))(spec_x)
        spec_x = Dropout(dropout)(spec_x)
    spec_x = TimeDistributed(Dense(n_classes))(spec_x)

    out = Activation('sigmoid', name='strong_out')(spec_x)
    model = Model(inputs=spec_start, outputs=out)
    return model

首先尝试使用具有适度参数的低复杂度模型。

model = build_sednet(input_shape, n_classes=1,
                         filters=10,
                         cnn_pooling=[2, 2, 2],
                         rnn_units=[5, 5],
                         dense_units=[16],
                         dropout=0.1)

使用训练好的模型

def merge_overlapped_predictions(window_predictions, window_hop):
    
    # flatten the predictions from overlapped windows
    predictions = []
    for win_no, win_pred in enumerate(window_predictions):
        win_start = window_hop * win_no
        for frame_no, p in enumerate(win_pred):
            s = {
                'frame': win_start + frame_no,
                'probability': p,
            }
        
            predictions.append(s)
        
    df = pandas.DataFrame.from_records(predictions)
    df['time'] = pandas.to_timedelta(df['frame'] * time_resolution, unit='s')
    df = df.drop(columns=['frame'])
    
    # merge predictions from multiple windows 
    out = df.groupby('time').median()
    return out

def predict_spectrogram(model, spec):
    
    # prepare input data. NOTE: must match the training preparation in getXY
    window_hop = 1
    wins = compute_windows(spec, frames=window_length, step=window_hop)       
    X = numpy.expand_dims(numpy.stack( [ (w-Xm).T for w in wins ]), -1)
    
    # make predictions on windows
    y = numpy.squeeze(model.predict(X, verbose=False))
    
    out = merge_overlapped_predictions(y, window_hop=window_hop)

    return out

【讨论】:

  • 因此,您实际上是在建议建立一种 CNN,将频谱图图像作为输入,并将手动注释作为标签用于训练目的?然后使用这个模型来提取特定匹配中的所有时间戳?
  • 是的,这是一个很好的通用方法。 CNN 应该处理短时间窗口,足以包含感兴趣的事件,而不是更多。标签将是此窗口内是否存在事件。
  • 我有最后一个问题:即使你建议从每场比赛中拍摄 50 次,我也必须拍摄更多的时间窗口,并且还需要捕获事件未发生的时间窗口以进行训练,对吗?
  • 是的,您还需要“负面”数据。所以选择一些较长的时间段(比如 5 分钟),然后完成所有这些。标记那个时期所有感兴趣的事件。然后该部分中没有注释的任何时间都隐含地“无事件”。不应在标记期间拆分为窗口,而应在培训期间进行。
  • 我试过这样做,但我几乎没有达到 50% 的准确率。关于如何提高准确性的任何想法?我一直在事件发生的时间戳周围拍摄 1 秒的窗口,并将其转换为模型的频谱图图像。该模型似乎没有学习任何东西。如果有帮助,我可以提供我的代码!
猜你喜欢
  • 2017-12-17
  • 2017-04-22
  • 2018-11-09
  • 2017-06-08
  • 2023-03-13
  • 2010-11-22
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多