Calculate the IoU between a Mask and a Box

Do you have any insights on how to calculate the Intersection-over-Union between Instance Segmentation Mask and Bounding box?.

First, Convert Bounding Box to the Segmentation Mask, and then you could use some function like this.

I hope this helps.

I get that, or the other way around. But for RCNN (for example) the bounding box size is (batch, 4 coordinates). How can I turn that into segmentation mask?

In mask rcnn, predicted masks come from a RoI’s of size (batch, channels, roi_width, roi_height). Totally unrelated to the bounding box

If you have coordinates, you can use something like this.

Here you can choose between two formats for the coordinates:

  • Midpoint: x, y, w, h
  • Corners: x1, y1, x2, y2

This way you won’t have to turn it into a segmentation mask.

@Matias_Vasquez a What do you mean by

This way you won’t have to turn it into a segmentation mask.

It is already in mask. After RoI extraction, masks are predicted as well as bounding boxes but in different sizes, just like I mentioned in my previous reply to @gug .

Oh sorry,

what I meant is that you can go in the other direction (as you also said in your second post) and use this format instead of the segmentation masks to get the IoU.

Torchvision has a function to get the bounding boxes out of a mask. (example)

from torchvision.ops import masks_to_boxes

boxes = masks_to_boxes(masks)

So now if you have everything in coordinates you can use the approach in my first post.

But maybe I´m not understanding correctly, if so please let me know if this approach does not

1 Like

I did not know about this. Thank you.

1 Like