Merge branch 'develop' of github.com:pyannote/pyannote-audio into develop

This commit is contained in:
Hervé Bredin committed 2020-05-02 14:12:34 +02:00
commit c0125c7c06
2 files changed
+78 -34

No files matched your search

@@ -25,8 +25,10 @@
# AUTHORS
# Hervé BREDIN - http://herve.niderb.fr
# Juan Manuel CORIA
# Juan Manuel CORIA - https://juanmc2005.github.io
import math
import warnings
import torch
import torch.nn as nn
import torch.nn.functional as F
@@ -34,7 +36,7 @@ from .classification import Classification
class ArcLinear(nn.Module):
"""Additive Angular Margin linear module (ArcFace)
"""Additive Angular Margin classification module
Parameters
----------
@@ -44,18 +46,18 @@ class ArcLinear(nn.Module):
Number of classes
margin : float
Angular margin to penalize distances between embeddings and centers
s : float
scale : float
Scaling factor for the logits
"""
def __init__(self, nfeat, nclass, margin, s):
def __init__(self, nfeat, nclass, margin, scale):
super(ArcLinear, self).__init__()
eps = 1e-4
self.min_cos = eps - 1
self.max_cos = 1 - eps
self.nclass = nclass
self.margin = margin
self.s = s
self.scale = scale
self.W = nn.Parameter(torch.Tensor(nclass, nfeat))
nn.init.xavier_uniform_(self.W)
@@ -92,13 +94,16 @@ class ArcLinear(nn.Module):
one_hot = torch.zeros_like(cos_theta_j)
one_hot.scatter_(1, target, 1.0)
# project margin differences into cosθj
return self.s * (cos_theta_j + one_hot * (cos_theta_yi_margin - cos_theta_yi))
return self.scale * (cos_theta_j + one_hot * (cos_theta_yi_margin - cos_theta_yi))
class AdditiveAngularMarginLoss(Classification):
"""Additive angular margin loss
TODO explain
Penalize the distance between an example and its class center
with an angular margin, applying a scaling parameter to all logits.
The intuition is to bring examples from a single class closer together
in the embedding space by means of the angular margin.
Parameters
----------
@@ -124,13 +129,13 @@ class AdditiveAngularMarginLoss(Classification):
Defaults to 0 (i.e. keep them all).
margin : float, optional
Angular margin value. Defaults to 0.1.
s : float, optional
Scaling parameter value for the logits. Defaults to 7.
scale : float, optional
Scaling parameter value for the logits. Defaults to sqrt(2) * log(n_classes - 1).
Reference
---------
TODO
ArcFace: Additive Angular Margin Loss for Deep Face Recognition
http://openaccess.thecvf.com/content_CVPR_2019/html/Deng_ArcFace_Additive_Angular_Margin_Loss_for_Deep_Face_Recognition_CVPR_2019_paper.html
"""
def __init__(
@@ -143,7 +148,9 @@ class AdditiveAngularMarginLoss(Classification):
per_epoch: float = None,
label_min_duration: float = 0.0,
margin: float = 0.1,
s: float = 7.0,
# `s` is deprecated in favor of `scale`
s: float = None,
scale: float = None
):
super().__init__(
@@ -155,8 +162,15 @@ class AdditiveAngularMarginLoss(Classification):
per_epoch=per_epoch,
label_min_duration=label_min_duration,
)
self.margin = margin
self.s = s
if s is not None:
msg = "The 's' parameter is deprecated in favor of 'scale', " \
"and will be removed in a future release"
warnings.warn(msg, FutureWarning)
self.scale = s
else:
self.scale = scale
def more_parameters(self):
"""Initialize classifier layer
@@ -167,11 +181,16 @@ class AdditiveAngularMarginLoss(Classification):
Parameters
"""
nclass = len(self.specifications["y"]["classes"])
# Use scaling initialization trick from AdaCos
# Reference: https://arxiv.org/abs/1905.00292
scale = math.sqrt(2) * math.log(nclass - 1) if self.scale is None else self.scale
self.classifier_ = ArcLinear(
self.model.dimension,
len(self.specifications["y"]["classes"]),
nclass,
self.margin,
self.s,
scale,
).to(self.device)
return self.classifier_.parameters()
@@ -25,8 +25,10 @@
# AUTHORS
# Hervé BREDIN - http://herve.niderb.fr
# Juan Manuel CORIA
# Juan Manuel CORIA - https://juanmc2005.github.io
import math
import warnings
import torch
from torch import nn
import torch.nn.functional as F
@@ -36,19 +38,19 @@ from .classification import Classification
class CocoLinear(nn.Module):
"""Congenerous Cosine linear module (for CoCo loss)
Parameters
----------
nfeat : int
Embedding dimension
nclass : int
Number of classes
alpha : float
Scaling factor used in embedding L2-normalization
"""
Parameters
----------
nfeat : int
Embedding dimension
nclass : int
Number of classes
scale : float
Scaling factor used in embedding L2-normalization
"""
def __init__(self, nfeat, nclass, alpha):
def __init__(self, nfeat, nclass, scale):
super(CocoLinear, self).__init__()
self.alpha = alpha
self.scale = scale
self.centers = nn.Parameter(torch.randn(nclass, nfeat))
def forward(self, x, target=None):
@@ -67,14 +69,19 @@ class CocoLinear(nn.Module):
# normalize centers
cnorm = F.normalize(self.centers)
# normalize scaled embeddings
xnorm = self.alpha * F.normalize(x)
xnorm = self.scale * F.normalize(x)
# calculate logits like in `nn.Linear`
logits = torch.matmul(xnorm, torch.transpose(cnorm, 0, 1))
return logits
class CongenerousCosineLoss(Classification):
"""Train embeddings as last hidden layer of an additive angular margin loss classifier
"""Congenerous cosine loss
Train embeddings by maximizing the cosine similarity between
an embedding and its class center.
A hyper-parameter `alpha` is used to scale logits before
applying softmax.
Parameters
----------
@@ -98,8 +105,13 @@ class CongenerousCosineLoss(Classification):
label_min_duration : `float`, optional
Remove speakers with less than that many seconds of speech.
Defaults to 0 (i.e. keep them all).
alpha : float
Scaling factor used in embedding L2-normalization. Defaults to 6.25.
scale : float
Scaling factor used in embedding L2-normalization. Defaults to sqrt(2) * log(n_classes - 1).
Reference
---------
Rethinking Feature Discrimination and Polymerization for Large-scale Recognition
https://arxiv.org/abs/1710.00870
"""
def __init__(
@@ -111,7 +123,9 @@ class CongenerousCosineLoss(Classification):
per_fold: int = 32,
per_epoch: float = None,
label_min_duration: float = 0.0,
alpha: float = 6.25,
# `alpha` is deprecated in favor of `scale`
alpha: float = None,
scale: float = None
):
super().__init__(
@@ -124,7 +138,13 @@ class CongenerousCosineLoss(Classification):
label_min_duration=label_min_duration,
)
self.alpha = alpha
if alpha is not None:
msg = "The 'alpha' parameter is deprecated in favor of 'scale', " \
"and will be removed in a future release"
warnings.warn(msg, FutureWarning)
self.scale = alpha
else:
self.scale = scale
def more_parameters(self):
"""Initialize trainable trainer parameters
@@ -135,8 +155,13 @@ class CongenerousCosineLoss(Classification):
Trainable trainer parameters
"""
nclass = len(self.specifications["y"]["classes"])
# Use scaling initialization trick from AdaCos
# Reference: https://arxiv.org/abs/1905.00292
scale = math.sqrt(2) * math.log(nclass - 1) if self.scale is None else self.scale
self.classifier_ = CocoLinear(
self.model.dimension, len(self.specifications["y"]["classes"]), self.alpha
self.model.dimension, nclass, scale
).to(self.device)
return self.classifier_.parameters()