Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 2 weight functions in multi-label #2952

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions ppcls/loss/dmlloss.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
import paddle.nn as nn
import paddle.nn.functional as F

from ppcls.loss.multilabelloss import ratio2weight
from ppcls.loss.multilabelloss import ratio2weight_1, ratio2weight_2, ratio2weight_3


class DMLLoss(nn.Layer):
"""
DMLLoss
"""

def __init__(self, act="softmax", sum_across_class_dim=False, eps=1e-12):
def __init__(self, act="softmax", sum_across_class_dim=False, eps=1e-12, weight_type=1, weight_alpha=0.1):
super().__init__()
if act is not None:
assert act in ["softmax", "sigmoid"]
Expand All @@ -36,6 +36,8 @@ def __init__(self, act="softmax", sum_across_class_dim=False, eps=1e-12):
self.act = None
self.eps = eps
self.sum_across_class_dim = sum_across_class_dim
self.weight_type = weight_type
self.weight_alpha = weight_alpha

def _kldiv(self, x, target):
class_num = x.shape[-1]
Expand All @@ -54,7 +56,15 @@ def forward(self, x, target, gt_label=None):
if gt_label is not None:
gt_label, label_ratio = gt_label[:, 0, :], gt_label[:, 1, :]
targets_mask = paddle.cast(gt_label > 0.5, 'float32')
weight = ratio2weight(targets_mask, paddle.to_tensor(label_ratio))
if self.weight_type == 2:
weight = ratio2weight_2(
targets_mask, paddle.to_tensor(label_ratio))
elif self.weight_type == 3:
weight = ratio2weight_3(
targets_mask, paddle.to_tensor(label_ratio), self.weight_alpha)
else:
weight = ratio2weight_1(
targets_mask, paddle.to_tensor(label_ratio))
weight = weight * (gt_label > -1)
loss = loss * weight

Expand Down
119 changes: 116 additions & 3 deletions ppcls/loss/multilabelloss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
import paddle.nn.functional as F


def ratio2weight(targets, ratio):
def ratio2weight_1(targets, ratio):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的1、2、3是官方repo的命名方式吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/valencebond/Rethinking_of_PAR/blob/master/tools/function.py 官方 repo 只有一个,命名中无后缀。1、2、3 是按论文给的顺序给出来的。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议这里只定义一个函数ratio2weight,里边增加参数mode,mode可以是1,2,3,在注释中说明下区别

'''
Math formula:
```
w_j = y_{ij} * e^{1 - r_j} + (1 - {y_ij}) * e^{r_j}
```
REF: https://arxiv.org/abs/2107.03576v2
'''
pos_weights = targets * (1. - ratio)
neg_weights = (1. - targets) * ratio
weights = paddle.exp(neg_weights + pos_weights)
Expand All @@ -14,17 +21,63 @@ def ratio2weight(targets, ratio):
return weights


def ratio2weight_2(targets, ratio):
'''
Math formula:
```
w_j = y_{ij} * \sqrt{\frac{1}{2 * r_j}} + (1 - {y_ij}) * \sqrt{\frac{1}{2 * (1 - r_j)}}
```
REF: https://arxiv.org/abs/2107.03576v2
'''

pos_weights = targets * ratio
neg_weights = (1. - targets) * (1 - ratio)
weights = paddle.sqrt(0.5 * paddle.reciprocal(neg_weights + pos_weights))

# for RAP dataloader, targets element may be 2, with or without smooth, some element must great than 1
weights = weights - weights * (targets > 1)

return weights


def ratio2weight_3(targets, ratio, alpha):
'''
Math formula:
```
w_j = y_{ij} * \frac{(1/r_j)^\alpha}{(1/r_j)^\alpha + (1/(1 - r_j))^\alpha}
+ (1 - {y_ij}) * \frac{(1/(1 - r_j))^\alpha}{(1/r_j)^\alpha + (1/(1 - r_j))^\alpha}
```
REF: https://arxiv.org/abs/2107.03576v2
'''

