RuntimeError: inconsistent tensor sizes at /pytorch/torch/lib/THC/generic/THCTensorMath.cu:141 my data is 7*7 single picture,Thanks!

class Bottleneck(nn.Module):
    def __init__(self, nChannels, growthRate):
        super(Bottleneck, self).__init__()
        interChannels = 4*growthRate
        self.bn1 = nn.BatchNorm2d(nChannels)
        self.conv1 = nn.Conv2d(nChannels, interChannels, kernel_size=1,stride=1,padding=1,
                           bias=False)
        self.bn2 = nn.BatchNorm2d(interChannels)
        self.conv2 = nn.Conv2d(interChannels, growthRate, kernel_size=3,stride=1,
                           padding=1, bias=False)
    def forward(self, x):
        out = self.conv1(F.relu(self.bn1(x)))
        out = self.conv2(F.relu(self.bn2(out)))
        out = torch.cat((x, out), 1)
        return out
class SingleLayer(nn.Module):
    def __init__(self, nChannels, growthRate):
        super(SingleLayer, self).__init__()
        self.bn1 = nn.BatchNorm2d(nChannels)
        self.conv1 = nn.Conv2d(nChannels, growthRate, kernel_size=3,stride=1,
                           padding=1, bias=False)
    def forward(self, x):
        out = self.conv1(F.relu(self.bn1(x)))
        out = torch.cat((x, out), 1)
        return out
class Transition(nn.Module):
    def __init__(self, nChannels, nOutChannels):
        super(Transition, self).__init__()
        self.bn1 = nn.BatchNorm2d(nChannels)
        self.conv1 = nn.Conv2d(nChannels, nOutChannels, kernel_size=1,stride=1,padding=1,
                           bias=False)
    def forward(self, x):
        out = self.conv1(F.relu(self.bn1(x)))
        return out
class DenseNet(nn.Module):
     def __init__(self, growthRate=12, depth=16, reduction=0.5, nClasses=5, bottleneck=True):
     super(DenseNet, self).__init__()

     nDenseBlocks = (depth-4) // 3
     if bottleneck:
         nDenseBlocks //= 2

     nChannels = 2*growthRate
     self.conv1 = nn.Conv2d(1, nChannels, kernel_size=3, padding=1,stride=1,
                           bias=False)
     self.dense1 = self._make_dense(nChannels, growthRate, nDenseBlocks, bottleneck)
     nChannels += nDenseBlocks*growthRate
     nOutChannels = int(math.floor(nChannels*reduction))
     self.trans1 = Transition(nChannels, nOutChannels)

     nChannels = nOutChannels
     self.dense2 = self._make_dense(nChannels, growthRate, nDenseBlocks, bottleneck)
     nChannels += nDenseBlocks*growthRate
     nOutChannels = int(math.floor(nChannels*reduction))
     self.trans2 = Transition(nChannels, nOutChannels)

     nChannels = nOutChannels
     self.dense3 = self._make_dense(nChannels, growthRate, nDenseBlocks, bottleneck)
     nChannels += nDenseBlocks*growthRate

     self.bn1 = nn.BatchNorm2d(nChannels)
     self.fc = nn.Linear(nChannels, nClasses)

    for m in self.modules():
        if isinstance(m, nn.Conv2d):
            n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
            m.weight.data.normal_(0, math.sqrt(2. / n))
        elif isinstance(m, nn.BatchNorm2d):
            m.weight.data.fill_(1)
            m.bias.data.zero_()
        elif isinstance(m, nn.Linear):
            m.bias.data.zero_()

def _make_dense(self, nChannels, growthRate, nDenseBlocks, bottleneck):
    layers = []
    for i in range(int(nDenseBlocks)):
        if bottleneck:
            layers.append(Bottleneck(nChannels, growthRate))
        else:
            layers.append(SingleLayer(nChannels, growthRate))
        nChannels += growthRate
    return nn.Sequential(*layers)

def forward(self, x):
    out = self.conv1(x)
    out = self.trans1(self.dense1(out))
    out = self.trans2(self.dense2(out))
    out = self.dense3(out)
    out = torch.squeeze(F.avg_pool2d(F.relu(self.bn1(out)), 2))
    out = F.log_softmax(self.fc(out))
    return out
densenet=DenseNet(growthRate=12, depth=16, reduction=0.5, nClasses=5, bottleneck=True).cuda()


my network is defined above,my data is 7*7 single picture.
I’ dont know how to solve the error,Thank you so much!

out = torch.cat((x, out), 1) is throwing the error, because the sizes do not match.
Print the sizes of both tensors and you can see why the code is failing.

2 Likes

Sorry, I always don’t understand it,Please explain it in detail!Thanks so much!!!:grinning::grinning::grinning:

The docu says:

All tensors must either have the same shape (except in the cat dimension) or be empty.

This example demonstrates you error:

a = torch.randn(10, 10)
b = torch.randn(10, 20)
c = torch.cat((a, b), dim=0)
>> RuntimeError

This will give the RuntimeError you are seeing, because I tried to cat both tensors in dimension 0, even though the size is different in dimension 1.

However,

c = torch.cat((a, b), dim=1)

will be fine.

1 Like

Firsty,thanks to your explain!I understand "

"
ζ•θŽ·
the size of x:64x24x7x7
the size of the second out:64x12x7x7
so,the size is different in dimension 1
I think ζ•θŽ·is right.
Now,


error is different,I would not fix the error,Could you help me?

It seems that self.fc(out) is throwing the size mismatch error. Check the dimension of this operation.

1 Like

Thanks so much!!!:hugs::hugs::hugs: