Spaces:
Build error
Build error
Commit
·
525a4f7
1
Parent(s):
cb3973b
Upload VariancePredictor.py
Browse files- VariancePredictor.py +65 -0
VariancePredictor.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2019 Tomoki Hayashi
|
| 2 |
+
# MIT License (https://opensource.org/licenses/MIT)
|
| 3 |
+
# Adapted by Florian Lux 2021
|
| 4 |
+
|
| 5 |
+
from abc import ABC
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
from Layers.LayerNorm import LayerNorm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class VariancePredictor(torch.nn.Module, ABC):
|
| 13 |
+
"""
|
| 14 |
+
Variance predictor module.
|
| 15 |
+
|
| 16 |
+
This is a module of variance predictor described in `FastSpeech 2:
|
| 17 |
+
Fast and High-Quality End-to-End Text to Speech`_.
|
| 18 |
+
|
| 19 |
+
.. _`FastSpeech 2: Fast and High-Quality End-to-End Text to Speech`:
|
| 20 |
+
https://arxiv.org/abs/2006.04558
|
| 21 |
+
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
def __init__(self, idim, n_layers=2, n_chans=384, kernel_size=3, bias=True, dropout_rate=0.5, ):
|
| 25 |
+
"""
|
| 26 |
+
Initilize duration predictor module.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
idim (int): Input dimension.
|
| 30 |
+
n_layers (int, optional): Number of convolutional layers.
|
| 31 |
+
n_chans (int, optional): Number of channels of convolutional layers.
|
| 32 |
+
kernel_size (int, optional): Kernel size of convolutional layers.
|
| 33 |
+
dropout_rate (float, optional): Dropout rate.
|
| 34 |
+
"""
|
| 35 |
+
super().__init__()
|
| 36 |
+
self.conv = torch.nn.ModuleList()
|
| 37 |
+
for idx in range(n_layers):
|
| 38 |
+
in_chans = idim if idx == 0 else n_chans
|
| 39 |
+
self.conv += [
|
| 40 |
+
torch.nn.Sequential(torch.nn.Conv1d(in_chans, n_chans, kernel_size, stride=1, padding=(kernel_size - 1) // 2, bias=bias, ), torch.nn.ReLU(),
|
| 41 |
+
LayerNorm(n_chans, dim=1), torch.nn.Dropout(dropout_rate), )]
|
| 42 |
+
self.linear = torch.nn.Linear(n_chans, 1)
|
| 43 |
+
|
| 44 |
+
def forward(self, xs, x_masks=None):
|
| 45 |
+
"""
|
| 46 |
+
Calculate forward propagation.
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
xs (Tensor): Batch of input sequences (B, Tmax, idim).
|
| 50 |
+
x_masks (ByteTensor, optional):
|
| 51 |
+
Batch of masks indicating padded part (B, Tmax).
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
Tensor: Batch of predicted sequences (B, Tmax, 1).
|
| 55 |
+
"""
|
| 56 |
+
xs = xs.transpose(1, -1) # (B, idim, Tmax)
|
| 57 |
+
for f in self.conv:
|
| 58 |
+
xs = f(xs) # (B, C, Tmax)
|
| 59 |
+
|
| 60 |
+
xs = self.linear(xs.transpose(1, 2)) # (B, Tmax, 1)
|
| 61 |
+
|
| 62 |
+
if x_masks is not None:
|
| 63 |
+
xs = xs.masked_fill(x_masks, 0.0)
|
| 64 |
+
|
| 65 |
+
return xs
|