espnet2.tasks package

espnet2.tasks.abs_task

class espnet2.tasks.abs_task.AbsTask[source]

Bases: abc.ABC

abstract classmethod add_task_arguments(parser: argparse.ArgumentParser)[source]
classmethod build_chunk_iter_factory(args: argparse.Namespace, iter_options: espnet2.tasks.abs_task.IteratorOptions, mode: str) → espnet2.iterators.abs_iter_factory.AbsIterFactory[source]
abstract classmethod build_collate_fn(args: argparse.Namespace, train: bool) → Callable[[Sequence[Dict[str, numpy.ndarray]]], Dict[str, torch.Tensor]][source]

Return “collate_fn”, which is a callable object and given to DataLoader.

>>> from torch.utils.data import DataLoader
>>> loader = DataLoader(collate_fn=cls.build_collate_fn(args, train=True), ...)

In many cases, you can use our common collate_fn.

classmethod build_iter_factory(args: argparse.Namespace, distributed_option: espnet2.train.distributed_utils.DistributedOption, mode: str, kwargs: dict = None) → espnet2.iterators.abs_iter_factory.AbsIterFactory[source]

Build a factory object of mini-batch iterator.

This object is invoked at every epochs to build the iterator for each epoch as following:

>>> iter_factory = cls.build_iter_factory(...)
>>> for epoch in range(1, max_epoch):
...     for keys, batch in iter_fatory.build_iter(epoch):
...         model(**batch)

The mini-batches for each epochs are fully controlled by this class. Note that the random seed used for shuffling is decided as “seed + epoch” and the generated mini-batches can be reproduces when resuming.

Note that the definition of “epoch” doesn’t always indicate to run out of the whole training corpus. “–num_iters_per_epoch” option restricts the number of iterations for each epoch and the rest of samples for the originally epoch are left for the next epoch. e.g. If The number of mini-batches equals to 4, the following two are same:

  • 1 epoch without “–num_iters_per_epoch”

  • 4 epoch with “–num_iters_per_epoch” == 4

classmethod build_iter_options(args: argparse.Namespace, distributed_option: espnet2.train.distributed_utils.DistributedOption, mode: str)[source]
abstract classmethod build_model(args: argparse.Namespace) → espnet2.train.abs_espnet_model.AbsESPnetModel[source]
classmethod build_model_from_file(config_file: Union[pathlib.Path, str], model_file: Union[pathlib.Path, str] = None, device: str = 'cpu') → Tuple[espnet2.train.abs_espnet_model.AbsESPnetModel, argparse.Namespace][source]

This method is used for inference or fine-tuning.

Parameters
  • config_file – The yaml file saved when training.

  • model_file – The model file saved when training.

  • device

classmethod build_multiple_iter_factory(args: argparse.Namespace, distributed_option: espnet2.train.distributed_utils.DistributedOption, mode: str)[source]
classmethod build_optimizers(args: argparse.Namespace, model: torch.nn.modules.module.Module) → List[torch.optim.optimizer.Optimizer][source]
abstract classmethod build_preprocess_fn(args: argparse.Namespace, train: bool) → Optional[Callable[[str, Dict[str, numpy.array]], Dict[str, numpy.ndarray]]][source]
classmethod build_sequence_iter_factory(args: argparse.Namespace, iter_options: espnet2.tasks.abs_task.IteratorOptions, mode: str) → espnet2.iterators.abs_iter_factory.AbsIterFactory[source]
classmethod build_streaming_iterator(data_path_and_name_and_type, preprocess_fn, collate_fn, key_file: str = None, batch_size: int = 1, dtype: str = <class 'numpy.float32'>, num_workers: int = 1, allow_variable_data_keys: bool = False, ngpu: int = 0, inference: bool = False) → torch.utils.data.dataloader.DataLoader[source]

Build DataLoader using iterable dataset

classmethod build_task_iter_factory(args: argparse.Namespace, iter_options: espnet2.tasks.abs_task.IteratorOptions, mode: str) → espnet2.iterators.abs_iter_factory.AbsIterFactory[source]

Build task specific iterator factory

Example

>>> class YourTask(AbsTask):
... @classmethod
... def add_task_arguments(cls, parser: argparse.ArgumentParser):
...     parser.set_defaults(iterator_type="task")
...
... @classmethod
... def build_task_iter_factory(
...     cls,
...     args: argparse.Namespace,
...     iter_options: IteratorOptions,
...     mode: str,
... ):
...     return FooIterFactory(...)
...
... @classmethod
... def build_iter_options(
....    args: argparse.Namespace,
...     distributed_option: DistributedOption,
...     mode: str
... ):
...     # if you need to customize options object
classmethod check_required_command_args(args: argparse.Namespace)[source]
classmethod check_task_requirements(dataset: Union[espnet2.train.dataset.AbsDataset, espnet2.train.iterable_dataset.IterableESPnetDataset], allow_variable_data_keys: bool, train: bool, inference: bool = False) → None[source]

