Torchvision Mask-rcnn with Resnext101 backbone occur Nan loss during the training

YeongHwa_Jin: <p>Hi!<br>
When I train mask rcnn with resnext101 backbone, Loss goes to Nan.<br>
My code is made from pytorch tutorial.</p>
<pre><code class="lang-auto">backbone = resnet_fpn_backbone('resnext101_32x8d', pretrained=True, trainable_layers=5)
model = MaskRCNN(backbone, num_classes)
    in_features = model.roi_heads.box_predictor.cls_score.in_features
    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
    in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels
    hidden_layer = 256
    model.roi_heads.mask_predictor = MaskRCNNPredictor(in_features_mask,
                                                       hidden_layer,
                                                       num_classes)
</code></pre>
<p>error message :</p>
<pre><code class="lang-auto">Epoch: [0]  [   0/1388]  eta: 0:23:07  lr: 0.000010  loss: 2.0952 (2.0952)  loss_classifier: 0.6831 (0.6831)  loss_box_reg: 0.0190 (0.0190)  loss_mask: 0.6889 (0.6889)  loss_objectness: 0.6918 (0.6918)  loss_rpn_box_reg: 0.0123 (0.0123)  time: 0.9999  data: 0.3882  max mem: 0
Loss is nan, stopping training
{'loss_classifier': tensor(nan, device='cuda:1', grad_fn=&lt;NllLossBackward&gt;), 'loss_box_reg': tensor(nan, device='cuda:1', grad_fn=&lt;DivBackward0&gt;), 'loss_mask': tensor(nan, device='cuda:1', grad_fn=&lt;BinaryCrossEntropyWithLogitsBackward&gt;), 'loss_objectness': tensor(0.6931, device='cuda:1', grad_fn=&lt;BinaryCrossEntropyWithLogitsBackward&gt;), 'loss_rpn_box_reg': tensor(0.0050, device='cuda:1', grad_fn=&lt;DivBackward0&gt;)}
</code></pre>
<p>When I change the backbone to resnet50 or resnet101, no error occurs.</p>
<p>I try to reduce lr but error still occurs with resnext-like.<br>
How can I solve it?</p>

ptrblck: <p>You could check the forward activations for invalid values via forward hooks as described <a href="https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6">here</a>. Once you’ve isolated which layer creates the NaN outputs, check it’s inputs as well as parameters.<br>
If the parameters show invalid values, most likely the gradients were too large, the model was diverging, and the parameters were overflowing. On the other hand, if the inputs are containing NaNs, check the previous operation and see, if/how it could create invalid values.</p>

YeongHwa_Jin: <p>As you saying, I checked the outputs of layers, but I didn’t find any invalid values. Instead, I found loss is very large value on first epoch, first batch:</p>
<pre><code class="lang-auto">{'loss_classifier': tensor(2.4077e+09, grad_fn=&lt;NllLossBackward&gt;),
 'loss_box_reg': tensor(2.9896e+09, grad_fn=&lt;DivBackward0&gt;),
 'loss_mask': tensor(8.4899e+10, grad_fn=&lt;BinaryCrossEntropyWithLogitsBackward&gt;),
 'loss_objectness': tensor(2.1843e+09, grad_fn=&lt;BinaryCrossEntropyWithLogitsBackward&gt;),
 'loss_rpn_box_reg': tensor(1.9489e+08, grad_fn=&lt;DivBackward0&gt;)}
</code></pre>
<p>After update, in second batch, losses go to Nan.</p>
<p>It can be clue of solving the problem?</p>
<p>p.s. I use SGD optimizer, lr = 0.0005 with warm-up starting.</p>

ptrblck: <p>Your model seems to be diverging. Since the forward activations seem to be in an expected range, you could check the loss function, which seems to blow up the values.</p>

YeongHwa_Jin: <p>Thanks your advice. But I still didn’t understand about that why MaskRcnn with ResNet backbone is okay but that with ResNext is invalid. This Nan always occurs with any dataset including COCO2017 when ResNext or Wide-Resnet is used as backbone. I just got the backbone by using   <a href="https://github.com/pytorch/vision" rel="noopener nofollow ugc">vision</a>/<a href="https://github.com/pytorch/vision/tree/master/torchvision" rel="noopener nofollow ugc">torchvision</a>/<a href="https://github.com/pytorch/vision/tree/master/torchvision/models" rel="noopener nofollow ugc">models</a>/<a href="https://github.com/pytorch/vision/tree/master/torchvision/models/detection" rel="noopener nofollow ugc">detection</a>/<a href="https://github.com/pytorch/vision/blob/master/torchvision/models/detection/backbone_utils.py" rel="noopener nofollow ugc">backbone_utils.py</a>, and changed the default <a href="https://github.com/pytorch/vision/blob/master/torchvision/models/detection/mask_rcnn.py" rel="noopener nofollow ugc">Mask Rcnn provided from torchvision</a>. Is this a possible situation?</p>

ptrblck: <p>I don’t know, why the change in backbone would cause this issue, but based on your last post:</p>
<aside class="quote no-group" data-username="YeongHwa_Jin" data-post="3" data-topic="112268">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yeonghwa_jin/48/14795_2.png" class="avatar"> YeongHwa_Jin:</div>
<blockquote>
<p>I checked the outputs of layers, but I didn’t find any invalid values. Instead, I found loss is very large value on first epoch, first batch:</p>
</blockquote>
</aside>
<p>it seems that the output activations of the backbone look alright, while the loss is really high (or were the output activations already high?).<br>
In the latter case, I would guess that your current hyperparameters are not suitable for the new backbone and let the model diverge.</p>