Source code for openspeech.modules.vgg_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 VGGExtractor(Conv2dExtractor):
r"""
VGG extractor for automatic speech recognition described in
"Advances in Joint CTC-Attention based End-to-End Speech Recognition with a Deep CNN Encoder and RNN-LM" paper
- https://arxiv.org/pdf/1706.02737.pdf
Args:
input_dim (int): Dimension of input vector
in_channels (int): Number of channels in the input image
out_channels (int or tuple): 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 or tuple = (64, 128),
activation: str = 'hardtanh',
):
super(VGGExtractor, 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[0], kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(num_features=out_channels[0]),
self.activation,
nn.Conv2d(out_channels[0], out_channels[0], kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(num_features=out_channels[0]),
self.activation,
nn.MaxPool2d(2, stride=2),
nn.Conv2d(out_channels[0], out_channels[1], kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(num_features=out_channels[1]),
self.activation,
nn.Conv2d(out_channels[1], out_channels[1], kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(num_features=out_channels[1]),
self.activation,
nn.MaxPool2d(2, stride=2),
)
)
[docs] def forward(self, inputs: torch.Tensor, input_lengths: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
return super().forward(inputs, input_lengths)