You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Torchaudio构建UrbanSound8K数据集时遇RuntimeError错误

解决Windows下Torchaudio加载UrbanSound8K音频时的Backend错误

问题重现

在Windows系统运行Torchaudio处理UrbanSound8K数据集时,出现以下错误:

RuntimeError: Couldn't find appropriate backend to handle uri C:\Users\hbhavnag\Documents\Hussain\ASU\collision detection\urban sounds\UrbanSound8K\audio\5\100263-2-0-121.wav and format None.

使用的代码如下:

from torch.utils.data import Dataset
import pandas as pd
import torchaudio
import os

class UrbanSoundDataset(Dataset):

    def __init__(self,annotation_file,audio_dir):
        self.annotations = pd.read_csv(annotation_file)
        self.audio_dir = audio_dir

    def __len__(self):
        return len(self.annotations)

    def __getitem__(self, index):
        audio_sample_path = self._get_audio_sample_path(index)
        label = self._get_audio_sample_label(index)
        signal, sr = torchaudio.load(audio_sample_path)
        return signal, label
    
    def _get_audio_sample_path(self,index):
        fold = f"fold{self.annotations.iloc[index,5]}"
        path = os.path.join(self.audio_dir, fold, self.annotations.iloc[index,0])
        return path
    
    def _get_audio_sample_label(self, index):
        return self.annotations.iloc[index,6]
    
if __name__ == "__main__":
    annotations_file = r"C:\Users\hbhavnag\Documents\Hussain\ASU\collision detection\urban sounds\UrbanSound8K\metadata\UrbanSound8K.csv"
    audio_dir = r"C:\Users\hbhavnag\Documents\Hussain\ASU\collision detection\urban sounds\UrbanSound8K\audio"
    usd = UrbanSoundDataset(annotations_file, audio_dir)
    print (f"There are {len(usd)} samples in the dataset")

signal, label = usd[2]

解决方案

1. 安装缺失的音频后端依赖

Torchaudio在Windows上默认可能缺少处理WAV文件的后端库,先安装soundfile:

pip install soundfile

如果安装后仍报错,尝试安装FFmpeg相关依赖:

pip install ffmpeg-python

2. 显式指定加载后端

调用torchaudio.load时手动指定后端,修改__getitem__方法:

def __getitem__(self, index):
    audio_sample_path = self._get_audio_sample_path(index)
    label = self._get_audio_sample_label(index)
    # 指定soundfile后端
    signal, sr = torchaudio.load(audio_sample_path, backend='soundfile')
    # 或者用ffmpeg后端:signal, sr = torchaudio.load(audio_sample_path, backend='ffmpeg')
    return signal, label

3. 匹配Torch与Torchaudio版本

版本不兼容是常见原因,卸载现有版本后重新安装对应匹配的版本:

# 卸载现有包
pip uninstall torch torchaudio -y
# 安装CPU版本(如需CUDA版本,替换为对应CUDA版本的安装命令)
pip install torch torchvision torchaudio

4. 验证文件路径有效性

在__getitem__中添加路径打印,确认文件实际存在:

def __getitem__(self, index):
    audio_sample_path = self._get_audio_sample_path(index)
    print(f"Loading file: {audio_sample_path}")
    # 检查文件是否存在
    assert os.path.exists(audio_sample_path), f"File not found: {audio_sample_path}"
    label = self._get_audio_sample_label(index)
    signal, sr = torchaudio.load(audio_sample_path, backend='soundfile')
    return signal, label

内容的提问来源于stack exchange,提问作者Hussain Bhavnagarwala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 06:45:09