You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Matterport MaskRCNN导出的.h5模型转换为ONNX用于推理

Matterport MaskRCNN自定义模型h5转ONNX解决方案

1 解决load_model加载报错问题

  • T2报错核心原因是custom_objects里的层名和原实现类名不匹配,原MaskRCNN实现中的ROI对齐层名称为PyramidROIAlign,不是填写的PyramidROIAlign1/PyramidROIAlign2,修正后即可解决未知层报错。
  • 运行环境需要和训练时完全对齐,优先使用tf1.15 + Keras2.2.4的组合,避免版本兼容导致的No model found in config file报错。
  • 正确加载代码示例:
import tensorflow as tf
# 从训练用的mrcnn源码中导入所有自定义层
from mrcnn.model import BatchNorm, ProposalLayer, PyramidROIAlign, DetectionLayer

custom_objects = {
    "BatchNorm": BatchNorm,
    "tf": tf,
    "ProposalLayer": ProposalLayer,
    "PyramidROIAlign": PyramidROIAlign,
    "DetectionLayer": DetectionLayer
}
model = tf.keras.models.load_model("model.h5", custom_objects=custom_objects, compile=False)

2 绕过load_model直接通过pb文件转换方案

  • T3报错是因为指定的输出节点名和pb图中实际节点名不匹配,不要默认填output0。可以通过TensorBoard查看pb文件的图结构,确认输入输出节点的准确名称,MaskRCNN常规节点如下:
    • 输入节点:input_image:0、input_image_meta:0、input_anchors:0
    • 输出节点:mrcnn_detection/Reshape_1:0(检测框)、mrcnn_class/Reshape_1:0(分类结果)、mrcnn_bbox/Reshape:0(边框回归结果)、mrcnn_mask/Reshape_1:0(掩码结果)
  • 正确转换命令,指定准确的输入输出、固定输入尺寸(适配Barracuda的静态尺寸要求)、选择适配的opset版本:
python -m tf2onnx.convert \
--input frozen_model.pb \
--inputs input_image:0,input_image_meta:0,input_anchors:0 \
--shape input_image:0=[1,1024,1024,3] input_image_meta:0=[1,93] input_anchors:0=[1,261888,4] \
--outputs mrcnn_detection/Reshape_1:0,mrcnn_class/Reshape_1:0,mrcnn_bbox/Reshape:0,mrcnn_mask/Reshape_1:0 \
--opset 11 \
--output model.onnx
  • 注意opset选择11或12即可,过高版本的opset Unity Barracuda不支持,会导致后续推理报错。

3 T4报错修复方案

遇到的变量重名报错是tf2环境下控制流op生成的变量冲突导致的,切换到tf1.15环境导出SavedModel再执行转换即可解决。

内容的提问来源于stack exchange,提问作者Caife

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 23:00:03