pos_weights = targets * ratio
neg_weights = (1. - targets) * (1 - ratio)
combined_weights = pos_weights + neg_weights
weights = paddle.divide(
paddle.reciprocal(paddle.pow(combined_weights, alpha)),
paddle.reciprocal(paddle.pow(combined_weights, alpha)) +
paddle.reciprocal((paddle.pow(1 - combined_weights, alpha)))
)

# for RAP dataloader, targets element may be 2, with or without smooth, some element must great than 1
weights = weights - weights * (targets > 1)

return weights


class MultiLabelLoss(nn.Layer):
"""
Multi-label loss
"""

def __init__(self, epsilon=None, size_sum=False, weight_ratio=False):
def __init__(self, epsilon=None, size_sum=False, weight_ratio=False, weight_type=1, weight_alpha=0.1):
super().__init__()
if epsilon is not None and (epsilon <= 0 or epsilon >= 1):
epsilon = None
self.epsilon = epsilon
self.weight_ratio = weight_ratio
self.weight_type = weight_type
self.weight_alpha = weight_alpha
self.size_sum = size_sum

def _labelsmoothing(self, target, class_num):
Expand All @@ -46,7 +99,15 @@ def _binary_crossentropy(self, input, target, class_num):

if self.weight_ratio:
targets_mask = paddle.cast(target > 0.5, 'float32')
weight = ratio2weight(targets_mask, paddle.to_tensor(label_ratio))
if self.weight_type == 2:
weight = ratio2weight_2(
targets_mask, paddle.to_tensor(label_ratio))
elif self.weight_type == 3:
weight = ratio2weight_3(targets_mask, paddle.to_tensor(
label_ratio), self.weight_alpha)
else:
weight = ratio2weight_1(
targets_mask, paddle.to_tensor(label_ratio))
weight = weight * (target > -1)
cost = cost * weight

Expand All @@ -62,3 +123,55 @@ def forward(self, x, target):
loss = self._binary_crossentropy(x, target, class_num)
loss = loss.mean()
return {"MultiLabelLoss": loss}


class MultiLabelAsymmetricLoss(nn.Layer):
"""
Multi-label asymmetric loss, introduced by
Emanuel Ben-Baruch at el. in https://arxiv.org/pdf/2009.14119v4.pdf.
"""

def __init__(self,
gamma_pos=1,
gamma_neg=4,
clip=0.05,
epsilon=1e-8,
disable_focal_loss_grad=True,
reduction="sum"):
super().__init__()
self.gamma_pos = gamma_pos
self.gamma_neg = gamma_neg
self.clip = clip
self.epsilon = epsilon
self.disable_focal_loss_grad = disable_focal_loss_grad
assert reduction in ["mean", "sum", "none"]
self.reduction = reduction

def forward(self, x, target):
if isinstance(x, dict):
x = x["logits"]
pred_sigmoid = F.sigmoid(x)
target = target.astype(pred_sigmoid.dtype)

# Asymmetric Clipping and Basic CE calculation
if self.clip and self.clip > 0:
pt = (1 - pred_sigmoid + self.clip).clip(max=1) \
* (1 - target) + pred_sigmoid * target
else:
pt = (1 - pred_sigmoid) * (1 - target) + pred_sigmoid * target

# Asymmetric Focusing
if self.disable_focal_loss_grad:
paddle.set_grad_enabled(False)
asymmetric_weight = (1 - pt).pow(
self.gamma_pos * target + self.gamma_neg * (1 - target))
if self.disable_focal_loss_grad:
paddle.set_grad_enabled(True)

loss = -paddle.log(pt.clip(min=self.epsilon)) * asymmetric_weight

if self.reduction == 'mean':
loss = loss.mean()
elif self.reduction == 'sum':
loss = loss.sum()
return {"MultiLabelAsymmetricLoss": loss}