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

Lohith_Kumaar: <p>I am getting this error of batch size and i am not able to train the model</p>
<pre><code class="lang-auto">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&gt;best_accuracy:
        torch.save(model.state_dict(),'best_checkpoint.model')
        best_accuracy=test_accuracy

</code></pre>
<p>The error that i am getting is</p>
<blockquote>
<p>ValueError                                Traceback (most recent call last)<br>
Input In [8], in &lt;cell line: 5&gt;()<br>
17 optimizer.zero_grad()<br>
19 outputs=model(images)<br>
—&gt; 20 loss=loss_function(outputs,labels)<br>
21 loss.backward()<br>
22 optimizer.step()</p>
</blockquote>
<p>File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\modules\module.py:1110, in Module._call_impl(self, *input, **kwargs)<br>
1106 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1107 # this function, and just call forward.<br>
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1109         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1110     return forward_call(*input, **kwargs)<br>
1111 # Do not call functions when jit is used<br>
1112 full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>File ~\anaconda3\envs\ownmodel\lib\site-packages\torch\nn\modules\loss.py:1163, in CrossEntropyLoss.forward(self, input, target)<br>
1162 def forward(self, input: Tensor, target: Tensor) → Tensor:<br>
 → 1163     return F.cross_entropy(input, target, weight=self.weight,<br>
1164                            ignore_index=self.ignore_index, reduction=self.reduction,<br>
1165                            label_smoothing=self.label_smoothing)</p>
<p>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)<br>
2994 if size_average is not None or reduce is not None:<br>
2995     reduction = _Reduction.legacy_get_string(size_average, reduce)<br>
 → 2996 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)</p>
<p>ValueError: Expected input batch_size (128) to match target batch_size (8).</p>

Shashank_Agnihotri: <aside class="quote no-group" data-username="Lohith_Kumaar" data-post="1" data-topic="150038">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lohith_kumaar/48/46252_2.png" class="avatar"> Lohith_Kumaar:</div>
<blockquote>
<p><code>outputs=model(images)</code></p>
</blockquote>
</aside>
<p>Hi, after this line, can you please check the sizes of images, labels and outputs with a simple .size()?<br>
And reply with the sizes, so that is it easier to pinpoint where exactly the size mismatch is.</p>

Lohith_Kumaar: <p>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??</p>

Shashank_Agnihotri: <aside class="quote no-group" data-username="Lohith_Kumaar" data-post="1" data-topic="150038">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lohith_kumaar/48/46252_2.png" class="avatar"> Lohith_Kumaar:</div>
<blockquote>
<p><code>model</code></p>
</blockquote>
</aside>
<p>After this line, type the following and run the code:<br>
print(“images: {}, labels: {}, output: {}”.format(images.size(), labels.size(), output.size()))</p>
<p>A good place to learn pytorch is: <a href="https://pytorch.org/tutorials/beginner/pytorch_with_examples.html" class="inline-onebox" rel="noopener nofollow ugc">Learning PyTorch with Examples — PyTorch Tutorials 2.1.1+cu121 documentation</a> and  <a href="https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html" class="inline-onebox" rel="noopener nofollow ugc">Deep Learning with PyTorch: A 60 Minute Blitz — PyTorch Tutorials 2.1.1+cu121 documentation</a></p>

Lohith_Kumaar: <p>Thank you for the help man</p>

Shashank_Agnihotri: <aside class="quote no-group" data-username="Lohith_Kumaar" data-post="1" data-topic="150038">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lohith_kumaar/48/46252_2.png" class="avatar"> Lohith_Kumaar:</div>
<blockquote>
<p><code>outputs=model(images)</code></p>
</blockquote>
</aside>
<p>Sorry, this line, it wasn’t clear in the previous reply.<br>
You are welcome.</p>