Source code for openspeech.modules.conv2d_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 math
import torch
import torch.nn as nn
from torch import Tensor
from typing import Tuple

from openspeech.modules.swish import Swish
from openspeech.utils import get_class_name


[docs]class Conv2dExtractor(nn.Module): r""" Provides inteface of convolutional extractor. Note: Do not use this class directly, use one of the sub classes. Define the 'self.conv' class variable. 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 """ supported_activations = { 'hardtanh': nn.Hardtanh(0, 20, inplace=True), 'relu': nn.ReLU(inplace=True), 'elu': nn.ELU(inplace=True), 'leaky_relu': nn.LeakyReLU(inplace=True), 'gelu': nn.GELU(), 'swish': Swish(), } def __init__(self, input_dim: int, activation: str = 'hardtanh') -> None: super(Conv2dExtractor, self).__init__() self.input_dim = input_dim self.activation = Conv2dExtractor.supported_activations[activation] self.conv = None def get_output_lengths(self, seq_lengths: torch.Tensor): assert self.conv is not None, "self.conv should be defined" for module in self.conv: if isinstance(module, nn.Conv2d): numerator = seq_lengths + 2 * module.padding[1] - module.dilation[1] * (module.kernel_size[1] - 1) - 1 seq_lengths = numerator.float() / float(module.stride[1]) seq_lengths = seq_lengths.int() + 1 elif isinstance(module, nn.MaxPool2d): seq_lengths >>= 1 return seq_lengths.int() def get_output_dim(self): if get_class_name(self) == "VGGExtractor": output_dim = (self.input_dim - 1) << 5 if self.input_dim % 2 else self.input_dim << 5 elif get_class_name(self) == "DeepSpeech2Extractor": output_dim = int(math.floor(self.input_dim + 2 * 20 - 41) / 2 + 1) output_dim = int(math.floor(output_dim + 2 * 10 - 21) / 2 + 1) output_dim <<= 5 elif get_class_name(self) == "Conv2dSubsampling": factor = ((self.input_dim - 1) // 2 - 1) // 2 output_dim = self.out_channels * factor else: raise ValueError(f"Unsupported Extractor : {self.extractor}") return output_dim
[docs] def forward(self, inputs: Tensor, input_lengths: Tensor) -> Tuple[Tensor, Tensor]: r""" inputs: torch.FloatTensor (batch, time, dimension) input_lengths: torch.IntTensor (batch) """ outputs, output_lengths = self.conv(inputs.unsqueeze(1).transpose(2, 3), input_lengths) batch_size, channels, dimension, seq_lengths = outputs.size() outputs = outputs.permute(0, 3, 1, 2) outputs = outputs.view(batch_size, seq_lengths, channels * dimension) return outputs, output_lengths