MCPcopy Create free account
hub / github.com/MeiGen-AI/MultiTalk / ProsodyPredictor

Class ProsodyPredictor

kokoro/modules.py:91–134  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

89
90
91class ProsodyPredictor(nn.Module):
92 def __init__(self, style_dim, d_hid, nlayers, max_dur=50, dropout=0.1):
93 super().__init__()
94 self.text_encoder = DurationEncoder(sty_dim=style_dim, d_model=d_hid,nlayers=nlayers, dropout=dropout)
95 self.lstm = nn.LSTM(d_hid + style_dim, d_hid // 2, 1, batch_first=True, bidirectional=True)
96 self.duration_proj = LinearNorm(d_hid, max_dur)
97 self.shared = nn.LSTM(d_hid + style_dim, d_hid // 2, 1, batch_first=True, bidirectional=True)
98 self.F0 = nn.ModuleList()
99 self.F0.append(AdainResBlk1d(d_hid, d_hid, style_dim, dropout_p=dropout))
100 self.F0.append(AdainResBlk1d(d_hid, d_hid // 2, style_dim, upsample=True, dropout_p=dropout))
101 self.F0.append(AdainResBlk1d(d_hid // 2, d_hid // 2, style_dim, dropout_p=dropout))
102 self.N = nn.ModuleList()
103 self.N.append(AdainResBlk1d(d_hid, d_hid, style_dim, dropout_p=dropout))
104 self.N.append(AdainResBlk1d(d_hid, d_hid // 2, style_dim, upsample=True, dropout_p=dropout))
105 self.N.append(AdainResBlk1d(d_hid // 2, d_hid // 2, style_dim, dropout_p=dropout))
106 self.F0_proj = nn.Conv1d(d_hid // 2, 1, 1, 1, 0)
107 self.N_proj = nn.Conv1d(d_hid // 2, 1, 1, 1, 0)
108
109 def forward(self, texts, style, text_lengths, alignment, m):
110 d = self.text_encoder(texts, style, text_lengths, m)
111 m = m.unsqueeze(1)
112 lengths = text_lengths if text_lengths.device == torch.device('cpu') else text_lengths.to('cpu')
113 x = nn.utils.rnn.pack_padded_sequence(d, lengths, batch_first=True, enforce_sorted=False)
114 self.lstm.flatten_parameters()
115 x, _ = self.lstm(x)
116 x, _ = nn.utils.rnn.pad_packed_sequence(x, batch_first=True)
117 x_pad = torch.zeros([x.shape[0], m.shape[-1], x.shape[-1]], device=x.device)
118 x_pad[:, :x.shape[1], :] = x
119 x = x_pad
120 duration = self.duration_proj(nn.functional.dropout(x, 0.5, training=False))
121 en = (d.transpose(-1, -2) @ alignment)
122 return duration.squeeze(-1), en
123
124 def F0Ntrain(self, x, s):
125 x, _ = self.shared(x.transpose(-1, -2))
126 F0 = x.transpose(-1, -2)
127 for block in self.F0:
128 F0 = block(F0, s)
129 F0 = self.F0_proj(F0)
130 N = x.transpose(-1, -2)
131 for block in self.N:
132 N = block(N, s)
133 N = self.N_proj(N)
134 return F0.squeeze(1), N.squeeze(1)
135
136
137class DurationEncoder(nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected