Commit 62233110 authored by Anthony Larcher's avatar Anthony Larcher
Browse files

add score normalization

parent 48467edb
......@@ -241,13 +241,6 @@ class RawPreprocessor(torch.nn.Module):
class ResBlock(torch.nn.Module):
"""
......
......@@ -48,7 +48,7 @@ from .xsets import SideSet
from .xsets import FileSet
from .xsets import IdMapSet
from .xsets import IdMapSet_per_speaker
from .res_net import RawPreprocessor, ResBlockWFMS
from .res_net import RawPreprocessor, ResBlockWFMS, ResBlock
from ..bosaris import IdMap
from ..bosaris import Key
from ..bosaris import Ndx
......@@ -411,6 +411,8 @@ class Xtractor(torch.nn.Module):
self.after_speaker_embedding_weight_decay = 0.00
else:
is_first_resblock = True
if isinstance(model_archi, dict):
cfg = model_archi
else:
......@@ -481,6 +483,13 @@ class Xtractor(torch.nn.Module):
cfg["segmental"][k]["output"])))
input_size = cfg["segmental"][k]["output"]
elif k.startswith("conv2D"):
segmental_layers.append((k, torch.nn.Conv2d(in_channels=1,
out_channels=entry_conv_out_channels,
kernel_size=entry_conv_kernel_size,
padding=3,
stride=1))
elif k.startswith("conv"):
segmental_layers.append((k, torch.nn.Conv1d(input_size,
cfg["segmental"][k]["output_channels"],
......@@ -499,6 +508,12 @@ class Xtractor(torch.nn.Module):
elif k.startswith('batch_norm'):
segmental_layers.append((k, torch.nn.BatchNorm1d(input_size)))
elif k.startswith('resblock'):
segmental_layers.append((ResBlock(cfg["segmental"][k]["input_channel"],
cfg["segmental"][k]["output_channel"],
is_first_resblock)))
is_first_resblock = False
self.sequence_network = torch.nn.Sequential(OrderedDict(segmental_layers))
self.sequence_network_weight_decay = cfg["segmental"]["weight_decay"]
......@@ -573,9 +588,13 @@ class Xtractor(torch.nn.Module):
self.after_speaker_embedding = torch.nn.Sequential(OrderedDict(after_embedding_layers))
elif self.loss == "aam":
print(f"input_size = {input_size}")
self.after_speaker_embedding = ArcMarginProduct(input_size, int(self.speaker_number), s=64, m=0.2, easy_margin=True)
#self.norm_embedding = True
self.norm_embedding = True
self.after_speaker_embedding = ArcMarginProduct(input_size,
int(self.speaker_number),
s=64,
m=0.2,
easy_margin=True)
#self.after_speaker_embedding = ArcLinear(input_size,
# self.speaker_number,
# margin=self.aam_margin,
......
# -*- coding: utf-8 -*-
#
# This file is part of SIDEKIT.
#
# SIDEKIT is a python package for speaker verification.
# Home page: http://www-lium.univ-lemans.fr/sidekit/
#
# SIDEKIT is a python package for speaker verification.
# Home page: http://www-lium.univ-lemans.fr/sidekit/
#
# SIDEKIT is free software: you can redistribute it and/or modify
# it under the terms of the GNU LLesser General Public License as
# published by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# SIDEKIT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with SIDEKIT. If not, see <http://www.gnu.org/licenses/>.
"""
Copyright 2014-2019 Anthony Larcher
:mod:`sidekit_io` provides methods to read and write from and to different
formats.
"""
import numpy
__license__ = "LGPL"
__author__ = "Anthony Larcher"
__copyright__ = "Copyright 2014-2019 Anthony Larcher"
__maintainer__ = "Anthony Larcher"
__email__ = "anthony.larcher@univ-lemans.fr"
__status__ = "Production"
__docformat__ = 'reStructuredText'
def znorm(enrol_test_scores, enrol_imp_scores, sym=False):
"""
imp_scores are formed by scoring enrollment utterance ewith all files from the impostor cohort
thus: enrol_test_scores.modelset and enrol_imp_scores.modelset must be the same
This function assumes that all models from enrol_test_scores are in enrol_imp_scores
:param enrol_test_scores:
:param enrol_imp_scores:
:return:
"""
# Align enrol_test_scores.modelset and enrol_imp_scores.modelset
enrol_imp_scores.filter(enrol_test_scores.modelset, enrol_imp_scores.segset, keep=True)
enrol_test_scores.sort()
enrol_imp_scores.sort()
# Compute the new enrol_test_scores normalized scores
if sym:
mean_per_model = (enrol_imp_scores.scoremat.sum(1) - numpy.diag(enrol_imp_scores.scoremat)) / (enrol_imp_scores.scoremat.shape[1] - 1)
tmp = numpy.square(enrol_imp_scores.scoremat - mean_per_model)
std_per_model = (tmp.sum(1) - numpy.diag(tmp)) / (tmp.shape[1] - 1)
else:
mean_per_model = enrol_imp_scores.scoremat.mean(1)
std_per_model = enrol_imp_scores.scoremat.std(1)
enrol_test_scores = (enrol_test_scores.scoremat - mean_per_model) / std_per_model
return enrol_test_scores
def tnorm(enrol_test_scores, imp_test_scores):
"""
:param enrol_test_scores:
:param imp_test_scores:
:return:
"""
# Align enrol_test_scores.segset and imp_test_scores.segset
imp_test_scores.filter(imp_test_scores.segset, enrol_test_scores.segset, keep=True)
enrol_test_scores.sort()
imp_test_scores.sort()
# Compute the new enrol_test_scores normalized scores
mean_per_segment = imp_test_scores.mean(0)
std_per_segment = imp_test_scores.std(0)
enrol_test_scores.scoremat = (enrol_test_scores.scoremat - mean_per_segment) / std_per_segment
return enrol_test_scores
def ztnorm(enrol_test_scores, enrol_imp_scores, imp_test_scores, imp_imp_scores):
"""
:param enrol_test_scores:
:param enrol_imp_scores:
:param imp_test_scores:
:param imp_imp_scores:
:return:
"""
# Apply Z-norm first on enrol_test_scores by using enrol_imp_scores
# and on imp_test_scores by using imp_imp_scores
#
# to produce Z_enrol_test_scores and Z_imp_test_scores
z_enrol_test_scores = znorm(enrol_test_scores, enrol_imp_scores)
z_imp_test_scores = znorm(imp_test_scores, imp_imp_scores, sym=True)
# Apply t-norm on Z_enrol_test_scores with Z_imp_test_scores
zt_enrol_test_scores = tnorm(z_enrol_test_scores, z_imp_test_scores)
return zt_enrol_test_scores
def snorm(enrol_test_scores, enrol_imp_scores, imp_test_scores):
"""
:param enrol_test_scores:
:return:
"""
# Compute z-norm scores
z_enrol_test_scores = znorm(enrol_test_scores, enrol_imp_scores)
# Compute t-norm scores
t_enrol_test_scores = tnorm(enrol_test_scores, imp_test_scores)
# Average the z and t normed scores
s_enrol_test_scores.scoremat = 0.5 * (z_enrol_test_scores.scoremat + t_enrol_test_scores.scoremat)
return s_enrol_test_scores
def asnorm(enrol_test_scores):
pass
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment