nan for focal loss when training ssd
See original GitHub issueHi, I want to use focal loss to train ssd, and I change the ssd code, but the loss is always nan. the file I change is below: in ssd_head.py: ` def loss_single(self, cls_score, bbox_pred, labels, label_weights, bbox_targets, bbox_weights, num_total_samples, cfg): # loss_cls_all = F.cross_entropy( # cls_score, labels, reduction=‘none’) * label_weights # pos_inds = (labels > 0).nonzero().view(-1) # neg_inds = (labels == 0).nonzero().view(-1) # print("just to see anchor’s imbalance: ") # print("positive anchors: ") # print(pos_inds.size(0)) # print(neg_inds.size(0)) # print(“negtive relative to positive:”) # print(neg_inds.size(0) / pos_inds.size(0)) # num_pos_samples = pos_inds.size(0) # num_neg_samples = cfg.neg_pos_ratio * num_pos_samples # if num_neg_samples > neg_inds.size(0): # num_neg_samples = neg_inds.size(0) # topk_loss_cls_neg, _ = loss_cls_all[neg_inds].topk(num_neg_samples) # loss_cls_pos = loss_cls_all[pos_inds].sum() # loss_cls_neg = topk_loss_cls_neg.sum() # loss_cls = (loss_cls_pos + loss_cls_neg) / num_total_samples
labels = torch.tensor(labels, dtype=torch.long)
labels = torch.nn.functional.one_hot(labels, num_classes=21).cuda()
loss_cls = py_sigmoid_focal_loss(
cls_score, labels, label_weights, avg_factor=num_total_samples)
loss_bbox = smooth_l1_loss(
bbox_pred,
bbox_targets,
bbox_weights,
beta=cfg.smoothl1_beta,
avg_factor=num_total_samples)
return loss_cls, loss_bbox #loss_cls[None]`
I comment the entropy cross loss, and change to focal loss just like above. before enter focal loss, I convert label to one-hot format. The training output:
I search on the internet, and I try : pred_sigmoid = pred_sigmoid.clamp(min=0.0001, max=1.0) in py_sigmoid_focal_loss , but it’s no use.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
@yzl96 hello, I have the same problem, can you note here how to modify all the codes? thanks
Thanks you, I have solve the problem, after I limit the gradient, the loss can drop, but the performance of focal loss is lower than 1:3 loss, I did not figure why.