BUG解决:RuntimeError: Given groups=1, weight of size [14, 464, 1, 1], expected input[16, 116, 56, 1] to have 464 channels, but got 116 channels instead
首选说一下这个问题,这个问题提示想要得到的是464个通道数但是实际上得到的是116个通道。
例如我给某个深度学习网络中加CBAM注意力集中机制,具体可参照此文章链接: link.(以下为实现代码):

# 通道注意力机制
class ChannelAttention(nn.Module):def __init__(self, in_planes, ratio=16):super(ChannelAttention, self).__init__()self.avg_pool = nn.AdaptiveAvgPool2d(1)self.max_pool = nn.AdaptiveMaxPool2d(1)self.fc1 = nn.Conv2d(in_planes, in_planes //ratio, 1, bias=False)self.relu1 = nn.ReLU()self.fc2 = nn.Conv2d(in_planes //ratio, in_planes, 1, bias=False)self.sigmoid = nn.Sigmoid()def forward(self, x):avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))out = avg_out + max_outreturn self.sigmoid(out)# 空间注意力机制
class SpatialAttention(nn.Module):def __init__(self, kernel_size=7):super(SpatialAttention, self).__init__()assert kernel_size in (3, 7), 'kernel size must be 3 or 7'padding = 3 if kernel_size == 7 else 1self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)self.sigmoid = nn.Sigmoid()def forward(self, x):avg_out = torch.mean(x, dim=1, keepdim=True)max_out, _ = torch.max(x, dim=1, keepdim=True)x = torch.cat([avg_out, max_out], dim=1)x = self.conv1(x)return self.sigmoid(x)

问题出现的可能原因1:

 # 在网络的某层加入CBAM注意力机制self.ca = ChannelAttention(self.inplanes)self.sa = SpatialAttention()

self.inplanes修改为你上一层输出的通道数;
出现原因2:是我在假的过程中出现的错误,是一个非常小的错误,就是在初始层和末尾分别加入CBAM的时候,没有区分不同位置加入后的函数名,因此出现错误,例如如下:

 # 在网络的第一层加入CBAM注意力机制self.ca = ChannelAttention(self.inplanes)self.sa = SpatialAttention()# 在网络的最后层加入CBAM注意力机制self.ca1 = ChannelAttention(self.inplanes)self.sa1 = SpatialAttention()

哎!!!!需要区分函数名;
出现原因3:如果不是在开始或者最后层加入的注意力机制,而是在网络结构中加入,例如可以在resnet中的残差结构中,加入后可print(model)看一看是不是和自己想的一样,我出现的问题是我想在每个block中加入注意力集中机制,因此把加入的部分写在的模型结构的block中,结果也出现了2所出现的问题,原因还是和2一样。

总结:其实加入注意力集中机制还是比较容易的,仔细再仔细,一定没问题,共勉。

附集中注意力集中机制实现代码(PYTORCH):

