Replies: 1 comment
|
Probably not fusion. Nothing is trainable unless you name it.
The names have to match the initializers tf2onnx produced, not your Keras layer names, so easiest to just list them: model = onnx.load("model.onnx")
names = [i.name for i in model.graph.initializer]
artifacts.generate_artifacts(model, requires_grad=names, loss=..., optimizer=...)Anything you don't want trained goes in |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Use 'generate_artifacts' to convert the'. h5 'file to onnx for training
But the training effect in Qt is completely inferior to using Python's'. h5 '
I suspect it's a code conversion issue with 'onnxrruntime. training. artifacts' that caused the fusion of operators in training_madel.onnx, which can only predict but cannot be trained
import os
import onnx
import tf2onnx
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model, save_model
from tensorflow.keras.layers import Activation
from tensorflow.keras.models import clone_model
from tensorflow.keras.activations import relu
import onnxruntime.training.artifacts as artifacts
def export_initial_models(keras_model_path, output_dir):
"""
Args:
keras_model_path: Keras模型文件路径 (.h5)
output_dir: 输出目录
"""
# 创建输出目录
baseline_dir = os.path.join(output_dir, 'baseline')
current_dir = os.path.join(output_dir, 'current')
os.makedirs(baseline_dir, exist_ok=True)
os.makedirs(current_dir, exist_ok=True)
# 加载Keras模型
print("Loading Keras model...")
keras_model = load_model(keras_model_path)
# ==========================================================
# 导出 ONNX + ORT Training Artifacts
# ==========================================================
try:
print("开始导出 ONNX...")
# ONNX输出路径
onnx_path = os.path.join(baseline_dir, 'inference_model.onnx')
spec = (
tf.TensorSpec(
(1, 1, 5, 1000),
tf.float32,
name="input"
),
)
model_proto, _ = tf2onnx.convert.from_keras(
keras_model,
input_signature=spec,
opset=17
)
with open(onnx_path, "wb") as f:
f.write(model_proto.SerializeToString())
print("✓ inference_model.onnx 导出成功")
# ======================================================
# 生成 ORT Training Artifacts
# ======================================================
print("开始生成 ORT Training Artifacts...")
onnx_model = onnx.load(onnx_path)
if name == "main":
keras_model_path = "./Model/CrossSubject/Net.h5"
export_initial_models(keras_model_path, "./models")
All reactions