Getting size mismatch error

I am creating designing a Convolutional Neural Network to predict labels of the images, using CIFAR10.

num_workers = 2
# how many samples per batch to load
BATCH_SIZE = 4
# percentage of training set to use as validation
valid_size = 0.2

# convert data to a normalized torch.FloatTensor
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((.5,.5,.5),(.5,.5,.5))])

# choose the training and test datasets
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)

# prepare data loaders (combine dataset and sampler)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=num_workers)
testloader = torch.utils.data.DataLoader(testset, batch_size=BATCH_SIZE, shuffle=False, num_workers=num_workers)

rgb_img = np.squeeze(images[3])

channels = ['red channel', 'green channel', 'blue channel']

fig = plt.figure(figsize = (36, 36)) 
for idx in np.arange(rgb_img.shape[0]):
    ax = fig.add_subplot(1, 3, idx + 1)
    img = rgb_img[idx]
    ax.imshow(img, cmap='gray')
    ax.set_title(channels[idx])
    width, height = img.shape
    thresh = img.max()/2.5

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(400, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
        
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
# create a complete CNN
net = Net()
print(net)

# move tensors to GPU if CUDA is available
if train_on_gpu:
    net.cuda()

n_epochs = 10           
BATCH_SIZE = 20
LR = 0.001    

# specify loss function
loss_func = nn.CrossEntropyLoss()

# specify optimizer
optimizer = optim.SGD(model.parameters(), lr=LR)

net.train()
for data, target in trainloader:
        # move tensors to GPU if CUDA is available
    if train_on_gpu:
            data, target = data.cuda(), target.cuda()
        # clear the gradients of all optimized variables
    optimizer.zero_grad()
        # forward pass: compute predicted outputs by passing inputs to the model
    output = net(data)
        # calculate the batch loss
    loss = loss_func(output, target)
        # backward pass: compute gradient of the loss with respect to model parameters
    loss.backward()
        # perform a single optimization step (parameter update)
    optimizer.step()
        # update training loss
    train_loss += loss.item()*data.size(0)

# validate the model
net.eval()
for data, target in testloader:
        # move tensors to GPU if CUDA is available
    if train_on_gpu:
        data, target = data.cuda(), target.cuda()
        # forward pass: compute predicted outputs by passing inputs to the model
    output = net(data)[0]
        # calculate the batch loss
    loss = loss_func(output, target)
        # update average validation loss 
    valid_loss += loss.item()*data.size(0)
    
# calculate average losses
train_loss = train_loss/len(trainloader.dataset)
valid_loss = valid_loss/len(testloader.dataset)
train_losslist.append(train_loss)
        
# print training/validation statistics 
print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
        epoch, train_loss, valid_loss)) 

Getting a mismatch error

RuntimeError                              Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8172/3310213662.py in <module>
      8     output = net(data)[0]
      9         # calculate the batch loss
---> 10     loss = loss_func(output, target)
     11         # update average validation loss
     12     valid_loss += loss.item()*data.size(0)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

~\anaconda3\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
   1161 
   1162     def forward(self, input: Tensor, target: Tensor) -> Tensor:
-> 1163         return F.cross_entropy(input, target, weight=self.weight,
   1164                                ignore_index=self.ignore_index, reduction=self.reduction,
   1165                                label_smoothing=self.label_smoothing)

~\anaconda3\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
   2994     if size_average is not None or reduce is not None:
   2995         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2996     return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
   2997 
   2998 

RuntimeError: size mismatch (got input: [10], target: [4])

Usually these shape mismatches are caused by a wrong view operation, which would change the batch size.
In your code you are using:

x = x.view(-1, 16 * 5 * 5)

which might indeed change the size of dim0.
Use

x = x.view(x.size(0), -1)

instead and check for shape mismatches in self.fc1. If a shape mismatch is raised in self.fc1 set the in_features to the expected value (the feature dimension of the input activation, i.e. x.size(1) after the flattening operation).

1 Like

Thank you so much!

If you could help me again… I got another error

IndexError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10600/2418918815.py in
23 # calculate test accuracy for each object class
24 for i in range(BATCH_SIZE):
—> 25 label = target.data[i]
26 class_correct[label] += correct[i].item()
27 class_total[label] += 1

IndexError: index 4 is out of bounds for dimension 0 with size 4

Blockquote

`test_loss = 0.0
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))

net.eval()
# iterate over test data
for data, target in testloader:
    # move tensors to GPU if CUDA is available
    if train_on_gpu:
        data, target = data.cuda(), target.cuda()
    # forward pass: compute predicted outputs by passing inputs to the model
    output = net(data)
    # calculate the batch loss
    loss = loss_func(output, target)
    # update test loss 
    test_loss += loss.item()*data.size(0)
    # convert output probabilities to predicted class
    _, pred = torch.max(output, 1)    
    # compare predictions to true label
    correct_tensor = pred.eq(target.data.view_as(pred))
    correct = np.squeeze(correct_tensor.numpy()) if not train_on_gpu else np.squeeze(correct_tensor.cpu().numpy())
    
    # calculate test accuracy for each object class
    for i in range(BATCH_SIZE):
        label = target.data[i]
        class_correct[label] += correct[i].item()
        class_total[label] += 1`

Check the shape of class_total and make sure that al valid label indices are able to be used as an index.
Currently it seems class_total has a size of 4 and will thus accept indices as [0, 1, 2, 3] while you are passing an index as 4 to it.

1 Like