Confusion about Mask R CNN output

babke: <p>Hello,<br>
I am using the pytorch implementation of Mask R-CNN following the object detection finetuning tutorial. I am trying to finetune it so it would be able to perform instance segmentation on images of nano particles (256x256x1). There are only two classes background + nanoparticle.<br>
The model is performing horrendously - validation mAP for ‘bbox’ around 0.1, and mAP for ‘segm’ around 0.06. Right now I’m trying to pinpoint the exact problem on why it is performing so badly and after running the evaluate method(from their github) I noticed that the “masks” output is always 100 masks, even though some of my images have over 4000 masks, why is this 100 value hard-coded and could it be the culprit behind the bad performance?</p>
<p>Below is how I create the model. I configured the min_size and max_size parameters, because my GPU (RTX 3060) was running out of memory quickly while performing calculations with the default values (800,1333).</p>
<pre><code class="lang-auto">from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor

model = torchvision.models.detection.maskrcnn_resnet50_fpn_v2(
    weights='MaskRCNN_ResNet50_FPN_V2_Weights.COCO_V1',
    min_size=(256, ),
    max_size=256,
    trainable_backbone_layers = 2,)

in_features = model.roi_heads.box_predictor.cls_score.in_features

model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes=2)

in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels

hidden_layer = 256
### and replace the mask predictor with a new one
model.roi_heads.mask_predictor = MaskRCNNPredictor(in_features_mask,
                                                   hidden_layer, 2)
device = 'cuda'
</code></pre>
<p>Thank you in advance.</p>

KFrank: <p>Hi Dom!</p>
<aside class="quote no-group quote-modified" data-username="babke" data-post="1" data-topic="174830" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/73ab20/48.png" class="avatar"> babke:</div>
<blockquote>
<p>I am using the pytorch implementation of Mask R-CNN<br>
…<br>
some of my images have over 4000 masks</p>
</blockquote>
</aside>
<p>I am not knowledgeable about Mask R-CNN, but my gut reaction is that<br>
it is not well suited to detecting that many objects in an image.</p>
<p>If your objects don’t normally touch / overlap, I would suggest using<br>
semantic segmentation (e.g., U-Net) followed by some post-processing<br>
(e.g., connected components) to identify the instances.</p>
<p>Also, depending on the details of your use case, you might look into<br>
using something like StarDist.</p>
<p>Best.</p>
<p>K. Frank</p>

babke: <p>Thank you for the information, I’ll look into this if mask R-CNN fails me.</p>
<p>I managed to increase the amount of detections possible in an image, by passing the parameter box_detections_per_img = 4680 to the model, like so:</p>
<pre><code class="lang-auto">model = torchvision.models.detection.maskrcnn_resnet50_fpn_v2(
    weights='MaskRCNN_ResNet50_FPN_V2_Weights.COCO_V1',
    min_size=(256, ),
    max_size=256,
    trainable_backbone_layers = 3,
    box_detections_per_img = 4680)
</code></pre>
<p>The problem with low mAP still persisted though even after these changes. Here’s my last train epoch output before evaluation:</p>
<pre><code class="lang-auto">eta: 0:01:38  lr: 0.000060  loss: 0.5324 (0.5478)  loss_classifier: 0.0323 (0.0407)  loss_box_reg: 0.0924 (0.0906)  loss_mask: 0.2363 (0.2463)  loss_objectness: 0.0285 (0.0281)  loss_rpn_box_reg: 0.1232 (0.1420)  time: 0.4965  data: 0.1733  max mem: 6582
</code></pre>
<p>And the validation evaluation results:</p>
<pre><code class="lang-auto">Accumulating evaluation results...
DONE (t=0.64s).
Accumulating evaluation results...
DONE (t=0.62s).
IoU metric: bbox
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.105
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.148
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.134
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.105
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.001
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.012
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.108
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.108
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000
IoU metric: segm
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.062
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.137
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.042
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.062
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.001
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.009
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.069
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.069
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000
</code></pre>
<p>I even evaluated the train set and got nearly identical results to the ones evaluating validation data. Does anyone know what the problem might be? It’s as if it stops learning at 0.105 mAP for IoU 0.50:0.95.</p>

rauffatali: <p>Hi Dom. I would like to know if you figured it out what was the problem? I am facing with the almost same problem as you. I tested many things, but still getting super bad results with default Mask R-CNN.</p>

mycul: <p>What do the resulting images look like?</p>
<p>I imagine there might be a case of it accidentally predicting the same item multiple times which then lowers the accuracy as one of those predictions gets counted as a false positive. I would visualize the results of the masks and then look into NMS (Non-maximum suppression)</p>

Michael_Chee_Fu_Yoon: <p>Hi, I also got metrics that perform very poor, but I have visualise all prediction masks, it masks correctly. Is there any solution?</p>