Spaces:
Running
Running
| # This module is from [WeNet](https://github.com/wenet-e2e/wenet). | |
| # ## Citations | |
| # ```bibtex | |
| # @inproceedings{yao2021wenet, | |
| # title={WeNet: Production oriented Streaming and Non-streaming End-to-End Speech Recognition Toolkit}, | |
| # author={Yao, Zhuoyuan and Wu, Di and Wang, Xiong and Zhang, Binbin and Yu, Fan and Yang, Chao and Peng, Zhendong and Chen, Xiaoyu and Xie, Lei and Lei, Xin}, | |
| # booktitle={Proc. Interspeech}, | |
| # year={2021}, | |
| # address={Brno, Czech Republic }, | |
| # organization={IEEE} | |
| # } | |
| # @article{zhang2022wenet, | |
| # title={WeNet 2.0: More Productive End-to-End Speech Recognition Toolkit}, | |
| # author={Zhang, Binbin and Wu, Di and Peng, Zhendong and Song, Xingchen and Yao, Zhuoyuan and Lv, Hang and Xie, Lei and Yang, Chao and Pan, Fuping and Niu, Jianwei}, | |
| # journal={arXiv preprint arXiv:2203.15455}, | |
| # year={2022} | |
| # } | |
| # | |
| """Subsampling layer definition.""" | |
| from typing import Tuple, Union | |
| import torch | |
| from modules.wenet_extractor.transformer.subsampling import BaseSubsampling | |
| class Conv2dSubsampling2(BaseSubsampling): | |
| """Convolutional 2D subsampling (to 1/4 length). | |
| Args: | |
| idim (int): Input dimension. | |
| odim (int): Output dimension. | |
| dropout_rate (float): Dropout rate. | |
| """ | |
| def __init__( | |
| self, idim: int, odim: int, dropout_rate: float, pos_enc_class: torch.nn.Module | |
| ): | |
| """Construct an Conv2dSubsampling4 object.""" | |
| super().__init__() | |
| self.conv = torch.nn.Sequential(torch.nn.Conv2d(1, odim, 3, 2), torch.nn.ReLU()) | |
| self.out = torch.nn.Sequential(torch.nn.Linear(odim * ((idim - 1) // 2), odim)) | |
| self.pos_enc = pos_enc_class | |
| # The right context for every conv layer is computed by: | |
| # (kernel_size - 1) * frame_rate_of_this_layer | |
| self.subsampling_rate = 2 | |
| # 2 = (3 - 1) * 1 | |
| self.right_context = 2 | |
| def forward( | |
| self, | |
| x: torch.Tensor, | |
| x_mask: torch.Tensor, | |
| offset: Union[int, torch.Tensor] = 0, | |
| ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | |
| """Subsample x. | |
| Args: | |
| x (torch.Tensor): Input tensor (#batch, time, idim). | |
| x_mask (torch.Tensor): Input mask (#batch, 1, time). | |
| Returns: | |
| torch.Tensor: Subsampled tensor (#batch, time', odim), | |
| where time' = time // 2. | |
| torch.Tensor: Subsampled mask (#batch, 1, time'), | |
| where time' = time // 2. | |
| torch.Tensor: positional encoding | |
| """ | |
| x = x.unsqueeze(1) # (b, c=1, t, f) | |
| x = self.conv(x) | |
| b, c, t, f = x.size() | |
| x = self.out(x.transpose(1, 2).contiguous().view(b, t, c * f)) | |
| x, pos_emb = self.pos_enc(x, offset) | |
| return x, pos_emb, x_mask[:, :, :-2:2] | |