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

The image is size [3, H, W], there are a total 63 classes.

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:

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

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.

Isn’t 14 your batch size?

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

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.

If it does not, could you make a small code sample that we can run that reproduces the issue?

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.

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.

Alright, I found it! Nothing to do with sizes at all! Turns out, formats of tensors were incorrect. I changed to:

self.lab[‘labels’] = torch.tensor(labels, dtype=torch.int64)
self.lab[‘boxes’] = torch.tensor(boxes, dtype = torch.float)
self.lab[‘masks’] = torch.tensor(masks, dtype = torch.uint8)

Before that they all were uint8. Damn it!

1 Like

I am having the same issue when applying the loss function in this GitHub:

I have tried changing the types by no luck. what do you think the issue is

Screen Shot 2021-04-20 at 10.14.36 PM|690x254