"""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("