beginner/audio_datasets_tutorial
Run in Google Colab
Colab
Download Notebook
Notebook
View on GitHub
GitHub
참고
Go to the end to download the full example code.
오디오 데이터셋#
``torchaudio``는 공용으로 접근할 수 있는 일반적인 데이터셋에 쉽게 접근할 수 있는 기능을 제공합니다. 사용할 수 있는 데이터셋 목록은 공식 문서를 참고하세요.
import torch
import torchaudio
print(torch.__version__)
print(torchaudio.__version__)
2.8.0+cu128
2.8.0+cu128
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%|▎ | 128k/4.49M [00:00<00:25, 181kB/s]
6%|▌ | 256k/4.49M [00:00<00:14, 298kB/s]
11%|█ | 512k/4.49M [00:01<00:07, 546kB/s]
20%|█▉ | 896k/4.49M [00:01<00:04, 889kB/s]
42%|████▏ | 1.88M/4.49M [00:01<00:01, 1.94MB/s]
81%|████████ | 3.62M/4.49M [00:01<00:00, 3.68MB/s]
100%|██████████| 4.49M/4.49M [00:01<00:00, 2.41MB/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)
![Sample 1: [0, 0, 0, 1, 0, 0, 0, 1]](../_images/sphx_glr_audio_datasets_tutorial_001.png)
/opt/conda/lib/python3.11/site-packages/torchaudio/_backend/utils.py:213: UserWarning:
In 2.9, this function's implementation will be changed to use torchaudio.load_with_torchcodec` under the hood. Some parameters like ``normalize``, ``format``, ``buffer_size``, and ``backend`` will be ignored. We recommend that you port your code to rely directly on TorchCodec's decoder instead: https://docs.pytorch.org/torchcodec/stable/generated/torchcodec.decoders.AudioDecoder.html#torchcodec.decoders.AudioDecoder.
/opt/conda/lib/python3.11/site-packages/torchaudio/_backend/ffmpeg.py:88: UserWarning:
torio.io._streaming_media_decoder.StreamingMediaDecoder has been deprecated. This deprecation is part of a large refactoring effort to transition TorchAudio into a maintenance phase. The decoding and encoding capabilities of PyTorch for both audio and video are being consolidated into TorchCodec. Please see https://github.com/pytorch/audio/issues/3902 for more information. It will be removed from the 2.9 release.
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)
![Sample 3: [0, 0, 1, 0, 0, 0, 1, 0]](../_images/sphx_glr_audio_datasets_tutorial_002.png)
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)
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)
Total running time of the script: (0 minutes 3.885 seconds)