【问题标题】:DenseNet, Sizes of tensors must matchDenseNet,张量的大小必须匹配
【发布时间】:2021-03-07 02:32:23
【问题描述】:

您是否知道我如何调整此代码以使张量的大小必须匹配,因为我有此错误:x = torch.cat([x1,x2],1) RuntimeError: Sizes of tensors must match except in dimension 0. Got 32 and 1 (The offending index is 0)

我的图片尺寸为 416x416。

提前感谢您的帮助,

num_classes = 20
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
                
        self.inc = models.inception_v3(pretrained=True)
        self.inc.aux_logits = False

        for child in list(self.inc.children())[:-5]:
            for param in child.parameters():
                param.requires_grad = False

        self.inc.fc = nn.Sequential()
                    
        self.dens121 = models.densenet121(pretrained=True)

        for child in list(self.dens121.children())[:-6]:
            for param in child.parameters():
                param.requires_grad = False

        self.dens121 = nn.Sequential(*list(self.dens121.children())[:-1])
           
        self.SiLU = nn.SiLU()      
        self.linear = nn.Linear(4096, num_classes)
        self.dropout = nn.Dropout(0.2)
        
    def forward(self, x):
        x1 = self.SiLU(self.dens121(x))
        x1 = x1.view(-1, 2048)
        
        x2 = self.inc(x).view(-1, 2048)
        x = torch.cat([x1,x2],1)

        return self.linear(self.dropout(x))

【问题讨论】:

    标签: deep-learning neural-network computer-vision pytorch size


    【解决方案1】:

    两个张量的形状非常不同,这就是torch.cat() 失败的原因。我尝试使用以下示例运行您的代码:

    def forward(self, x):
        x1 = self.SiLU(self.dens121(x))
        x1 = x1.view(-1, 2048)
            
        x2 = self.inc(x).view(-1, 2048)
        print(x1.shape, x2.shape)
        x = torch.cat([x1,x2], dim=1)
    
        return self.linear(self.dropout(x))
    

    这是驱动程序代码

    inputs = torch.randn(2, 3, 416, 416)
    model = Net()
    outputs = model(inputs)
    

    x2的x1的形状如下:

    torch.Size([169, 2048]) torch.Size([2, 2048])
    

    您的 DenseNet 应该输出与 Inceptionv3 的输出相同的形状,反之亦然。 DenseNet 的输出形状为torch.Size([2, 1024, 13, 13]),Inceptionv3 的输出形状为torch.Size([2, 2048])

    编辑 将此行添加到 init 方法中:

    self.conv_reshape= nn.Conv2d(1024, 2048, kernel_size=13, stride=1)
    

    将这些行添加到您的forward()

    x1 = self.SiLU(self.dens121(x))
    
    out = self.conv_reshape(x1)
    x1 = out.view(-1, out.size(1))
    
    x2 = self.inc(x).view(-1, 2048)
    

    【讨论】:

    • 好的,谢谢!那么有没有一种有效的方法以torch.Size([2, 2048])的形式为DenseNet提供输出?或将 torch.Size([2, 1024, 13, 13]) 转换为 torch.Size([2, 2048]) 的方法?
    • 有一些方法可以做到这一点,但我不确定您的模型是否会在这样的操作/技巧/黑客之后学习适当的功能。我已经编辑了我对更改的原始答案。你可以尝试实现它,看看你的模型是否能够学习。
    猜你喜欢
    • 2021-05-12
    • 2019-07-29
    • 2021-03-09
    • 2021-01-26
    • 2021-07-12
    • 2020-11-24
    • 2019-11-09
    • 2020-12-18
    • 2020-12-13
    相关资源
    最近更新 更多