mxnet to onnx to caffe

烈酒焚心 提交于 2019-12-10 03:29:50

三个步骤

1、mxnet to onnx

https://blog.csdn.net/lhl_blog/article/details/90672695

2、onnx 经过简化操作去除onnx 自己加的层

https://github.com/daquexian/onnx-simplifier

3、onnx to caffe,

https://github.com/MTlab/onnx2caffe

1、raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [10:51:40] src/ndarray/ndarray.cc:1805: Check failed: fi->Read(data) Invalid NDArray file format

模型虽然下载了,但是没有下载完全,

2、ImportError: numpy.core.multiarray failed to import
更新numpy 版本

3、 mxnet onnx to simplifer onnx, error
onnxruntime::BatchNorm < T >::BatchNorm(const
onnxruntime::OpKernelInfo &) [
with T = float] spatial == 1 was false.BatchNormalization kernel for CPU provider does not support non-spatial cases

mxnet 转onnx的代码,里面的bn设置
https://github.com/apache/incubator-mxnet/blob/745a41ca1a6d74a645911de8af46dece03db93ea/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L357

@mx_op.register("BatchNorm")
def convert_batchnorm(node, **kwargs):
    """Map MXNet's BatchNorm operator attributes to onnx's BatchNormalization operator
    and return the created node.
    """
    name, input_nodes, attrs = get_inputs(node, kwargs)

    momentum = float(attrs.get("momentum", 0.9))
    eps = float(attrs.get("eps", 0.001))

    bn_node = onnx.helper.make_node(
        "BatchNormalization",
        input_nodes,
        [name],
        name=name,
        epsilon=eps,
        momentum=momentum,
        # MXNet computes mean and variance per feature for batchnorm
        # Default for onnx is across all spatial features. So disabling the parameter.
        spatial=0
    )
    return [bn_node]

问题原因是 mxnet 转换onnx spatial ==0,然后再onnxruntime 里面说不支持spatial=0,需要spatial=1,所以就报错了
方法1,改动onnxruntime,源码编译
onnxruntime 修改,支持这个操作,
https://github.com/microsoft/onnxruntime/pull/2092
详细修改过程,需要重新编译onnxruntime
https://github.com/microsoft/onnxruntime/pull/2092/files/830dba84578cbe88b382559dd7ae55cffe147104
方法2,改变spatial 从0到1 的理由
https://github.com/onnx/models/issues/156
在这里插入图片描述
在这里插入图片描述
方法3,模型修改值重新保存,重新保存,
在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!