Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

discriminators.py 6.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  1. import torch
  2. import torch.nn.functional as F
  3. import torch.nn as nn
  4. from torch.nn import Conv1d, AvgPool1d, Conv2d
  5. from torch.nn.utils import weight_norm, spectral_norm
  6. from .utils import get_padding
  7. LRELU_SLOPE = 0.1
  8. def stft(x, fft_size, hop_size, win_length, window):
  9. """Perform STFT and convert to magnitude spectrogram.
  10. Args:
  11. x (Tensor): Input signal tensor (B, T).
  12. fft_size (int): FFT size.
  13. hop_size (int): Hop size.
  14. win_length (int): Window length.
  15. window (str): Window function type.
  16. Returns:
  17. Tensor: Magnitude spectrogram (B, #frames, fft_size // 2 + 1).
  18. """
  19. x_stft = torch.stft(x, fft_size, hop_size, win_length, window,
  20. return_complex=True)
  21. real = x_stft[..., 0]
  22. imag = x_stft[..., 1]
  23. return torch.abs(x_stft).transpose(2, 1)
  24. class SpecDiscriminator(nn.Module):
  25. """docstring for Discriminator."""
  26. def __init__(self, fft_size=1024, shift_size=120, win_length=600, window="hann_window", use_spectral_norm=False):
  27. super(SpecDiscriminator, self).__init__()
  28. norm_f = weight_norm if use_spectral_norm == False else spectral_norm
  29. self.fft_size = fft_size
  30. self.shift_size = shift_size
  31. self.win_length = win_length
  32. self.window = getattr(torch, window)(win_length)
  33. self.discriminators = nn.ModuleList([
  34. norm_f(nn.Conv2d(1, 32, kernel_size=(3, 9), padding=(1, 4))),
  35. norm_f(nn.Conv2d(32, 32, kernel_size=(3, 9), stride=(1,2), padding=(1, 4))),
  36. norm_f(nn.Conv2d(32, 32, kernel_size=(3, 9), stride=(1,2), padding=(1, 4))),
  37. norm_f(nn.Conv2d(32, 32, kernel_size=(3, 9), stride=(1,2), padding=(1, 4))),
  38. norm_f(nn.Conv2d(32, 32, kernel_size=(3, 3), stride=(1,1), padding=(1, 1))),
  39. ])
  40. self.out = norm_f(nn.Conv2d(32, 1, 3, 1, 1))
  41. def forward(self, y):
  42. fmap = []
  43. y = y.squeeze(1)
  44. y = stft(y, self.fft_size, self.shift_size, self.win_length, self.window.to(y.get_device()))
  45. y = y.unsqueeze(1)
  46. for i, d in enumerate(self.discriminators):
  47. y = d(y)
  48. y = F.leaky_relu(y, LRELU_SLOPE)
  49. fmap.append(y)
  50. y = self.out(y)
  51. fmap.append(y)
  52. return torch.flatten(y, 1, -1), fmap
  53. class MultiResSpecDiscriminator(torch.nn.Module):
  54. def __init__(self,
  55. fft_sizes=[1024, 2048, 512],
  56. hop_sizes=[120, 240, 50],
  57. win_lengths=[600, 1200, 240],
  58. window="hann_window"):
  59. super(MultiResSpecDiscriminator, self).__init__()
  60. self.discriminators = nn.ModuleList([
  61. SpecDiscriminator(fft_sizes[0], hop_sizes[0], win_lengths[0], window),
  62. SpecDiscriminator(fft_sizes[1], hop_sizes[1], win_lengths[1], window),
  63. SpecDiscriminator(fft_sizes[2], hop_sizes[2], win_lengths[2], window)
  64. ])
  65. def forward(self, y, y_hat):
  66. y_d_rs = []
  67. y_d_gs = []
  68. fmap_rs = []
  69. fmap_gs = []
  70. for i, d in enumerate(self.discriminators):
  71. y_d_r, fmap_r = d(y)
  72. y_d_g, fmap_g = d(y_hat)
  73. y_d_rs.append(y_d_r)
  74. fmap_rs.append(fmap_r)
  75. y_d_gs.append(y_d_g)
  76. fmap_gs.append(fmap_g)
  77. return y_d_rs, y_d_gs, fmap_rs, fmap_gs
  78. class DiscriminatorP(torch.nn.Module):
  79. def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
  80. super(DiscriminatorP, self).__init__()
  81. self.period = period
  82. norm_f = weight_norm if use_spectral_norm == False else spectral_norm
  83. self.convs = nn.ModuleList([
  84. norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
  85. norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
  86. norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
  87. norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
  88. norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(2, 0))),
  89. ])
  90. self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
  91. def forward(self, x):
  92. fmap = []
  93. # 1d to 2d
  94. b, c, t = x.shape
  95. if t % self.period != 0: # pad first
  96. n_pad = self.period - (t % self.period)
  97. x = F.pad(x, (0, n_pad), "reflect")
  98. t = t + n_pad
  99. x = x.view(b, c, t // self.period, self.period)
  100. for l in self.convs:
  101. x = l(x)
  102. x = F.leaky_relu(x, LRELU_SLOPE)
  103. fmap.append(x)
  104. x = self.conv_post(x)
  105. fmap.append(x)
  106. x = torch.flatten(x, 1, -1)
  107. return x, fmap
  108. class MultiPeriodDiscriminator(torch.nn.Module):
  109. def __init__(self):
  110. super(MultiPeriodDiscriminator, self).__init__()
  111. self.discriminators = nn.ModuleList([
  112. DiscriminatorP(2),
  113. DiscriminatorP(3),
  114. DiscriminatorP(5),
  115. DiscriminatorP(7),
  116. DiscriminatorP(11),
  117. ])
  118. def forward(self, y, y_hat):
  119. y_d_rs = []
  120. y_d_gs = []
  121. fmap_rs = []
  122. fmap_gs = []
  123. for i, d in enumerate(self.discriminators):
  124. y_d_r, fmap_r = d(y)
  125. y_d_g, fmap_g = d(y_hat)
  126. y_d_rs.append(y_d_r)
  127. fmap_rs.append(fmap_r)
  128. y_d_gs.append(y_d_g)
  129. fmap_gs.append(fmap_g)
  130. return y_d_rs, y_d_gs, fmap_rs, fmap_gs
  131. class WavLMDiscriminator(nn.Module):
  132. """docstring for Discriminator."""
  133. def __init__(self, slm_hidden=768,
  134. slm_layers=13,
  135. initial_channel=64,
  136. use_spectral_norm=False):
  137. super(WavLMDiscriminator, self).__init__()
  138. norm_f = weight_norm if use_spectral_norm == False else spectral_norm
  139. self.pre = norm_f(Conv1d(slm_hidden * slm_layers, initial_channel, 1, 1, padding=0))
  140. self.convs = nn.ModuleList([
  141. norm_f(nn.Conv1d(initial_channel, initial_channel * 2, kernel_size=5, padding=2)),
  142. norm_f(nn.Conv1d(initial_channel * 2, initial_channel * 4, kernel_size=5, padding=2)),
  143. norm_f(nn.Conv1d(initial_channel * 4, initial_channel * 4, 5, 1, padding=2)),
  144. ])
  145. self.conv_post = norm_f(Conv1d(initial_channel * 4, 1, 3, 1, padding=1))
  146. def forward(self, x):
  147. x = self.pre(x)
  148. fmap = []
  149. for l in self.convs:
  150. x = l(x)
  151. x = F.leaky_relu(x, LRELU_SLOPE)
  152. fmap.append(x)
  153. x = self.conv_post(x)
  154. x = torch.flatten(x, 1, -1)
  155. return x
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...