From a1e355b8ceb12ac66c9d82e7786df4c3c40be479 Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Wed, 21 Oct 2026 20:08:53 +0200 Subject: Init --- devtools/export_framesed_classes.py | 46 ++++++++++++++ devtools/export_framesed_weights.py | 118 ++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 devtools/export_framesed_classes.py create mode 100644 devtools/export_framesed_weights.py (limited to 'devtools') diff --git a/devtools/export_framesed_classes.py b/devtools/export_framesed_classes.py new file mode 100644 index 0000000..e02752a --- /dev/null +++ b/devtools/export_framesed_classes.py @@ -0,0 +1,46 @@ +"""Generate C class metadata from the model-ordered index,mid,display_name CSV.""" + +import argparse +import csv +import json +from pathlib import Path + + +def render(path: Path) -> str: + with path.open(newline="", encoding="utf-8") as source: + rows = list(csv.DictReader(source)) + if len(rows) != 447: + raise ValueError("frame_mn10 requires exactly 447 classes") + mids = set() + labels = set() + lines = ["/* Model output order; regenerate with make regenerate-classes. */"] + for index, row in enumerate(rows): + mid, label = row["mid"], row["display_name"] + if int(row["index"]) != index: + raise ValueError("class indices must be contiguous and ordered from zero") + if not mid or not label or mid in mids or label in labels: + raise ValueError("class IDs and labels must be nonempty and unique") + if any(ord(c) < 32 for c in mid + label): + raise ValueError("class metadata must not contain control characters") + mids.add(mid) + labels.add(label) + lines.append(f" {{{json.dumps(mid)}, {json.dumps(label)}}},") + return "\n".join(lines) + "\n" + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("labels", type=Path) + parser.add_argument("output", type=Path) + parser.add_argument("--check", action="store_true") + args = parser.parse_args() + content = render(args.labels) + if args.check: + if args.output.read_text(encoding="utf-8") != content: + parser.exit(1, f"{args.output}: regenerate with make regenerate-classes\n") + else: + args.output.write_text(content, encoding="utf-8") + + +if __name__ == "__main__": + main() diff --git a/devtools/export_framesed_weights.py b/devtools/export_framesed_weights.py new file mode 100644 index 0000000..cdce3a9 --- /dev/null +++ b/devtools/export_framesed_weights.py @@ -0,0 +1,118 @@ +"""Export PretrainedSED frame_mn10_strong_1.pt to the C library's weight blob. + +Requires torch, torchaudio, and numpy; no upstream checkout is needed. +Architecture and frontend follow PretrainedSED revision +1aa47e482f7e89904cba2338999345025d8b4e36 (models/frame_mn and +models/frame_passt/preprocess.py). Only the strong (frame-level) head is used. +""" + +import argparse +import hashlib +from pathlib import Path +import struct +import tempfile +import urllib.request + +URL = ("https://github.com/fschmid56/PretrainedSED/releases/download/" + "v0.0.1/frame_mn10_strong_1.pt") +CHECKPOINT_SHA256 = "4a0fe320d5369987b772394c51881fb20a602967e6842f72f3e5c8181065ece7" + + +def export_tensors(checkpoint): + import torch + import torch.nn.functional as F + from torchaudio.compliance.kaldi import get_mel_banks + + # weights_only avoids arbitrary pickle code execution. Pin the checkpoint + # digest as well: other MobileNet variants are not supported by the C code. + digest = hashlib.sha256(checkpoint.read_bytes()).hexdigest() + if digest != CHECKPOINT_SHA256: + raise ValueError(f"unexpected checkpoint SHA-256: {digest}") + state = torch.load(checkpoint, map_location="cpu", weights_only=True) + tensors = {} + + def add(name, tensor): + if not torch.isfinite(tensor).all(): + raise ValueError(f"nonfinite tensor: {name}") + tensors[name] = tensor.detach().cpu().numpy().astype("