张量的大小必须匹配,维度2除外.得到16和32(违规指数为0)

  • 本文关键字:指数 得到 除外 维度 张量 python
  • 更新时间 :
  • 英文 :


我得到以下错误:

张量的大小必须匹配,除了维度2。得到16和32(违规指数为0)

x1=self.avg_pool(l1)
print('x1:', x1.shape)
x2=self.avg_pool(l2)
print('x2:', x2.shape)
x3=self.avg_pool(l3)
print('x3:', x3.shape)
x4=self.avg_pool(l4)
print('x4:', x4.shape) 
x = self.aspp(x)
print('x:', x.shape)
x=torch.cat((x4,x3,x2,x1,x),dim=1)
cout1=x

我得到x1 x2 x3 x4 x的形状作为

x1: torch.Size([5, 256, 32, 32])
x2: torch.Size([5, 512, 32, 32])
x3: torch.Size([5, 1024, 32, 32])
x4: torch.Size([5, 2048, 32, 32])
x: torch.Size([5, 256, 16, 16])

你在dim=1上连接,这意味着你需要在dim=1上一个接一个地连接张量。如果张量的形状在其他维度上也匹配,那么沿着dim=1拼接后得到的值就是value=256+512+1024+2048+256tensor x的大小应该是x=(5,256,32,32)

没有提供输入(l1, l2, l3, l4的值),所以我只是猜测并试图模仿您的代码。下面的代码段可以正常工作。

import torch
import torch.nn as nn
import torch.nn.functional as F
class Pyramid_Pooling(nn.Module):
def __init__(self, levels, inChans, outChans):
super(Pyramid_Pooling, self).__init__()
self.inChans = inChans
self.outChans = outChans
assert len(levels) == 4
self.pool_4 = nn.AvgPool2d((levels[3], levels[3]))
self.pool_3 = nn.AvgPool2d((levels[2], levels[2]))
self.pool_2 = nn.AvgPool2d((levels[1], levels[1]))
self.pool_1 = nn.AvgPool2d((levels[0], levels[0]))

# lower the number of channels to desired size
self.bottleneck_pyramid = nn.Conv2d(
self.inChans, self.outChans, kernel_size=1
)
def forward(self, en1, en2, en3, en4):
pooled_out_1 = self.pool_1(en1)
print(pooled_out_1.shape)
pooled_out_2 = self.pool_2(en2)
print(pooled_out_2.shape)
pooled_out_3 = self.pool_3(en3)
print(pooled_out_3.shape)
pooled_out_4 = self.pool_4(en4)
print(pooled_out_4.shape)

cat = torch.cat((pooled_out_1, pooled_out_2, pooled_out_3, pooled_out_4), 1)
out = self.bottleneck_pyramid(cat)

return out

我试着猜测你的输入和问题可能是在输入维度。如果你想输出32 x 32,那么输入应该如下所示。还增加了一个1x1的转换,以降低通道到所需的输出。

x_train_0 = torch.randn((3, 256, 32, 32), device = torch.device('cuda'))
x_train_1 = torch.randn((3, 512, 64, 64), device = torch.device('cuda'))
x_train_2 = torch.randn((3, 1024, 128, 128), device = torch.device('cuda'))
x_train_3 = torch.randn((3, 2048, 256, 256), device = torch.device('cuda'))
inChans = x_train_0.shape[1] + x_train_1.shape[1] + x_train_2.shape[1] + x_train_3.shape[1]
outChans = 512
# kernel_size = [1,2,4,8]
pyramid_pooling = Pyramid_Pooling([1, 2, 4, 8], inChans, outChans)
pyramid_pooling.to(torch.device('cuda'))
out = pyramid_pooling(x_train_0, x_train_1, x_train_2, x_train_3)
print(f"Output shape: {out.shape}")

输出如下所示:

torch.Size([3, 256, 32, 32])
torch.Size([3, 512, 32, 32])
torch.Size([3, 1024, 32, 32])
torch.Size([3, 2048, 32, 32])
Output shape: torch.Size([3, 512, 32, 32])

最新更新