注意
点击 此处 下载完整的示例代码
音频数据集¶
作者: Moto Hira
torchaudio
提供对常见、公开可访问的数据集的轻松访问。请参阅官方文档以获取可用数据集的列表。
import torch
import torchaudio
print(torch.__version__)
print(torchaudio.__version__)
2.5.0
2.5.0
import os
import IPython
import matplotlib.pyplot as plt
_SAMPLE_DIR = "_assets"
YESNO_DATASET_PATH = os.path.join(_SAMPLE_DIR, "yes_no")
os.makedirs(YESNO_DATASET_PATH, exist_ok=True)
def plot_specgram(waveform, sample_rate, title="Spectrogram"):
waveform = waveform.numpy()
figure, ax = plt.subplots()
ax.specgram(waveform[0], Fs=sample_rate)
figure.suptitle(title)
figure.tight_layout()
在这里,我们展示如何使用 torchaudio.datasets.YESNO
数据集。
dataset = torchaudio.datasets.YESNO(YESNO_DATASET_PATH, download=True)
0%| | 0.00/4.49M [00:00<?, ?B/s]
3%|2 | 128k/4.49M [00:00<00:08, 546kB/s]
11%|#1 | 512k/4.49M [00:00<00:02, 1.47MB/s]
36%|###6 | 1.62M/4.49M [00:00<00:00, 3.89MB/s]
100%|##########| 4.49M/4.49M [00:00<00:00, 7.28MB/s]
i = 1
waveform, sample_rate, label = dataset[i]
plot_specgram(waveform, sample_rate, title=f"Sample {i}: {label}")
IPython.display.Audio(waveform, rate=sample_rate)
i = 3
waveform, sample_rate, label = dataset[i]
plot_specgram(waveform, sample_rate, title=f"Sample {i}: {label}")
IPython.display.Audio(waveform, rate=sample_rate)
i = 5
waveform, sample_rate, label = dataset[i]
plot_specgram(waveform, sample_rate, title=f"Sample {i}: {label}")
IPython.display.Audio(waveform, rate=sample_rate)
脚本的总运行时间:(0 分钟 1.965 秒)