RuntimeError: Given groups=1, weight of size [16, 1, 3, 3], expected input[1, 2, 64, 64] to have 1 c

class FastFC(nn.Module):
    def __init__(self, in_depth, out_depth=None, AF='prelu'):
        super().__init__()
        self.in_depth = in_depth // 2
        self.out_depth = out_depth if out_depth is not None else self.in_depth  # 添加这行
        self.AF1 = nn.ReLU() if AF  ==  'relu' else nn.PReLU(self.in_depth)
        self.AF2 = nn.ReLU() if AF  ==  'relu' else nn.PReLU(self.in_depth)
        self.conv_ll = nn.Conv2d(self.in_depth, self.in_depth, 3, padding='same')
        self.conv_lg = nn.Conv2d(self.in_depth, self.in_depth, 3, padding='same')
        self.conv_gl = nn.Conv2d(self.in_depth, self.in_depth, 3, padding='same')

        self.conv_gg = Spectra(self.in_depth, self.out_depth, AF)  # 修改这行

        self.bnaf1 = nn.Sequential(nn.BatchNorm2d(self.in_depth), self.AF1)
        self.bnaf2 = nn.Sequential(nn.BatchNorm2d(self.in_depth), self.AF2)

    def forward(self, x):
        mid = x.shape[1] // 2
        x_loc = x[:, :mid, :, :]  
        x_glo = x[:, mid:, :, :] 
        x_ll = self.conv_ll(x_loc)
        x_lg = self.conv_lg(x_loc)
        x_gl = self.conv_gl(x_glo)

        x_gg = self.conv_gg(x_glo)
        out_loc = torch.add(self.bnaf1(x_ll + x_gl), x_loc)
        out_glo = torch.add(self.bnaf2(x_gg + x_lg), x_glo)
        out = torch.cat((out_loc, out_glo), dim=1)
        return out

在这个代码中,这行代码“x_gl = self.conv_gl(x_glo)”报错
RuntimeError: Given groups=1, weight of size [16, 1, 3, 3], expected input[1, 2, 64, 64] to have 1 channels, but got 2 channels instead

x = torch.randn(1, 3, 64, 64)  # 假设 in_depth 是 3
model = FastFC(3, 16)
out = model(x)

请问是为什么,应该怎么修改

修改:
错误信息表明有一个不匹配问题发生在卷积层 self.conv_gl 中,这是因为该层的权重期望输入数据有1个通道,但实际上提供的输入数据有2个通道。这个问题源于 FastFC 类的定义和它处理通道分割的方式。

问题分析

在类 FastFC 中,self.in_depth 被设定为 in_depth // 2。这意味着如果 in_depth 为奇数,如3,当被除以2时,将产生一个小于原值的整数(此例中为1)。然后,当你试图使用 x[:, :mid, :, :] 和 x[:, mid:, :, :] 来分割通道时,实际分割的结果并不均等。给定 x 的通道数为3,mid 的计算结果将是1,导致 x_loc 有1个通道,而 x_glo 有2个通道。但是,所有卷积层都是基于 self.in_depth(此处为1)来定义的,预期每个输入都只有一个通道。

解决方案
要解决这个问题,确保卷积层的输入通道数与它们的定义匹配。这可以通过确保 in_depth 是偶数来实现,或者通过修改类的结构来适应任意的通道数。

方法1: 确保传递偶数通道数

这是最简单的解决方案,只需要在实例化模型前确保 in_depth 是一个偶数:
下面展示一些 内联代码片

x = torch.randn(1, 4, 64, 64)  # 确保 in_depth 是偶数
model = FastFC(4, 16)
out = model(x)

方法2: 动态调整卷积层来适应任意通道数

如果你需要 FastFC 类能处理任意通道数的输入,你可以动态调整 self.conv_gl 和 self.conv_lg 的输入通道数。这可以通过在类的 init 方法中动态设置卷积层来实现

class FastFC(nn.Module):
    def __init__(self, in_depth, out_depth=None, AF='prelu'):
        super().__init__()
        self.in_depth = in_depth // 2
        self.out_depth = out_depth if out_depth is not None else self.in_depth
        self.AF1 = nn.ReLU() if AF == 'relu' else nn.PReLU(self.in_depth)
        self.AF2 = nn.ReLU() if AF == 'relu' else nn.PReLU(self.in_depth)
        
        # 适应动态通道数的变化
        actual_in_depth_lg = in_depth - self.in_depth
        actual_in_depth_gl = actual_in_depth_lg

        self.conv_ll = nn.Conv2d(self.in_depth, self.in_depth, 3, padding='same')
        self.conv_lg = nn.Conv2d(self.in_depth, self.in_depth, 3, padding='same')
        self.conv_gl = nn.Conv2d(actual_in_depth_gl, self.in_depth, 3, padding='same')  # 修改此处以适应实际输入通道数

        self.conv_gg = Spectra( actual_in_depth_gl , self.out_depth, AF)  # 确保Spectra也适应实际输入

        self.bnaf1 = nn.Sequential(nn.BatchNorm2d(self.in_depth), self.AF1)
        self.bnaf2 = nn.Sequential(nn.BatchNorm2d(self.in_depth), self.AF2)

    def forward(self, x):
        mid = x.shape[1] // 2
        x_loc = x[:, :mid, :, :]  
        x_glo = x[:, mid:, :, :] 
        x_ll = self.conv_ll(x_loc)
        x_lg = self.conv_lg(x_loc)
        x_gl = self.conv_gl(x_glo)

        x_gg = self.conv_gg(x_glo)
        out_loc = torch.add(self.bnaf1(x_ll + x_gl), x_loc)
        out_glo = torch.add(self.bnaf2(x_gg + x_lg), x_glo)
        out = torch.cat((out_loc, out_glo), dim=1)
        return out

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值