Source code for openspeech.modules.conv2d_subsampling

# 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 Conv2dSubsampling(Conv2dExtractor): r""" Convolutional 2D subsampling (to 1/4 length) 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 - **inputs** (batch, time, dim): Tensor containing sequence of inputs - **input_lengths** (batch): list of sequence input lengths Returns: outputs, output_lengths - **outputs** (batch, time, dim): Tensor produced by the convolution - **output_lengths** (batch): list of sequence output lengths """ def __init__( self, input_dim: int, in_channels: int, out_channels: int, activation: str = 'relu', ) -> None: super(Conv2dSubsampling, self).__init__(input_dim, 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=3, stride=2), self.activation, nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=2), self.activation, ) )
[docs] def forward(self, inputs: torch.Tensor, input_lengths: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: outputs, input_lengths = super().forward(inputs, input_lengths) output_lengths = input_lengths >> 2 output_lengths -= 1 return outputs, output_lengths