mindsponge.metrics.BinaryFocal

View Source On Gitee
class mindsponge.metrics.BinaryFocal(alpha=0.25, gamma=2.0, feed_in=False, not_focal=False)[source]

Focal error for Binary classifications. Compute the binary classes focal error between prediction and the ground truth target.

Reference:

Lin, Tsung-Yi, et al. 'Focal loss for dense object detection' .

\[\mathrm{FL}\left(p_{\mathrm{t}}\right)=-\alpha_{\mathrm{t}}\left(1-p_{\mathrm{t}}\right)^{\gamma} \log \left(p_{\mathrm{t}}\right)\]
Parameters
  • alpha (float) – The weight of cross entropy, default: 0.25.

  • gamma (float) – The hyperparameters, modulating loss from hard to easy, default: 2.0.

  • feed_in (bool) – Whether to convert prediction, default: False.

  • not_focal (bool) – Whether focal loss, default: False.

Inputs:
  • prediction (Tensor) - Predict values, shape is \((batch\_size, ndim)\).

  • target (Tensor) - Label values, shape is \((batch\_size, ndim)\).

Outputs:

Tensor, shape is \((batch\_size,)\).

Supported Platforms:

Ascend GPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> from mindsponge.metrics import BinaryFocal
>>> net = BinaryFocal()
>>> prediction = Tensor(np.random.randn(32, 10).astype(np.float32))
>>> target = Tensor(np.random.randn(32, 10).astype(np.float32))
>>> out = net(prediction, target)
>>> print(out.shape)
(32,)