Check if the dataset satisfy the requirement of current Task

class_choices_list = []
classmethod exclude_opts() → Tuple[str, ...][source]

The options not to be shown by –print_config

classmethod get_default_config() → Dict[str, Any][source]

Return the configuration as dict.

This method is used by print_config()

classmethod get_parser() → espnet2.utils.config_argparse.ArgumentParser[source]
classmethod main(args: argparse.Namespace = None, cmd: Sequence[str] = None)[source]
classmethod main_worker(args: argparse.Namespace)[source]
num_optimizers = 1
abstract classmethod optional_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the optional names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as follows,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “optional_data_names” should be as

>>> optional_data_names = ('opt',)
classmethod print_config(file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>) → None[source]
abstract classmethod required_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the required names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as following,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “required_data_names” should be as

>>> required_data_names = ('input', 'output')
static resume(checkpoint: Union[str, pathlib.Path], model: torch.nn.modules.module.Module, reporter: espnet2.train.reporter.Reporter, optimizers: Sequence[torch.optim.optimizer.Optimizer], schedulers: Sequence[Optional[espnet2.schedulers.abs_scheduler.AbsScheduler]], scaler: Optional[torch.cuda.amp.grad_scaler.GradScaler], ngpu: int = 0)[source]
trainer

alias of espnet2.train.trainer.Trainer

class espnet2.tasks.abs_task.IteratorOptions(preprocess_fn: <built-in function callable>, collate_fn: <built-in function callable>, data_path_and_name_and_type: list, shape_files: list, batch_size: int, batch_bins: int, batch_type: str, max_cache_size: float, max_cache_fd: int, distributed: bool, num_batches: Union[int, NoneType], num_iters_per_epoch: Union[int, NoneType], train: bool)[source]

Bases: object

espnet2.tasks.asr

class espnet2.tasks.asr.ASRTask[source]

Bases: espnet2.tasks.abs_task.AbsTask

classmethod add_task_arguments(parser: argparse.ArgumentParser)[source]
classmethod build_collate_fn(args: argparse.Namespace, train: bool) → Callable[[Collection[Tuple[str, Dict[str, numpy.ndarray]]]], Tuple[List[str], Dict[str, torch.Tensor]]][source]

Return “collate_fn”, which is a callable object and given to DataLoader.

>>> from torch.utils.data import DataLoader
>>> loader = DataLoader(collate_fn=cls.build_collate_fn(args, train=True), ...)

In many cases, you can use our common collate_fn.

classmethod build_model(args: argparse.Namespace) → espnet2.asr.espnet_model.ESPnetASRModel[source]
classmethod build_preprocess_fn(args: argparse.Namespace, train: bool) → Optional[Callable[[str, Dict[str, numpy.array]], Dict[str, numpy.ndarray]]][source]
class_choices_list = [<espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>]
num_optimizers = 1
classmethod optional_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the optional names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as follows,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “optional_data_names” should be as

>>> optional_data_names = ('opt',)
classmethod required_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the required names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as following,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “required_data_names” should be as

>>> required_data_names = ('input', 'output')
trainer

alias of espnet2.train.trainer.Trainer

espnet2.tasks.tts

class espnet2.tasks.tts.TTSTask[source]

Bases: espnet2.tasks.abs_task.AbsTask

classmethod add_task_arguments(parser: argparse.ArgumentParser)[source]
classmethod build_collate_fn(args: argparse.Namespace, train: bool) → Callable[[Collection[Tuple[str, Dict[str, numpy.ndarray]]]], Tuple[List[str], Dict[str, torch.Tensor]]][source]

Return “collate_fn”, which is a callable object and given to DataLoader.

>>> from torch.utils.data import DataLoader
>>> loader = DataLoader(collate_fn=cls.build_collate_fn(args, train=True), ...)

In many cases, you can use our common collate_fn.

classmethod build_model(args: argparse.Namespace) → espnet2.tts.espnet_model.ESPnetTTSModel[source]
classmethod build_preprocess_fn(args: argparse.Namespace, train: bool) → Optional[Callable[[str, Dict[str, numpy.array]], Dict[str, numpy.ndarray]]][source]
class_choices_list = [<espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>]
num_optimizers = 1
classmethod optional_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the optional names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as follows,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “optional_data_names” should be as

>>> optional_data_names = ('opt',)
classmethod required_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the required names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as following,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “required_data_names” should be as

>>> required_data_names = ('input', 'output')
trainer

