Answers.Body: <p>Object detection models usually generate multiple detections per object. Duplicates are removed in a post-processing step called <a href="https://arxiv.org/abs/2002.07662" rel="nofollow noreferrer">Non-Maximum Suppression (NMS)</a>.</p>
  <p>The <code>Pytorch</code> code that performs this post-processing is <a href="https://github.com/pytorch/vision/blob/5339e63148304ce32fd1cbd1e8bb74ea79458691/torchvision/models/detection/rpn.py#L356" rel="nofollow noreferrer">called here</a> in the <code>RegionProposalNetwork</code> class. The filtering <a href="https://github.com/pytorch/vision/blob/5339e63148304ce32fd1cbd1e8bb74ea79458691/torchvision/models/detection/rpn.py#L256-L279" rel="nofollow noreferrer">loop</a> you've mentioned performs the NMS and applies the <code>score_thresh</code> threshold (although it seems to be zero by default).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-08T13:57:03.243Z
Answers.LastActivityDate: 2021-05-08T13:57:03.243Z
Answers.UserDisplayName: Kostya
Body: <p>Recently, I was reading <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" rel="nofollow noreferrer">Pytorch's official tutorial</a> about Mask R-CNN.
  When I run the code on colab, it turned out that it automatically outputs a different number of channels during prediction. If the image has 2 people on it, it would output a mask with the shape <code>2xHxW</code>. If the image has 3 people on it, it would output the mask with the shape <code>3xHxW</code>.</p>
  <p>How does Mask R-CNN change the channels? Does it have a <code>for</code> loop inside it?</p>
  <p>My guess is that it has region proposals and it outputs masks based on those regions, and then it thresholds them (it removes masks that have low probability prediction). Is this right?</p>
  
Tags: <object-detection><semantic-segmentation><mask-rcnn><non-max-suppression>
Title: How does Mask R-CNN automatically output a different number of objects on the image?
UserDisplayName: dato nefaridze
ThreadText: # How does Mask R-CNN automatically output a different number of objects on the image?
  
  (Asked by: username_0 on 2021-05-08)
  
  Recently, I was reading [Pytorch's official tutorial](https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html) about Mask R-CNN.
  When I run the code on colab, it turned out that it automatically outputs a different number of channels during prediction. If the image has 2 people on it, it would output a mask with the shape `2xHxW`. If the image has 3 people on it, it would output the mask with the shape `3xHxW`.
  
  How does Mask R-CNN change the channels? Does it have a `for` loop inside it?
  
  My guess is that it has region proposals and it outputs masks based on those regions, and then it thresholds them (it removes masks that have low probability prediction). Is this right?
  
  **Tags:** object-detection, semantic-segmentation, mask-rcnn, non-max-suppression
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-05-08. Score: 2 | Accepted answer
  
  Object detection models usually generate multiple detections per object. Duplicates are removed in a post-processing step called [Non-Maximum Suppression (NMS)](https://arxiv.org/abs/2002.07662).
  
  The `Pytorch` code that performs this post-processing is [called here](https://github.com/pytorch/vision/blob/5339e63148304ce32fd1cbd1e8bb74ea79458691/torchvision/models/detection/rpn.py#L356) in the `RegionProposalNetwork` class. The filtering [loop](https://github.com/pytorch/vision/blob/5339e63148304ce32fd1cbd1e8bb74ea79458691/torchvision/models/detection/rpn.py#L256-L279) you've mentioned performs the NMS and applies the `score_thresh` threshold (although it seems to be zero by default).
  
  
  ---
  
  