ValueError: Expected input batch_size (128) to match target batch_size (8)

I am getting this error of batch size and i am not able to train the model

train_path = 'D:/Anaconda Environment/Object Detection Pytorch/Classification/data4/train'
test_path = 'D:/Anaconda Environment/Object Detection Pytorch/Classification/data4/val'

train_loader = DataLoader(
    torchvision.datasets.ImageFolder(train_path, transform=transformer),
    batch_size=8, shuffle=True
)
test_path = DataLoader(
    torchvision.datasets.ImageFolder(train_path, transform=transformer),
    batch_size=4, shuffle=True
)

class Net(nn.Module):
    def __init__(self, num_classes=7):
        super(Net,self).__init__()
        
        
        #Input shape = (64,3,256,256)
        
        self.conv1=nn.Conv2d(in_channels=3,out_channels=12,kernel_size=3,stride=1,padding=1)
        #output = (64,12,256,256)
        self.bn1=nn.BatchNorm2d(num_features=12)
        #output = (64,12,256,256)
        self.relu1=nn.ReLU()
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,12,128,128)
        
        
        self.conv2=nn.Conv2d(in_channels=12,out_channels=24,kernel_size=3,stride=1,padding=1)
        #output = (64,24,128,128)
        self.bn2=nn.BatchNorm2d(num_features=24)
        #output = (64,12,128,128)
        self.relu2=nn.ReLU()
        #output = (64,12,128,128)
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,12,64,64)
        
        
        self.conv3=nn.Conv2d(in_channels=24,out_channels=48,kernel_size=3,stride=1,padding=1)
        #output = (64,48,128,128)
        self.bn3=nn.BatchNorm2d(num_features=48)
        #output = (64,48,128,128)
        self.relu3=nn.ReLU()
        #output = (64,12,128,128)
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,48,32,32)
        
        
        self.conv4=nn.Conv2d(in_channels=48,out_channels=96,kernel_size=3
                             ,stride=1,padding=1)
        #output = (64,96,32,32)
        self.bn4=nn.BatchNorm2d(num_features=96)
        #output = (64,96,32,32)
        self.relu4=nn.ReLU()
        #output = (64,96,32,32)
        self.pool=nn.MaxPool2d(kernel_size=2)
        #Reduse the image by a factor 2
        #output = (64,96,16,16)
    
    
        self.fc=nn.Linear(in_features=96*16*16,out_features=num_classes)
        
        
    def forward(self, input):
            output=self.conv1(input)
            output=self.bn1(output)
            output=self.relu1(output)
            
            output=self.pool(output)
            
            output=self.conv2(output)
            output=self.bn2(output)
            output=self.relu2(output)
            
            output=self.conv3(output)
            output=self.bn3(output)
            output=self.relu3(output)
            
            
            output=self.pool(output)
            
            
            output=self.conv4(output)
            output=self.bn4(output)
            output=self.relu4(output)
            
            #Above ouput will be in the matrix form, with shape (64,96,16,16)
            
            output=output.view(-1, 96*16*16)
            
            output=self.fc(output)    
            
            return output
model = Net(num_classes=7).to(device)

#Optimizer and Loss Function

optimizer = optim.Adam(model.parameters(),lr=0.001, weight_decay=0.001)
loss_function=nn.CrossEntropyLoss()

num_epochs=10

#Model training and saving best model

best_accuracy=0.0

for epoch in range(num_epochs):
    
    #Evaluation and training on training dataset
    model.train()
    train_accuracy=0.0
    train_loss=0.0
    
    for i, (images,labels) in enumerate(train_loader):
        if torch.cuda.is_available():
            images=Variable(images.cuda())
            labels=Variable(labels.cuda())
            
        optimizer.zero_grad()
        
        outputs=model(images)
        loss=loss_function(outputs,labels)
        loss.backward()
        optimizer.step()
        
        
        train_loss+= loss.cpu().data*images.size(0)
        _,prediction=torch.max(outputs.data,1)
        
        train_accuracy+=int(torch.sum(prediction==labels.data))
        
    train_accuracy=train_accuracy/train_count
    train_loss=train_loss/train_count
    
    
    # Evaluation on testing dataset
    model.eval()
    
    test_accuracy=0.0
    for i, (images,labels) in enumerate(test_loader):
        if torch.cuda.is_available():
            images=Variable(images.cuda())
            labels=Variable(labels.cuda())
            
        outputs=model(images)
        _,prediction=torch.max(outputs.data,1)
        test_accuracy+=int(torch.sum(prediction==labels.data))
    
    test_accuracy=test_accuracy/test_count
    
    
    print('Epoch: '+str(epoch)+' Train Loss: '+str(train_loss)+' Train Accuracy: '+str(train_accuracy)+' Test Accuracy: '+str(test_accuracy))
    
    #Save the best model
    if test_accuracy>best_accuracy:
        torch.save(model.state_dict(),'best_checkpoint.model')
        best_accuracy=test_accuracy

The error that i am getting is

ValueError Traceback (most recent call last)
Input In [8], in <cell line: 5>()
17 optimizer.zero_grad()
19 outputs=model(images)
—> 20 loss=loss_function(outputs,labels)
21 loss.backward()
22 optimizer.step()

File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don’t have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
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 = [], []

File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\modules\loss.py:1163, in CrossEntropyLoss.forward(self, input, target)
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)

File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\functional.py:2996, 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)

ValueError: Expected input batch_size (128) to match target batch_size (8).

Hi, after this line, can you please check the sizes of images, labels and outputs with a simple .size()?
And reply with the sizes, so that is it easier to pinpoint where exactly the size mismatch is.

1 Like

Actually i am new to pytorch can you help me with that i don’t know how to check those calculation and if possible can you suggest me a course from where i can learn all this??

After this line, type the following and run the code:
print(“images: {}, labels: {}, output: {}”.format(images.size(), labels.size(), output.size()))

A good place to learn pytorch is: Learning PyTorch with Examples — PyTorch Tutorials 1.11.0+cu102 documentation and Deep Learning with PyTorch: A 60 Minute Blitz — PyTorch Tutorials 1.11.0+cu102 documentation

Thank you for the help man

Sorry, this line, it wasn’t clear in the previous reply.
You are welcome.