Source code for openspeech.modules.deepspeech2_extractor

# 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
from typing import Tuple

from openspeech.modules import Conv2dExtractor


[docs]class DeepSpeech2Extractor(Conv2dExtractor): r""" DeepSpeech2 extractor for automatic speech recognition described in "Deep Speech 2: End-to-End Speech Recognition in English and Mandarin" paper - https://arxiv.org/abs/1512.02595 Args: input_dim (int): Dimension of input vector in_channels (int): Number of channels in the input vector out_channels (int): Number of channels produced by the convolution activation (str): Activation function Inputs: inputs, input_lengths - **inputs** (batch, time, dim): Tensor containing input vectors - **input_lengths**: Tensor containing containing sequence lengths Returns: outputs, output_lengths - **outputs**: Tensor produced by the convolution - **output_lengths**: Tensor containing sequence lengths produced by the convolution """ def __init__( self, input_dim: int, in_channels: int = 1, out_channels: int = 32, activation: str = 'hardtanh', ) -> None: super(DeepSpeech2Extractor, self).__init__(input_dim=input_dim, activation=activation) self.in_channels = in_channels self.out_channels = out_channels from openspeech.modules import MaskConv2d self.conv = MaskConv2d( nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size=(41, 11), stride=(2, 2), padding=(20, 5), bias=False), nn.BatchNorm2d(out_channels), self.activation, nn.Conv2d(out_channels, out_channels, kernel_size=(21, 11), stride=(2, 1), padding=(10, 5), bias=False), nn.BatchNorm2d(out_channels), self.activation, ) )
[docs] def forward(self, inputs: torch.Tensor, input_lengths: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: return super().forward(inputs, input_lengths)