InceptionV3 Transfer Learning -Target size (torch.Size([15])) must be the same as input size (torch.Size([15, 1]))

Hello, I’m trying to implement a pre-trained InceptionV3 model but I keep getting this error:

ValueError: Target size (torch.Size([15])) must be the same as input size (torch.Size([15, 1]))

I’m using BCEWithLogitsLoss and I’m pretty sure this had to do with self.fc = nn.Linear(2048, num_classes). I have a batch of 15 and the outputs should return 15 classifications.

Why is it adding the [1] and how can I remove that?

This is my training code:

for batch_idx, (inputs, targets, filename) in enumerate(trainloader):
        if USE_CUDA:
            inputs = inputs.cuda()
            targets = torch.FloatTensor(np.array(targets).astype(float)).cuda()

        

        optimizer.zero_grad()
        inputs, targets = Variable(inputs), Variable(targets)
        outputs = net(inputs)
        _, preds = torch.max(outputs, 1)
        loss = criterion(outputs, targets)

That one means you are displaying your matrix in a higher dimensional space.

PyTorch usually dedicates one dimension to the batch size.

You can fix it by slicing your tensor

input[:1]

or by adding an aditional dimension to the GT

target.unsqueeze(1)

I did targets = torch.unsqueeze(targets, 1) but now I get:

RuntimeError: output with shape [8, 1] doesn't match the broadcast shape [8, 8]. On my InceptionV3 I pass my num_classes = 1, so I guess it is returning one class for each batch (size = 8) when it should return some probabilistic number for each image of the batch. Can you help me please? I think I’m messing up hard :frowning:

Oh, sorry I though you had 8 classes :slight_smile:

Okay let’s pose it
You have a batch size 8 and your output is a single number, thus you get a 8x1 vector, That’s fine.

The problem could be about how you set the dataloader. Are you using the torch dataloader?
If so, it automatically creates batches.

in the getitem you should return a tensor with a single number (your ground truth in fact) of size [].
So I guess the problem is when pytorch generates the batch you are getting a tensor of size [batch]
This happens because a tensor of a number has no dimensions. To fix it you have to unsqueeze that tensor in getitem
Check this

import torch

a=torch.tensor(11)

a.size()
Out[3]: torch.Size([])

a=a.unsqueeze(0)

a.size()
Out[5]: torch.Size([1])

This way, when the dataloader loads a batch, you will get a tensor of size batchx1 which is what you need to match your output.

The best you can do is to run the dataloader until your batches has that desired shape :slight_smile:

1 Like