RuntimeError: torchaudio.sox_effects.sox_effects.apply_effects_file requires sox解决

最近在训练数据集时,需要提取噪声的特征,代码如下:

 noise_waveform, noise_sr = torchaudio.sox_effects.apply_effects_file(noise_path, effects=[])

直接运行报错

Traceback (most recent call last):
  File "main.py", line 189, in 
    cli()
  File "C:\Users\Name\.conda\envs\deeplearning\lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\Name\.conda\envs\deeplearning\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\Users\Name\.conda\envs\deeplearning\lib\site-packages\click\core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\Name\.conda\envs\deeplearning\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\Name\.conda\envs\deeplearning\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "main.py", line 70, in train_command
    get_data.SubsetSC(subset="training"),
  File "D:\Project\Python project\BC-ResNet-main\get_data.py", line 92, in __init__
    noise_waveform, noise_sr = torchaudio.sox_effects.apply_effects_file(noise_path, effects=[])
  File "C:\Users\Name\.conda\envs\deeplearning\lib\site-packages\torchaudio\_internal\module_utils.py", line 143, in wrapped
    raise RuntimeError(f"{func.__module__}.{func.__name__} requires sox")

利用Debug工具发现调用了torch.ops.torchaudio.sox_effects_apply_effects_file这个函数

    if not torch.jit.is_scripting():
        if hasattr(path, "read"):
            return torchaudio._torchaudio.apply_effects_fileobj(path, effects, normalize, channels_first, format)
        path = os.fspath(path)
    return torch.ops.torchaudio.sox_effects_apply_effects_file(path, effects, normalize, channels_first, format)

实际上torch.ops中并没有torchaudio,所以考虑可能是后面的版本并不支持这样操作,因为实际上我只需要得到噪声的wave以及采样率,所以可以直接调用torchaudio来操作,将代码改成以下这样:

# noise_waveform, noise_sr = torchaudio.sox_effects.apply_effects_file(noise_path, effects=[]) 不使用这个函数提取
noise_waveform, noise_sr= torchaudio.load(noise_path)

至此,问题解决。
总结:
有一些代码是过时或者依赖版本的,只有搞清楚代码在做什么事,善用debug工具才能更快解决问题。

你可能感兴趣的:(python,深度学习,人工智能)