File size: 7,637 Bytes
fac7535 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #!/usr/bin/env python3
"""
test_onnx.py
============
生成一段测试音频,然后用它把 `onnx/` 里所有模型跑一遍,确认能通。
每个模型的输入契约不同,脚本按模型名查表构造输入:
* 谱域模型(roformer / SCNet)—— 自己算 STFT,帧数必须与模型静态形状一致
* 波形域模型(dedup 5 个)—— 直接切 16384 采样
* 说话人两件套 —— redimnet 吃原始音频,activity 吃 redimnet 的输出
用法
----
python scripts/test_onnx.py # 全部跑
python scripts/test_onnx.py --only voice2stem
"""
from __future__ import annotations
import argparse
import json
import os
import sys
import time
import warnings
import numpy as np
import onnxruntime as ort
import torch
warnings.filterwarnings("ignore")
HERE = os.path.dirname(os.path.abspath(__file__))
ONNX_DIR = os.path.join(HERE, "..", "onnx")
OUT_DIR = os.path.join(HERE, "..", "output")
SR = 44100
DURATION = 13.0 # 秒,够 SCNet 要的 11.03 s
# 模型 -> (类型, 额外参数)
SPEC = {
# 谱域:n_fft / hop / 帧数 / 声道
"voice2stem_v03_fp16c_t2": ("stft", dict(n_fft=2048, hop=441, frames=801, ch=2)),
"voicesep2stem_fp16c_t2": ("stft", dict(n_fft=2048, hop=441, frames=801, ch=2)),
"voice_restore_adaptive_fp16": ("stft", dict(n_fft=2048, hop=441, frames=801, ch=2)),
"drumsep_1.3": ("stft", dict(n_fft=2048, hop=512, frames=690, ch=2)),
"cinema4stem_fp16c_t2": ("stft", dict(n_fft=2048, hop=470, frames=751, ch=1)),
"musicnew4stem_fp16c_t2": ("stft", dict(n_fft=4096, hop=1024, frames=476, ch=2, no_batch=True)),
# 波形域
"indoor_dedup_v01": ("audio", dict(n=16384)),
"indoor-noisy_dedup_v01": ("audio", dict(n=16384)),
"outdoor_dedup_v01": ("audio", dict(n=16384)),
"outdoor-traffic_dedup_v01": ("audio", dict(n=16384)),
"outdoor-wind-v2_dedup_v01": ("audio", dict(n=16384)),
"redimnet_ptn_activity_v1": ("redimnet", {}),
"speaker_activity_single_pass_v1": ("activity", {}),
}
def make_test_audio(path: str) -> np.ndarray:
"""合成一段立体声测试信号:低频正弦 + 中频和弦 + 高频噪声 + 人声样调制。"""
t = np.arange(int(SR * DURATION)) / SR
sig = (
0.20 * np.sin(2 * np.pi * 60 * t) # bass
+ 0.15 * np.sin(2 * np.pi * 440 * t) # A4
+ 0.10 * np.sin(2 * np.pi * 660 * t) # E5
+ 0.08 * np.sin(2 * np.pi * 1200 * t) * np.sin(2 * np.pi * 3 * t) # 调制成分
+ 0.03 * np.random.RandomState(0).randn(t.size) # 噪声
)
stereo = np.stack([sig, sig * 0.85], axis=0).astype(np.float32)
os.makedirs(os.path.dirname(path), exist_ok=True)
try:
import soundfile as sf
sf.write(path, stereo.T, SR)
except Exception as e:
print(f" (写入 wav 失败,跳过:{e})")
return stereo
def stft_input(audio: np.ndarray, n_fft: int, hop: int, frames: int, ch: int) -> np.ndarray:
"""截取 (frames-1)*hop 采样,做 STFT,输出 [1, ch, F, frames, 2]。"""
need = (frames - 1) * hop
x = audio[:ch, :need]
if x.shape[1] < need: # 不够就补零
x = np.pad(x, ((0, 0), (0, need - x.shape[1])))
win = torch.hann_window(n_fft)
outs = []
for c in range(ch):
spec = torch.stft(torch.from_numpy(x[c]), n_fft=n_fft, hop_length=hop,
win_length=n_fft, window=win, center=True,
return_complex=True)
outs.append(torch.view_as_real(spec))
spec = torch.stack(outs, dim=0) # [ch, F, T, 2]
spec = spec.unsqueeze(0).numpy().astype(np.float32)
assert spec.shape[1] == ch and spec.shape[3] == frames, spec.shape
return spec
def build_inputs(name: str, sess, audio: np.ndarray, cache: dict) -> dict:
kind, p = SPEC.get(name, ("random", {}))
feed = {}
if kind == "stft":
x = stft_input(audio, p["n_fft"], p["hop"], p["frames"], p["ch"])
if p.get("no_batch"): # SCNet 的输入没有 batch 维:[ch, F, T, 2]
x = x[0]
return {sess.get_inputs()[0].name: x}
if kind == "audio":
mono = audio[0, : p["n"]].reshape(1, 1, 1, p["n"]).astype(np.float32)
return {sess.get_inputs()[0].name: mono}
if kind == "redimnet":
seg = audio[0, :32000].reshape(1, 32000).astype(np.float32)
feed["audio"] = seg
feed["slot_starts"] = np.array([0], dtype=np.int64)
return feed
if kind == "activity":
# 依赖 redimnet 的输出
emb, feat, short, mel = cache["redimnet"]
T = 120
f = np.ascontiguousarray(feat[:, :T])
sp = np.ascontiguousarray(short[:, :T])
me = np.ascontiguousarray(mel[:, :T])
en = np.abs(f).sum(axis=-1, keepdims=True).astype(np.float32)
K = 2
refs = np.repeat(emb[:, None, :], K, axis=1)
bank = np.repeat(np.repeat(emb[:, None, None, :], 24, axis=2), K, axis=1)
return {"features": f, "shortpool": sp, "mel": me, "energy": en,
"references": refs, "bank": bank,
"mask": np.ones((1, K), dtype=bool)}
# 兜底:按声明的形状随机
for i in sess.get_inputs():
shape = [d if isinstance(d, int) and d > 0 else 1 for d in i.shape]
dt = np.float32 if "float" in i.type else np.int64
feed[i.name] = np.zeros(shape, dtype=dt)
return feed
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--only", default=None)
ap.add_argument("--providers", default="CPUExecutionProvider")
args = ap.parse_args()
wav = os.path.join(OUT_DIR, "test_audio.wav")
audio = make_test_audio(wav)
print(f"测试音频:{wav} {audio.shape[1]/SR:.1f}s {SR} Hz 2ch\n")
files = sorted(n for n in os.listdir(ONNX_DIR) if n.endswith(".onnx"))
if args.only:
files = [n for n in files if args.only.lower() in n.lower()]
cache = {}
rows = []
for fn in files:
name = fn[: -len(".onnx")]
path = os.path.join(ONNX_DIR, fn)
t0 = time.time()
try:
sess = ort.InferenceSession(path, providers=[args.providers])
feed = build_inputs(name, sess, audio, cache)
outs = sess.run(None, feed)
bad = [o.name for o, v in zip(sess.get_outputs(), outs)
if not np.isfinite(v).all()]
if name == "redimnet_ptn_activity_v1":
cache["redimnet"] = outs
shapes = ", ".join(f"{o.name}{list(v.shape)}" for o, v in zip(sess.get_outputs(), outs))
rows.append((name, "OK" if not bad else "非有限值", round(time.time() - t0, 2),
len(feed), shapes[:60]))
except Exception as e:
rows.append((name, f"FAIL {type(e).__name__}", round(time.time() - t0, 2), 0,
str(e)[:50]))
print(f"{'模型':<34}{'结果':<12}{'耗时':>7}{'输入数':>7} 输出")
print("-" * 110)
ok = 0
for name, status, sec, nin, shapes in rows:
if status == "OK":
ok += 1
print(f"{name:<34}{status:<12}{sec:>6}s{nin:>7} {shapes}")
print(f"\n通过 {ok}/{len(rows)}")
return 0 if ok == len(rows) else 1
if __name__ == "__main__":
sys.exit(main())
|