|
import argparse |
|
from typing import Callable |
|
from typing import Collection |
|
from typing import Dict |
|
from typing import List |
|
from typing import Optional |
|
from typing import Tuple |
|
|
|
import numpy as np |
|
import torch |
|
from typeguard import check_argument_types |
|
from typeguard import check_return_type |
|
|
|
from espnet2.asr.encoder.abs_encoder import AbsEncoder |
|
from espnet2.asr.encoder.conformer_encoder import ConformerEncoder |
|
from espnet2.asr.encoder.rnn_encoder import RNNEncoder |
|
from espnet2.asr.encoder.transformer_encoder import TransformerEncoder |
|
from espnet2.asr.frontend.abs_frontend import AbsFrontend |
|
from espnet2.asr.frontend.default import DefaultFrontend |
|
from espnet2.asr.frontend.windowing import SlidingWindow |
|
from espnet2.diar.decoder.abs_decoder import AbsDecoder |
|
from espnet2.diar.decoder.linear_decoder import LinearDecoder |
|
from espnet2.diar.espnet_model import ESPnetDiarizationModel |
|
from espnet2.layers.abs_normalize import AbsNormalize |
|
from espnet2.layers.global_mvn import GlobalMVN |
|
from espnet2.layers.label_aggregation import LabelAggregate |
|
from espnet2.layers.utterance_mvn import UtteranceMVN |
|
from espnet2.tasks.abs_task import AbsTask |
|
from espnet2.torch_utils.initialize import initialize |
|
from espnet2.train.class_choices import ClassChoices |
|
from espnet2.train.collate_fn import CommonCollateFn |
|
from espnet2.train.preprocessor import CommonPreprocessor |
|
from espnet2.train.trainer import Trainer |
|
from espnet2.utils.get_default_kwargs import get_default_kwargs |
|
from espnet2.utils.nested_dict_action import NestedDictAction |
|
from espnet2.utils.types import int_or_none |
|
from espnet2.utils.types import str2bool |
|
from espnet2.utils.types import str_or_none |
|
|
|
frontend_choices = ClassChoices( |
|
name="frontend", |
|
classes=dict(default=DefaultFrontend, sliding_window=SlidingWindow), |
|
type_check=AbsFrontend, |
|
default="default", |
|
) |
|
normalize_choices = ClassChoices( |
|
"normalize", |
|
classes=dict( |
|
global_mvn=GlobalMVN, |
|
utterance_mvn=UtteranceMVN, |
|
), |
|
type_check=AbsNormalize, |
|
default="utterance_mvn", |
|
optional=True, |
|
) |
|
label_aggregator_choices = ClassChoices( |
|
"label_aggregator", |
|
classes=dict(label_aggregator=LabelAggregate), |
|
default="label_aggregator", |
|
) |
|
encoder_choices = ClassChoices( |
|
"encoder", |
|
classes=dict( |
|
conformer=ConformerEncoder, |
|
transformer=TransformerEncoder, |
|
rnn=RNNEncoder, |
|
), |
|
type_check=AbsEncoder, |
|
default="rnn", |
|
) |
|
decoder_choices = ClassChoices( |
|
"decoder", |
|
classes=dict(linear=LinearDecoder), |
|
type_check=AbsDecoder, |
|
default="linear", |
|
) |
|
|
|
|
|
class DiarizationTask(AbsTask): |
|
|
|
num_optimizers: int = 1 |
|
|
|
|
|
class_choices_list = [ |
|
|
|
frontend_choices, |
|
|
|
normalize_choices, |
|
|
|
encoder_choices, |
|
|
|
decoder_choices, |
|
|
|
label_aggregator_choices, |
|
] |
|
|
|
|
|
trainer = Trainer |
|
|
|
@classmethod |
|
def add_task_arguments(cls, parser: argparse.ArgumentParser): |
|
group = parser.add_argument_group(description="Task related") |
|
|
|
group.add_argument( |
|
"--num_spk", |
|
type=int_or_none, |
|
default=None, |
|
help="The number fo speakers (for each recording) used in system training", |
|
) |
|
|
|
group.add_argument( |
|
"--init", |
|
type=lambda x: str_or_none(x.lower()), |
|
default=None, |
|
help="The initialization method", |
|
choices=[ |
|
"chainer", |
|
"xavier_uniform", |
|
"xavier_normal", |
|
"kaiming_uniform", |
|
"kaiming_normal", |
|
None, |
|
], |
|
) |
|
|
|
group.add_argument( |
|
"--input_size", |
|
type=int_or_none, |
|
default=None, |
|
help="The number of input dimension of the feature", |
|
) |
|
|
|
group.add_argument( |
|
"--model_conf", |
|
action=NestedDictAction, |
|
default=get_default_kwargs(ESPnetDiarizationModel), |
|
help="The keyword arguments for model class.", |
|
) |
|
|
|
group = parser.add_argument_group(description="Preprocess related") |
|
group.add_argument( |
|
"--use_preprocessor", |
|
type=str2bool, |
|
default=True, |
|
help="Apply preprocessing to data or not", |
|
) |
|
|
|
for class_choices in cls.class_choices_list: |
|
|
|
|
|
class_choices.add_arguments(group) |
|
|
|
@classmethod |
|
def build_collate_fn( |
|
cls, args: argparse.Namespace, train: bool |
|
) -> Callable[ |
|
[Collection[Tuple[str, Dict[str, np.ndarray]]]], |
|
Tuple[List[str], Dict[str, torch.Tensor]], |
|
]: |
|
assert check_argument_types() |
|
|
|
return CommonCollateFn(float_pad_value=0.0, int_pad_value=-1) |
|
|
|
@classmethod |
|
def build_preprocess_fn( |
|
cls, args: argparse.Namespace, train: bool |
|
) -> Optional[Callable[[str, Dict[str, np.array]], Dict[str, np.ndarray]]]: |
|
assert check_argument_types() |
|
if args.use_preprocessor: |
|
|
|
retval = CommonPreprocessor(train=train) |
|
else: |
|
retval = None |
|
assert check_return_type(retval) |
|
return retval |
|
|
|
@classmethod |
|
def required_data_names( |
|
cls, train: bool = True, inference: bool = False |
|
) -> Tuple[str, ...]: |
|
if not inference: |
|
retval = ("speech", "spk_labels") |
|
else: |
|
|
|
retval = ("speech",) |
|
return retval |
|
|
|
@classmethod |
|
def optional_data_names( |
|
cls, train: bool = True, inference: bool = False |
|
) -> Tuple[str, ...]: |
|
|
|
retval = () |
|
assert check_return_type(retval) |
|
return retval |
|
|
|
@classmethod |
|
def build_model(cls, args: argparse.Namespace) -> ESPnetDiarizationModel: |
|
assert check_argument_types() |
|
|
|
|
|
if args.input_size is None: |
|
|
|
frontend_class = frontend_choices.get_class(args.frontend) |
|
frontend = frontend_class(**args.frontend_conf) |
|
input_size = frontend.output_size() |
|
else: |
|
|
|
args.frontend = None |
|
args.frontend_conf = {} |
|
frontend = None |
|
input_size = args.input_size |
|
|
|
|
|
if args.normalize is not None: |
|
normalize_class = normalize_choices.get_class(args.normalize) |
|
normalize = normalize_class(**args.normalize_conf) |
|
else: |
|
normalize = None |
|
|
|
|
|
label_aggregator_class = label_aggregator_choices.get_class( |
|
args.label_aggregator |
|
) |
|
label_aggregator = label_aggregator_class(**args.label_aggregator_conf) |
|
|
|
|
|
encoder_class = encoder_choices.get_class(args.encoder) |
|
|
|
encoder = encoder_class(input_size=input_size, **args.encoder_conf) |
|
|
|
|
|
decoder_class = decoder_choices.get_class(args.decoder) |
|
decoder = decoder_class( |
|
num_spk=args.num_spk, |
|
encoder_output_size=encoder.output_size(), |
|
**args.decoder_conf, |
|
) |
|
|
|
|
|
model = ESPnetDiarizationModel( |
|
frontend=frontend, |
|
normalize=normalize, |
|
label_aggregator=label_aggregator, |
|
encoder=encoder, |
|
decoder=decoder, |
|
**args.model_conf, |
|
) |
|
|
|
|
|
|
|
if args.init is not None: |
|
initialize(model, args.init) |
|
|
|
assert check_return_type(model) |
|
return model |
|
|