IndexError: The shape of the mask at index 0 does not match the shape of the indexed tensor index 1

username: sigma_x
cooked: <p>The image is size [3, H, W], there are a total 63 classes.</p>
<p>IF there are 5 objects in the image, labels are size [5], bounding boxes size [5,4], masks [5,H,W]. Why do I get this error message:</p>
<p>IndexError: The shape of the mask [14] at index 0 does not match the shape of the indexed tensor [14, 63, 28, 28] at index 1</p>
<p>Where does 14 come from? Here’s the architecture of the model, it seems correct and it worked on other problem. I don’t understand this error at all.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/3/83e5694b979de39c23a1c1aed8aba4b82dbcf877.png" data-download-href="https://discuss.pytorch.org/uploads/default/83e5694b979de39c23a1c1aed8aba4b82dbcf877" title="mask_problem"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/3/83e5694b979de39c23a1c1aed8aba4b82dbcf877_2_690x487.png" alt="mask_problem" data-base62-sha1="iOO3IZQHScJejCgKFQmWvwISR3p" width="690" height="487" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/3/83e5694b979de39c23a1c1aed8aba4b82dbcf877_2_690x487.png, https://discuss.pytorch.org/uploads/default/original/3X/8/3/83e5694b979de39c23a1c1aed8aba4b82dbcf877.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/3/83e5694b979de39c23a1c1aed8aba4b82dbcf877.png 2x" data-dominant-color="3C1831"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">mask_problem</span><span class="informations">883×624 90.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: albanD
cooked: <p>Isn’t 14 your batch size?</p>

username: sigma_x
cooked: <p>What do you mean? Theres 1 image with 5 objects/labels/bboxes/masks. I have no idea where 14 comes. 63 is the number of classes</p>

username: albanD
cooked: <p>It seems like both the mask and the indexed Tensor have a size 14 for the 0th dimension. In neural network, that usually correspond to batch.</p>
<p>If it does not, could you make a small code sample that we can run that reproduces the issue?</p>

username: sigma_x
cooked: <p>It cant be small, it’s pretty much the whole dataset + training script. The sizes of the labels are specified above. My other code for Mask rcnn doesn’t report error like this. I found a couple of masks with all 0s, that’s not what’s causing it.</p>
<p>To begin with i dont understand where 14 comes from, it’s not a number that pops up anywhere in my code. It’s something mask rcnn does internally, maybe fpn output? the intended output size of mask module during training is num-class×28×28.</p>

username: sigma_x
cooked: <p>Alright, I found it! Nothing to do with sizes at all! Turns out, formats of tensors were incorrect. I changed to:</p>
<p>self.lab[‘labels’] = torch.tensor(labels, dtype=<strong>torch.int64</strong>)<br>
self.lab[‘boxes’] = torch.tensor(boxes, dtype = <strong>torch.float</strong>)<br>
self.lab[‘masks’] = torch.tensor(masks, dtype = <strong>torch.uint8</strong>)</p>
<p>Before that they all were uint8. Damn it!</p>

username: Sam_Shamsan1
cooked: <p>I am having the same issue when applying the loss function in this GitHub:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/alexa/dialoglue/blob/master/bert_models.py">
  <header class="source">

      <a href="https://github.com/alexa/dialoglue/blob/master/bert_models.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/alexa/dialoglue/blob/master/bert_models.py" target="_blank" rel="noopener nofollow ugc">alexa/dialoglue/blob/master/bert_models.py</a></h4>


      <pre><code class="lang-py">import torch
import torch.nn.functional as F

from collections import defaultdict
from torch import nn
from torch.nn import CrossEntropyLoss, NLLLoss 
from torch.nn import Dropout
from transformers import BertConfig, BertModel, BertForMaskedLM
from typing import Any

class BertPretrain(torch.nn.Module):
    def __init__(self,
                 model_name_or_path: str):
        super(BertPretrain, self).__init__()
        self.bert_model = BertForMaskedLM.from_pretrained(model_name_or_path)

    def forward(self, 
                input_ids: torch.tensor,
                mlm_labels: torch.tensor):
        outputs = self.bert_model(input_ids, masked_lm_labels=mlm_labels)
</code></pre>



  This file has been truncated. <a href="https://github.com/alexa/dialoglue/blob/master/bert_models.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I have tried changing the types by no luck. what do you think the issue is</p>
<p><a href="/uploads/short-url/A5idaRh2lmAz1yEvGi8ZGfbsAnd.png">Screen Shot 2021-04-20 at 10.14.36 PM|690x254</a></p>