Skip to content

Commit

Permalink
add OCsoftmaxLoss (#71)
Browse files Browse the repository at this point in the history
Co-authored-by: Tony_glb <[email protected]>
  • Loading branch information
Tony-Xie-182 and Tony_glb authored Dec 30, 2022
1 parent 141f904 commit 1435872
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion pytorch/libs/nnet/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,43 @@ def extra_repr(self):
return '(~affine): (input_dim={input_dim}, num_targets={num_targets}, ' \
'sub_k={sub_k}, method={method}, add_m={add_m} ' \
'adapt_method={adapt} ada_m={ada_m}, s={s} ' \
'loss_type={loss_type}, eps={eps}, scale_init={scale_init})'.format(**self.__dict__)
'loss_type={loss_type}, eps={eps}, scale_init={scale_init})'.format(**self.__dict__)



class OCSoftmax(TopVirtualLoss):
"""Xingjia Xie 2022-12-30
Reference:
[1] Y. Zhang, F. Jiang, and Z. Duan, “One-class learning towards synthetic voice spoofing detection,”
IEEE Signal Processing Letters, vol. 28, pp. 937–941, 2021.
"""
def __init__(self, feat_dim=160, r_real=0.9, r_fake=0.2, alpha=20.0):
super(OCSoftmax, self).__init__()
self.feat_dim = feat_dim
self.r_real = r_real
self.r_fake = r_fake
self.alpha = alpha
self.center = nn.Parameter(torch.randn(1, self.feat_dim))
nn.init.kaiming_uniform_(self.center, 0.25)
self.softplus = nn.Softplus()

def forward(self, x, labels):
"""
Args:
x: feature matrix with shape (batch_size, feat_dim).
labels: ground truth labels with shape (batch_size).
"""
w = F.normalize(self.center, p=2, dim=1)
x = F.normalize(x, p=2, dim=1)

scores = x @ w.transpose(0, 1)
output_scores = scores.clone()

# Xingjia Xie 2022-12-01 label: bona fide->1; spoof->0
scores[labels == 1] = scores[labels == 1] - self.r_real
scores[labels == 0] = self.r_fake - scores[labels == 0]

loss = self.softplus(self.alpha * scores).mean()

return loss

0 comments on commit 1435872

Please sign in to comment.