Tencent / ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform
Other
20.13k stars 4.14k forks source link

关于ReLU6转换出错的问题 #1651

Closed Arctanxy closed 4 years ago

Arctanxy commented 4 years ago

PyTorch提供的MobileNetv2使用了ReLU6这个激活函数,是使用hardtanh实现的,为了转成onnx,我用ReLU改写了一下ReLU6,写成了下面这种形式:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

    def forward(self, x):
        x = F.relu(x)
        x = 6 - F.relu(6 - x)
        return x

转化成ONNX之后,使用Netron打开是下面的形式:

image

但是转成ncnn之后,形式就乱了:

image

而param文件的内容如下:

7767517
5 5
Input            x.1                      0 1 x.1
ReLU             1                        1 1 x.1 1
BinaryOp         3                        1 1 3 0=1
ReLU             4                        1 1 3 4
BinaryOp         6                        1 1 6 0=1

想问下为何会出现这种情况?

我的环境: Pytorch:1.4 onnx:1.6 ncnn:2020年3月26日下载的最新源码

nihui commented 4 years ago

relu6 用 clip 实现

Arctanxy commented 4 years ago

relu6 用 clip 实现 好哒,谢谢

nihui commented 1 month ago

针对onnx模型转换的各种问题,推荐使用最新的pnnx工具转换到ncnn In view of various problems in onnx model conversion, it is recommended to use the latest pnnx tool to convert your model to ncnn

pip install pnnx
pnnx model.onnx inputshape=[1,3,224,224]

详细参考文档 Detailed reference documentation https://github.com/pnnx/pnnx https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#how-to-use-pnnx