Upload train_nano_job.py with huggingface_hub
Browse files- train_nano_job.py +106 -0
train_nano_job.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.11"
|
| 3 |
+
# dependencies = ["datasets>=4.0", "torch", "torchvision", "huggingface_hub>=0.30", "pillow", "numpy"]
|
| 4 |
+
# ///
|
| 5 |
+
"""Train the 96x96 nano two-head CNN for classic ESP32 (TFLite-Micro) and export int8 TFLite.
|
| 6 |
+
|
| 7 |
+
Run as HF Job (t4-small), same pattern as work/job_train.py but:
|
| 8 |
+
- input 96x96 (windows_5s spectrogram PNGs resized)
|
| 9 |
+
- tiny depthwise CNN4: ~150-300k params, two heads (wake_logit, sound_class_logits[4])
|
| 10 |
+
- export to TFLite int8 via onnx -> onnx2tf (add onnx2tf to deps when running)
|
| 11 |
+
|
| 12 |
+
Usage (in job): uv run train_nano.py --epochs 15 --out /tmp/nano
|
| 13 |
+
"""
|
| 14 |
+
import argparse, os, json
|
| 15 |
+
import numpy as np
|
| 16 |
+
|
| 17 |
+
def build_nano():
|
| 18 |
+
import torch.nn as nn
|
| 19 |
+
class DW(nn.Module):
|
| 20 |
+
def __init__(s, cin, cout, stride=1):
|
| 21 |
+
super().__init__()
|
| 22 |
+
s.c = nn.Sequential(
|
| 23 |
+
nn.Conv2d(cin, cin, 3, stride, 1, groups=cin, bias=False),
|
| 24 |
+
nn.BatchNorm2d(cin), nn.ReLU(inplace=True),
|
| 25 |
+
nn.Conv2d(cin, cout, 1, 1, 0, bias=False),
|
| 26 |
+
nn.BatchNorm2d(cout), nn.ReLU(inplace=True))
|
| 27 |
+
def forward(s, x): return s.c(x)
|
| 28 |
+
class Nano(nn.Module):
|
| 29 |
+
def __init__(s):
|
| 30 |
+
super().__init__()
|
| 31 |
+
s.f = nn.Sequential(
|
| 32 |
+
nn.Conv2d(3, 16, 3, 2, 1, bias=False), nn.BatchNorm2d(16), nn.ReLU(inplace=True),
|
| 33 |
+
DW(16, 32, 2), DW(32, 64, 2), DW(64, 128, 2), DW(128, 256, 2),
|
| 34 |
+
nn.AdaptiveAvgPool2d(1))
|
| 35 |
+
s.wake = nn.Linear(256, 1)
|
| 36 |
+
s.cls = nn.Linear(256, 4)
|
| 37 |
+
def forward(s, x):
|
| 38 |
+
f = s.f(x).flatten(1)
|
| 39 |
+
return s.wake(f), s.cls(f)
|
| 40 |
+
return Nano()
|
| 41 |
+
|
| 42 |
+
def main():
|
| 43 |
+
ap = argparse.ArgumentParser()
|
| 44 |
+
ap.add_argument("--epochs", type=int, default=15)
|
| 45 |
+
ap.add_argument("--batch", type=int, default=128)
|
| 46 |
+
ap.add_argument("--out", default="/tmp/nano")
|
| 47 |
+
args = ap.parse_args()
|
| 48 |
+
os.makedirs(args.out, exist_ok=True)
|
| 49 |
+
import torch, torch.nn as nn
|
| 50 |
+
from torch.utils.data import DataLoader, Dataset
|
| 51 |
+
from datasets import load_dataset
|
| 52 |
+
from PIL import Image
|
| 53 |
+
import io
|
| 54 |
+
|
| 55 |
+
ds = load_dataset("Tonic/unified-drone-acoustic-cuas", "windows_5s", token=os.environ["HF_TOKEN"])
|
| 56 |
+
CL = ["drone_buzz", "vehicle_mechanical", "explosion", "ambient_outdoor", "unknown"]
|
| 57 |
+
|
| 58 |
+
class Win(Dataset):
|
| 59 |
+
def __init__(s, split, n=None):
|
| 60 |
+
d = ds[split]
|
| 61 |
+
if n: d = d.shuffle(seed=42).select(range(min(n, len(d))))
|
| 62 |
+
s.d = d
|
| 63 |
+
def __len__(s): return len(s.d)
|
| 64 |
+
def __getitem__(s, i):
|
| 65 |
+
r = s.d[i]
|
| 66 |
+
im = Image.open(io.BytesIO(r["spectrogram"]["bytes"])).convert("RGB").resize((96, 96))
|
| 67 |
+
x = np.asarray(im, np.float32) / 255.0
|
| 68 |
+
x = (x - np.array([0.485, 0.456, 0.406], np.float32)) / np.array([0.229, 0.224, 0.225], np.float32)
|
| 69 |
+
x = np.transpose(x, (2, 0, 1))
|
| 70 |
+
y = CL.index(CL[r["sound_class"]] if isinstance(r["sound_class"], str) else CL[r["sound_class"]])
|
| 71 |
+
return torch.from_numpy(x), torch.tensor(y), torch.tensor(1 if y == 0 else 0, dtype=torch.float32)
|
| 72 |
+
|
| 73 |
+
tr = DataLoader(Win("train", 120000), batch_size=args.batch, shuffle=True, num_workers=4)
|
| 74 |
+
va = DataLoader(Win("validation", 4000), batch_size=256)
|
| 75 |
+
dev = "cuda" if torch.cuda.is_available() else "cpu"
|
| 76 |
+
m = build_nano().to(dev)
|
| 77 |
+
opt = torch.optim.AdamW(m.parameters(), lr=3e-4)
|
| 78 |
+
cw = torch.tensor([1.0, 6.0, 6.0, 1.0, 3.0]) # boost rare classes
|
| 79 |
+
for ep in range(args.epochs):
|
| 80 |
+
m.train(); tot = 0
|
| 81 |
+
for x, y, w in tr:
|
| 82 |
+
x, y, w = x.to(dev), y.to(dev), w.to(dev)
|
| 83 |
+
opt.zero_grad()
|
| 84 |
+
wl, cl = m(x)
|
| 85 |
+
loss = nn.BCEWithLogitsLoss()(wl.squeeze(1), w) + nn.CrossEntropyLoss(weight=cw.to(dev))(cl, y)
|
| 86 |
+
loss.backward(); opt.step()
|
| 87 |
+
tot += loss.item() * len(x)
|
| 88 |
+
m.eval(); f1s = []
|
| 89 |
+
with torch.no_grad():
|
| 90 |
+
for x, y, w in va:
|
| 91 |
+
x, y, w = x.to(dev), y.to(dev), w.to(dev)
|
| 92 |
+
wl, cl = m(x)
|
| 93 |
+
p = torch.sigmoid(wl.squeeze(1))
|
| 94 |
+
f1s.append(((p > 0.5) == (w > 0.5)).float().mean().item())
|
| 95 |
+
print(f"[ep {ep+1}/{args.epochs}] loss={tot/len(tr.dataset):.4f} val_wake_acc={np.mean(f1s):.4f}", flush=True)
|
| 96 |
+
torch.save({"state_dict": m.state_dict(), "arch": "nano96"}, os.path.join(args.out, "best.pt"))
|
| 97 |
+
m = m.cpu()
|
| 98 |
+
dummy = torch.zeros(1, 3, 96, 96)
|
| 99 |
+
onnx = os.path.join(args.out, "model_nano_96.onnx")
|
| 100 |
+
torch.onnx.export(m, dummy, onnx, opset_version=17,
|
| 101 |
+
input_names=["spectrogram"], output_names=["wake_logit", "sound_class_logits"])
|
| 102 |
+
print("onnx:", onnx, "| params:", sum(p.numel() for p in m.parameters()), flush=True)
|
| 103 |
+
print("convert to int8 tflite with tools/export_tflite.py (onnx2tf) + xxd -i", flush=True)
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
main()
|