esp32/audioset: update export_nano_tflite_job.py
Browse files- export_nano_tflite_job.py +94 -88
export_nano_tflite_job.py
CHANGED
|
@@ -1,88 +1,94 @@
|
|
| 1 |
-
# /// script
|
| 2 |
-
# requires-python = ">=3.11"
|
| 3 |
-
# dependencies = ["torch", "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"]
|
| 4 |
-
# ///
|
| 5 |
-
"""Convert the trained nano96 ESP32 checkpoint to an int8 TFLite model + model_data.h.
|
| 6 |
-
|
| 7 |
-
Waits for esp32/nano96_best.pt in Tonic/challenge5-acoustic-code (uploaded by the
|
| 8 |
-
train_nano job), then: best.pt -> ONNX (1x3x96x96, static) -> SavedModel (onnx2tf)
|
| 9 |
-
-> TFLite dynamic-range int8 -> C header model_data.h for firmware/node.
|
| 10 |
-
|
| 11 |
-
Uploads to code repo: esp32/model_nano_96.tflite, esp32/model_nano_96_dynamic_int8.onnx,
|
| 12 |
-
esp32/model_data.h
|
| 13 |
-
"""
|
| 14 |
-
import os, sys, time, importlib.util
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
model
|
| 45 |
-
model.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
onnx2tf.
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.11"
|
| 3 |
+
# dependencies = ["torch", "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"]
|
| 4 |
+
# ///
|
| 5 |
+
"""Convert the trained nano96 ESP32 checkpoint to an int8 TFLite model + model_data.h.
|
| 6 |
+
|
| 7 |
+
Waits for esp32/nano96_best.pt in Tonic/challenge5-acoustic-code (uploaded by the
|
| 8 |
+
train_nano job), then: best.pt -> ONNX (1x3x96x96, static) -> SavedModel (onnx2tf)
|
| 9 |
+
-> TFLite dynamic-range int8 -> C header model_data.h for firmware/node.
|
| 10 |
+
|
| 11 |
+
Uploads to code repo: esp32/model_nano_96.tflite, esp32/model_nano_96_dynamic_int8.onnx,
|
| 12 |
+
esp32/model_data.h
|
| 13 |
+
"""
|
| 14 |
+
import os, sys, time, importlib.util
|
| 15 |
+
import numpy as np
|
| 16 |
+
|
| 17 |
+
tok = os.environ["HF_TOKEN"]
|
| 18 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 19 |
+
api = HfApi(token=tok)
|
| 20 |
+
CODE = "Tonic/challenge5-acoustic-code"
|
| 21 |
+
|
| 22 |
+
# 1) wait for the nano checkpoint (train_nano job uploads it at the end)
|
| 23 |
+
print("waiting for esp32/nano96_best.pt ...", flush=True)
|
| 24 |
+
ckpt = None
|
| 25 |
+
for i in range(90): # up to ~45 min
|
| 26 |
+
try:
|
| 27 |
+
ckpt = hf_hub_download(CODE, "esp32/nano96_best.pt", repo_type="model", token=tok)
|
| 28 |
+
break
|
| 29 |
+
except Exception:
|
| 30 |
+
time.sleep(30)
|
| 31 |
+
if ckpt is None:
|
| 32 |
+
raise SystemExit("nano96_best.pt never appeared")
|
| 33 |
+
print("ckpt:", os.path.getsize(ckpt) // 1024, "KB", flush=True)
|
| 34 |
+
|
| 35 |
+
import torch
|
| 36 |
+
ck = torch.load(ckpt, map_location="cpu", weights_only=False)
|
| 37 |
+
sd = ck.get("state_dict", ck) if isinstance(ck, dict) else ck
|
| 38 |
+
|
| 39 |
+
# 2) rebuild the nano arch from the trainer script in the same repo
|
| 40 |
+
tj = hf_hub_download(CODE, "train_nano_job.py", repo_type="model", token=tok)
|
| 41 |
+
spec = importlib.util.spec_from_file_location("train_nano_job", tj)
|
| 42 |
+
tn = importlib.util.module_from_spec(spec)
|
| 43 |
+
spec.loader.exec_module(tn)
|
| 44 |
+
model = tn.build_nano()
|
| 45 |
+
model.load_state_dict(sd, strict=True)
|
| 46 |
+
model.eval()
|
| 47 |
+
print("nano params:", sum(p.numel() for p in model.parameters()), flush=True)
|
| 48 |
+
|
| 49 |
+
# 3) ONNX (static 1x3x96x96 — TFLite-Micro friendly)
|
| 50 |
+
onnx_path = "/tmp/model_nano_96.onnx"
|
| 51 |
+
torch.onnx.export(model, torch.zeros(1, 3, 96, 96), onnx_path, opset_version=17, dynamo=False,
|
| 52 |
+
input_names=["spectrogram"], output_names=["wake_logit", "sound_class_logits"])
|
| 53 |
+
print("onnx exported", flush=True)
|
| 54 |
+
|
| 55 |
+
# 4) ONNX -> SavedModel -> TFLite dynamic int8
|
| 56 |
+
import onnx2tf
|
| 57 |
+
import onnx2tf.utils.common_functions as _cf
|
| 58 |
+
import onnx2tf.onnx2tf as _o2t
|
| 59 |
+
_cf.download_test_image_data = lambda *a, **k: np.zeros((1, 96, 96, 3), dtype=np.float32)
|
| 60 |
+
if hasattr(_o2t, "download_test_image_data"):
|
| 61 |
+
_o2t.download_test_image_data = _cf.download_test_image_data
|
| 62 |
+
sm_dir = "/tmp/saved_model_nano"
|
| 63 |
+
onnx2tf.convert(input_onnx_file_path=onnx_path, output_folder_path=sm_dir,
|
| 64 |
+
not_use_onnxsim=True, verbosity="error")
|
| 65 |
+
print("onnx2tf done", flush=True)
|
| 66 |
+
import tensorflow as tf
|
| 67 |
+
conv = tf.lite.TFLiteConverter.from_saved_model(sm_dir)
|
| 68 |
+
conv.optimizations = [tf.lite.Optimize.DEFAULT]
|
| 69 |
+
tfl = conv.convert()
|
| 70 |
+
tfl_path = "/tmp/model_nano_96.tflite"
|
| 71 |
+
open(tfl_path, "wb").write(tfl)
|
| 72 |
+
print("tflite dynamic-int8:", os.path.getsize(tfl_path) // 1024, "KB", flush=True)
|
| 73 |
+
|
| 74 |
+
# 5) C header for the firmware (xxd -i compatible names used by node.ino)
|
| 75 |
+
with open(tfl_path, "rb") as f:
|
| 76 |
+
data = f.read()
|
| 77 |
+
lines = []
|
| 78 |
+
for i in range(0, len(data), 12):
|
| 79 |
+
lines.append(" " + ", ".join(f"0x{b:02x}" for b in data[i:i + 12]) + ",")
|
| 80 |
+
hdr = ("// generated by export_nano_tflite_job.py — do not edit\n"
|
| 81 |
+
"#pragma once\n#include <cstdint>\n"
|
| 82 |
+
"const unsigned char model_tflite[] = {\n" + "\n".join(lines) + "\n};\n"
|
| 83 |
+
f"const unsigned int model_tflite_len = {len(data)};\n")
|
| 84 |
+
hdr_path = "/tmp/model_data.h"
|
| 85 |
+
open(hdr_path, "w").write(hdr)
|
| 86 |
+
print("model_data.h:", os.path.getsize(hdr_path) // 1024, "KB", flush=True)
|
| 87 |
+
|
| 88 |
+
# 6) upload artifacts
|
| 89 |
+
for p, rp in [(tfl_path, "esp32/model_nano_96.tflite"),
|
| 90 |
+
(onnx_path, "esp32/model_nano_96_dynamic_int8.onnx"),
|
| 91 |
+
(hdr_path, "esp32/model_data.h")]:
|
| 92 |
+
api.upload_file(path_or_fileobj=p, path_in_repo=rp, repo_id=CODE, repo_type="model",
|
| 93 |
+
token=tok, commit_message=f"esp32: {rp} ({os.path.getsize(p)//1024} KB)")
|
| 94 |
+
print("UPLOADED esp32 nano tflite + model_data.h", flush=True)
|