f0_predictor.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. from torch.nn.utils import weight_norm
  17. class ConvRNNF0Predictor(nn.Module):
  18. def __init__(self,
  19. num_class: int = 1,
  20. in_channels: int = 80,
  21. cond_channels: int = 512
  22. ):
  23. super().__init__()
  24. self.num_class = num_class
  25. self.condnet = nn.Sequential(
  26. weight_norm(
  27. nn.Conv1d(in_channels, cond_channels, kernel_size=3, padding=1)
  28. ),
  29. nn.ELU(),
  30. weight_norm(
  31. nn.Conv1d(cond_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. )
  47. self.classifier = nn.Linear(in_features=cond_channels, out_features=self.num_class)
  48. def forward(self, x: torch.Tensor) -> torch.Tensor:
  49. x = self.condnet(x)
  50. x = x.transpose(1, 2)
  51. return torch.abs(self.classifier(x).squeeze(-1))