esp32: update export_job.py
Browse files- export_job.py +9 -0
export_job.py
CHANGED
|
@@ -20,7 +20,9 @@ ckpt = hf_hub_download("Tonic/challenge5-acoustic-squeezenet1_1-s7", "best.pt",
|
|
| 20 |
print("ckpt:", os.path.getsize(ckpt) // 1024, "KB", flush=True)
|
| 21 |
|
| 22 |
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
| 23 |
ck = torch.load(ckpt, map_location="cpu", weights_only=False)
|
|
|
|
| 24 |
sd = ck.get("state_dict", ck) if isinstance(ck, dict) else ck
|
| 25 |
arch = ck.get("arch", "squeezenet1_1") if isinstance(ck, dict) else "squeezenet1_1"
|
| 26 |
|
|
@@ -28,7 +30,9 @@ import importlib.util
|
|
| 28 |
jt_path = hf_hub_download("Tonic/challenge5-acoustic-code", "job_train.py", repo_type="model", token=tok)
|
| 29 |
spec = importlib.util.spec_from_file_location("job_train", jt_path)
|
| 30 |
jt = importlib.util.module_from_spec(spec)
|
|
|
|
| 31 |
spec.loader.exec_module(jt)
|
|
|
|
| 32 |
backbone, feat = jt.build_backbone(arch, pretrained=False)
|
| 33 |
class WakeCNN(torch.nn.Module):
|
| 34 |
def __init__(self, b, f):
|
|
@@ -41,7 +45,9 @@ class WakeCNN(torch.nn.Module):
|
|
| 41 |
return self.head_wake(f).squeeze(-1), self.head_class(f)
|
| 42 |
model = WakeCNN(backbone, feat)
|
| 43 |
sd = {k: v for k, v in sd.items() if k.startswith("backbone.") or k.startswith("head_")}
|
|
|
|
| 44 |
model.load_state_dict(sd, strict=True)
|
|
|
|
| 45 |
model.eval()
|
| 46 |
print("arch:", arch, "params:", sum(p.numel() for p in model.parameters()), flush=True)
|
| 47 |
|
|
@@ -52,13 +58,16 @@ torch.onnx.export(model, torch.zeros(1, 3, SIZE, SIZE), onnx_path, opset_version
|
|
| 52 |
dynamic_axes={"spectrogram": {0: "batch"}})
|
| 53 |
print("onnx exported", flush=True)
|
| 54 |
|
|
|
|
| 55 |
import onnx2tf
|
| 56 |
sm_dir = "/tmp/saved_model"
|
| 57 |
onnx2tf.convert(input_onnx_file_path=onnx_path, output_folder_path=sm_dir,
|
| 58 |
not_use_onnxsim=True, verbosity="error")
|
| 59 |
print("onnx2tf done", flush=True)
|
| 60 |
|
|
|
|
| 61 |
import tensorflow as tf
|
|
|
|
| 62 |
conv = tf.lite.TFLiteConverter.from_saved_model(sm_dir)
|
| 63 |
conv.optimizations = [tf.lite.Optimize.DEFAULT] # dynamic-range int8 weights
|
| 64 |
tfl = conv.convert()
|
|
|
|
| 20 |
print("ckpt:", os.path.getsize(ckpt) // 1024, "KB", flush=True)
|
| 21 |
|
| 22 |
here = os.path.dirname(os.path.abspath(__file__))
|
| 23 |
+
print("step: torch.load", flush=True)
|
| 24 |
ck = torch.load(ckpt, map_location="cpu", weights_only=False)
|
| 25 |
+
print("step: ckpt loaded", flush=True)
|
| 26 |
sd = ck.get("state_dict", ck) if isinstance(ck, dict) else ck
|
| 27 |
arch = ck.get("arch", "squeezenet1_1") if isinstance(ck, dict) else "squeezenet1_1"
|
| 28 |
|
|
|
|
| 30 |
jt_path = hf_hub_download("Tonic/challenge5-acoustic-code", "job_train.py", repo_type="model", token=tok)
|
| 31 |
spec = importlib.util.spec_from_file_location("job_train", jt_path)
|
| 32 |
jt = importlib.util.module_from_spec(spec)
|
| 33 |
+
print("step: exec job_train", flush=True)
|
| 34 |
spec.loader.exec_module(jt)
|
| 35 |
+
print("step: job_train imported", flush=True)
|
| 36 |
backbone, feat = jt.build_backbone(arch, pretrained=False)
|
| 37 |
class WakeCNN(torch.nn.Module):
|
| 38 |
def __init__(self, b, f):
|
|
|
|
| 45 |
return self.head_wake(f).squeeze(-1), self.head_class(f)
|
| 46 |
model = WakeCNN(backbone, feat)
|
| 47 |
sd = {k: v for k, v in sd.items() if k.startswith("backbone.") or k.startswith("head_")}
|
| 48 |
+
print("step: load_state_dict", flush=True)
|
| 49 |
model.load_state_dict(sd, strict=True)
|
| 50 |
+
print("step: state loaded", flush=True)
|
| 51 |
model.eval()
|
| 52 |
print("arch:", arch, "params:", sum(p.numel() for p in model.parameters()), flush=True)
|
| 53 |
|
|
|
|
| 58 |
dynamic_axes={"spectrogram": {0: "batch"}})
|
| 59 |
print("onnx exported", flush=True)
|
| 60 |
|
| 61 |
+
print("step: import onnx2tf", flush=True)
|
| 62 |
import onnx2tf
|
| 63 |
sm_dir = "/tmp/saved_model"
|
| 64 |
onnx2tf.convert(input_onnx_file_path=onnx_path, output_folder_path=sm_dir,
|
| 65 |
not_use_onnxsim=True, verbosity="error")
|
| 66 |
print("onnx2tf done", flush=True)
|
| 67 |
|
| 68 |
+
print("step: import tensorflow", flush=True)
|
| 69 |
import tensorflow as tf
|
| 70 |
+
print("step: tf imported", flush=True)
|
| 71 |
conv = tf.lite.TFLiteConverter.from_saved_model(sm_dir)
|
| 72 |
conv.optimizations = [tf.lite.Optimize.DEFAULT] # dynamic-range int8 weights
|
| 73 |
tfl = conv.convert()
|