def prepare_eval_data(audio_id):
    file_path = os.path.join(ONBOARDING_DATA_PATH, "dnn2016_%d.pkl" % audio_id)
    with open(file_path, 'rb') as file_handler:
        data = pickle.load(file_handler)
        compact_window = np.array((
            data['zxx_log'],
            np.roll(data['zxx_log'], -1, axis=1),
            np.roll(data['zxx_log'], -2, axis=1),
            np.roll(data['zxx_log'], -3, axis=1),
            np.roll(data['zxx_log'], -4, axis=1),
            np.roll(data['zxx_log'], -5, axis=1),
            np.roll(data['zxx_log'], -6, axis=1),
            np.roll(data['zxx_log'], -7, axis=1),
            np.roll(data['zxx_log'], -8, axis=1),
            np.roll(data['zxx_log'], -9, axis=1),
            np.roll(data['zxx_log'], -10, axis=1),
            np.roll(data['zxx_log'], -11, axis=1),
            np.roll(data['zxx_log'], -12, axis=1),
            np.roll(data['zxx_log'], -13, axis=1),
            np.roll(data['zxx_log'], -14, axis=1),
            np.roll(data['zxx_log'], -15, axis=1),
        ))
        compact_window = np.swapaxes(
            np.swapaxes(np.swapaxes(compact_window, 0, 1), 0, 2), 1,
            2).reshape((-1, 64, 16))

        return th.from_numpy(compact_window).double(), rms_to_db(data['rms'])
Пример #2
0
def label_segmentation(label_list, decibel_lower_bound=0.0):
    df_list = []
    table_fine = pd.read_csv(os.path.join(BASE_PATH,
                                          './fine_grained_annotation.csv'),
                             index_col=0)

    for index, row in tqdm(label_list.iterrows(), total=label_list.shape[0]):
        record_label = row['label']
        audio_id = index
        filepath = os.path.join(DATA_PATH, "dnn2016_%d.pkl" % audio_id)
        try:
            with open(filepath, 'rb') as handler:
                data = pickle.load(handler)
                audio_length = data['zxx_log'].shape[1]
                decibel = rms_to_db(data['rms'])

                # COUGH
                if record_label:
                    # Load Fine Grained Data
                    record_list = table_fine.loc[
                        table_fine.coarse_grained_annotation_id == audio_id,
                        ['label', 'label_end', 'label_start']]
                    cough_list = record_list[record_list.label == 'Cough']

                    # Encode One-hot Array for the cough event
                    time_seq = np.repeat(False, audio_length)
                    for index, cough in cough_list.iterrows():
                        label_start = round(
                            cough['label_start'] * SR) // WINDOW_SIZE
                        label_end = round(
                            cough['label_end'] * SR) // WINDOW_SIZE
                        time_seq[label_start:label_end - 1] = True

                    # Sliding Window
                    padded = np.pad(time_seq, (0, 16), constant_values=(0, 0))
                    rolling_list = np.array((
                        padded,
                        np.roll(padded, -1),
                        np.roll(padded, -2),
                        np.roll(padded, -3),
                        np.roll(padded, -4),
                        np.roll(padded, -5),
                        np.roll(padded, -6),
                        np.roll(padded, -7),
                        np.roll(padded, -8),
                        np.roll(padded, -9),
                        np.roll(padded, -10),
                        np.roll(padded, -11),
                        np.roll(padded, -12),
                        np.roll(padded, -13),
                        np.roll(padded, -14),
                        np.roll(padded, -15),
                    ))

                    labels = np.sum(rolling_list, axis=0) / 16.0

                    df = pd.DataFrame({
                        "audio":
                        audio_id,
                        "window_index":
                        np.arange(0, labels[:-16 - 15].shape[0]),
                        "label":
                        labels[:-16 - 15],
                        "db":
                        decibel[:-15]
                    })

                    df = df[df.db >= decibel_lower_bound].drop(columns=['db'])

                    df_list.append(df)

                    # NON-COUGH
                else:
                    df = pd.DataFrame({
                        "audio":
                        audio_id,
                        "window_index":
                        np.arange(0, audio_length - 15),
                        "label":
                        0,
                        "db":
                        decibel[:-15]
                    })

                    df = df[df.db >= decibel_lower_bound].drop(columns=['db'])

                    df_list.append(df)

                    pass
        except Exception as e:
            pass

    return pd.concat(df_list, ignore_index=True)
Пример #3
0
def label_segmentation(label_list, decibel_lower_bound=0.0):
    df_list = []
    table_fine = pd.read_csv(os.path.join(BASE_PATH,
                                          './fine_grained_annotation.csv'),
                             index_col=0)

    for index, row in tqdm(label_list.iterrows(), total=label_list.shape[0]):
        record_label = row['label']
        audio_id = index
        filepath = os.path.join(DATA_PATH, "dnn2016_%d.pkl" % audio_id)
        try:
            with open(filepath, 'rb') as handler:
                data = pickle.load(handler)
                audio_length = data['zxx_log'].shape[1]
                decibel = rms_to_db(data['rms'])

                # COUGH
                if record_label:
                    # Load Fine Grained Data
                    record_list = table_fine.loc[
                        table_fine.coarse_grained_annotation_id == audio_id,
                        ['label', 'label_end', 'label_start']]
                    cough_list = record_list[record_list.label == 'Cough']

                    # Encode One-hot Array for the cough event
                    #time_seq = np.repeat(False, audio_length)
                    for index, cough in cough_list.iterrows():
                        label_start = round(
                            cough['label_start'] * SR) // WINDOW_SIZE
                        label_end = round(
                            cough['label_end'] * SR) // WINDOW_SIZE
                        #time_seq[label_start: label_end - 1] = True
                        label_end = audio_length if label_end > audio_length else label_end
                        df = pd.DataFrame({
                            "audio":
                            audio_id,
                            "window_index":
                            np.arange(label_start, label_end - 15,
                                      16),  # no Sliding
                            "label":
                            1,
                            "db":
                            decibel[label_start:label_end - 15:16]
                        })

                        df = df[df.db >= decibel_lower_bound].drop(
                            columns=['db'])

                        df_list.append(df)

                    # NON-COUGH
                else:
                    df = pd.DataFrame({
                        "audio":
                        audio_id,
                        "window_index":
                        np.arange(0, audio_length - 15,
                                  16 * DROP_NON_COUGH_RATIO),  # no Sliding
                        "label":
                        0,
                        "db":
                        decibel[:-15:16 * DROP_NON_COUGH_RATIO]
                    })

                    df = df[df.db >= decibel_lower_bound].drop(columns=['db'])

                    df_list.append(df)

                    pass
        except Exception as e:
            pass

    return pd.concat(df_list, ignore_index=True)