f0_predictor.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (c) 2024 Alibaba Inc (authors: Xiang Lyu, Kai Hu)
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import torch
  15. import torch.nn as nn
  16. try:
  17. from torch.nn.utils.parametrizations import weight_norm
  18. except ImportError:
  19. from torch.nn.utils import weight_norm
  20. from cosyvoice.transformer.convolution import CausalConv1d
  21. class ConvRNNF0Predictor(nn.Module):
  22. def __init__(self,
  23. num_class: int = 1,
  24. in_channels: int = 80,
  25. cond_channels: int = 512
  26. ):
  27. super().__init__()
  28. self.num_class = num_class
  29. self.condnet = nn.Sequential(
  30. weight_norm(
  31. nn.Conv1d(in_channels, cond_channels, kernel_size=3, padding=1)
  32. ),
  33. nn.ELU(),
  34. weight_norm(
  35. nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
  36. ),
  37. nn.ELU(),
  38. weight_norm(
  39. nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
  40. ),
  41. nn.ELU(),
  42. weight_norm(
  43. nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
  44. ),
  45. nn.ELU(),
  46. weight_norm(
  47. nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
  48. ),
  49. nn.ELU(),
  50. )
  51. self.classifier = nn.Linear(in_features=cond_channels, out_features=self.num_class)
  52. def forward(self, x: torch.Tensor) -> torch.Tensor:
  53. x = self.condnet(x)
  54. x = x.transpose(1, 2)
  55. return torch.abs(self.classifier(x).squeeze(-1))
  56. class CausalConvRNNF0Predictor(nn.Module):
  57. def __init__(self,
  58. num_class: int = 1,
  59. in_channels: int = 80,
  60. cond_channels: int = 512
  61. ):
  62. super().__init__()
  63. self.num_class = num_class
  64. self.condnet = nn.Sequential(
  65. weight_norm(
  66. CausalConv1d(in_channels, cond_channels, kernel_size=4, causal_type='right')
  67. ),
  68. nn.ELU(),
  69. weight_norm(
  70. CausalConv1d(cond_channels, cond_channels, kernel_size=3, causal_type='left')
  71. ),
  72. nn.ELU(),
  73. weight_norm(
  74. CausalConv1d(cond_channels, cond_channels, kernel_size=3, causal_type='left')
  75. ),
  76. nn.ELU(),
  77. weight_norm(
  78. CausalConv1d(cond_channels, cond_channels, kernel_size=3, causal_type='left')
  79. ),
  80. nn.ELU(),
  81. weight_norm(
  82. CausalConv1d(cond_channels, cond_channels, kernel_size=3, causal_type='left')
  83. ),
  84. nn.ELU(),
  85. )
  86. self.classifier = nn.Linear(in_features=cond_channels, out_features=self.num_class)
  87. def forward(self, x: torch.Tensor, finalize: bool = True) -> torch.Tensor:
  88. if finalize is True:
  89. x = self.condnet[0](x)
  90. else:
  91. x = self.condnet[0](x[:, :, :-self.condnet[0].causal_padding], x[:, :, -self.condnet[0].causal_padding:])
  92. for i in range(1, len(self.condnet)):
  93. x = self.condnet[i](x)
  94. x = x.transpose(1, 2)
  95. return torch.abs(self.classifier(x).squeeze(-1))