Tencent / ncnn

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

pnnx 转换 ECA-attention 结果不对 #4138

Open gs-ren opened 2 years ago

gs-ren commented 2 years ago

error log | 日志或报错信息 | ログ

context | 编译/运行环境 | バックグラウンド

pnnx version:pnnx-20220720-windows ncnn version:pyhton 版本,pip 安装的,1.0.20220729

how to reproduce | 复现步骤 | 再現方法

1.模型取自 https://github.com/BangguWu/ECANet/blob/master/models/eca_mobilenetv2.py

import torch
from torch import nn
from torch.nn.parameter import Parameter

class eca_layer(nn.Module):
    """Constructs a ECA module.
    Args:
        channel: Number of channels of the input feature map
        k_size: Adaptive selection of kernel size
    """
    def __init__(self, channel, k_size=3):
        super(eca_layer, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1) // 2, bias=False) 
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        # feature descriptor on the global spatial information
        y = self.avg_pool(x)

        # Two different branches of ECA module
        y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)

        # Multi-scale information fusion
        y = self.sigmoid(y)

        return x * y.expand_as(x)
  1. 转出来的模型结果为常数,修复 layer Tensor.expand_as not exists or registered 问题后还是会出错

more | 其他 | その他

KleinXin commented 1 year ago

I also failed to transfer onnx eca model to ncnn with error "Shape not supported yet".

'Unsqueeze' OP is not supported by ncnn. It should be simplified with onnx-simplifier https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#simplify-onnx-model https://github.com/daquexian/onnx-simplifier

I did the simplication but still failed.

So I implement unsqueeze in another form which replace the "# Two different branches of ECA module" in forward with

y = self.conv(y.squeeze(3).transpose(2, 1)).transpose(2, 1)[:,:,:,None]

However, it still failed with the same error "Shape not supported yet"

I think I can try to use pnnx.

gs-ren commented 1 year ago

Hi, just try change the implement of ECA to:y = self.conv(y.reshape(batch_size, 1, -1)).reshape(batch_size, -1, 1, 1) , that maybe solve your problem. @KleinXin

nihui commented 2 months 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