首页 > 解决方案 > 如何将频谱图图像与其人工标记的数据结合起来,在 Python 中使用 CNN 进行处理?

问题描述

我正在校园里做一个最后的项目:使用 CNN 从歌曲中估计音高。

CNN 的输入是一首歌曲的频谱图,由 生成plt.specgram(),大小为 334 x 217。歌曲数据集取自MIR-QBSH,具有以下规范:8 秒持续时间,单声道,8KHz 采样,8 位量化,帧大小 = 256 ,重叠 = 0,第一帧从音频文件的第一个样本开始。

这是频谱图的一个示例:
频谱图示例

据我现在了解,我需要数据标签(在我的情况下:音高标签)与 CNN 的频谱图相结合,以便能够处理计算。我的数据标签包含一首歌曲的 250 个音高标签。这些音高标签以半音(MIDI 编号)为单位。

这是上面频谱图的音高标签示例。我已经math.floor()对原始文件中的这些音高标签做了方法来简化计算。

Pitch values:  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 50, 50, 52, 52, 53, 54, 54, 53, 53, 53, 53, 54, 54, 54, 54, 54, 53, 0, 0, 54, 54, 54, 54, 54, 54, 53, 0, 0, 0, 0, 46, 46, 46, 47, 48, 48, 48, 48, 48, 49, 49, 49, 50, 50, 50, 50, 0, 0, 0, 50, 50, 50, 50, 50, 50, 50, 49, 0, 0, 51, 47, 47, 47, 47, 47, 47, 47, 47, 48, 49, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 57, 58, 58, 58, 58, 58, 57, 57, 57, 57, 57, 57, 57, 58, 58, 57, 57, 57, 57, 56, 55, 55, 56, 56, 56, 56, 56, 55, 56, 56, 56, 56, 55, 55, 55, 56, 56, 54, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 52, 52, 53, 53, 54, 54, 0, 0, 54, 54, 54, 54, 54, 54, 54, 54, 0, 0, 0, 54, 54, 54, 54, 54, 53, 52, 51, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 49, 49, 49, 49, 50, 50, 50, 50, 49, 0, 0, 0, 50, 49, 49, 49, 49, 49, 50, 50, 49, 0, 0, 47, 47, 47, 48, 48, 48, 48, 0, 0, 0, 0, 0, 0, 0]

我的问题是,在 Python 中的 CNN 处理频谱图之前,我应该如何组合频谱图及其音高标签?

标签: pythonconv-neural-network

解决方案


我已经解决了我的问题。它是这样工作的:

    image_data = []
    tm = time.time()
    for img_item in os.listdir(image_path): #for every image in path
        try:
          img_array = cv2.imread(os.path.join(image_path, img_item))

          spectrogram_preprocessing = resize_recolor_spectrogram(img_array) # convert image to grayscale and resize it to 250 x 160

          # imread to array
          spectrogram_preprocessing = np.array(spectrogram_preprocessing)

          label = extract_pitch_label(os.path.join(label_path, img_item))

          # combining label and image
          image_data.append([spectrogram_preprocessing, label])
        except Exception as e:
          raise e

推荐阅读