forked from mindspore-lab/mindcv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jsd loss and asymmetric loss (mindspore-lab#682)
- Loading branch information
Showing
5 changed files
with
318 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import numpy as np | ||
|
||
import mindspore.nn as nn | ||
from mindspore import Tensor, ops | ||
|
||
|
||
class AsymmetricLossMultilabel(nn.LossBase): | ||
def __init__(self, gamma_neg=4, gamma_pos=1, clip=0.05, eps=1e-8): | ||
super(AsymmetricLossMultilabel, self).__init__() | ||
self.gamma_neg = gamma_neg | ||
self.gamma_pos = gamma_pos | ||
self.clip = clip | ||
self.eps = eps | ||
|
||
def construct(self, logits, labels): | ||
""" | ||
logits: output from models | ||
labels: multi-label binarized vector | ||
""" | ||
x_sigmoid = ops.Sigmoid()(logits) | ||
xs_pos = x_sigmoid | ||
xs_neg = 1 - x_sigmoid | ||
|
||
if self.clip > 0: | ||
xs_neg = ops.clip_by_value(xs_neg + self.clip, clip_value_max=Tensor(1.0)) | ||
|
||
los_pos = labels * ops.log(ops.clip_by_value(xs_pos, clip_value_min=Tensor(self.eps))) | ||
los_neg = (1 - labels) * ops.log(ops.clip_by_value(xs_neg, clip_value_min=Tensor(self.eps))) | ||
|
||
loss = los_pos + los_neg | ||
|
||
if self.gamma_pos > 0 and self.gamma_neg > 0: | ||
pt0 = xs_pos * labels | ||
pt1 = xs_neg * (1 - labels) | ||
pt = pt0 + pt1 | ||
one_sided_gamma = self.gamma_pos * labels + self.gamma_neg * (1 - labels) | ||
one_sided_w = ops.pow(1 - pt, one_sided_gamma) | ||
|
||
loss *= one_sided_w | ||
|
||
return -loss.sum() | ||
|
||
|
||
class AsymmetricLossSingleLabel(nn.LossBase): | ||
def __init__(self, gamma_pos=1, gamma_neg=4, eps=0.1, reduction="mean", smoothing=0.1): | ||
super(AsymmetricLossSingleLabel, self).__init__() | ||
|
||
self.eps = eps | ||
self.logsoftmax = nn.LogSoftmax(axis=-1) | ||
self.targets_classes = [] | ||
self.gamma_pos = gamma_pos | ||
self.gamma_neg = gamma_neg | ||
self.reduction = reduction | ||
self.smoothing = smoothing | ||
|
||
def construct(self, logits, labels): | ||
num_classes = logits.shape[-1] | ||
log_preds = self.logsoftmax(logits) | ||
labels_e = ops.ExpandDims()(labels, 1) | ||
labels_e_shape = labels_e.shape | ||
targets = ops.tensor_scatter_elements( | ||
ops.ZerosLike()(logits), labels_e, Tensor(np.ones(labels_e_shape, dtype=np.float32)), 1 | ||
) | ||
|
||
anti_targets = 1 - targets | ||
xs_pos = ops.exp((log_preds)) | ||
xs_neg = 1 - xs_pos | ||
xs_pos = xs_pos * targets | ||
xs_neg = xs_neg * anti_targets | ||
|
||
asymmetric_w = ops.pow(1 - xs_pos - xs_neg, self.gamma_pos * targets + self.gamma_neg * anti_targets) | ||
|
||
log_preds = log_preds * asymmetric_w | ||
|
||
targets = targets * (1 - self.smoothing) + self.smoothing / num_classes | ||
|
||
loss = -targets * log_preds | ||
loss = ops.ReduceSum()(loss, -1) | ||
|
||
if self.reduction == "mean": | ||
loss = loss.mean() | ||
|
||
return loss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from mindspore import nn, ops | ||
|
||
from .cross_entropy_smooth import CrossEntropySmooth | ||
|
||
|
||
class JSDCrossEntropy(nn.LossBase): | ||
""" | ||
JSD loss is implemented according to "AugMix: A Simple Data Processing Method to Improve Robustness and Uncertainty" | ||
https://arxiv.org/abs/1912.02781 | ||
Please note that JSD loss should be used when "aug_splits = 3". | ||
""" | ||
|
||
def __init__(self, num_splits=3, alpha=12, smoothing=0.1, weight=None, reduction="mean", aux_factor=0.0): | ||
super().__init__() | ||
self.num_splits = num_splits | ||
self.alpha = alpha | ||
self.smoothing = smoothing | ||
self.weight = weight | ||
self.reduction = reduction | ||
self.kldiv = ops.KLDivLoss(reduction="batchmean") | ||
self.map = ops.Map() | ||
|
||
self.softmax = ops.Softmax(axis=1) | ||
self.aux_factor = aux_factor | ||
|
||
def construct(self, logits, labels): | ||
if self.training: | ||
split_size = logits.shape[0] // self.num_splits | ||
log_split = ops.split(logits, 0, self.num_splits) | ||
|
||
loss = ops.cross_entropy( | ||
log_split[0], | ||
labels[:split_size], | ||
weight=self.weight, | ||
reduction=self.reduction, | ||
label_smoothing=self.smoothing, | ||
) | ||
|
||
probs = self.map(self.softmax, log_split) | ||
stack_probs = ops.stack(probs) | ||
clip_probs = ops.clip_by_value(stack_probs.mean(axis=0), 1e-7, 1) | ||
log_probs = ops.log(clip_probs) | ||
|
||
for p_split in probs: | ||
loss += self.alpha * self.kldiv(log_probs, p_split) / self.num_splits | ||
|
||
return loss | ||
else: | ||
return CrossEntropySmooth( | ||
smoothing=self.smoothing, aux_factor=self.aux_factor, reduction=self.reduction, weight=self.weight | ||
)(logits, labels) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters