音频课程文档

加载和探索音频数据集

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

加载和探索音频数据集

在本课程中,我们将使用 🤗 Datasets 库来处理音频数据集。 🤗 Datasets 是一个开源库,用于下载和准备来自所有模态(包括音频)的数据集。 该库可以轻松访问 Hugging Face Hub 上公开提供的无与伦比的机器学习数据集选择。 此外,🤗 Datasets 包括针对音频数据集量身定制的多种功能,这些功能简化了研究人员和从业人员使用此类数据集的工作。

要开始使用音频数据集,请确保您已安装 🤗 Datasets 库

pip install datasets[audio]

🤗 Datasets 的关键定义特征之一是能够仅使用一行 Python 代码和 load_dataset() 函数即可下载和准备数据集。

让我们加载和探索一个名为 MINDS-14 的音频数据集,其中包含人们用多种语言和方言向电子银行系统提问的录音。

要加载 MINDS-14 数据集,我们需要复制 Hub 上的数据集标识符 (PolyAI/minds14) 并将其传递给 load_dataset 函数。 我们还将指定我们只对数据的澳大利亚子集 (en-AU) 感兴趣,并将其限制为训练集

from datasets import load_dataset

minds = load_dataset("PolyAI/minds14", name="en-AU", split="train")
minds

输出

Dataset(
    {
        features: [
            "path",
            "audio",
            "transcription",
            "english_transcription",
            "intent_class",
            "lang_id",
        ],
        num_rows: 654,
    }
)

该数据集包含 654 个音频文件,每个文件都附带一个文本记录、一个英文翻译和一个标签,指示该人查询背后的意图。 音频列包含原始音频数据。 让我们仔细看看其中一个示例

example = minds[0]
example

输出

{
    "path": "/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-AU~PAY_BILL/response_4.wav",
    "audio": {
        "path": "/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-AU~PAY_BILL/response_4.wav",
        "array": array(
            [0.0, 0.00024414, -0.00024414, ..., -0.00024414, 0.00024414, 0.0012207],
            dtype=float32,
        ),
        "sampling_rate": 8000,
    },
    "transcription": "I would like to pay my electricity bill using my card can you please assist",
    "english_transcription": "I would like to pay my electricity bill using my card can you please assist",
    "intent_class": 13,
    "lang_id": 2,
}

您可能会注意到音频列包含多个特征。 以下是它们的内容

  • path:音频文件的路径(在本例中为 *.wav)。
  • array:解码后的音频数据,表示为一维 NumPy 数组。
  • sampling_rate。 音频文件的采样率(在本例中为 8,000 Hz)。

intent_class 是音频记录的分类类别。 要将此数字转换为有意义的字符串,我们可以使用 int2str() 方法

id2label = minds.features["intent_class"].int2str
id2label(example["intent_class"])

输出

"pay_bill"

如果您查看文本记录特征,您可以看到音频文件确实记录了一个人询问有关支付账单的问题。

如果您计划在此数据子集上训练音频分类器,您可能不一定需要所有特征。 例如,lang_id 对于所有示例都将具有相同的值,并且不会有用。 english_transcription 可能会在此子集中复制 transcription,因此我们可以安全地删除它们。

您可以使用 🤗 Datasets 的 remove_columns 方法轻松删除不相关的特征

columns_to_remove = ["lang_id", "english_transcription"]
minds = minds.remove_columns(columns_to_remove)
minds

输出

Dataset({features: ["path", "audio", "transcription", "intent_class"], num_rows: 654})

现在我们已经加载并检查了数据集的原始内容,让我们听几个例子! 我们将使用 Gradio 中的 BlocksAudio 特征来解码数据集中的一些随机样本

import gradio as gr


def generate_audio():
    example = minds.shuffle()[0]
    audio = example["audio"]
    return (
        audio["sampling_rate"],
        audio["array"],
    ), id2label(example["intent_class"])


with gr.Blocks() as demo:
    with gr.Column():
        for _ in range(4):
            audio, label = generate_audio()
            output = gr.Audio(audio, label=label)

demo.launch(debug=True)

如果您愿意,您还可以可视化一些示例。 让我们绘制第一个示例的波形。

import librosa
import matplotlib.pyplot as plt
import librosa.display

array = example["audio"]["array"]
sampling_rate = example["audio"]["sampling_rate"]

plt.figure().set_figwidth(12)
librosa.display.waveshow(array, sr=sampling_rate)
Waveform plot

试试看! 下载 MINDS-14 数据集的另一种方言或语言,收听并可视化一些示例,以了解整个数据集中的变化。 您可以在此处找到可用语言的完整列表。

< > GitHub 上更新