Spaces:
Build error
Build error
Commit
·
83cd2da
1
Parent(s):
9aefa26
Upload InferenceHiFiGAN.py
Browse files- InferenceHiFiGAN.py +91 -0
InferenceHiFiGAN.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
from Layers.ResidualBlock import HiFiGANResidualBlock as ResidualBlock
|
4 |
+
|
5 |
+
|
6 |
+
class HiFiGANGenerator(torch.nn.Module):
|
7 |
+
|
8 |
+
def __init__(self,
|
9 |
+
path_to_weights,
|
10 |
+
in_channels=80,
|
11 |
+
out_channels=1,
|
12 |
+
channels=512,
|
13 |
+
kernel_size=7,
|
14 |
+
upsample_scales=(8, 6, 4, 4),
|
15 |
+
upsample_kernel_sizes=(16, 12, 8, 8),
|
16 |
+
resblock_kernel_sizes=(3, 7, 11),
|
17 |
+
resblock_dilations=[(1, 3, 5), (1, 3, 5), (1, 3, 5)],
|
18 |
+
use_additional_convs=True,
|
19 |
+
bias=True,
|
20 |
+
nonlinear_activation="LeakyReLU",
|
21 |
+
nonlinear_activation_params={"negative_slope": 0.1},
|
22 |
+
use_weight_norm=True, ):
|
23 |
+
super().__init__()
|
24 |
+
assert kernel_size % 2 == 1, "Kernal size must be odd number."
|
25 |
+
assert len(upsample_scales) == len(upsample_kernel_sizes)
|
26 |
+
assert len(resblock_dilations) == len(resblock_kernel_sizes)
|
27 |
+
self.num_upsamples = len(upsample_kernel_sizes)
|
28 |
+
self.num_blocks = len(resblock_kernel_sizes)
|
29 |
+
self.input_conv = torch.nn.Conv1d(in_channels,
|
30 |
+
channels,
|
31 |
+
kernel_size,
|
32 |
+
1,
|
33 |
+
padding=(kernel_size - 1) // 2, )
|
34 |
+
self.upsamples = torch.nn.ModuleList()
|
35 |
+
self.blocks = torch.nn.ModuleList()
|
36 |
+
for i in range(len(upsample_kernel_sizes)):
|
37 |
+
self.upsamples += [
|
38 |
+
torch.nn.Sequential(getattr(torch.nn, nonlinear_activation)(**nonlinear_activation_params),
|
39 |
+
torch.nn.ConvTranspose1d(channels // (2 ** i),
|
40 |
+
channels // (2 ** (i + 1)),
|
41 |
+
upsample_kernel_sizes[i],
|
42 |
+
upsample_scales[i],
|
43 |
+
padding=(upsample_kernel_sizes[i] - upsample_scales[i]) // 2, ), )]
|
44 |
+
for j in range(len(resblock_kernel_sizes)):
|
45 |
+
self.blocks += [ResidualBlock(kernel_size=resblock_kernel_sizes[j],
|
46 |
+
channels=channels // (2 ** (i + 1)),
|
47 |
+
dilations=resblock_dilations[j],
|
48 |
+
bias=bias,
|
49 |
+
use_additional_convs=use_additional_convs,
|
50 |
+
nonlinear_activation=nonlinear_activation,
|
51 |
+
nonlinear_activation_params=nonlinear_activation_params, )]
|
52 |
+
self.output_conv = torch.nn.Sequential(
|
53 |
+
torch.nn.LeakyReLU(),
|
54 |
+
torch.nn.Conv1d(channels // (2 ** (i + 1)),
|
55 |
+
out_channels,
|
56 |
+
kernel_size,
|
57 |
+
1,
|
58 |
+
padding=(kernel_size - 1) // 2, ),
|
59 |
+
torch.nn.Tanh(), )
|
60 |
+
if use_weight_norm:
|
61 |
+
self.apply_weight_norm()
|
62 |
+
self.load_state_dict(torch.load(path_to_weights, map_location='cpu')["generator"])
|
63 |
+
|
64 |
+
def forward(self, c, normalize_before=False):
|
65 |
+
if normalize_before:
|
66 |
+
c = (c - self.mean) / self.scale
|
67 |
+
c = self.input_conv(c.unsqueeze(0))
|
68 |
+
for i in range(self.num_upsamples):
|
69 |
+
c = self.upsamples[i](c)
|
70 |
+
cs = 0.0 # initialize
|
71 |
+
for j in range(self.num_blocks):
|
72 |
+
cs = cs + self.blocks[i * self.num_blocks + j](c)
|
73 |
+
c = cs / self.num_blocks
|
74 |
+
c = self.output_conv(c)
|
75 |
+
return c.squeeze(0).squeeze(0)
|
76 |
+
|
77 |
+
def remove_weight_norm(self):
|
78 |
+
def _remove_weight_norm(m):
|
79 |
+
try:
|
80 |
+
torch.nn.utils.remove_weight_norm(m)
|
81 |
+
except ValueError:
|
82 |
+
return
|
83 |
+
|
84 |
+
self.apply(_remove_weight_norm)
|
85 |
+
|
86 |
+
def apply_weight_norm(self):
|
87 |
+
def _apply_weight_norm(m):
|
88 |
+
if isinstance(m, torch.nn.Conv1d) or isinstance(m, torch.nn.ConvTranspose1d):
|
89 |
+
torch.nn.utils.weight_norm(m)
|
90 |
+
|
91 |
+
self.apply(_apply_weight_norm)
|