Remove the df/libdf example script (replaced by DeepFilterNet3Onnx.py)
Browse files- enhance_onnx.py +0 -65
enhance_onnx.py
DELETED
|
@@ -1,65 +0,0 @@
|
|
| 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])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|