|
| 1 | +# Copyright (c) 2022 NVIDIA CORPORATION. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +# Adapted from https://github.com/jik876/hifi-gan under the MIT license. |
| 5 | +# LICENSE is in incl_licenses directory. |
| 6 | + |
| 7 | +import torch |
| 8 | +from alias_free_torch import Activation1d |
| 9 | +from torch.nn import Conv1d |
| 10 | +from torch.nn import ConvTranspose1d |
| 11 | +from torch.nn import ModuleList |
| 12 | +from torch.nn.utils import remove_weight_norm |
| 13 | +from torch.nn.utils import weight_norm |
| 14 | + |
| 15 | +from Architectures.Vocoder.AMP import AMPBlock1 |
| 16 | +from Architectures.Vocoder.Snake import SnakeBeta |
| 17 | + |
| 18 | + |
| 19 | +class BigVGAN(torch.nn.Module): |
| 20 | + # this is the main BigVGAN model. Applies anti-aliased periodic activation for resblocks. |
| 21 | + |
| 22 | + def __init__(self, |
| 23 | + num_mels=128, |
| 24 | + upsample_initial_channel=512, |
| 25 | + upsample_rates=(8, 6, 4, 2), # CAREFUL: Avocodo discriminator assumes that there are always 4 upsample scales, because it takes intermediate results. |
| 26 | + upsample_kernel_sizes=(16, 12, 8, 4), |
| 27 | + resblock_kernel_sizes=(3, 7, 11), |
| 28 | + resblock_dilation_sizes=((1, 3, 5), (1, 3, 5), (1, 3, 5)), |
| 29 | + weights=None |
| 30 | + ): |
| 31 | + super(BigVGAN, self).__init__() |
| 32 | + |
| 33 | + self.num_kernels = len(resblock_kernel_sizes) |
| 34 | + self.num_upsamples = len(upsample_rates) |
| 35 | + |
| 36 | + # pre conv |
| 37 | + self.conv_pre = weight_norm(Conv1d(num_mels, upsample_initial_channel, 7, 1, padding=3)) |
| 38 | + |
| 39 | + # transposed conv-based upsamplers. does not apply anti-aliasing |
| 40 | + self.ups = ModuleList() |
| 41 | + for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)): |
| 42 | + self.ups.append(ModuleList([ |
| 43 | + weight_norm(ConvTranspose1d(upsample_initial_channel // (2 ** i), |
| 44 | + upsample_initial_channel // (2 ** (i + 1)), |
| 45 | + k, u, padding=(k - u) // 2)) |
| 46 | + ])) |
| 47 | + |
| 48 | + # residual blocks using anti-aliased multi-periodicity composition modules (AMP) |
| 49 | + self.resblocks = ModuleList() |
| 50 | + for i in range(len(self.ups)): |
| 51 | + ch = upsample_initial_channel // (2 ** (i + 1)) |
| 52 | + for j, (k, d) in enumerate(zip(resblock_kernel_sizes, resblock_dilation_sizes)): |
| 53 | + self.resblocks.append(AMPBlock1(ch, k, d)) |
| 54 | + |
| 55 | + # post conv |
| 56 | + activation_post = SnakeBeta(ch, alpha_logscale=True) |
| 57 | + self.activation_post = Activation1d(activation=activation_post) |
| 58 | + |
| 59 | + self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3)) |
| 60 | + |
| 61 | + # weight initialization |
| 62 | + for i in range(len(self.ups)): |
| 63 | + self.ups[i].apply(init_weights) |
| 64 | + self.conv_post.apply(init_weights) |
| 65 | + |
| 66 | + # for Avocodo discriminator |
| 67 | + self.out_proj_x1 = torch.nn.Conv1d(upsample_initial_channel // 4, 1, 7, 1, padding=3) |
| 68 | + self.out_proj_x2 = torch.nn.Conv1d(upsample_initial_channel // 8, 1, 7, 1, padding=3) |
| 69 | + |
| 70 | + if weights is not None: |
| 71 | + self.load_state_dict(weights) |
| 72 | + |
| 73 | + def forward(self, x): |
| 74 | + # pre conv |
| 75 | + x = self.conv_pre(x) |
| 76 | + |
| 77 | + for i in range(self.num_upsamples): |
| 78 | + # upsampling |
| 79 | + for i_up in range(len(self.ups[i])): |
| 80 | + x = self.ups[i][i_up](x) |
| 81 | + # AMP blocks |
| 82 | + xs = None |
| 83 | + for j in range(self.num_kernels): |
| 84 | + if xs is None: |
| 85 | + xs = self.resblocks[i * self.num_kernels + j](x) |
| 86 | + else: |
| 87 | + xs += self.resblocks[i * self.num_kernels + j](x) |
| 88 | + x = xs / self.num_kernels |
| 89 | + if i == 1: |
| 90 | + x1 = self.out_proj_x1(x) |
| 91 | + elif i == 2: |
| 92 | + x2 = self.out_proj_x2(x) |
| 93 | + |
| 94 | + # post conv |
| 95 | + x = self.activation_post(x) |
| 96 | + x = self.conv_post(x) |
| 97 | + x = torch.tanh(x) |
| 98 | + |
| 99 | + return x, x2, x1 |
| 100 | + |
| 101 | + def remove_weight_norm(self): |
| 102 | + print('Removing weight norm...') |
| 103 | + for l in self.ups: |
| 104 | + for l_i in l: |
| 105 | + remove_weight_norm(l_i) |
| 106 | + for l in self.resblocks: |
| 107 | + l.remove_weight_norm() |
| 108 | + remove_weight_norm(self.conv_pre) |
| 109 | + remove_weight_norm(self.conv_post) |
| 110 | + |
| 111 | + |
| 112 | +def init_weights(m, mean=0.0, std=0.01): |
| 113 | + classname = m.__class__.__name__ |
| 114 | + if classname.find("Conv") != -1: |
| 115 | + m.weight.data.normal_(mean, std) |
| 116 | + |
| 117 | + |
| 118 | +def apply_weight_norm(m): |
| 119 | + classname = m.__class__.__name__ |
| 120 | + if classname.find("Conv") != -1: |
| 121 | + weight_norm(m) |
| 122 | + |
| 123 | + |
| 124 | +def get_padding(kernel_size, dilation=1): |
| 125 | + return int((kernel_size * dilation - dilation) / 2) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == '__main__': |
| 129 | + print(BigVGAN()(torch.randn([1, 128, 100]))[0].shape) |
0 commit comments