awesome-semantic-segmentation-pytorch icon indicating copy to clipboard operation
awesome-semantic-segmentation-pytorch copied to clipboard

RuntimeError: Given groups=1, weight of size [128, 512, 1, 1], expected input[4, 2048, 1, 1] to have 512 channels, but got 2048 channels instead

Open AngLancer opened this issue 2 years ago • 4 comments

大神你好,我是个初学者,在用voc训练psp的时候默认设置可以跑通,但是我想把Ci弄小一点,于是改成了: class _PSPHead(nn.Module): def init(self, nclass, norm_layer=nn.BatchNorm2d, norm_kwargs=None, **kwargs): super(_PSPHead, self).init() self.psp = _PyramidPooling(512, norm_layer=norm_layer, norm_kwargs=norm_kwargs) self.block = nn.Sequential( nn.Conv2d(1024, 128, 3, padding=1, bias=False),#Ci,Co,kernelsize norm_layer(128, **({} if norm_kwargs is None else norm_kwargs)), nn.ReLU(True), nn.Dropout(0.1), nn.Conv2d(128, nclass, 1) ) 运行就报错了: Traceback (most recent call last): File "./train.py", line 335, in trainer.train() File "./train.py", line 224, in train outputs = self.model(images) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/home/cse/lza/awesome-semantic-segmentation-pytorch/core/models/pspnet.py", line 51, in forward x = self.head(c4) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/home/cse/lza/awesome-semantic-segmentation-pytorch/core/models/pspnet.py", line 113, in forward x = self.psp(x) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/home/cse/lza/awesome-semantic-segmentation-pytorch/core/models/pspnet.py", line 87, in forward feat1 = F.interpolate(self.conv1(self.avgpool1(x)), size, mode='bilinear', align_corners=True) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py", line 117, in forward input = module(input) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py", line 423, in forward return self._conv_forward(input, self.weight) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py", line 420, in _conv_forward self.padding, self.dilation, self.groups) RuntimeError: Given groups=1, weight of size [128, 512, 1, 1], expected input[4, 2048, 1, 1] to have 512 channels, but got 2048 channels instead 调了好久都是同类型的错,请问我该如何让Ci变小呢?

AngLancer avatar Mar 11 '22 07:03 AngLancer

我也是初学者,试着回答一下,不对的话请大神指正,nn.Conv2d第一个参数是输入通道数,这个输入通道数是不可改变的,是由你的图片通道数决定的

luojunyi520 avatar Mar 11 '22 10:03 luojunyi520

我也是初学者,试着回答一下,不对的话请大神指正,nn.Conv2d第一个参数是输入通道数,这个输入通道数是不可改变的,是由你的图片通道数决定的

但是代码里本来是这么写的: class _PSPHead(nn.Module): def init(self, nclass, norm_layer=nn.BatchNorm2d, norm_kwargs=None, **kwargs): super(_PSPHead, self).init() self.psp = _PyramidPooling(2048, norm_layer=norm_layer, norm_kwargs=norm_kwargs) self.block = nn.Sequential( nn.Conv2d(4096, 512, 3, padding=1, bias=False), norm_layer(512, **({} if norm_kwargs is None else norm_kwargs)), nn.ReLU(True), nn.Dropout(0.1), nn.Conv2d(512, nclass, 1) ) 第一个参数是4096,我是不想要4096这么大的,voc图片通道数本身有这么大吗老哥

AngLancer avatar Mar 13 '22 12:03 AngLancer

我也是初学者,试着回答一下,不对的话请大神指正,nn.Conv2d第一个参数是输入通道数,这个输入通道数是不可改变的,是由你的图片通道数决定的

但是代码里本来是这么写的: class _PSPHead(nn.Module): def init(self, nclass, norm_layer=nn.BatchNorm2d, norm_kwargs=None, **kwargs): super(_PSPHead, self).init() self.psp = _PyramidPooling(2048, norm_layer=norm_layer, norm_kwargs=norm_kwargs) self.block = nn.Sequential( nn.Conv2d(4096, 512, 3, padding=1, bias=False), norm_layer(512, **({} if norm_kwargs is None else norm_kwargs)), nn.ReLU(True), nn.Dropout(0.1), nn.Conv2d(512, nclass, 1) ) 第一个参数是4096,我是不想要4096这么大的,voc图片通道数本身有这么大吗老哥

   VOC使用的是RGB 图片,本身通道数只有3,随着图片不断进行下采样,图片尺寸缩小,图片通道数会不断增大。下一层卷积的输入通道数是上一层卷积的输出通道数,你改动其中一层卷积的输入输出通道,肯定会造成某一个环节的通道不匹配。
  
   你要想改通道数,当然也是可以的,原始self.psp = _PyramidPooling(2048, norm_layer=norm_layer, norm_kwargs=norm_kwargs)输入2048,输出4096,你设定输入是512,输出是1024,那么你就得一步步修改class _PyramidPooling(nn.Module):这个类里边self.avgpool1,self.avgpool2等池化层的参数,使其满足_PyramidPooling输入是512,输出是1024,计算每一步的通道数。当然不建议你那么做,因为很麻烦

luojunyi520 avatar Mar 13 '22 13:03 luojunyi520

我也是初学者,试着回答一下,不对的话请大神指正,nn.Conv2d第一个参数是输入通道数,这个输入通道数是不可改变的,是由你的图片通道数决定的

但是代码里本来是这么写的: class _PSPHead(nn.Module): def init(self, nclass, norm_layer=nn.BatchNorm2d, norm_kwargs=None, **kwargs): super(_PSPHead, self).init() self.psp = _PyramidPooling(2048, norm_layer=norm_layer, norm_kwargs=norm_kwargs) self.block = nn.Sequential( nn.Conv2d(4096, 512, 3, padding=1, bias=False), norm_layer(512, **({} if norm_kwargs is None else norm_kwargs)), nn.ReLU(True), nn.Dropout(0.1), nn.Conv2d(512, nclass, 1) ) 第一个参数是4096,我是不想要4096这么大的,voc图片通道数本身有这么大吗老哥

   VOC使用的是RGB 图片,本身通道数只有3,随着图片不断进行下采样,图片尺寸缩小,图片通道数会不断增大。下一层卷积的输入通道数是上一层卷积的输出通道数,你改动其中一层卷积的输入输出通道,肯定会造成某一个环节的通道不匹配。
  
   你要想改通道数,当然也是可以的,原始self.psp = _PyramidPooling(2048, norm_layer=norm_layer, norm_kwargs=norm_kwargs)输入2048,输出4096,你设定输入是512,输出是1024,那么你就得一步步修改class _PyramidPooling(nn.Module):这个类里边self.avgpool1,self.avgpool2等池化层的参数,使其满足_PyramidPooling输入是512,输出是1024,计算每一步的通道数。当然不建议你那么做,因为很麻烦

好的,太感谢了老哥!我回头试试

AngLancer avatar Mar 13 '22 13:03 AngLancer