Error: About self.pool(x)
QY1994-0919 opened this issue · 4 comments
Hello, I am more interested in the poolformer you proposed, but an error occurred during the use of PoolFormerBlock, as follows:
Traceback (most recent call last):
File "train.py", line 545, in
train(hyp, opt, device, tb_writer)
File "train.py", line 89, in train
model = Model(opt.cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
File "E:\Work\yolov5\models\yolo.py", line 106, in init
m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward
File "E:\Work\yolov5\models\yolo.py", line 138, in forward
return self.forward_once(x, profile) # single-scale inference, train
File "E:\Work\yolov5\models\yolo.py", line 157, in forward_once
x = m(x) # run # 执行网络组件操作
File "C:\conda\conda\envs\torch17\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "E:\Work\yolov5_T23\models\common.py", line 194, in forward
n = self.token_mixer(m)
File "C:\conda\conda\envs\torch17\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "E:\Work\yolov5_T23\models\Confor_VC.py", line 93, in forward
x1 = self.pool(x) - x # x1 = self.pool(x) - x
File "C:\conda\conda\envs\torch17\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "C:\conda\conda\envs\torch17\lib\site-packages\torch\nn\modules\pooling.py", line 594, in forward
return F.avg_pool2d(input, self.kernel_size, self.stride,
TypeError: avg_pool2d(): argument 'kernel_size' (position 2) must be tuple of ints, not bool
I want to put the poolformer behind a ConvBlock and the above problem occurred。
thank you!
Hi @QY1994-0919 , it seems that the error resulted from the instantiation of nn.AvgPool2d. Could you show it?
Hi @yuweihao I did not revise the instantiation of nn.AvgPool2d ,the code of nn.AvgPool2d as follow:
class Pooling(nn.Module):
"""
Implementation of pooling for PoolFormer
--pool_size: pooling size
"""
def init(self, pool_size=3):
super().init()
self.pool = nn.AvgPool2d(pool_size, stride=1, padding=pool_size//2, count_include_pad=False)
"""
pool_size//2 nn.AvgPool2d
self.token_mixer = nn.Conv2d(in_channels=dim, out_channels=dim, kernel_size=3, stride=1, padding=1, groups=dim).
"""
def forward(self, x):
"""
[B, C, H, W] = x.shape Subtraction of the input itself is added since the block already has a residual connection.
"""
print(x.size(), "Pooling_in") #[1, 512, 16, 16]
x1 = self.pool(x) # x = self.pool(x) - x
print(x1.size(), "Pooling_x1")
x = x1 - x
print(x.size(), "Pooling_out")
return x
So I wonder why there is such an error,thank you
Hi @QY1994-0919 , How do you use Pooling?
Thank you! I have solved this problem