RuntimeError: Given input size: (512x1x3). Calculated output size: (512x0x1). Output size is too small

farah_jabeen: <blockquote>
<p>I am working on Cardiac Ct images by implementing the 2DCNN model. I am trying to train vgg model. But every time I got this issue. I know this is related to layers as I am new in PyTorch I am not able to understand. If someone can guide me related to it</p>
</blockquote>
<blockquote>
<p>Here is the error and code. My torch.Size([2, 1, 65, 65]) I am using the dense-net model and it has the same torch.Size([2, 1, 65, 65]) and it’s working fine .</p>
</blockquote>
<blockquote>
<p>VGG_MODEL</p>
</blockquote>
<pre><code class="lang-auto">
Traceback (most recent call last):
  File "ct_pretrained.py", line 201, in &lt;module&gt;
    loss, metric = train(model, train_loader, optimizer)
  File "ct_pretrained.py", line 59, in train
    output = model(axial, sagittal, coronal, emr)
  File "/root/miniconda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/data/heart_ct/torch/models/vgg.py", line 44, in forward
    sagittal_feature = self.sa_co_model(sagittal)
  File "/root/miniconda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/root/miniconda/lib/python3.8/site-packages/torchvision/models/vgg.py", line 43, in forward
    x = self.features(x)
  File "/root/miniconda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/root/miniconda/lib/python3.8/site-packages/torch/nn/modules/container.py", line 117, in forward
    input = module(input)
  File "/root/miniconda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/root/miniconda/lib/python3.8/site-packages/torch/nn/modules/pooling.py", line 153, in forward
    return F.max_pool2d(input, self.kernel_size, self.stride,
  File "/root/miniconda/lib/python3.8/site-packages/torch/_jit_internal.py", line 267, in fn
    return if_false(*args, **kwargs)
  File "/root/miniconda/lib/python3.8/site-packages/torch/nn/functional.py", line 585, in _max_pool2d
    return torch.max_pool2d(
RuntimeError: Given input size: (512x1x3). Calculated output size: (512x0x1). Output size is too small
</code></pre>
<pre><code class="lang-auto">import torch

import torch.nn as nn

from torchvision import models

__all__ = ['Vgg']

class Vgg(nn.Module):

    def __init__(self, is_emr=False, mode='sum'):

        super().__init__()

        self.is_emr = is_emr

        self.mode = mode

        in_dim = 45

        self.axial_model = models.vgg13(pretrained=True)

        out_channels = self.axial_model.features[0].out_channels

        self.axial_model.features[0] = nn.Conv2d(1, out_channels, kernel_size=7, stride=1, padding=0, bias=False)

        self.axial_model.features[3] = nn.MaxPool2d(1)

        num_ftrs = self.axial_model.classifier[6].in_features

        self.axial_model.classifier[6] = nn.Linear(num_ftrs, 15)

        self.sa_co_model = models.vgg13(pretrained=True)

        self.sa_co_model.features[0] = nn.Conv2d(1, out_channels, kernel_size=7, stride=1, padding=(3,0), bias=False)

        self.sa_co_model.features[3] = nn.MaxPool2d(1)

        num_ftrs = self.sa_co_model.classifier[6].in_features

        self.sa_co_model.classifier[6] = nn.Linear(num_ftrs, 15)

        if self.is_emr:

            self.emr_model = EMRModel()

            if self.mode == 'concat': in_dim = 90

        self.classifier = Classifier(in_dim)

        #print(self.classifier)

 

    def forward(self, axial, sagittal, coronal, emr):

        #print(axial.shape)

        axial = axial[:,:,:-3,:-3]

        sagittal = sagittal[:,:,:,:-3]

        coronal = coronal[:,:,:,:-3]

       

        axial_feature = self.axial_model(axial)

        sagittal_feature = self.sa_co_model(sagittal)

        coronal_feature = self.sa_co_model(coronal)

        out = torch.cat([axial_feature, sagittal_feature, coronal_feature], dim=1)

        out = self.classifier(out)

        if self.is_emr:

            emr_feature = self.emr_model(emr)

            out += emr_feature

        return axial_feature

class EMRModel(nn.Module):

    def __init__(self):

        super().__init__()

        self.layer = nn.Sequential(

            nn.Linear(7, 256),

            nn.BatchNorm1d(256),

            nn.LeakyReLU(negative_slope=0.2),

            nn.Dropout(p=0.2, inplace=True),

            nn.Linear(256, 256),

            nn.BatchNorm1d(256),

            nn.LeakyReLU(negative_slope=0.2),

            nn.Dropout(p=0.2, inplace=True),

            nn.Linear(256, 5),

        )

    def forward(self, x):

        return self.layer(x)

class Classifier(nn.Module):

    def __init__(self, in_dim):

        super().__init__()

        self.layer = nn.Sequential(

            nn.Linear(in_dim, 5)

        )

    def forward(self, x):

        return self.layer(x)

class ConvBN(nn.Module):

    def __init__(self, in_dim, out_dim, **kwargs):

        super().__init__()

        self.layer = nn.Sequential(

            nn.Conv2d(in_dim, out_dim, bias=False, **kwargs),

            nn.BatchNorm2d(out_dim),

            nn.LeakyReLU(negative_slope=0.2))

    def forward(self, x):

        return self.layer(x)

'''

if __name__ == "__main__":

     images = torch.randn(2,1,65,65)

     model = Vgg()

     out = model(images,images,images)

     model = models.vgg16(pretrained=True)

     for k, v in model.state_dict().items():

         print(k)

  '''
</code></pre>

ptrblck: <p>The spatial size of the input is too small for the model architecture, as one of the layers would create an empty tensor as given in the error message. Increase the spatial size of the input and it should work.</p>

farah_jabeen: <p>Increase the spatial size mean increase the size of input image?</p>

ptrblck: <p>Yes, you would need to increase the height and width of the input image.</p>

farah_jabeen: <p>thank you for guiding me:)</p>

farah_jabeen: <p>Hi again</p>
<p>Can you guide me a little? I have patches of axial/sagittal/coronal images of size 64 * 64. And when I try to train them using pre-trained models of any CNN models it throws the same message “RuntimeError: Given input size: (512x1x3). Calculated output size: (512x0x1). Output size is too small”<br>
what should I do to avoid it?</p>

ptrblck: <p>A specific layer in your model is trying to reduce the spatial size of the input activation and would return an empty tensor as the input is too small.<br>
This error is usually raised by pooling or conv layers as both are often used to reduce the spatial size of an input activation.<br>
You could reduce the size reduction e.g. by removing some pooling layers (or by using a less aggressive pooling) and in case you are using convs to reduce the spatial size of an activation you could also reduce the dilation in these layers.<br>
As you see, the actual “fix” depends on the model architecture and different approaches would work.</p>