Input size(mb) of a 6 x 224 x 224 tensor shows 84GB

username: Wai_Tik_Chan
cooked: <p>Hi there,</p>
<p>Good days everyone, I am trying on a network with input shape of 6x224x224 tensor, however, the torch.summary() report that the input size is around 86436.00 MB which is 84GB , so that it is killed.<br>
I have google around on how is the input size calculated but it didn’t match with the output shows.<br>
Is there any thing wrong in my code ?<br>
Thanks ~~</p>
<p>Dick</p>
<p>Here is the output of torch.summary()</p>
<hr>
<pre><code>    Layer (type)               Output Shape         Param #
</code></pre>
<h1>================================================================<br>
Conv2d-1         [-1, 64, 224, 224]           3,520<br>
ReLU-2         [-1, 64, 224, 224]               0<br>
Conv2d-3         [-1, 64, 224, 224]          36,928<br>
ReLU-4         [-1, 64, 224, 224]               0<br>
MaxPool2d-5         [-1, 64, 112, 112]               0<br>
Conv2d-6        [-1, 128, 112, 112]          73,856<br>
ReLU-7        [-1, 128, 112, 112]               0<br>
Conv2d-8        [-1, 128, 112, 112]         147,584<br>
ReLU-9        [-1, 128, 112, 112]               0<br>
MaxPool2d-10          [-1, 128, 56, 56]               0<br>
Conv2d-11          [-1, 256, 56, 56]         295,168<br>
ReLU-12          [-1, 256, 56, 56]               0<br>
Conv2d-13          [-1, 256, 56, 56]         590,080<br>
ReLU-14          [-1, 256, 56, 56]               0<br>
MaxPool2d-15          [-1, 256, 28, 28]               0<br>
Conv2d-16          [-1, 512, 28, 28]       1,180,160<br>
ReLU-17          [-1, 512, 28, 28]               0<br>
Conv2d-18          [-1, 512, 28, 28]       2,359,808<br>
ReLU-19          [-1, 512, 28, 28]               0<br>
Conv2d-20          [-1, 512, 28, 28]       2,359,808<br>
ReLU-21          [-1, 512, 28, 28]               0<br>
MaxPool2d-22          [-1, 512, 14, 14]               0<br>
Conv2d-23          [-1, 512, 14, 14]       2,359,808<br>
ReLU-24          [-1, 512, 14, 14]               0<br>
Conv2d-25          [-1, 512, 14, 14]       2,359,808<br>
ReLU-26          [-1, 512, 14, 14]               0<br>
Conv2d-27          [-1, 512, 14, 14]       2,359,808<br>
ReLU-28          [-1, 512, 14, 14]               0<br>
MaxPool2d-29            [-1, 512, 7, 7]               0<br>
Linear-30                 [-1, 4096]     102,764,544<br>
ReLU-31                 [-1, 4096]               0<br>
Linear-32                 [-1, 4096]      16,781,312<br>
ReLU-33                 [-1, 4096]               0<br>
Linear-34                    [-1, 1]           4,097</h1>
<h2>Total params: 133,676,289<br>
Trainable params: 133,676,289<br>
Non-trainable params: 0</h2>
<h2>Input size (MB): 86436.00<br>
Forward/backward pass size (MB): 206.27<br>
Params size (MB): 509.93<br>
Estimated Total Size (MB): 87152.20</h2>
<p>Here is the forward code, I have use cat to stack up 2 image to 6 channel<br>
```<br>
def forward(self,leftimage,rightimage):<br>
combine=torch.cat((leftimage,rightimage),1)<br>
combine=self.encoder(combine)<br>
#combine=self.encoder(leftimage)</p>
<pre><code>    #print(combine.shape)
    combine=torch.flatten(combine,1)
    #print(combine.shape)
    combine=self.classifier(combine)
    return combine
</code></pre>
<pre><code class="lang-auto"></code></pre>

username: JuanFMontesinos
cooked: <p>what is your batch size? that’s prob the problem</p>

username: Wai_Tik_Chan
cooked: <p>HI Juan ,</p>
<p>Thanks for your reply. The batch size is 2 which means 4 images(3x224x224) to be load in batch.<br>
I have try to the following config on the model :</p>
<ol>
<li>3 channel -&gt; 0.57MB (3x244x224)</li>
<li>4 channel -&gt; 3.8GB (2x224x224 , 2x224x224)</li>
</ol>
<p>Here is the code for the model :</p>
<pre><code class="lang-auto">class Vgg16PairInput(nn.Module):
    
    def weight_init(self,m):
        classname=m.__class__.__name__
        if classname.find('ConvTran')!=-1:
            m.weight.data.normal_(1,0.5)
    
    def __init__(self):
        super(Vgg16PairInput,self).__init__()
        #self.pretrained_model = models.vgg16(pretrained=True)
        #self.pretrained_model = models.vgg16(pretrained=True)
        self.encoder = nn.Sequential(
                nn.Conv2d(6,64,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(64),
                nn.ReLU(),
                nn.Conv2d(64,64,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(64),
                nn.ReLU(),
                nn.MaxPool2d(kernel_size=2, stride=2), #128 112
                nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(128),
                nn.ReLU(),
                nn.Conv2d(128,128,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(128),
                nn.ReLU(),
                nn.MaxPool2d(kernel_size=2, stride=2), #64 56
                nn.Conv2d(128,256,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(256),
                nn.ReLU(),
                nn.Conv2d(256,256,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(256),
                nn.ReLU(),
                nn.MaxPool2d(kernel_size=2, stride=2), #32 28
                nn.Conv2d(256,512,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(512),
                nn.ReLU(),
                nn.Conv2d(512,512,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(512),
                nn.ReLU(),
                nn.Conv2d(512,512,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(512),
                nn.ReLU(),
                nn.MaxPool2d(kernel_size=2, stride=2), #16 14
                nn.Conv2d(512,512,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(512),
                nn.ReLU(),
                nn.Conv2d(512,512,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(512),
                nn.ReLU(),
                nn.Conv2d(512,512,kernel_size=3,stride=1,padding=1),
                #nn.BatchNorm2d(512),
                nn.ReLU(),
                nn.MaxPool2d(kernel_size=2, stride=2), #8  7                    
                                
        )
        
        
        self.classifier=nn.Sequential(
                nn.Linear(7*7*512,4096),
                nn.ReLU(),
                nn.Linear(4096,4096),
                nn.ReLU(),
                nn.Linear(4096,1)
        )
                        
        #del self.pretrained_model
        #self.encoder.apply(self.weight_init)
        
        
    def encode(self,images):
        code=self.encoder(images)
        return code
    
    
    
    def forward(self,leftimage,rightimage):
        combine=torch.cat((leftimage,rightimage),1)
        combine=self.encoder(combine)
        #combine=self.encoder(leftimage)
        
        #print(combine.shape)
        combine=torch.flatten(combine,1)
        #print(combine.shape)
        combine=self.classifier(combine)
        return combine
</code></pre>
<p>Thanks,<br>
Dick</p>

username: Wai_Tik_Chan
cooked: <p>Hi All,</p>
<p>I have check out on the pytorch-summary source code and find the input size is calculate as follow :</p>
<pre><code class="lang-auto"> # assume 4 bytes/number (float on cuda).
total_input_size = abs(np.prod(sum(input_size, ()))
                           * batch_size * 4. / (1024 ** 2.))
</code></pre>
<p>The 8GB memory seems like it will multiply those 2 tensors shape together :<br>
224 * 224 * 3 * 224 * 224 * 3<br>
Then *4  = 90634715136 / (1024^2) = 86436<br>
which match the display.<br>
So I misunderstand that it would get the input size by following the forward function.</p>
<p>To consider in my case , the input size of the model should be (224 * 244 * 6 * 4)/(1024^2) = 1.148MB ?</p>
<p>Thanks,<br>
Dick</p>

username: JuanFMontesinos
cooked: <p>Hi,<br>
The basic input image (for a 224x224x3 tensor) would be<br>
224<em>224</em>3=150528 elements (numbers)<br>
602112 bytes = 0.574 Gb<br>
So in the end for a Batch size 2 and having 2 images per input we get<br>
0.574<em>2</em>2 = 2.3 Gb</p>
<p>Therefore I think you prob have a bug in the dataset/loader<br>
Soo I would suggest to inspect the shape of the tensor before calling sending the tensor to the gpu as trying to allocate 86 Gb is way too far wrt the size it should be asking.</p>
<p>Another options is that you passed a wrong input to the summary.</p>

username: Wai_Tik_Chan
cooked: <p>Hi Juan,</p>
<p>Sorry for my very late reply and Thanks for your help again.<br>
The data size problem is solved and it is found that the model is not fit for torch summary to calculate the parameter size correctly, and after that my Jetson Nano is gone and I have changed to using Desktop to continuous on the journey, but actually, it is better than using jetson nano as it got more RAM to run instead of 4GB limitation. <img src="https://discuss.pytorch.org/images/emoji/apple/rofl.png?v=9" title=":rofl:" class="emoji" alt=":rofl:"></p>
<p>Furthermore, I have got a little breakthrough on the model as well, if you are interested in, here is the notebook link for your review<br>
<a href="https://www.kaggle.com/tik65536/low-cost-diamond-feature-preliminary-report" class="onebox" target="_blank" rel="nofollow noopener">https://www.kaggle.com/tik65536/low-cost-diamond-feature-preliminary-report</a></p>
<p>I value your comments . <img src="https://discuss.pytorch.org/images/emoji/apple/muscle.png?v=9" title=":muscle:" class="emoji" alt=":muscle:"></p>
<p>Thanks,<br>
Dick</p>