#SE
class SELayer(nn.Module):def __init__(self, c1, r=16):super(SELayer, self).__init__()self.avgpool = nn.AdaptiveAvgPool2d(1)self.l1 = nn.Linear(c1, c1 // r, bias=False)self.relu = nn.ReLU(inplace=True)self.l2 = nn.Linear(c1 // r, c1, bias=False)self.sig = nn.Sigmoid()def forward(self, x):b, c, _, _ = x.size()y = self.avgpool(x).view(b, c)y = self.l1(y)y = self.relu(y)y = self.l2(y)y = self.sig(y)y = y.view(b, c, 1, 1)return x * y.expand_as(x)
# ECA注意力机制
class eca_layer(nn.Module):"""Constructs a ECA module.Args:channel: Number of channels of the input feature mapk_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 informationy = self.avg_pool(x)# Two different branches of ECA moduley = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)# Multi-scale information fusiony = self.sigmoid(y)x=x*y.expand_as(x)return x * y.expand_as(x)
#CoorAttention
class h_sigmoid(nn.Module):def __init__(self, inplace=True):super(h_sigmoid, self).__init__()self.relu = nn.ReLU6(inplace=inplace)def forward(self, x):return self.relu(x + 3) / 6class h_swish(nn.Module):def __init__(self, inplace=True):super(h_swish, self).__init__()self.sigmoid = h_sigmoid(inplace=inplace)def forward(self, x):return x * self.sigmoid(x)class CoordAtt(nn.Module):def __init__(self, inp, oup, reduction=32):super(CoordAtt, self).__init__()self.pool_h = nn.AdaptiveAvgPool2d((None, 1))self.pool_w = nn.AdaptiveAvgPool2d((1, None))mip = max(8, inp // reduction)self.conv1 = nn.Conv2d(inp, mip, kernel_size=1, stride=1, padding=0)self.bn1 = nn.BatchNorm2d(mip)self.act = h_swish()self.conv_h = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)self.conv_w = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)def forward(self, x):identity = xn, c, h, w = x.size()x_h = self.pool_h(x)x_w = self.pool_w(x).permute(0, 1, 3, 2)y = torch.cat([x_h, x_w], dim=2)y = self.conv1(y)y = self.bn1(y)y = self.act(y)x_h, x_w = torch.split(y, [h, w], dim=2)x_w = x_w.permute(0, 1, 3, 2)a_h = self.conv_h(x_h).sigmoid()a_w = self.conv_w(x_w).sigmoid()out = identity * a_w * a_hreturn out

RuntimeError: Given groups=1, weight of size [14, 464, 1, 1], expected input问题解决相关推荐

  1. RuntimeError: Given groups=1, weight of size [1, 1, 3, 3], expected input[1, 3, 1402, 1200] to have

    RuntimeError: Given groups=1, weight of size [1, 1, 3, 3], expected input[1, 3, 1402, 1200] to have ...

  2. (36)RuntimeError: Given groups=4, weight of size [4, 1, 11, 11], expected input xxxxxxxxx

    问题描述: 在测试AODNet去雾网络时,计算测试集的指标ssim和psnr,从test_loader中读取测试集的清晰图像和去雾之后的图像,作为ssim的输入进行计算,原文代码如下: for ite ...

  3. Pytorch RuntimeERROR: Given groups=1 weights of size [256,64,1,1] expected input[1,16,256,256] to

    错误 Pytorch RuntimeERROR: Given groups=1 weights of size [256,64,1,1] expected input[1,16,256,256] to ...

  4. BUG解决:RuntimeError:Given groups=1,weight of size...expected input...but got 3 channels instead.

    https://www.codeleading.com/article/31383072717/

  5. 成功解决IndexError: index 14 is out of bounds for axis 1 with size 14

    成功解决IndexError: index 14 is out of bounds for axis 1 with size 14 目录 解决问题 解决思路 解决方法 解决问题 IndexError: ...

  6. [pcl::VoxelGrid::applyFilter] Leaf size is too small for the input dataset. Integer indices would ov

    1. 报错日志: Python-pcl 点云下采样时报错如下: [pcl::VoxelGrid::applyFilter] Leaf size is too small for the input d ...

  7. 【Python】解决CNN中训练权重参数不匹配size mismatch for fc.weight,size mismatch for fc.bias

    目录 1.问题描述 2.问题原因 3.问题解决 3.1思路1--忽视最后一层权重 额外说明:假如载入权重不写strict=False, 直接是model.load_state_dict(pre_wei ...

  8. RuntimeError: cuDNN error: CUDNN_STATUS_EXECUTION_FAILED,以及tensorflow1.14.0+torch1.2.0+CUDA10.0配置

    环境: Anaconda3-5.2.0 python3.6.5 初始tensorflow版本是1.8.0,cuda9.0,升级tensorflow至1.10.0时,出现torch1.1.0的运行错误: ...

  9. ubuntu 14.04 使用apt-get出现如下问题解决办法

    Some packages could not be installed. This may mean that you have requested an impossible situation ...

最新文章

  1. 8.27 直播| 挖掘传统行业日志大数据的无限价值
  2. SpringBoot无法书写主启动类的情况之一
  3. web頁面優化以及SEO
  4. Python——文本进度条
  5. 浏览器工作原理与实践学习笔记
  6. 作者:王小兵,男,农业部市场与经济信息司副司长。
  7. Java中引入泛型的好处
  8. 基于以太坊的去中心化存储协议Swarm计划在2季度发布1.0版
  9. 大鱼风控笔记 1:量化风控体系的风险板块
  10. 现在物价虽然高得离谱,但是内存条都白菜价格了,需要调整程序架构的思维“与时俱进” --- 改进系列之一...
  11. 使用SAX读取XML文件
  12. 【最优化导论】全局搜索算法
  13. 暑假教师计算机培训总结,教师信息化培训心得(精选5篇)
  14. 鸿蒙系统是怎样一种系统,鸿蒙系统pc版怎么安装 鸿蒙系统pc版安装教程
  15. 【操作系统实验】模拟单处理器系统的进程调度
  16. Ubuntu 18.4 qt5.12 安装搜狗拼音输入法
  17. 【239天】网易云课堂计算机专业基础课程系列——计算机专业导论(4)
  18. CF785C (1600)
  19. 具有连续调制光栅区域的光波导优化
  20. 记录|深度学习100例-卷积神经网络(CNN)彩色图片分类 | 第2天

热门文章

  1. matlab 随机数有效数字,MATLAB中生成随机数方法总结
  2. 图解隐马尔可夫模型(HMM)
  3. [StackExchange]Redis 发布订阅
  4. Android中webview拨打加载网页中的电话超链接
  5. 使用Python进行自动化录屏
  6. ehcach文件报错_java - 分布式缓存-Terracotta和Ehcache-客户端错误:无法创建工具包 - 堆栈内存溢出...
  7. 想要搞懂数据可视化,看这篇就够了!
  8. 服务器的寿命到底有多长
  9. 《硅谷》码农必看的一部美剧
  10. Ubuntu安装Hadoop3.1.3教程