Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #189

Open
AngLancer opened this issue Mar 11, 2022 · 4 comments

Comments

@AngLancer
Copy link

大神你好,我是个初学者,在用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变小呢?

@luojunyi520
Copy link

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

@AngLancer
Copy link
Author

我也是初学者,试着回答一下,不对的话请大神指正,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图片通道数本身有这么大吗老哥

@luojunyi520
Copy link

我也是初学者,试着回答一下,不对的话请大神指正,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
Copy link
Author

我也是初学者,试着回答一下,不对的话请大神指正,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,计算每一步的通道数。当然不建议你那么做,因为很麻烦

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants