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_weights.py | 118 ++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 devtools/export_framesed_weights.py (limited to 'devtools/export_framesed_weights.py') 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("