alias of espnet2.train.trainer.Trainer

espnet2.tasks.enh

class espnet2.tasks.enh.EnhancementTask[source]

Bases: espnet2.tasks.abs_task.AbsTask

classmethod add_task_arguments(parser: argparse.ArgumentParser)[source]
classmethod build_collate_fn(args: argparse.Namespace, train: bool) → Callable[[Collection[Tuple[str, Dict[str, numpy.ndarray]]]], Tuple[List[str], Dict[str, torch.Tensor]]][source]

Return “collate_fn”, which is a callable object and given to DataLoader.

>>> from torch.utils.data import DataLoader
>>> loader = DataLoader(collate_fn=cls.build_collate_fn(args, train=True), ...)

In many cases, you can use our common collate_fn.

classmethod build_model(args: argparse.Namespace) → espnet2.enh.espnet_model.ESPnetEnhancementModel[source]
classmethod build_preprocess_fn(args: argparse.Namespace, train: bool) → Optional[Callable[[str, Dict[str, numpy.array]], Dict[str, numpy.ndarray]]][source]
class_choices_list = [<espnet2.train.class_choices.ClassChoices object>]
num_optimizers = 1
classmethod optional_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the optional names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as follows,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “optional_data_names” should be as

>>> optional_data_names = ('opt',)
classmethod required_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the required names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as following,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “required_data_names” should be as

>>> required_data_names = ('input', 'output')
trainer

alias of espnet2.train.trainer.Trainer

espnet2.tasks.enh_asr

class espnet2.tasks.enh_asr.ASRTask[source]

Bases: espnet2.tasks.abs_task.AbsTask

classmethod add_task_arguments(parser: argparse.ArgumentParser)[source]
classmethod build_collate_fn(args: argparse.Namespace, train: bool) → Callable[[Collection[Tuple[str, Dict[str, numpy.ndarray]]]], Tuple[List[str], Dict[str, torch.Tensor]]][source]

Return “collate_fn”, which is a callable object and given to DataLoader.

>>> from torch.utils.data import DataLoader
>>> loader = DataLoader(collate_fn=cls.build_collate_fn(args, train=True), ...)

In many cases, you can use our common collate_fn.

classmethod build_model(args: argparse.Namespace) → espnet2.asr.espnet_joint_model.ESPnetEnhASRModel[source]
classmethod build_preprocess_fn(args: argparse.Namespace, train: bool) → Optional[Callable[[str, Dict[str, numpy.array]], Dict[str, numpy.ndarray]]][source]
class_choices_list = [<espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>, <espnet2.train.class_choices.ClassChoices object>]
num_optimizers = 1
classmethod optional_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the optional names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as follows,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “optional_data_names” should be as

>>> optional_data_names = ('opt',)
classmethod required_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the required names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as following,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “required_data_names” should be as

>>> required_data_names = ('input', 'output')
trainer

alias of espnet2.train.trainer.Trainer

espnet2.tasks.__init__

espnet2.tasks.lm

class espnet2.tasks.lm.LMTask[source]

Bases: espnet2.tasks.abs_task.AbsTask

classmethod add_task_arguments(parser: argparse.ArgumentParser)[source]
classmethod build_collate_fn(args: argparse.Namespace, train: bool) → Callable[[Collection[Tuple[str, Dict[str, numpy.ndarray]]]], Tuple[List[str], Dict[str, torch.Tensor]]][source]

Return “collate_fn”, which is a callable object and given to DataLoader.

>>> from torch.utils.data import DataLoader
>>> loader = DataLoader(collate_fn=cls.build_collate_fn(args, train=True), ...)

In many cases, you can use our common collate_fn.

classmethod build_model(args: argparse.Namespace) → espnet2.lm.espnet_model.ESPnetLanguageModel[source]
classmethod build_preprocess_fn(args: argparse.Namespace, train: bool) → Optional[Callable[[str, Dict[str, numpy.array]], Dict[str, numpy.ndarray]]][source]
class_choices_list = [<espnet2.train.class_choices.ClassChoices object>]
num_optimizers = 1
classmethod optional_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the optional names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as follows,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “optional_data_names” should be as

>>> optional_data_names = ('opt',)
classmethod required_data_names(train: bool = True, inference: bool = False) → Tuple[str, ...][source]

Define the required names by Task

This function is used by >>> cls.check_task_requirements() If your model is defined as following,

>>> from espnet2.train.abs_espnet_model import AbsESPnetModel
>>> class Model(AbsESPnetModel):
...     def forward(self, input, output, opt=None):  pass

then “required_data_names” should be as

>>> required_data_names = ('input', 'output')
trainer

alias of espnet2.train.trainer.Trainer