Exporting the operator hardsigmoid to ONNX opset version 11 is not supported

yoloe导出onnx时报错:

RuntimeError: Exporting the operator hardsigmoid to ONNX opset version 11 is not supported. Please open a bug to request ONNX export support for the missing operator.

在onnx opset 12下转以下模型时因不支持hardswish激活函数而报错

GhostNet
MobileNetv3Small
EfficientNetLite0
PP-LCNet
解决方案是找到对应的nn.Hardswish层,将其替换为自己覆写的Hardswish实现:

class Hardswish(nn.Module):  # export-friendly version of nn.Hardswish()
    @staticmethod
    def forward(x):
        # return x * F.hardsigmoid(x)  # for torchscript and CoreML
        return x * F.hardtanh(x + 3, 0., 6.) / 6.  # for torchscript, CoreML and ONNX


以PP-LCNet为例,找到哪些层是Hardswish层,替换方法为

# 替换函数, 参考https://zhuanlan.zhihu.com/p/356273702
def _set_module(model, submodule_key, module):
    tokens = submodule_key.split('.')
    sub_tokens =
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这个错误通常是由于你的TensorFlow版本启用了Eager Execution(即立即执行模式),而在Eager Execution模式下,无法导出/导入Meta Graph。 解决这个问题的方法之一是禁用Eager Execution。你可以使用以下代码在TensorFlow 2.0中禁用Eager Execution: ```python import tensorflow as tf tf.compat.v1.disable_eager_execution() ``` 如果你正在使用TensorFlow 1.x,则可以按照以下方式禁用Eager Execution: ```python import tensorflow as tf tf.compat.v1.disable_eager_execution() ``` 如果你需要使用Eager Execution,但又需要导出/导入Meta Graph,则可以使用`tf.compat.v1.Session()`来创建一个不启用Eager Execution的会话。例如,你可以按照以下方式导出Meta Graph: ```python import tensorflow as tf # 构建计算图 x = tf.placeholder(tf.float32, shape=[None, 784], name='x') y = tf.placeholder(tf.float32, shape=[None, 10], name='y') W = tf.Variable(tf.zeros([784, 10]), name='W') b = tf.Variable(tf.zeros([10]), name='b') logits = tf.matmul(x, W) + b loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y)) train_op = tf.train.GradientDescentOptimizer(0.5).minimize(loss) # 创建会话并导出Meta Graph with tf.compat.v1.Session() as sess: sess.run(tf.compat.v1.global_variables_initializer()) saver = tf.compat.v1.train.Saver() saver.save(sess, 'my-model', global_step=0, write_meta_graph=True) ``` 在这个示例中,我们使用`tf.compat.v1.Session()`创建了一个不启用Eager Execution的会话,并使用`saver.save()`方法导出了Meta Graph,使得我们可以在其他地方导入该图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值