Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
hifigan vocoder
Browse files
resources/app/python/hifigan/config.json
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"resblock": "1",
|
3 |
+
"num_gpus": 0,
|
4 |
+
"batch_size": 46,
|
5 |
+
"learning_rate": 0.0002,
|
6 |
+
"adam_b1": 0.8,
|
7 |
+
"adam_b2": 0.99,
|
8 |
+
"lr_decay": 0.999,
|
9 |
+
"seed": 1234,
|
10 |
+
|
11 |
+
"upsample_rates": [8,8,2,2],
|
12 |
+
"upsample_kernel_sizes": [16,16,4,4],
|
13 |
+
"upsample_initial_channel": 512,
|
14 |
+
"resblock_kernel_sizes": [3,7,11],
|
15 |
+
"resblock_dilation_sizes": [[1,3,5], [1,3,5], [1,3,5]],
|
16 |
+
|
17 |
+
"segment_size": 8192,
|
18 |
+
"num_mels": 80,
|
19 |
+
"num_freq": 1025,
|
20 |
+
"n_fft": 1024,
|
21 |
+
"hop_size": 256,
|
22 |
+
"win_size": 1024,
|
23 |
+
|
24 |
+
"sampling_rate": 22050,
|
25 |
+
|
26 |
+
"fmin": 0,
|
27 |
+
"fmax": 8000,
|
28 |
+
"fmax_for_loss": null,
|
29 |
+
|
30 |
+
"num_workers": 8,
|
31 |
+
|
32 |
+
"dist_config": {
|
33 |
+
"dist_backend": "nccl",
|
34 |
+
"dist_url": "tcp://localhost:54321",
|
35 |
+
"world_size": 1
|
36 |
+
}
|
37 |
+
}
|
resources/app/python/hifigan/hifi.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:771eaf4876485a35e25577563d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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
|
4 |
+
import torch
|
5 |
+
import torch.nn.functional as F
|
6 |
+
import torch.nn as nn
|
7 |
+
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
8 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
9 |
+
# from utils import init_weights, get_padding
|
10 |
+
def init_weights(m, mean=0.0, std=0.01):
|
11 |
+
classname = m.__class__.__name__
|
12 |
+
if classname.find("Conv") != -1:
|
13 |
+
m.weight.data.normal_(mean, std)
|
14 |
+
def get_padding(kernel_size, dilation=1):
|
15 |
+
return int((kernel_size*dilation - dilation)/2)
|
16 |
+
|
17 |
+
LRELU_SLOPE = 0.1
|
18 |
+
|
19 |
+
|
20 |
+
class ResBlock1(torch.nn.Module):
|
21 |
+
def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5)):
|
22 |
+
super(ResBlock1, self).__init__()
|
23 |
+
self.h = h
|
24 |
+
self.convs1 = nn.ModuleList([
|
25 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
|
26 |
+
padding=get_padding(kernel_size, dilation[0]))),
|
27 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
|
28 |
+
padding=get_padding(kernel_size, dilation[1]))),
|
29 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
|
30 |
+
padding=get_padding(kernel_size, dilation[2])))
|
31 |
+
])
|
32 |
+
self.convs1.apply(init_weights)
|
33 |
+
|
34 |
+
self.convs2 = nn.ModuleList([
|
35 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
36 |
+
padding=get_padding(kernel_size, 1))),
|
37 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
38 |
+
padding=get_padding(kernel_size, 1))),
|
39 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
|
40 |
+
padding=get_padding(kernel_size, 1)))
|
41 |
+
])
|
42 |
+
self.convs2.apply(init_weights)
|
43 |
+
|
44 |
+
def forward(self, x):
|
45 |
+
for c1, c2 in zip(self.convs1, self.convs2):
|
46 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
47 |
+
xt = c1(xt)
|
48 |
+
xt = F.leaky_relu(xt, LRELU_SLOPE)
|
49 |
+
xt = c2(xt)
|
50 |
+
x = xt + x
|
51 |
+
return x
|
52 |
+
|
53 |
+
def remove_weight_norm(self):
|
54 |
+
for l in self.convs1:
|
55 |
+
remove_weight_norm(l)
|
56 |
+
for l in self.convs2:
|
57 |
+
remove_weight_norm(l)
|
58 |
+
|
59 |
+
|
60 |
+
class ResBlock2(torch.nn.Module):
|
61 |
+
def __init__(self, h, channels, kernel_size=3, dilation=(1, 3)):
|
62 |
+
super(ResBlock2, self).__init__()
|
63 |
+
self.h = h
|
64 |
+
self.convs = nn.ModuleList([
|
65 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
|
66 |
+
padding=get_padding(kernel_size, dilation[0]))),
|
67 |
+
weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
|
68 |
+
padding=get_padding(kernel_size, dilation[1])))
|
69 |
+
])
|
70 |
+
self.convs.apply(init_weights)
|
71 |
+
|
72 |
+
def forward(self, x):
|
73 |
+
for c in self.convs:
|
74 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
75 |
+
xt = c(xt)
|
76 |
+
x = xt + x
|
77 |
+
return x
|
78 |
+
|
79 |
+
def remove_weight_norm(self):
|
80 |
+
for l in self.convs:
|
81 |
+
remove_weight_norm(l)
|
82 |
+
|
83 |
+
|
84 |
+
class Generator(torch.nn.Module):
|
85 |
+
def __init__(self, h):
|
86 |
+
super(Generator, self).__init__()
|
87 |
+
self.h = h
|
88 |
+
self.num_kernels = len(h.resblock_kernel_sizes)
|
89 |
+
self.num_upsamples = len(h.upsample_rates)
|
90 |
+
self.conv_pre = weight_norm(Conv1d(80, h.upsample_initial_channel, 7, 1, padding=3))
|
91 |
+
resblock = ResBlock1 if h.resblock == '1' else ResBlock2
|
92 |
+
|
93 |
+
self.ups = nn.ModuleList()
|
94 |
+
for i, (u, k) in enumerate(zip(h.upsample_rates, h.upsample_kernel_sizes)):
|
95 |
+
self.ups.append(weight_norm(
|
96 |
+
ConvTranspose1d(h.upsample_initial_channel//(2**i), h.upsample_initial_channel//(2**(i+1)),
|
97 |
+
k, u, padding=(k-u)//2)))
|
98 |
+
|
99 |
+
self.resblocks = nn.ModuleList()
|
100 |
+
for i in range(len(self.ups)):
|
101 |
+
ch = h.upsample_initial_channel//(2**(i+1))
|
102 |
+
for j, (k, d) in enumerate(zip(h.resblock_kernel_sizes, h.resblock_dilation_sizes)):
|
103 |
+
self.resblocks.append(resblock(h, ch, k, d))
|
104 |
+
|
105 |
+
self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
|
106 |
+
self.ups.apply(init_weights)
|
107 |
+
self.conv_post.apply(init_weights)
|
108 |
+
|
109 |
+
def forward(self, x):
|
110 |
+
x = self.conv_pre(x)
|
111 |
+
for i in range(self.num_upsamples):
|
112 |
+
x = F.leaky_relu(x, LRELU_SLOPE)
|
113 |
+
x = self.ups[i](x)
|
114 |
+
xs = None
|
115 |
+
for j in range(self.num_kernels):
|
116 |
+
if xs is None:
|
117 |
+
xs = self.resblocks[i*self.num_kernels+j](x)
|
118 |
+
else:
|
119 |
+
xs += self.resblocks[i*self.num_kernels+j](x)
|
120 |
+
x = xs / self.num_kernels
|
121 |
+
x = F.leaky_relu(x)
|
122 |
+
x = self.conv_post(x)
|
123 |
+
x = torch.tanh(x)
|
124 |
+
|
125 |
+
return x
|
126 |
+
|
127 |
+
def remove_weight_norm(self):
|
128 |
+
print('Removing weight norm...')
|
129 |
+
for l in self.ups:
|
130 |
+
remove_weight_norm(l)
|
131 |
+
for l in self.resblocks:
|
132 |
+
l.remove_weight_norm()
|
133 |
+
remove_weight_norm(self.conv_pre)
|
134 |
+
remove_weight_norm(self.conv_post)
|
135 |
+
|
136 |
+
|
137 |
+
class DiscriminatorP(torch.nn.Module):
|
138 |
+
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
139 |
+
super(DiscriminatorP, self).__init__()
|
140 |
+
self.period = period
|
141 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
142 |
+
self.convs = nn.ModuleList([
|
143 |
+
norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
|
144 |
+
norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
|
145 |
+
norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
|
146 |
+
norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
|
147 |
+
norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(2, 0))),
|
148 |
+
])
|
149 |
+
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
150 |
+
|
151 |
+
def forward(self, x):
|
152 |
+
fmap = []
|
153 |
+
|
154 |
+
# 1d to 2d
|
155 |
+
b, c, t = x.shape
|
156 |
+
if t % self.period != 0: # pad first
|
157 |
+
n_pad = self.period - (t % self.period)
|
158 |
+
x = F.pad(x, (0, n_pad), "reflect")
|
159 |
+
t = t + n_pad
|
160 |
+
x = x.view(b, c, t // self.period, self.period)
|
161 |
+
|
162 |
+
for l in self.convs:
|
163 |
+
x = l(x)
|
164 |
+
x = F.leaky_relu(x, LRELU_SLOPE)
|
165 |
+
fmap.append(x)
|
166 |
+
x = self.conv_post(x)
|
167 |
+
fmap.append(x)
|
168 |
+
x = torch.flatten(x, 1, -1)
|
169 |
+
|
170 |
+
return x, fmap
|
171 |
+
|
172 |
+
|
173 |
+
class MultiPeriodDiscriminator(torch.nn.Module):
|
174 |
+
def __init__(self):
|
175 |
+
super(MultiPeriodDiscriminator, self).__init__()
|
176 |
+
self.discriminators = nn.ModuleList([
|
177 |
+
DiscriminatorP(2),
|
178 |
+
DiscriminatorP(3),
|
179 |
+
DiscriminatorP(5),
|
180 |
+
DiscriminatorP(7),
|
181 |
+
DiscriminatorP(11),
|
182 |
+
])
|
183 |
+
|
184 |
+
def forward(self, y, y_hat):
|
185 |
+
y_d_rs = []
|
186 |
+
y_d_gs = []
|
187 |
+
fmap_rs = []
|
188 |
+
fmap_gs = []
|
189 |
+
for i, d in enumerate(self.discriminators):
|
190 |
+
y_d_r, fmap_r = d(y)
|
191 |
+
y_d_g, fmap_g = d(y_hat)
|
192 |
+
y_d_rs.append(y_d_r)
|
193 |
+
fmap_rs.append(fmap_r)
|
194 |
+
y_d_gs.append(y_d_g)
|
195 |
+
fmap_gs.append(fmap_g)
|
196 |
+
|
197 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
198 |
+
|
199 |
+
|
200 |
+
class DiscriminatorS(torch.nn.Module):
|
201 |
+
def __init__(self, use_spectral_norm=False):
|
202 |
+
super(DiscriminatorS, self).__init__()
|
203 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
204 |
+
self.convs = nn.ModuleList([
|
205 |
+
norm_f(Conv1d(1, 128, 15, 1, padding=7)),
|
206 |
+
norm_f(Conv1d(128, 128, 41, 2, groups=4, padding=20)),
|
207 |
+
norm_f(Conv1d(128, 256, 41, 2, groups=16, padding=20)),
|
208 |
+
norm_f(Conv1d(256, 512, 41, 4, groups=16, padding=20)),
|
209 |
+
norm_f(Conv1d(512, 1024, 41, 4, groups=16, padding=20)),
|
210 |
+
norm_f(Conv1d(1024, 1024, 41, 1, groups=16, padding=20)),
|
211 |
+
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
212 |
+
])
|
213 |
+
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
214 |
+
|
215 |
+
def forward(self, x):
|
216 |
+
fmap = []
|
217 |
+
for l in self.convs:
|
218 |
+
x = l(x)
|
219 |
+
x = F.leaky_relu(x, LRELU_SLOPE)
|
220 |
+
fmap.append(x)
|
221 |
+
x = self.conv_post(x)
|
222 |
+
fmap.append(x)
|
223 |
+
x = torch.flatten(x, 1, -1)
|
224 |
+
|
225 |
+
return x, fmap
|
226 |
+
|
227 |
+
|
228 |
+
class MultiScaleDiscriminator(torch.nn.Module):
|
229 |
+
def __init__(self):
|
230 |
+
super(MultiScaleDiscriminator, self).__init__()
|
231 |
+
self.discriminators = nn.ModuleList([
|
232 |
+
DiscriminatorS(use_spectral_norm=True),
|
233 |
+
DiscriminatorS(),
|
234 |
+
DiscriminatorS(),
|
235 |
+
])
|
236 |
+
self.meanpools = nn.ModuleList([
|
237 |
+
AvgPool1d(4, 2, padding=2),
|
238 |
+
AvgPool1d(4, 2, padding=2)
|
239 |
+
])
|
240 |
+
|
241 |
+
def forward(self, y, y_hat):
|
242 |
+
y_d_rs = []
|
243 |
+
y_d_gs = []
|
244 |
+
fmap_rs = []
|
245 |
+
fmap_gs = []
|
246 |
+
for i, d in enumerate(self.discriminators):
|
247 |
+
if i != 0:
|
248 |
+
y = self.meanpools[i-1](y)
|
249 |
+
y_hat = self.meanpools[i-1](y_hat)
|
250 |
+
y_d_r, fmap_r = d(y)
|
251 |
+
y_d_g, fmap_g = d(y_hat)
|
252 |
+
y_d_rs.append(y_d_r)
|
253 |
+
fmap_rs.append(fmap_r)
|
254 |
+
y_d_gs.append(y_d_g)
|
255 |
+
fmap_gs.append(fmap_g)
|
256 |
+
|
257 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
258 |
+
|
259 |
+
|
260 |
+
def feature_loss(fmap_r, fmap_g):
|
261 |
+
loss = 0
|
262 |
+
for dr, dg in zip(fmap_r, fmap_g):
|
263 |
+
for rl, gl in zip(dr, dg):
|
264 |
+
loss += torch.mean(torch.abs(rl - gl))
|
265 |
+
|
266 |
+
return loss*2
|
267 |
+
|
268 |
+
|
269 |
+
def discriminator_loss(disc_real_outputs, disc_generated_outputs):
|
270 |
+
loss = 0
|
271 |
+
r_losses = []
|
272 |
+
g_losses = []
|
273 |
+
for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
|
274 |
+
r_loss = torch.mean((1-dr)**2)
|
275 |
+
g_loss = torch.mean(dg**2)
|
276 |
+
loss += (r_loss + g_loss)
|
277 |
+
r_losses.append(r_loss.item())
|
278 |
+
g_losses.append(g_loss.item())
|
279 |
+
|
280 |
+
return loss, r_losses, g_losses
|
281 |
+
|
282 |
+
|
283 |
+
def generator_loss(disc_outputs):
|
284 |
+
loss = 0
|
285 |
+
gen_losses = []
|
286 |
+
for dg in disc_outputs:
|
287 |
+
l = torch.mean((1-dg)**2)
|
288 |
+
gen_losses.append(l)
|
289 |
+
loss += l
|
290 |
+
|
291 |
+
return loss, gen_losses
|
292 |
+
|
293 |
+
|
294 |
+
|
295 |
+
|
296 |
+
# from python.hifigan.hifi_gan import Generator
|
297 |
+
|
298 |
+
class AttrDict(dict):
|
299 |
+
def __init__(self, *args, **kwargs):
|
300 |
+
super(AttrDict, self).__init__(*args, **kwargs)
|
301 |
+
self.__dict__ = self
|
302 |
+
|
303 |
+
class HiFi_GAN(object):
|
304 |
+
def __init__(self, logger, PROD, device, models_manager):
|
305 |
+
super(HiFi_GAN, self).__init__()
|
306 |
+
|
307 |
+
self.logger = logger
|
308 |
+
self.PROD = PROD
|
309 |
+
self.models_manager = models_manager
|
310 |
+
self.device = device
|
311 |
+
self.ckpt_path = None
|
312 |
+
|
313 |
+
config_file = os.path.join(f'{"./resources/app" if self.PROD else "."}/python/hifigan/config.json')
|
314 |
+
with open(config_file) as f:
|
315 |
+
data = f.read()
|
316 |
+
json_config = json.loads(data)
|
317 |
+
h = AttrDict(json_config)
|
318 |
+
|
319 |
+
self.model = Generator(h).to(self.device)
|
320 |
+
self.isReady = True
|
321 |
+
|
322 |
+
|
323 |
+
def load_state_dict (self, ckpt_path, sd):
|
324 |
+
self.ckpt_path = ckpt_path
|
325 |
+
self.model.load_state_dict(sd["generator"])
|
326 |
+
|
327 |
+
def set_device (self, device):
|
328 |
+
self.device = device
|
329 |
+
self.model = self.model.to(device)
|
330 |
+
self.model.device = device
|