昇思模型转onnx模型不支持SiLU函数及解决

1 系统环境

硬件环境(Ascend/GPU/CPU): Ascend
MindSpore版本: mindspore =2.5.0、mindyolo=0.5.0
执行模式(PyNative/ Graph): 不限
Python版本: Python=3.10
操作系统平台: Ubuntu18.04

2 报错信息

2.1 转换信息

python ./deploy/export.py --config ./yolov7-tiny.yaml --weight runs/2025.07.06-13.33.58/weights/EMA_yolov7-tiny-50_4.ckpt --per_batch_size 1 --file_format ONNX --device_target CPU

2.2 报错信息

[mindspore@cloud-a0bb5125b-4ac4-4eba-8f76-417e965f517f-5c5885d4b8-kz.jpm TRAIN]$ python ./deploy/export.py --config ./yolov7-tiny.yaml --weight runs/2025.07.05-11.28.32/weights/EMA_yolov7-tiny-50_4.ckpt --per_batch_size 1 --file_format ONNX --device_target CPU
/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:549: UserWarning: The value of the smallest subnormal for <class 'numpy.float64'> type is zero.
setattr(self, word, getattr(machar, word).flat[0])
/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for <class 'numpy.float64'> type is zero.
return self._float_to_str(self.smallest_subnormal)
/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:549: UserWarning: The value of the smallest subnormal for <class 'numpy.float32'> type is zero.
setattr(self, word, getattr(machar, word).flat[0])
/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for <class 'numpy.float32'> type is zero.
return self._float_to_str(self.smallest_subnormal)
2025-07-05 12:02:30,849 [WARNING] Parse Model, args: nearest, keep str type
2025-07-05 12:02:30,908 [WARNING] Parse Model, args: nearest, keep str type
2025-07-05 12:02:31,128 [INFO] number of network params, total: 6.032533M, trainable: 6.017694M
[WARNING] ME (46998:2814743264803872, MainProcess):2025-07-05-12:02:42.588.505 [mindspore/train/serialization.py:1956] For 'load_param_into_net', remove parameter prefix name: ema., continue to load.
2025-07-05 12:02:42,606 [INFO] Load checkpoint from [runs/2025.07.05-11.28.32/weights/EMA_yolov7-tiny-50_4.ckpt] success.
Traceback (most recent call last):
File "/home/mindspore/work/mindyolol/TRAIN/./deploy/export.py", line 87, in <module>
export_weight(args)
File "/home/mindspore/work/mindyolol/TRAIN/./deploy/export.py", line 71, in export_weight
from mindyolo.models.layers.activation import SiLU
ImportError: cannot import name 'SiLU' from 'mindyolo.models.layers.activation' (/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/mindyolo/models/layers/activation.py)

3 根因分析

报错为ImportError: cannot import name ‘SiLU’ from ‘mindyolo.models.layers.activation’

from mindyolo.models.layers.activation import SiLU

0.5.0 activation.py代码里面是没有SiLU的类定义

且不更改代码的前提下运行导出是正常的.原本代码里面是没有这个导入的.

4 解决方案

去掉这个导入.或是按照master代码增加SiLU类.

master版本增加了在activation.py增加了 SiLU的定义.

https://github.com/mindspore-lab/mindyolo/blob/master/mindyolo/models/layers/activation.py

class SiLU(nn.Cell):
    """
    Applies the silu linear unit function element-wise.
    """

    def __init__(self):
        """Initialize SiLU."""
        super(SiLU, self).__init__()
        self.fused_op = True

    def construct(self, x):
        if self.fused_op:
            return ops.function.silu(x)
        return x * ops.sigmoid(x)