# /// script # requires-python = ">=3.11" # dependencies = ["torch", "torchvision", "pillow", "numpy", "onnx", "onnxscript", "onnx2tf", "onnx-graphsurgeon", "onnxsim", "psutil", "ai-edge-litert", "sng4onnx", "pandas", "flatbuffers", "joblib", "termcolor", "requests", "tqdm", "packaging", "tensorflow-cpu", "tf-keras", "huggingface_hub>=0.30", "timm", "datasets>=4.0"] # /// """HF-job credibility gate: convert the wave-3 winner checkpoint (best.pt) to a quantized TFLite model for ESP32 TFLite-Micro and upload the artifact to the code repo. Path: best.pt -> ONNX (torch.onnx) -> SavedModel (onnx2tf, float) -> TFLite with dynamic-range int8 weights (Optimize.DEFAULT). TFLite-Micro runs this fine on ESP32. """ import os, sys, io import numpy as np import torch from huggingface_hub import HfApi, hf_hub_download tok = os.environ["HF_TOKEN"] api = HfApi(token=tok) ckpt = hf_hub_download("Tonic/challenge5-acoustic-squeezenet1_1-s7", "best.pt", repo_type="model", token=tok) print("ckpt:", os.path.getsize(ckpt) // 1024, "KB", flush=True) here = os.path.dirname(os.path.abspath(__file__)) print("step: torch.load", flush=True) ck = torch.load(ckpt, map_location="cpu", weights_only=False) print("step: ckpt loaded", flush=True) sd = ck.get("state_dict", ck) if isinstance(ck, dict) else ck arch = ck.get("arch", "squeezenet1_1") if isinstance(ck, dict) else "squeezenet1_1" import importlib.util jt_path = hf_hub_download("Tonic/challenge5-acoustic-code", "job_train.py", repo_type="model", token=tok) spec = importlib.util.spec_from_file_location("job_train", jt_path) jt = importlib.util.module_from_spec(spec) print("step: exec job_train", flush=True) spec.loader.exec_module(jt) print("step: job_train imported", flush=True) backbone, feat = jt.build_backbone(arch, pretrained=False) class WakeCNN(torch.nn.Module): def __init__(self, b, f): super().__init__() self.backbone = b self.head_wake = torch.nn.Linear(f, 1) self.head_class = torch.nn.Linear(f, 4) def forward(self, x): f = self.backbone(x) return self.head_wake(f).squeeze(-1), self.head_class(f) model = WakeCNN(backbone, feat) sd = {k: v for k, v in sd.items() if k.startswith("backbone.") or k.startswith("head_")} print("step: load_state_dict", flush=True) model.load_state_dict(sd, strict=True) print("step: state loaded", flush=True) model.eval() print("arch:", arch, "params:", sum(p.numel() for p in model.parameters()), flush=True) SIZE = 224 onnx_path = "/tmp/model_full_224.onnx" torch.onnx.export(model, torch.zeros(1, 3, SIZE, SIZE), onnx_path, opset_version=17, dynamo=False, input_names=["spectrogram"], output_names=["wake_logit", "sound_class_logits"], dynamic_axes={"spectrogram": {0: "batch"}}) print("onnx exported", flush=True) print("step: import onnx2tf", flush=True) import onnx2tf import onnx2tf.utils.common_functions as _cf import onnx2tf.onnx2tf as _o2t _cf.download_test_image_data = lambda *a, **k: np.zeros((1, 224, 224, 3), dtype=np.float32) if hasattr(_o2t, "download_test_image_data"): _o2t.download_test_image_data = _cf.download_test_image_data sm_dir = "/tmp/saved_model" onnx2tf.convert(input_onnx_file_path=onnx_path, output_folder_path=sm_dir, not_use_onnxsim=True, verbosity="error") print("onnx2tf done", flush=True) print("step: import tensorflow", flush=True) import tensorflow as tf print("step: tf imported", flush=True) conv = tf.lite.TFLiteConverter.from_saved_model(sm_dir) conv.optimizations = [tf.lite.Optimize.DEFAULT] # dynamic-range int8 weights tfl = conv.convert() out = "/tmp/model_full_224_dynamic_int8.tflite" open(out, "wb").write(tfl) print("tflite dynamic-int8:", os.path.getsize(out) // 1024, "KB", flush=True) api.upload_file(path_or_fileobj=out, path_in_repo="esp32/model_full_224_dynamic_int8.tflite", repo_id="Tonic/challenge5-acoustic-code", repo_type="model", token=tok, commit_message=f"esp32: dynamic-int8 tflite of squeezenet1_1-s7 ({os.path.getsize(out)//1024} KB)") api.upload_file(path_or_fileobj=onnx_path, path_in_repo="esp32/model_full_224.onnx", repo_id="Tonic/challenge5-acoustic-code", repo_type="model", token=tok, commit_message="esp32: onnx of squeezenet1_1-s7") print("UPLOADED esp32/ artifacts", flush=True)