mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-04-29 03:00:14 -04:00
update loss calculation for regresionhead and some cleanups
This commit is contained in:
@@ -8,7 +8,7 @@ def dice_ce_loss(pred, tgt):
|
||||
dice = (1.0 - dice_score(pred, tgt, argmax=False, to_one_hot_x=False)).mean()
|
||||
return (dice + ce) / 2
|
||||
|
||||
def sigmoid_focal_loss(pred:Tensor, tgt:Tensor, alpha:float = 0.25, gamma:float = 2, reduction:str = "none") -> Tensor:
|
||||
def sigmoid_focal_loss(pred:Tensor, tgt:Tensor, alpha:float = 0.25, gamma:float = 2.0, reduction:str = "none") -> Tensor:
|
||||
assert reduction in ["mean", "sum", "none"], f"unsupported reduction {reduction}"
|
||||
p, ce_loss = pred.sigmoid(), pred.binary_crossentropy_logits(tgt, reduction="none")
|
||||
p_t = p * tgt + (1 - p) * (1 - tgt)
|
||||
|
||||
@@ -176,8 +176,9 @@ class RegressionHead:
|
||||
|
||||
def _compute_loss(self, x:Tensor, bboxes:Tensor, matches:Tensor, anchors:Tensor) -> Tensor:
|
||||
mask = (fg_idxs := matches >= 0).reshape(matches.shape[0], -1, 1)
|
||||
tgt = self.box_coder.encode(bboxes, anchors)
|
||||
loss = mask.where(l1_loss(x, tgt), 0).sum(-1).sum(-1)
|
||||
x = x * mask
|
||||
tgt = self.box_coder.encode(bboxes, anchors) * mask
|
||||
loss = l1_loss(x, tgt).sum(-1).sum(-1)
|
||||
loss = (loss / fg_idxs.sum(-1)).sum() / matches.shape[0]
|
||||
return loss
|
||||
|
||||
|
||||
Reference in New Issue
Block a user