# MIT License
#
# Copyright (c) 2021 Soohwan Kim and Sangchun Ha and Soyoung Cho
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import torch
import torch.nn as nn
import pytorch_lightning as pl
from typing import Dict
from omegaconf import DictConfig
from torch import Tensor
from torch.optim import Adam, Adagrad, Adadelta, Adamax, AdamW, SGD, ASGD
from openspeech.optim import AdamP, RAdam, Novograd
from openspeech.criterion import CRITERION_REGISTRY
from openspeech.metrics import WordErrorRate, CharacterErrorRate
from openspeech.optim.scheduler import SCHEDULER_REGISTRY
from openspeech.tokenizers.tokenizer import Tokenizer
[docs]class OpenspeechModel(pl.LightningModule):
r"""
Super class of openspeech models.
Note:
Do not use this class directly, use one of the sub classes.
Args:
configs (DictConfig): configuration set.
tokenizer (Tokenizer): tokenizer is in charge of preparing the inputs for a model.
Inputs:
inputs (torch.FloatTensor): A input sequence passed to encoders. Typically for inputs this will be a padded `FloatTensor` of size ``(batch, seq_length, dimension)``.
input_lengths (torch.LongTensor): The length of input tensor. ``(batch)``
Returns:
outputs (dict): Result of model predictions.
"""
def __init__(self, configs: DictConfig, tokenizer: Tokenizer) -> None:
super(OpenspeechModel, self).__init__()
self.configs = configs
self.num_classes = len(tokenizer)
self.gradient_clip_val = configs.trainer.gradient_clip_val
self.tokenizer = tokenizer
self.current_val_loss = 100.0
self.wer_metric = WordErrorRate(tokenizer)
self.cer_metric = CharacterErrorRate(tokenizer)
self.tokenizer = tokenizer
self.criterion = self.configure_criterion(configs.criterion.criterion_name)
def build_model(self):
raise NotImplementedError
def set_beam_decoder(self, beam_size: int = 3):
raise NotImplementedError
[docs] def info(self, dictionary: dict) -> None:
r"""
Logging information from dictionary.
Args:
dictionary (dict): dictionary contains information.
"""
for key, value in dictionary.items():
self.log(key, value, prog_bar=True)
[docs] def forward(self, inputs: torch.FloatTensor, input_lengths: torch.LongTensor) -> Dict[str, Tensor]:
r"""
Forward propagate a `inputs` and `targets` pair for inference.
Inputs:
inputs (torch.FloatTensor): A input sequence passed to encoders. Typically for inputs this will be a padded `FloatTensor` of size ``(batch, seq_length, dimension)``.
input_lengths (torch.LongTensor): The length of input tensor. ``(batch)``
Returns:
outputs (dict): Result of model predictions.
"""
raise NotImplementedError
[docs] def training_step(self, batch: tuple, batch_idx: int):
r"""
Forward propagate a `inputs` and `targets` pair for training.
Inputs:
batch (tuple): A train batch contains `inputs`, `targets`, `input_lengths`, `target_lengths`
batch_idx (int): The index of batch
Returns:
loss (torch.Tensor): loss for training
"""
raise NotImplementedError
[docs] def validation_step(self, batch: tuple, batch_idx: int):
r"""
Forward propagate a `inputs` and `targets` pair for validation.
Inputs:
batch (tuple): A train batch contains `inputs`, `targets`, `input_lengths`, `target_lengths`
batch_idx (int): The index of batch
Returns:
loss (torch.Tensor): loss for training
"""
raise NotImplementedError
[docs] def test_step(self, batch: tuple, batch_idx: int):
r"""
Forward propagate a `inputs` and `targets` pair for test.
Inputs:
batch (tuple): A train batch contains `inputs`, `targets`, `input_lengths`, `target_lengths`
batch_idx (int): The index of batch
Returns:
loss (torch.Tensor): loss for training
"""
raise NotImplementedError
def get_lr(self):
for g in self.optimizer.param_groups:
return g['lr']
def set_lr(self, lr):
for g in self.optimizer.param_groups:
g['lr'] = lr