f0_predictor.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. class ConvRNNF0Predictor(nn.Module):
  21. def __init__(self,
  22. num_class: int = 1,
  23. in_channels: int = 80,
  24. cond_channels: int = 512
  25. ):
  26. super().__init__()
  27. self.num_class = num_class
  28. self.condnet = nn.Sequential(
  29. weight_norm(
  30. nn.Conv1d(in_channels, cond_channels, kernel_size=3, padding=1)
  31. ),
  32. nn.ELU(),
  33. weight_norm(
  34. nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
  35. ),
  36. nn.ELU(),
  37. weight_norm(
  38. nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
  39. ),
  40. nn.ELU(),
  41. weight_norm(
  42. nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
  43. ),
  44. nn.ELU(),
  45. weight_norm(
  46. nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
  47. ),
  48. nn.ELU(),
  49. )
  50. self.classifier = nn.Linear(in_features=cond_channels, out_features=self.num_class)
  51. def forward(self, x: torch.Tensor) -> torch.Tensor:
  52. x = self.condnet(x)
  53. x = x.transpose(1, 2)
  54. return torch.abs(self.classifier(x).squeeze(-1))