megvii-model / YOLOF

MIT License
806 stars 115 forks source link

About NAN during training #51

Open yjh0410 opened 2 years ago

yjh0410 commented 2 years ago

@chensnathan

I think the following passage may be the main cause of the NAN problem:

normalized_cls_score = cls_score + objectness - torch.log(

    • torch.clamp(cls_score.exp(), max=self.INF) + torch.clamp( objectness.exp(), max=self.INF))

There is an exp operation in this code, and clip is used to clip it to avoid explosion, but this still has hidden dangers, that is, before clipping, the exp may have exploded and overflowed, so the clip is useless at this time.

So, I changed to clip first and then exp,:

normalized_cls_pred = cls_pred + obj_pred - torch.log(

    •     torch.clamp(cls_pred, max=DEFAULT_EXP_CLAMP).exp() + 
          torch.clamp(obj_pred, max=DEFAULT_EXP_CLAMP).exp())

      where DEFAULT_EXP_CLAMP = log(INF).

After above modification, NAN problem no longer encountered.

ytoon commented 2 years ago

Thanks for your suggestion, we will check it. If so, we will update our code with it.