#!/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())