Download recipe/scripts/quality.py from gowombat/tiny-voca-da: direct link, hf CLI and curl.
- Browser
- Download file 2.44 kB
-
https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/scripts/quality.py
- Command line
-
hf download hf://gowombat/tiny-voca-da/recipe/scripts/quality.py
-
curl -L -o quality.py https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/scripts/quality.py
2.44 kB
| """Score groups of WAV files with DNSMOS (torchmetrics) and UTMOS22 strong (SpeechMOS). | |
| Usage (inside the `quality` service, data root mounted at /work): | |
| python scripts/quality.py --out checks/quality/<name>.json \ | |
| --group "English ref=out/en-ref-M2/*.wav" --group "v2 20k=out/listen-v2-s00020000/probe_*.wav" | |
| Libraries do the work: torchmetrics' DNSMOS (official Microsoft ONNX models, P.835 SIG/BAK/OVRL | |
| plus P.808) and tarepan/SpeechMOS UTMOS22 via torch.hub. Relative comparisons only (see | |
| docker/Dockerfile.quality). Higher is better. | |
| """ | |
| import argparse | |
| import glob | |
| import json | |
| from pathlib import Path | |
| import librosa | |
| import numpy as np | |
| import torch | |
| import torchmetrics | |
| from torchmetrics.functional.audio.dnsmos import deep_noise_suppression_mean_opinion_score as dnsmos | |
| SR = 16000 | |
| KEYS = ('P808', 'SIG', 'BAK', 'OVRL', 'UTMOS') | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--group', action='append', required=True, help='"name=glob"') | |
| parser.add_argument('--out', type=Path, required=True) | |
| args = parser.parse_args() | |
| utmos = torch.hub.load('tarepan/SpeechMOS:v1.2.0', 'utmos22_strong', trust_repo=True).eval() | |
| report = {'metrics': {'dnsmos': f'torchmetrics {torchmetrics.__version__} (P808, SIG, BAK, OVRL)', | |
| 'utmos': 'tarepan/SpeechMOS v1.2.0 utmos22_strong'}, 'groups': {}} | |
| for spec in args.group: | |
| name, pattern = spec.split('=', 1) | |
| files = sorted(glob.glob(pattern)) | |
| if not files: | |
| raise SystemExit(f'no files for {name}: {pattern}') | |
| rows = [] | |
| for path in files: | |
| audio, _ = librosa.load(path, sr=SR, mono=True) | |
| wave = torch.from_numpy(audio) | |
| p808, sig, bak, ovrl = dnsmos(wave, SR, personalized=False).tolist() | |
| with torch.no_grad(): | |
| ut = float(utmos(wave[None], SR).item()) | |
| rows.append({'file': path, 'P808': p808, 'SIG': sig, 'BAK': bak, 'OVRL': ovrl, 'UTMOS': ut}) | |
| mean = {k: float(np.mean([r[k] for r in rows])) for k in KEYS} | |
| report['groups'][name] = {'files': len(rows), 'mean': mean, 'rows': rows} | |
| print(f"{name:34s} n={len(rows):3d} " + ' '.join(f'{k} {mean[k]:.2f}' for k in KEYS), flush=True) | |
| args.out.parent.mkdir(parents=True, exist_ok=True) | |
| args.out.write_text(json.dumps(report, indent=2, ensure_ascii=False)) | |
| if __name__ == '__main__': | |
| main() | |