Loss used in RetinaNet for dense detection: https://arxiv.org/abs/1708.02002. Args: inputs: A float tensor of arbitrary shape. The predictions for each example. targets: A float tensor with the same shape as inputs. Stores the binary classifi
(inputs,
targets,
num_boxes,
alpha: float = 0.25,
gamma: float = 2)
| 118 | |
| 119 | |
| 120 | def sigmoid_focal_loss(inputs, |
| 121 | targets, |
| 122 | num_boxes, |
| 123 | alpha: float = 0.25, |
| 124 | gamma: float = 2): |
| 125 | """ |
| 126 | Loss used in RetinaNet for dense detection: https://arxiv.org/abs/1708.02002. |
| 127 | Args: |
| 128 | inputs: A float tensor of arbitrary shape. |
| 129 | The predictions for each example. |
| 130 | targets: A float tensor with the same shape as inputs. Stores the binary |
| 131 | classification label for each element in inputs |
| 132 | (0 for the negative class and 1 for the positive class). |
| 133 | alpha: (optional) Weighting factor in range (0,1) to balance |
| 134 | positive vs negative examples. Default = -1 (no weighting). |
| 135 | gamma: Exponent of the modulating factor (1 - p_t) to |
| 136 | balance easy vs hard examples. |
| 137 | Returns: |
| 138 | Loss tensor |
| 139 | """ |
| 140 | prob = inputs.sigmoid() |
| 141 | ce_loss = F.binary_cross_entropy_with_logits(inputs, |
| 142 | targets, |
| 143 | reduction='none') |
| 144 | p_t = prob * targets + (1 - prob) * (1 - targets) |
| 145 | loss = ce_loss * ((1 - p_t)**gamma) |
| 146 | |
| 147 | if alpha >= 0: |
| 148 | alpha_t = alpha * targets + (1 - alpha) * (1 - targets) |
| 149 | loss = alpha_t * loss |
| 150 | |
| 151 | return loss.mean(1).sum() / num_boxes |
| 152 | |
| 153 | |
| 154 | class MLP(nn.Module): |
no outgoing calls
no test coverage detected