Repository navigation
Replies: 2 comments
|
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 |
|
Your code already passes from onnxruntime.training.api import CheckpointState
state = CheckpointState.load_checkpoint("models/baseline/checkpoint")
for name, parameter in state.parameters:
if parameter.requires_grad:
print(name, parameter.data.shape, parameter.data.dtype)Those checkpoint properties let you compare the result with the Keras weights you intend to update. Selecting every ONNX initializer can also select constants used for shapes or indexing. Another concrete difference to check is the loss: The model and Qt loop would be needed to identify the cause. Comparing the same batch's predictions and loss before the first update would help separate an export/loss mismatch from an update-loop problem. |
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