Spaces:
Build error
Build error
Upload 2 files
Browse files- data_utils.py +155 -0
- onnxexport/model_onnx.py +335 -0
data_utils.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
+
import torch.utils.data
|
7 |
+
|
8 |
+
import modules.commons as commons
|
9 |
+
import utils
|
10 |
+
from modules.mel_processing import spectrogram_torch, spec_to_mel_torch
|
11 |
+
from utils import load_wav_to_torch, load_filepaths_and_text
|
12 |
+
|
13 |
+
# import h5py
|
14 |
+
|
15 |
+
|
16 |
+
"""Multi speaker version"""
|
17 |
+
|
18 |
+
|
19 |
+
class TextAudioSpeakerLoader(torch.utils.data.Dataset):
|
20 |
+
"""
|
21 |
+
1) loads audio, speaker_id, text pairs
|
22 |
+
2) normalizes text and converts them to sequences of integers
|
23 |
+
3) computes spectrograms from audio files.
|
24 |
+
"""
|
25 |
+
|
26 |
+
def __init__(self, audiopaths, hparams, all_in_mem: bool = False):
|
27 |
+
self.audiopaths = load_filepaths_and_text(audiopaths)
|
28 |
+
self.max_wav_value = hparams.data.max_wav_value
|
29 |
+
self.sampling_rate = hparams.data.sampling_rate
|
30 |
+
self.filter_length = hparams.data.filter_length
|
31 |
+
self.hop_length = hparams.data.hop_length
|
32 |
+
self.win_length = hparams.data.win_length
|
33 |
+
self.sampling_rate = hparams.data.sampling_rate
|
34 |
+
self.use_sr = hparams.train.use_sr
|
35 |
+
self.spec_len = hparams.train.max_speclen
|
36 |
+
self.spk_map = hparams.spk
|
37 |
+
|
38 |
+
random.seed(1234)
|
39 |
+
random.shuffle(self.audiopaths)
|
40 |
+
|
41 |
+
self.all_in_mem = all_in_mem
|
42 |
+
if self.all_in_mem:
|
43 |
+
self.cache = [self.get_audio(p[0]) for p in self.audiopaths]
|
44 |
+
|
45 |
+
def get_audio(self, filename):
|
46 |
+
filename = filename.replace("\\", "/")
|
47 |
+
audio, sampling_rate = load_wav_to_torch(filename)
|
48 |
+
if sampling_rate != self.sampling_rate:
|
49 |
+
raise ValueError("{} SR doesn't match target {} SR".format(
|
50 |
+
sampling_rate, self.sampling_rate))
|
51 |
+
audio_norm = audio / self.max_wav_value
|
52 |
+
audio_norm = audio_norm.unsqueeze(0)
|
53 |
+
spec_filename = filename.replace(".wav", ".spec.pt")
|
54 |
+
|
55 |
+
# Ideally, all data generated after Mar 25 should have .spec.pt
|
56 |
+
if os.path.exists(spec_filename):
|
57 |
+
spec = torch.load(spec_filename)
|
58 |
+
else:
|
59 |
+
spec = spectrogram_torch(audio_norm, self.filter_length,
|
60 |
+
self.sampling_rate, self.hop_length, self.win_length,
|
61 |
+
center=False)
|
62 |
+
spec = torch.squeeze(spec, 0)
|
63 |
+
torch.save(spec, spec_filename)
|
64 |
+
|
65 |
+
spk = filename.split("/")[-2]
|
66 |
+
spk = torch.LongTensor([self.spk_map[spk]])
|
67 |
+
|
68 |
+
f0 = np.load(filename + ".f0.npy")
|
69 |
+
f0, uv = utils.interpolate_f0(f0)
|
70 |
+
f0 = torch.FloatTensor(f0)
|
71 |
+
uv = torch.FloatTensor(uv)
|
72 |
+
|
73 |
+
c = torch.load(filename+ ".soft.pt")
|
74 |
+
c = utils.repeat_expand_2d(c.squeeze(0), f0.shape[0])
|
75 |
+
|
76 |
+
|
77 |
+
lmin = min(c.size(-1), spec.size(-1))
|
78 |
+
assert abs(c.size(-1) - spec.size(-1)) < 3, (c.size(-1), spec.size(-1), f0.shape, filename)
|
79 |
+
assert abs(audio_norm.shape[1]-lmin * self.hop_length) < 3 * self.hop_length
|
80 |
+
spec, c, f0, uv = spec[:, :lmin], c[:, :lmin], f0[:lmin], uv[:lmin]
|
81 |
+
audio_norm = audio_norm[:, :lmin * self.hop_length]
|
82 |
+
|
83 |
+
return c, f0, spec, audio_norm, spk, uv
|
84 |
+
|
85 |
+
def random_slice(self, c, f0, spec, audio_norm, spk, uv):
|
86 |
+
# if spec.shape[1] < 30:
|
87 |
+
# print("skip too short audio:", filename)
|
88 |
+
# return None
|
89 |
+
if spec.shape[1] > 800:
|
90 |
+
start = random.randint(0, spec.shape[1]-800)
|
91 |
+
end = start + 790
|
92 |
+
spec, c, f0, uv = spec[:, start:end], c[:, start:end], f0[start:end], uv[start:end]
|
93 |
+
audio_norm = audio_norm[:, start * self.hop_length : end * self.hop_length]
|
94 |
+
|
95 |
+
return c, f0, spec, audio_norm, spk, uv
|
96 |
+
|
97 |
+
def __getitem__(self, index):
|
98 |
+
if self.all_in_mem:
|
99 |
+
return self.random_slice(*self.cache[index])
|
100 |
+
else:
|
101 |
+
return self.random_slice(*self.get_audio(self.audiopaths[index][0]))
|
102 |
+
|
103 |
+
def __len__(self):
|
104 |
+
return len(self.audiopaths)
|
105 |
+
|
106 |
+
|
107 |
+
class TextAudioCollate:
|
108 |
+
|
109 |
+
def __call__(self, batch):
|
110 |
+
batch = [b for b in batch if b is not None]
|
111 |
+
|
112 |
+
input_lengths, ids_sorted_decreasing = torch.sort(
|
113 |
+
torch.LongTensor([x[0].shape[1] for x in batch]),
|
114 |
+
dim=0, descending=True)
|
115 |
+
|
116 |
+
max_c_len = max([x[0].size(1) for x in batch])
|
117 |
+
max_wav_len = max([x[3].size(1) for x in batch])
|
118 |
+
|
119 |
+
lengths = torch.LongTensor(len(batch))
|
120 |
+
|
121 |
+
c_padded = torch.FloatTensor(len(batch), batch[0][0].shape[0], max_c_len)
|
122 |
+
f0_padded = torch.FloatTensor(len(batch), max_c_len)
|
123 |
+
spec_padded = torch.FloatTensor(len(batch), batch[0][2].shape[0], max_c_len)
|
124 |
+
wav_padded = torch.FloatTensor(len(batch), 1, max_wav_len)
|
125 |
+
spkids = torch.LongTensor(len(batch), 1)
|
126 |
+
uv_padded = torch.FloatTensor(len(batch), max_c_len)
|
127 |
+
|
128 |
+
c_padded.zero_()
|
129 |
+
spec_padded.zero_()
|
130 |
+
f0_padded.zero_()
|
131 |
+
wav_padded.zero_()
|
132 |
+
uv_padded.zero_()
|
133 |
+
|
134 |
+
for i in range(len(ids_sorted_decreasing)):
|
135 |
+
row = batch[ids_sorted_decreasing[i]]
|
136 |
+
|
137 |
+
c = row[0]
|
138 |
+
c_padded[i, :, :c.size(1)] = c
|
139 |
+
lengths[i] = c.size(1)
|
140 |
+
|
141 |
+
f0 = row[1]
|
142 |
+
f0_padded[i, :f0.size(0)] = f0
|
143 |
+
|
144 |
+
spec = row[2]
|
145 |
+
spec_padded[i, :, :spec.size(1)] = spec
|
146 |
+
|
147 |
+
wav = row[3]
|
148 |
+
wav_padded[i, :, :wav.size(1)] = wav
|
149 |
+
|
150 |
+
spkids[i, 0] = row[4]
|
151 |
+
|
152 |
+
uv = row[5]
|
153 |
+
uv_padded[i, :uv.size(0)] = uv
|
154 |
+
|
155 |
+
return c_padded, f0_padded, spec_padded, wav_padded, spkids, lengths, uv_padded
|
onnxexport/model_onnx.py
ADDED
@@ -0,0 +1,335 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
from torch.nn import functional as F
|
4 |
+
|
5 |
+
import modules.attentions as attentions
|
6 |
+
import modules.commons as commons
|
7 |
+
import modules.modules as modules
|
8 |
+
|
9 |
+
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
10 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
11 |
+
|
12 |
+
import utils
|
13 |
+
from modules.commons import init_weights, get_padding
|
14 |
+
from vdecoder.hifigan.models import Generator
|
15 |
+
from utils import f0_to_coarse
|
16 |
+
|
17 |
+
|
18 |
+
class ResidualCouplingBlock(nn.Module):
|
19 |
+
def __init__(self,
|
20 |
+
channels,
|
21 |
+
hidden_channels,
|
22 |
+
kernel_size,
|
23 |
+
dilation_rate,
|
24 |
+
n_layers,
|
25 |
+
n_flows=4,
|
26 |
+
gin_channels=0):
|
27 |
+
super().__init__()
|
28 |
+
self.channels = channels
|
29 |
+
self.hidden_channels = hidden_channels
|
30 |
+
self.kernel_size = kernel_size
|
31 |
+
self.dilation_rate = dilation_rate
|
32 |
+
self.n_layers = n_layers
|
33 |
+
self.n_flows = n_flows
|
34 |
+
self.gin_channels = gin_channels
|
35 |
+
|
36 |
+
self.flows = nn.ModuleList()
|
37 |
+
for i in range(n_flows):
|
38 |
+
self.flows.append(
|
39 |
+
modules.ResidualCouplingLayer(channels, hidden_channels, kernel_size, dilation_rate, n_layers,
|
40 |
+
gin_channels=gin_channels, mean_only=True))
|
41 |
+
self.flows.append(modules.Flip())
|
42 |
+
|
43 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
44 |
+
if not reverse:
|
45 |
+
for flow in self.flows:
|
46 |
+
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
47 |
+
else:
|
48 |
+
for flow in reversed(self.flows):
|
49 |
+
x = flow(x, x_mask, g=g, reverse=reverse)
|
50 |
+
return x
|
51 |
+
|
52 |
+
|
53 |
+
class Encoder(nn.Module):
|
54 |
+
def __init__(self,
|
55 |
+
in_channels,
|
56 |
+
out_channels,
|
57 |
+
hidden_channels,
|
58 |
+
kernel_size,
|
59 |
+
dilation_rate,
|
60 |
+
n_layers,
|
61 |
+
gin_channels=0):
|
62 |
+
super().__init__()
|
63 |
+
self.in_channels = in_channels
|
64 |
+
self.out_channels = out_channels
|
65 |
+
self.hidden_channels = hidden_channels
|
66 |
+
self.kernel_size = kernel_size
|
67 |
+
self.dilation_rate = dilation_rate
|
68 |
+
self.n_layers = n_layers
|
69 |
+
self.gin_channels = gin_channels
|
70 |
+
|
71 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
72 |
+
self.enc = modules.WN(hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels)
|
73 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
74 |
+
|
75 |
+
def forward(self, x, x_lengths, g=None):
|
76 |
+
# print(x.shape,x_lengths.shape)
|
77 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype)
|
78 |
+
x = self.pre(x) * x_mask
|
79 |
+
x = self.enc(x, x_mask, g=g)
|
80 |
+
stats = self.proj(x) * x_mask
|
81 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
82 |
+
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
|
83 |
+
return z, m, logs, x_mask
|
84 |
+
|
85 |
+
|
86 |
+
class TextEncoder(nn.Module):
|
87 |
+
def __init__(self,
|
88 |
+
out_channels,
|
89 |
+
hidden_channels,
|
90 |
+
kernel_size,
|
91 |
+
n_layers,
|
92 |
+
gin_channels=0,
|
93 |
+
filter_channels=None,
|
94 |
+
n_heads=None,
|
95 |
+
p_dropout=None):
|
96 |
+
super().__init__()
|
97 |
+
self.out_channels = out_channels
|
98 |
+
self.hidden_channels = hidden_channels
|
99 |
+
self.kernel_size = kernel_size
|
100 |
+
self.n_layers = n_layers
|
101 |
+
self.gin_channels = gin_channels
|
102 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
103 |
+
self.f0_emb = nn.Embedding(256, hidden_channels)
|
104 |
+
|
105 |
+
self.enc_ = attentions.Encoder(
|
106 |
+
hidden_channels,
|
107 |
+
filter_channels,
|
108 |
+
n_heads,
|
109 |
+
n_layers,
|
110 |
+
kernel_size,
|
111 |
+
p_dropout)
|
112 |
+
|
113 |
+
def forward(self, x, x_mask, f0=None, z=None):
|
114 |
+
x = x + self.f0_emb(f0).transpose(1, 2)
|
115 |
+
x = self.enc_(x * x_mask, x_mask)
|
116 |
+
stats = self.proj(x) * x_mask
|
117 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
118 |
+
z = (m + z * torch.exp(logs)) * x_mask
|
119 |
+
return z, m, logs, x_mask
|
120 |
+
|
121 |
+
|
122 |
+
class DiscriminatorP(torch.nn.Module):
|
123 |
+
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
124 |
+
super(DiscriminatorP, self).__init__()
|
125 |
+
self.period = period
|
126 |
+
self.use_spectral_norm = use_spectral_norm
|
127 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
128 |
+
self.convs = nn.ModuleList([
|
129 |
+
norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
130 |
+
norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
131 |
+
norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
132 |
+
norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
133 |
+
norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(get_padding(kernel_size, 1), 0))),
|
134 |
+
])
|
135 |
+
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
136 |
+
|
137 |
+
def forward(self, x):
|
138 |
+
fmap = []
|
139 |
+
|
140 |
+
# 1d to 2d
|
141 |
+
b, c, t = x.shape
|
142 |
+
if t % self.period != 0: # pad first
|
143 |
+
n_pad = self.period - (t % self.period)
|
144 |
+
x = F.pad(x, (0, n_pad), "reflect")
|
145 |
+
t = t + n_pad
|
146 |
+
x = x.view(b, c, t // self.period, self.period)
|
147 |
+
|
148 |
+
for l in self.convs:
|
149 |
+
x = l(x)
|
150 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
151 |
+
fmap.append(x)
|
152 |
+
x = self.conv_post(x)
|
153 |
+
fmap.append(x)
|
154 |
+
x = torch.flatten(x, 1, -1)
|
155 |
+
|
156 |
+
return x, fmap
|
157 |
+
|
158 |
+
|
159 |
+
class DiscriminatorS(torch.nn.Module):
|
160 |
+
def __init__(self, use_spectral_norm=False):
|
161 |
+
super(DiscriminatorS, self).__init__()
|
162 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
163 |
+
self.convs = nn.ModuleList([
|
164 |
+
norm_f(Conv1d(1, 16, 15, 1, padding=7)),
|
165 |
+
norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
|
166 |
+
norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
|
167 |
+
norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
|
168 |
+
norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
|
169 |
+
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
170 |
+
])
|
171 |
+
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
172 |
+
|
173 |
+
def forward(self, x):
|
174 |
+
fmap = []
|
175 |
+
|
176 |
+
for l in self.convs:
|
177 |
+
x = l(x)
|
178 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
179 |
+
fmap.append(x)
|
180 |
+
x = self.conv_post(x)
|
181 |
+
fmap.append(x)
|
182 |
+
x = torch.flatten(x, 1, -1)
|
183 |
+
|
184 |
+
return x, fmap
|
185 |
+
|
186 |
+
|
187 |
+
class F0Decoder(nn.Module):
|
188 |
+
def __init__(self,
|
189 |
+
out_channels,
|
190 |
+
hidden_channels,
|
191 |
+
filter_channels,
|
192 |
+
n_heads,
|
193 |
+
n_layers,
|
194 |
+
kernel_size,
|
195 |
+
p_dropout,
|
196 |
+
spk_channels=0):
|
197 |
+
super().__init__()
|
198 |
+
self.out_channels = out_channels
|
199 |
+
self.hidden_channels = hidden_channels
|
200 |
+
self.filter_channels = filter_channels
|
201 |
+
self.n_heads = n_heads
|
202 |
+
self.n_layers = n_layers
|
203 |
+
self.kernel_size = kernel_size
|
204 |
+
self.p_dropout = p_dropout
|
205 |
+
self.spk_channels = spk_channels
|
206 |
+
|
207 |
+
self.prenet = nn.Conv1d(hidden_channels, hidden_channels, 3, padding=1)
|
208 |
+
self.decoder = attentions.FFT(
|
209 |
+
hidden_channels,
|
210 |
+
filter_channels,
|
211 |
+
n_heads,
|
212 |
+
n_layers,
|
213 |
+
kernel_size,
|
214 |
+
p_dropout)
|
215 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
216 |
+
self.f0_prenet = nn.Conv1d(1, hidden_channels, 3, padding=1)
|
217 |
+
self.cond = nn.Conv1d(spk_channels, hidden_channels, 1)
|
218 |
+
|
219 |
+
def forward(self, x, norm_f0, x_mask, spk_emb=None):
|
220 |
+
x = torch.detach(x)
|
221 |
+
if spk_emb is not None:
|
222 |
+
x = x + self.cond(spk_emb)
|
223 |
+
x += self.f0_prenet(norm_f0)
|
224 |
+
x = self.prenet(x) * x_mask
|
225 |
+
x = self.decoder(x * x_mask, x_mask)
|
226 |
+
x = self.proj(x) * x_mask
|
227 |
+
return x
|
228 |
+
|
229 |
+
|
230 |
+
class SynthesizerTrn(nn.Module):
|
231 |
+
"""
|
232 |
+
Synthesizer for Training
|
233 |
+
"""
|
234 |
+
|
235 |
+
def __init__(self,
|
236 |
+
spec_channels,
|
237 |
+
segment_size,
|
238 |
+
inter_channels,
|
239 |
+
hidden_channels,
|
240 |
+
filter_channels,
|
241 |
+
n_heads,
|
242 |
+
n_layers,
|
243 |
+
kernel_size,
|
244 |
+
p_dropout,
|
245 |
+
resblock,
|
246 |
+
resblock_kernel_sizes,
|
247 |
+
resblock_dilation_sizes,
|
248 |
+
upsample_rates,
|
249 |
+
upsample_initial_channel,
|
250 |
+
upsample_kernel_sizes,
|
251 |
+
gin_channels,
|
252 |
+
ssl_dim,
|
253 |
+
n_speakers,
|
254 |
+
sampling_rate=44100,
|
255 |
+
**kwargs):
|
256 |
+
super().__init__()
|
257 |
+
self.spec_channels = spec_channels
|
258 |
+
self.inter_channels = inter_channels
|
259 |
+
self.hidden_channels = hidden_channels
|
260 |
+
self.filter_channels = filter_channels
|
261 |
+
self.n_heads = n_heads
|
262 |
+
self.n_layers = n_layers
|
263 |
+
self.kernel_size = kernel_size
|
264 |
+
self.p_dropout = p_dropout
|
265 |
+
self.resblock = resblock
|
266 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
267 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
268 |
+
self.upsample_rates = upsample_rates
|
269 |
+
self.upsample_initial_channel = upsample_initial_channel
|
270 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
271 |
+
self.segment_size = segment_size
|
272 |
+
self.gin_channels = gin_channels
|
273 |
+
self.ssl_dim = ssl_dim
|
274 |
+
self.emb_g = nn.Embedding(n_speakers, gin_channels)
|
275 |
+
|
276 |
+
self.pre = nn.Conv1d(ssl_dim, hidden_channels, kernel_size=5, padding=2)
|
277 |
+
|
278 |
+
self.enc_p = TextEncoder(
|
279 |
+
inter_channels,
|
280 |
+
hidden_channels,
|
281 |
+
filter_channels=filter_channels,
|
282 |
+
n_heads=n_heads,
|
283 |
+
n_layers=n_layers,
|
284 |
+
kernel_size=kernel_size,
|
285 |
+
p_dropout=p_dropout
|
286 |
+
)
|
287 |
+
hps = {
|
288 |
+
"sampling_rate": sampling_rate,
|
289 |
+
"inter_channels": inter_channels,
|
290 |
+
"resblock": resblock,
|
291 |
+
"resblock_kernel_sizes": resblock_kernel_sizes,
|
292 |
+
"resblock_dilation_sizes": resblock_dilation_sizes,
|
293 |
+
"upsample_rates": upsample_rates,
|
294 |
+
"upsample_initial_channel": upsample_initial_channel,
|
295 |
+
"upsample_kernel_sizes": upsample_kernel_sizes,
|
296 |
+
"gin_channels": gin_channels,
|
297 |
+
}
|
298 |
+
self.dec = Generator(h=hps)
|
299 |
+
self.enc_q = Encoder(spec_channels, inter_channels, hidden_channels, 5, 1, 16, gin_channels=gin_channels)
|
300 |
+
self.flow = ResidualCouplingBlock(inter_channels, hidden_channels, 5, 1, 4, gin_channels=gin_channels)
|
301 |
+
self.f0_decoder = F0Decoder(
|
302 |
+
1,
|
303 |
+
hidden_channels,
|
304 |
+
filter_channels,
|
305 |
+
n_heads,
|
306 |
+
n_layers,
|
307 |
+
kernel_size,
|
308 |
+
p_dropout,
|
309 |
+
spk_channels=gin_channels
|
310 |
+
)
|
311 |
+
self.emb_uv = nn.Embedding(2, hidden_channels)
|
312 |
+
self.predict_f0 = False
|
313 |
+
|
314 |
+
def forward(self, c, f0, mel2ph, uv, noise=None, g=None):
|
315 |
+
|
316 |
+
decoder_inp = F.pad(c, [0, 0, 1, 0])
|
317 |
+
mel2ph_ = mel2ph.unsqueeze(2).repeat([1, 1, c.shape[-1]])
|
318 |
+
c = torch.gather(decoder_inp, 1, mel2ph_).transpose(1, 2) # [B, T, H]
|
319 |
+
|
320 |
+
c_lengths = (torch.ones(c.size(0)) * c.size(-1)).to(c.device)
|
321 |
+
g = g.unsqueeze(0)
|
322 |
+
g = self.emb_g(g).transpose(1, 2)
|
323 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(c_lengths, c.size(2)), 1).to(c.dtype)
|
324 |
+
x = self.pre(c) * x_mask + self.emb_uv(uv.long()).transpose(1, 2)
|
325 |
+
|
326 |
+
if self.predict_f0:
|
327 |
+
lf0 = 2595. * torch.log10(1. + f0.unsqueeze(1) / 700.) / 500
|
328 |
+
norm_lf0 = utils.normalize_f0(lf0, x_mask, uv, random_scale=False)
|
329 |
+
pred_lf0 = self.f0_decoder(x, norm_lf0, x_mask, spk_emb=g)
|
330 |
+
f0 = (700 * (torch.pow(10, pred_lf0 * 500 / 2595) - 1)).squeeze(1)
|
331 |
+
|
332 |
+
z_p, m_p, logs_p, c_mask = self.enc_p(x, x_mask, f0=f0_to_coarse(f0), z=noise)
|
333 |
+
z = self.flow(z_p, c_mask, g=g, reverse=True)
|
334 |
+
o = self.dec(z * c_mask, g=g, f0=f0)
|
335 |
+
return o
|