Upload convert_to_onnx.py with huggingface_hub
Browse files- convert_to_onnx.py +41 -0
convert_to_onnx.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import sys, os
|
| 3 |
+
|
| 4 |
+
# FN_* modules use relative imports; run from repo/User
|
| 5 |
+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 6 |
+
USER_DIR = os.path.join(SCRIPT_DIR, 'repo', 'User')
|
| 7 |
+
sys.path.insert(0, USER_DIR)
|
| 8 |
+
os.chdir(USER_DIR)
|
| 9 |
+
|
| 10 |
+
import tensorflow as tf
|
| 11 |
+
import tf2onnx
|
| 12 |
+
import FN_configuration
|
| 13 |
+
import FN_model
|
| 14 |
+
|
| 15 |
+
cfg = FN_configuration.configuration()
|
| 16 |
+
cfg.configure(None)
|
| 17 |
+
|
| 18 |
+
print(f"Model input: (None, None, {cfg.SPECTRUM_NPOINTS})")
|
| 19 |
+
print(f"Model output: (None, None, {cfg.NPARAMS})")
|
| 20 |
+
|
| 21 |
+
model = FN_model.define_model(cfg)
|
| 22 |
+
|
| 23 |
+
ckpt = os.path.join(SCRIPT_DIR, 'expdir', 'mvt33_f6z1sTpF10', 'weights.sigmoid195-20.143')
|
| 24 |
+
print(f"\nLoading weights: {ckpt}")
|
| 25 |
+
model.load_weights(ckpt).expect_partial()
|
| 26 |
+
print("Weights loaded.")
|
| 27 |
+
|
| 28 |
+
input_sig = [tf.TensorSpec([None, None, cfg.SPECTRUM_NPOINTS], tf.float32, name='input')]
|
| 29 |
+
|
| 30 |
+
output_path = os.path.join(SCRIPT_DIR, 'formantnet.onnx')
|
| 31 |
+
print(f"\nConverting to ONNX (opset 15) → {output_path}")
|
| 32 |
+
|
| 33 |
+
model_proto, _ = tf2onnx.convert.from_keras(
|
| 34 |
+
model,
|
| 35 |
+
input_signature=input_sig,
|
| 36 |
+
opset=15,
|
| 37 |
+
output_path=output_path,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
print(f"Done. ONNX graph has {len(model_proto.graph.node)} nodes.")
|
| 41 |
+
print(f"Output: {output_path}")
|