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)
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
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