Mask RCNN Loss is NaN

Nikolas_Pitsillos: <p>I am following this <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" rel="nofollow noopener">tutorial</a> and I have only changed the number of classes.  Mine is 13.  Now I have also added another transformation to resize the images because they were too large.  I am training on a single GPU with a batch size of 1 and a learning rate of 0.005 but lowering still results in a Loss is NaN.  I haven’t tried gradient clipping or normalisation because I am not really certain how to do it in the pre-implemented architecture.  Additionally my dataset consists of single objects within the image.  Could it be that due to the fact that the tensor is sparse, this causes the loss to behave in this way?</p>

ptrblck: <p>Is the loss growing until it eventually yields a NaN values or do you encounter the NaN just in a single step?</p>

EboRobert: <p>In just a single step. What could possibly be wrong?</p>

mdtjan: <p>Hey I also experienced the same thing. Did you solve this already?</p>

brix: <p>Hi, I am having the same issue using 15 classes.<br>
Has anyone found a solution?</p>
<p>Thanks</p>

satrya-sabeni: <p>Same issue with, I just get this after one step:</p>
<pre><code class="lang-auto">Loss is nan, stopping training
{'loss_classifier': tensor(0.9567, device='cuda:0', grad_fn=&lt;NllLossBackward&gt;), 'loss_box_reg': tensor(nan, device='cuda:0', grad_fn=&lt;DivBackward0&gt;), 'loss_mask': tensor(2.6179, device='cuda:0', grad_fn=&lt;BinaryCrossEntropyWithLogitsBackward&gt;), 'loss_objectness': tensor(13.4737, device='cuda:0',
       grad_fn=&lt;BinaryCrossEntropyWithLogitsBackward&gt;), 'loss_rpn_box_reg': tensor(18.5134, device='cuda:0', grad_fn=&lt;DivBackward0&gt;)}
</code></pre>
<p>What does this mean?</p>

satrya-sabeni: <p>Reducing the learning rate, helps to get it further but it still stops after 3 steps</p>

satrya-sabeni: <p>I was able to fix it for my use case, for anyone out there who might have the same. The problem was that I had quite a lot of very small pixel regions/boxes which messed things up. The way the script I use works, it creates box for every isolated pixel region, even if it just the size of 3 pixels. So if you have masks with a lot of very small pieces of mask you probably have the same problem as I did. I removed all isolates pieces of the mask which surface’s are smaller than 2500 pixels. Now the training works well, without having to reduce the learning rate.</p>

azer: <p>Hey would you mind sharing your implementation I also have NaN  values.</p>

satrya-sabeni: <p>I’m sorry I don’t have the code at hand anymore. Have you already tried reducing learning rate, and are you sure that your dataset doesn’t contains labels that are super small?</p>

emcp: <p>I am getting the same error… on a different tensor <a href="https://discuss.pytorch.org/t/having-trouble-migrating-to-torchvision-fasterrcnn/79150" class="inline-onebox">Having trouble migrating to torchvision FasterRCNN</a>  without any mask data added yet.</p>
<p>seems the masks are NOT optional and perhaps I need to re-add it to my dataset and loader ?</p>

emcp: <p>I found out my mistake … check that you’re properly calculating the box min_max… for COCO i was under the assumption it was just part of the datasets data… when in fact you need something like this :</p>
<pre><code class="lang-auto">        # get bounding box coordinates for each mask
        num_targets = len(targets)
        boxes = []
        for i in range(num_targets):
            box = targets[i]["bbox"]
            xmin = box[0]
            xmax = box[0] + box[2]
            ymin = box[1]
            ymax = box[1] + box[3]
            boxes.append([xmin, ymin, xmax, ymax])
</code></pre>

Unity05: <p>Thank you so much!<br>
I’ve been thinking about a solution for quite a while now, and yeah, the given targets had the shape [x_min, y_min, w, h]. <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=9" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:"></p>

paschalis_m: <p>As <a class="mention" href="/u/satrya-sabeni">@satrya-sabeni</a>  mentioned, reducing the learning rate, worked for me as well…</p>

SriniMaiya: <p>I think normalizing the images plays a major role(Saying this because I had correct annotations and a moderately low learning rate). Especially when they are gray images or images which is dominant with a type of color.</p>
<p>From my experience, the loss_objectness was shooting up to ‘nan’ during the warmup phase and the initial loss was around 2400.<br>
Once I normalized the tensors, the warmup epoch started with a loss of 22 instead of 2400.</p>
<p>After normalizing the images, I can start the training with a learning rate of 0.001 without the nan problems.</p>

engineman285: <p>Thank you for your advice. For a general understanding, please tell me why to normalize twice. If there is a GeneralizedRCNNTransform in the Key point RC itself that does it repeatedly.</p>
<pre><code class="lang-auto">KeypointRCNN(
  (transform): GeneralizedRCNNTransform(
      Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
      Resize(min_size=(640, 672, 704, 736, 768, 800), max_size=1333, mode='bilinear')
  )
......
</code></pre>