Add DeepFilterNet3 ONNX export (converted from upstream checkpoint, MIT/Apache-2.0)
Browse files- README.md +84 -0
- enhance_onnx.py +65 -0
README.md
CHANGED
|
@@ -22,11 +22,95 @@ This is an unofficial conversion by a third party. It is not published by the De
|
|
| 22 |
- **Export script:** `df/scripts/export.py` from upstream commit `7b14b895fe7fc42cab72b4a4c1e49c982bd6404b` (2026-11-06), run with default options: opset 12, no simplification, no post-filter.
|
| 23 |
- **Versions:** PyTorch 2.2.2 (CPU), ONNX 1.23.2, ONNX Runtime 1.32.0.
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
## Files
|
| 26 |
|
| 27 |
- `enc.onnx`: encoder
|
| 28 |
- `erb_dec.onnx`: ERB decoder
|
| 29 |
- `df_dec.onnx`: deep-filter decoder
|
|
|
|
| 30 |
- `config.ini`: model configuration from the checkpoint
|
| 31 |
- `version.txt`: model name and epoch
|
| 32 |
- `SHA256SUMS.txt`: checksums of the files above
|
|
|
|
| 22 |
- **Export script:** `df/scripts/export.py` from upstream commit `7b14b895fe7fc42cab72b4a4c1e49c982bd6404b` (2026-11-06), run with default options: opset 12, no simplification, no post-filter.
|
| 23 |
- **Versions:** PyTorch 2.2.2 (CPU), ONNX 1.23.2, ONNX Runtime 1.32.0.
|
| 24 |
|
| 25 |
+
## Usage
|
| 26 |
+
|
| 27 |
+
The three ONNX models are the network only. The STFT, feature extraction, ERB mask and deep filtering come from the upstream Python package `df` and its Rust library `libdf`. Install them from the upstream repository (`DeepFilterNet/` and `pyDF/` directories), which needs a Rust toolchain for `libdf`. Then:
|
| 28 |
+
|
| 29 |
+
```sh
|
| 30 |
+
pip install onnxruntime soundfile numpy torch==2.2.2 torchaudio==2.2.2 # versions used for the export
|
| 31 |
+
python enhance_onnx.py noisy.wav enhanced.wav /path/to/this/repo
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
The input must be 48 kHz mono WAV. The script reads `config.ini` from the model directory, processes the whole file in one pass (not streaming), and writes `enhanced.wav`.
|
| 35 |
+
|
| 36 |
+
The script is `enhance_onnx.py` in this repository. It is reproduced here so that the steps are visible:
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
"""Enhance a 48 kHz mono WAV with the DeepFilterNet3 ONNX models.
|
| 40 |
+
|
| 41 |
+
The three ONNX models replace the PyTorch network. Feature extraction, mask
|
| 42 |
+
and deep-filter application come from the upstream `df` and `libdf` packages.
|
| 43 |
+
|
| 44 |
+
usage: python enhance_onnx.py <input.wav> <output.wav> <onnx_dir>
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
import sys
|
| 48 |
+
|
| 49 |
+
import numpy as np
|
| 50 |
+
import onnxruntime as ort
|
| 51 |
+
import soundfile as sf
|
| 52 |
+
import torch
|
| 53 |
+
from df.config import config
|
| 54 |
+
from df.deepfilternet3 import DfOutputReshapeMF # upstream DeepFilterNet3 modules
|
| 55 |
+
from df.enhance import df_features
|
| 56 |
+
from df.modules import Mask, erb_fb
|
| 57 |
+
from df.multiframe import DF as DfOp
|
| 58 |
+
from df.utils import as_complex
|
| 59 |
+
from libdf import DF
|
| 60 |
+
|
| 61 |
+
SR, FFT, HOP, NB_ERB, NB_DF, DF_ORDER, DF_LOOKAHEAD, CONV_LOOKAHEAD = 48000, 960, 480, 32, 96, 5, 2, 2
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def main(wav_in, wav_out, onnx_dir):
|
| 65 |
+
config.load(f"{onnx_dir}/config.ini", config_must_exist=True) # model hyper-parameters
|
| 66 |
+
audio, sr = sf.read(wav_in, dtype="float32", always_2d=True)
|
| 67 |
+
assert sr == SR, f"expected {SR} Hz, got {sr}"
|
| 68 |
+
audio = torch.from_numpy(audio.T[:1].copy()) # mono, [1, T]
|
| 69 |
+
orig_len = audio.shape[-1]
|
| 70 |
+
audio = torch.nn.functional.pad(audio, (0, FFT)) # same delay compensation as upstream enhance()
|
| 71 |
+
|
| 72 |
+
state = DF(sr=SR, fft_size=FFT, hop_size=HOP, nb_bands=NB_ERB, min_nb_erb_freqs=2)
|
| 73 |
+
spec, feat_erb, feat_spec = df_features(audio, state, NB_DF) # [1,1,T,F,2], [1,1,T,E], [1,1,T,NB_DF,2]
|
| 74 |
+
|
| 75 |
+
# Encoder inputs: the same look-ahead shift the PyTorch model applies (crop 2 frames, pad 2).
|
| 76 |
+
feat_erb_np = feat_erb.numpy()
|
| 77 |
+
feat_spec_np = feat_spec.squeeze(1).permute(0, 3, 1, 2).numpy() # [1,2,T,NB_DF]
|
| 78 |
+
feat_erb_np = np.pad(feat_erb_np[:, :, CONV_LOOKAHEAD:], ((0, 0), (0, 0), (0, CONV_LOOKAHEAD), (0, 0)))
|
| 79 |
+
feat_spec_np = np.pad(feat_spec_np[:, :, CONV_LOOKAHEAD:], ((0, 0), (0, 0), (0, CONV_LOOKAHEAD), (0, 0)))
|
| 80 |
+
|
| 81 |
+
enc = ort.InferenceSession(f"{onnx_dir}/enc.onnx", providers=["CPUExecutionProvider"])
|
| 82 |
+
e0, e1, e2, e3, emb, c0, _lsnr = enc.run(None, {"feat_erb": feat_erb_np, "feat_spec": feat_spec_np})
|
| 83 |
+
erb_dec = ort.InferenceSession(f"{onnx_dir}/erb_dec.onnx", providers=["CPUExecutionProvider"])
|
| 84 |
+
(m,) = erb_dec.run(None, {"emb": emb, "e3": e3, "e2": e2, "e1": e1, "e0": e0})
|
| 85 |
+
df_dec = ort.InferenceSession(f"{onnx_dir}/df_dec.onnx", providers=["CPUExecutionProvider"])
|
| 86 |
+
(coefs,) = df_dec.run(None, {"emb": emb, "c0": c0})
|
| 87 |
+
|
| 88 |
+
# Apply the ERB mask to the full spectrum, then deep-filter the lowest NB_DF bins.
|
| 89 |
+
erb_inv = erb_fb(state.erb_widths(), SR, inverse=True)
|
| 90 |
+
spec_m = Mask(erb_inv)(spec, torch.from_numpy(m))
|
| 91 |
+
coefs = DfOutputReshapeMF(DF_ORDER, NB_DF)(torch.from_numpy(coefs))
|
| 92 |
+
spec_e = DfOp(num_freqs=NB_DF, frame_size=DF_ORDER, lookahead=DF_LOOKAHEAD)(spec.clone(), coefs)
|
| 93 |
+
spec_e[..., NB_DF:, :] = spec_m[..., NB_DF:, :]
|
| 94 |
+
|
| 95 |
+
out = torch.as_tensor(state.synthesis(as_complex(spec_e.squeeze(1)).numpy()))
|
| 96 |
+
d = FFT - HOP # undo the STFT delay
|
| 97 |
+
out = out[:, d : orig_len + d].numpy()
|
| 98 |
+
sf.write(wav_out, out.T, SR)
|
| 99 |
+
print(f"wrote {wav_out}: {out.shape[-1]} samples")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
main(*sys.argv[1:4])
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
**Verified:** on upstream's `assets/noisy_snr0.wav` (508,591 samples), this output matches upstream's PyTorch `enhance()` with the same checkpoint to within a maximum absolute difference of 3.1e-5 (signal RMS 0.055).
|
| 107 |
+
|
| 108 |
## Files
|
| 109 |
|
| 110 |
- `enc.onnx`: encoder
|
| 111 |
- `erb_dec.onnx`: ERB decoder
|
| 112 |
- `df_dec.onnx`: deep-filter decoder
|
| 113 |
+
- `enhance_onnx.py`: example script (see Usage)
|
| 114 |
- `config.ini`: model configuration from the checkpoint
|
| 115 |
- `version.txt`: model name and epoch
|
| 116 |
- `SHA256SUMS.txt`: checksums of the files above
|
enhance_onnx.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Enhance a 48 kHz mono WAV with the DeepFilterNet3 ONNX models.
|
| 2 |
+
|
| 3 |
+
The three ONNX models replace the PyTorch network. Feature extraction, mask
|
| 4 |
+
and deep-filter application come from the upstream `df` and `libdf` packages.
|
| 5 |
+
|
| 6 |
+
usage: python usage_onnx.py <input.wav> <output.wav> <onnx_dir>
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import sys
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
import onnxruntime as ort
|
| 13 |
+
import soundfile as sf
|
| 14 |
+
import torch
|
| 15 |
+
from df.config import config
|
| 16 |
+
from df.deepfilternet3 import DfOutputReshapeMF # upstream DeepFilterNet3 modules
|
| 17 |
+
from df.enhance import df_features
|
| 18 |
+
from df.modules import Mask, erb_fb
|
| 19 |
+
from df.multiframe import DF as DfOp
|
| 20 |
+
from df.utils import as_complex
|
| 21 |
+
from libdf import DF
|
| 22 |
+
|
| 23 |
+
SR, FFT, HOP, NB_ERB, NB_DF, DF_ORDER, DF_LOOKAHEAD, CONV_LOOKAHEAD = 48000, 960, 480, 32, 96, 5, 2, 2
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def main(wav_in, wav_out, onnx_dir):
|
| 27 |
+
config.load(f"{onnx_dir}/config.ini", config_must_exist=True) # model hyper-parameters
|
| 28 |
+
audio, sr = sf.read(wav_in, dtype="float32", always_2d=True)
|
| 29 |
+
assert sr == SR, f"expected {SR} Hz, got {sr}"
|
| 30 |
+
audio = torch.from_numpy(audio.T[:1].copy()) # mono, [1, T]
|
| 31 |
+
orig_len = audio.shape[-1]
|
| 32 |
+
audio = torch.nn.functional.pad(audio, (0, FFT)) # same delay compensation as upstream enhance()
|
| 33 |
+
|
| 34 |
+
state = DF(sr=SR, fft_size=FFT, hop_size=HOP, nb_bands=NB_ERB, min_nb_erb_freqs=2)
|
| 35 |
+
spec, feat_erb, feat_spec = df_features(audio, state, NB_DF) # [1,1,T,F,2], [1,1,T,E], [1,1,T,NB_DF,2]
|
| 36 |
+
|
| 37 |
+
# Encoder inputs: the same look-ahead shift the PyTorch model applies (crop 2 frames, pad 2).
|
| 38 |
+
feat_erb_np = feat_erb.numpy()
|
| 39 |
+
feat_spec_np = feat_spec.squeeze(1).permute(0, 3, 1, 2).numpy() # [1,2,T,NB_DF]
|
| 40 |
+
feat_erb_np = np.pad(feat_erb_np[:, :, CONV_LOOKAHEAD:], ((0, 0), (0, 0), (0, CONV_LOOKAHEAD), (0, 0)))
|
| 41 |
+
feat_spec_np = np.pad(feat_spec_np[:, :, CONV_LOOKAHEAD:], ((0, 0), (0, 0), (0, CONV_LOOKAHEAD), (0, 0)))
|
| 42 |
+
|
| 43 |
+
enc = ort.InferenceSession(f"{onnx_dir}/enc.onnx", providers=["CPUExecutionProvider"])
|
| 44 |
+
e0, e1, e2, e3, emb, c0, _lsnr = enc.run(None, {"feat_erb": feat_erb_np, "feat_spec": feat_spec_np})
|
| 45 |
+
erb_dec = ort.InferenceSession(f"{onnx_dir}/erb_dec.onnx", providers=["CPUExecutionProvider"])
|
| 46 |
+
(m,) = erb_dec.run(None, {"emb": emb, "e3": e3, "e2": e2, "e1": e1, "e0": e0})
|
| 47 |
+
df_dec = ort.InferenceSession(f"{onnx_dir}/df_dec.onnx", providers=["CPUExecutionProvider"])
|
| 48 |
+
(coefs,) = df_dec.run(None, {"emb": emb, "c0": c0})
|
| 49 |
+
|
| 50 |
+
# Apply the ERB mask to the full spectrum, then deep-filter the lowest NB_DF bins.
|
| 51 |
+
erb_inv = erb_fb(state.erb_widths(), SR, inverse=True)
|
| 52 |
+
spec_m = Mask(erb_inv)(spec, torch.from_numpy(m))
|
| 53 |
+
coefs = DfOutputReshapeMF(DF_ORDER, NB_DF)(torch.from_numpy(coefs))
|
| 54 |
+
spec_e = DfOp(num_freqs=NB_DF, frame_size=DF_ORDER, lookahead=DF_LOOKAHEAD)(spec.clone(), coefs)
|
| 55 |
+
spec_e[..., NB_DF:, :] = spec_m[..., NB_DF:, :]
|
| 56 |
+
|
| 57 |
+
out = torch.as_tensor(state.synthesis(as_complex(spec_e.squeeze(1)).numpy()))
|
| 58 |
+
d = FFT - HOP # undo the STFT delay
|
| 59 |
+
out = out[:, d : orig_len + d].numpy()
|
| 60 |
+
sf.write(wav_out, out.T, SR)
|
| 61 |
+
print(f"wrote {wav_out}: {out.shape[-1]} samples")
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main(*sys.argv[1:4])
|