Initial commit: 基于旋律的音乐哼唱检索系统 (Query by Humming)
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
"""跑完整评测并输出可复现的报告。
|
||||
|
||||
用法::
|
||||
|
||||
python -m scripts.run_eval # 三个难度各跑一遍
|
||||
python -m scripts.run_eval --per-song 5 # 每首 5 条查询
|
||||
python -m scripts.run_eval --difficulty normal # 只跑中等难度
|
||||
python -m scripts.run_eval --markdown docs/评测报告.md
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
from qbh.config import DEFAULT # noqa: E402
|
||||
from qbh.evaluate import evaluate, make_query_specs # noqa: E402
|
||||
from qbh.library import Library # noqa: E402
|
||||
from qbh.search import SearchEngine # noqa: E402
|
||||
from qbh.store import Store # noqa: E402
|
||||
|
||||
DIFFICULTIES = ("easy", "normal", "hard")
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description="QBH 评测")
|
||||
parser.add_argument("--db", default="data/qbh.db")
|
||||
parser.add_argument("--per-song", type=int, default=3)
|
||||
parser.add_argument("--seed", type=int, default=20260908)
|
||||
parser.add_argument("--top-k", type=int, default=10)
|
||||
parser.add_argument("--difficulty", default="all", choices=("all",) + DIFFICULTIES)
|
||||
parser.add_argument("--out-dir", default="data")
|
||||
parser.add_argument("--markdown", default="")
|
||||
parser.add_argument("--verbose", action="store_true")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
store = Store(args.db)
|
||||
library = Library(store, DEFAULT)
|
||||
if library.count() == 0:
|
||||
print("曲库为空,先构建…")
|
||||
library.build()
|
||||
engine = SearchEngine(library, DEFAULT)
|
||||
|
||||
difficulties = DIFFICULTIES if args.difficulty == "all" else (args.difficulty,)
|
||||
reports = []
|
||||
print(f"曲库 {library.count()} 首,每首 {args.per_song} 条查询,随机种子 {args.seed}\n")
|
||||
|
||||
for difficulty in difficulties:
|
||||
specs = make_query_specs(
|
||||
library.melodies(),
|
||||
n_per_song=args.per_song,
|
||||
difficulty=difficulty,
|
||||
seed=args.seed,
|
||||
)
|
||||
report = evaluate(
|
||||
engine, specs, label=difficulty, top_k=args.top_k, cfg=DEFAULT, verbose=args.verbose
|
||||
)
|
||||
reports.append(report)
|
||||
print(report.summary_line())
|
||||
print(f" 音符数比 {report.note_count_ratio:.2f} · "
|
||||
f"音高误差中位数 {report.pitch_err_median:.2f} 半音 · "
|
||||
f"平均排名 {report.mean_rank:.2f}")
|
||||
out = Path(args.out_dir) / f"eval_report_{difficulty}.json"
|
||||
report.save(out)
|
||||
print(f" → {out}")
|
||||
|
||||
# 网页端读取的主报告用中等难度
|
||||
primary = next((r for r in reports if r.label == "normal"), reports[0])
|
||||
primary.save(Path(args.out_dir) / "eval_report.json")
|
||||
|
||||
if args.markdown:
|
||||
lines = [
|
||||
"## 评测结果(合成哼唱查询 → 完整流水线)",
|
||||
"",
|
||||
f"- 曲库:**{primary.n_songs} 首**(27 首真实 MIDI + 16 首内置曲谱)",
|
||||
f"- 查询:每首 {args.per_song} 条,随机种子 {args.seed},共 {primary.n_queries} 条/难度",
|
||||
"- 查询扰动:移调 ±5 半音(12% 概率再 ±12)、速度 0.82–1.28×、"
|
||||
"逐音抖动 σ≈0.35 半音、丢音/劈音/插音、哼唱音色渲染、按 SNR 加噪",
|
||||
"",
|
||||
"| 难度 | Top-1 | Top-3 | Top-5 | Top-10 | MRR | 粗召回率 | 延迟 p50 | 延迟 p95 |",
|
||||
"|---|---:|---:|---:|---:|---:|---:|---:|---:|",
|
||||
]
|
||||
for report in reports:
|
||||
lines.append(
|
||||
f"| {report.label} | {report.top1*100:.1f}% | {report.top3*100:.1f}% | "
|
||||
f"{report.top5*100:.1f}% | {report.top10*100:.1f}% | {report.mrr:.3f} | "
|
||||
f"{report.candidate_recall*100:.1f}% | {report.p50_latency_ms:.0f} ms | "
|
||||
f"{report.p95_latency_ms:.0f} ms |"
|
||||
)
|
||||
lines += [
|
||||
"",
|
||||
"> 说明:查询由合成器生成,曲库中 27 首为真实 MIDI、16 首为内置曲谱,"
|
||||
"因此这组数字是**自洽基准**而非绝对泛化能力。噪声鲁棒性见"
|
||||
" `docs/评测报告.md` 的 SNR 消融章节。",
|
||||
"",
|
||||
]
|
||||
Path(args.markdown).write_text("\n".join(lines), encoding="utf-8")
|
||||
print(f"\nMarkdown 片段已写入 {args.markdown}")
|
||||
|
||||
store.close()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user