如何为 PyTorch 音频安装 sox?

如何为 PyTorch 音频安装 sox?

我想在 Linux 上运行 PyTorch 代码。我使用 AMI 在 AWS 上启动虚拟机Deep Learning PyTorch,但令人惊讶的是,PyTorch 尚未安装。因此我运行:

python3 -m pip install torch torchaudio

当我加载 WAV 文件时,出现此错误:

RuntimeError: Couldn't find appropriate backend to handle uri /path/to/file.wav and format None.

阅读此主题,我安装sox:

python3 -m pip install sox

但我得到了同样的错误。阅读此主题,我测试了这段代码:

import torchaudio
print(torchaudio.utils.sox_utils.list_read_formats())

我收到此错误:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/site-packages/torchaudio/_extension/utils.py", line 207, in wrapped
    _init_sox()
  File "/opt/conda/lib/python3.10/site-packages/torchaudio/_extension/utils.py", line 70, in _init_sox
    _load_lib("libtorchaudio_sox")
  File "/opt/conda/lib/python3.10/site-packages/torchaudio/_extension/utils.py", line 64, in _load_lib
    torch.ops.load_library(path)
  File "/opt/conda/lib/python3.10/site-packages/torch/_ops.py", line 852, in load_library
    ctypes.CDLL(path)
  File "/opt/conda/lib/python3.10/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libsox.so: cannot open shared object file: No such file or directory

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.10/site-packages/torchaudio/_extension/utils.py", line 209, in wrapped
    raise RuntimeError(
RuntimeError: list_read_formats requires sox extension which is not available. Please refer to the stacktrace above for how to resolve this.

所以我安装的sox是:

sudo apt-get install -y sox

并且我确认它有效:

$ which sox
/usr/bin/sox

我重新启动了 Python3 REPL 和虚拟机,但仍然出现同样的错误。

如何安装 sox 以用于 PyTorch 音频?

答案1

我遇到了同样的问题,您需要安装 libsox-dev 包

sudo apt install libsox-dev

这个解决方案在 ubuntu 上对我有用

相关内容