Spaces:
Running
Running
JaesungHuh
commited on
Commit
•
8324298
1
Parent(s):
703bed7
test
Browse files- app.py +4 -3
- model.py +174 -0
- requirements.txt +2 -0
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
def greet(
|
4 |
-
|
5 |
|
6 |
-
demo = gr.Interface(fn=greet, inputs="
|
7 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from model import ECAPA_gender
|
3 |
|
4 |
+
def greet(audio):
|
5 |
+
print(audio)
|
6 |
|
7 |
+
demo = gr.Interface(fn=greet, inputs="audio", outputs="text")
|
8 |
demo.launch()
|
model.py
CHANGED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
import torchaudio
|
6 |
+
from torchaudio.functional import resample
|
7 |
+
import math
|
8 |
+
|
9 |
+
|
10 |
+
class SEModule(nn.Module):
|
11 |
+
def __init__(self, channels, bottleneck=128):
|
12 |
+
super(SEModule, self).__init__()
|
13 |
+
self.se = nn.Sequential(
|
14 |
+
nn.AdaptiveAvgPool1d(1),
|
15 |
+
nn.Conv1d(channels, bottleneck, kernel_size=1, padding=0),
|
16 |
+
nn.ReLU(),
|
17 |
+
# nn.BatchNorm1d(bottleneck), # I remove this layer
|
18 |
+
nn.Conv1d(bottleneck, channels, kernel_size=1, padding=0),
|
19 |
+
nn.Sigmoid(),
|
20 |
+
)
|
21 |
+
|
22 |
+
def forward(self, input):
|
23 |
+
x = self.se(input)
|
24 |
+
return input * x
|
25 |
+
|
26 |
+
class Bottle2neck(nn.Module):
|
27 |
+
|
28 |
+
def __init__(self, inplanes, planes, kernel_size=None, dilation=None, scale = 8):
|
29 |
+
super(Bottle2neck, self).__init__()
|
30 |
+
width = int(math.floor(planes / scale))
|
31 |
+
self.conv1 = nn.Conv1d(inplanes, width*scale, kernel_size=1)
|
32 |
+
self.bn1 = nn.BatchNorm1d(width*scale)
|
33 |
+
self.nums = scale -1
|
34 |
+
convs = []
|
35 |
+
bns = []
|
36 |
+
num_pad = math.floor(kernel_size/2)*dilation
|
37 |
+
for i in range(self.nums):
|
38 |
+
convs.append(nn.Conv1d(width, width, kernel_size=kernel_size, dilation=dilation, padding=num_pad))
|
39 |
+
bns.append(nn.BatchNorm1d(width))
|
40 |
+
self.convs = nn.ModuleList(convs)
|
41 |
+
self.bns = nn.ModuleList(bns)
|
42 |
+
self.conv3 = nn.Conv1d(width*scale, planes, kernel_size=1)
|
43 |
+
self.bn3 = nn.BatchNorm1d(planes)
|
44 |
+
self.relu = nn.ReLU()
|
45 |
+
self.width = width
|
46 |
+
self.se = SEModule(planes)
|
47 |
+
|
48 |
+
def forward(self, x):
|
49 |
+
residual = x
|
50 |
+
out = self.conv1(x)
|
51 |
+
out = self.relu(out)
|
52 |
+
out = self.bn1(out)
|
53 |
+
|
54 |
+
spx = torch.split(out, self.width, 1)
|
55 |
+
for i in range(self.nums):
|
56 |
+
if i==0:
|
57 |
+
sp = spx[i]
|
58 |
+
else:
|
59 |
+
sp = sp + spx[i]
|
60 |
+
sp = self.convs[i](sp)
|
61 |
+
sp = self.relu(sp)
|
62 |
+
sp = self.bns[i](sp)
|
63 |
+
if i==0:
|
64 |
+
out = sp
|
65 |
+
else:
|
66 |
+
out = torch.cat((out, sp), 1)
|
67 |
+
out = torch.cat((out, spx[self.nums]),1)
|
68 |
+
|
69 |
+
out = self.conv3(out)
|
70 |
+
out = self.relu(out)
|
71 |
+
out = self.bn3(out)
|
72 |
+
|
73 |
+
out = self.se(out)
|
74 |
+
out += residual
|
75 |
+
return out
|
76 |
+
|
77 |
+
class PreEmphasis(torch.nn.Module):
|
78 |
+
|
79 |
+
def __init__(self, coef: float = 0.97):
|
80 |
+
super().__init__()
|
81 |
+
self.coef = coef
|
82 |
+
self.register_buffer(
|
83 |
+
'flipped_filter', torch.FloatTensor([-self.coef, 1.]).unsqueeze(0).unsqueeze(0)
|
84 |
+
)
|
85 |
+
|
86 |
+
def forward(self, input: torch.tensor) -> torch.tensor:
|
87 |
+
input = input.unsqueeze(1)
|
88 |
+
input = F.pad(input, (1, 0), 'reflect')
|
89 |
+
return F.conv1d(input, self.flipped_filter).squeeze(1)
|
90 |
+
|
91 |
+
|
92 |
+
class ECAPA_gender(nn.Module, PyTorchModelHubMixin):
|
93 |
+
def __init__(self, config):
|
94 |
+
super(ECAPA_gender, self).__init__()
|
95 |
+
self.config = config
|
96 |
+
C = config["C"]
|
97 |
+
|
98 |
+
self.torchfbank = torch.nn.Sequential(
|
99 |
+
PreEmphasis(),
|
100 |
+
torchaudio.transforms.MelSpectrogram(sample_rate=16000, n_fft=512, win_length=400, hop_length=160, \
|
101 |
+
f_min = 20, f_max = 7600, window_fn=torch.hamming_window, n_mels=80),
|
102 |
+
)
|
103 |
+
|
104 |
+
self.conv1 = nn.Conv1d(80, C, kernel_size=5, stride=1, padding=2)
|
105 |
+
self.relu = nn.ReLU()
|
106 |
+
self.bn1 = nn.BatchNorm1d(C)
|
107 |
+
self.layer1 = Bottle2neck(C, C, kernel_size=3, dilation=2, scale=8)
|
108 |
+
self.layer2 = Bottle2neck(C, C, kernel_size=3, dilation=3, scale=8)
|
109 |
+
self.layer3 = Bottle2neck(C, C, kernel_size=3, dilation=4, scale=8)
|
110 |
+
# I fixed the shape of the output from MFA layer, that is close to the setting from ECAPA paper.
|
111 |
+
self.layer4 = nn.Conv1d(3*C, 1536, kernel_size=1)
|
112 |
+
self.attention = nn.Sequential(
|
113 |
+
nn.Conv1d(4608, 256, kernel_size=1),
|
114 |
+
nn.ReLU(),
|
115 |
+
nn.BatchNorm1d(256),
|
116 |
+
nn.Tanh(), # I add this layer
|
117 |
+
nn.Conv1d(256, 1536, kernel_size=1),
|
118 |
+
nn.Softmax(dim=2),
|
119 |
+
)
|
120 |
+
self.bn5 = nn.BatchNorm1d(3072)
|
121 |
+
self.fc6 = nn.Linear(3072, 192)
|
122 |
+
self.bn6 = nn.BatchNorm1d(192)
|
123 |
+
self.fc7 = nn.Linear(192, 2)
|
124 |
+
self.pred2gender = {0 : 'male', 1 : 'female'}
|
125 |
+
|
126 |
+
def forward(self, x):
|
127 |
+
with torch.no_grad():
|
128 |
+
x = self.torchfbank(x)+1e-6
|
129 |
+
x = x.log()
|
130 |
+
x = x - torch.mean(x, dim=-1, keepdim=True)
|
131 |
+
|
132 |
+
x = self.conv1(x)
|
133 |
+
x = self.relu(x)
|
134 |
+
x = self.bn1(x)
|
135 |
+
|
136 |
+
x1 = self.layer1(x)
|
137 |
+
x2 = self.layer2(x+x1)
|
138 |
+
x3 = self.layer3(x+x1+x2)
|
139 |
+
|
140 |
+
x = self.layer4(torch.cat((x1,x2,x3),dim=1))
|
141 |
+
x = self.relu(x)
|
142 |
+
|
143 |
+
t = x.size()[-1]
|
144 |
+
|
145 |
+
global_x = torch.cat((x,torch.mean(x,dim=2,keepdim=True).repeat(1,1,t), torch.sqrt(torch.var(x,dim=2,keepdim=True).clamp(min=1e-4)).repeat(1,1,t)), dim=1)
|
146 |
+
|
147 |
+
w = self.attention(global_x)
|
148 |
+
|
149 |
+
mu = torch.sum(x * w, dim=2)
|
150 |
+
sg = torch.sqrt( ( torch.sum((x**2) * w, dim=2) - mu**2 ).clamp(min=1e-4) )
|
151 |
+
|
152 |
+
x = torch.cat((mu,sg),1)
|
153 |
+
x = self.bn5(x)
|
154 |
+
x = self.fc6(x)
|
155 |
+
x = self.bn6(x)
|
156 |
+
x = self.relu(x)
|
157 |
+
x = self.fc7(x)
|
158 |
+
|
159 |
+
return x
|
160 |
+
|
161 |
+
def load_audio(self, path):
|
162 |
+
audio, sr = torchaudio.load(path)
|
163 |
+
if sr != 16000:
|
164 |
+
audio = resample(audio, sr, 16000)
|
165 |
+
return audio
|
166 |
+
|
167 |
+
def predict(self, audio):
|
168 |
+
audio = self.load_audio(audio)
|
169 |
+
self.eval()
|
170 |
+
with torch.no_grad():
|
171 |
+
output = self.forward(audio)
|
172 |
+
_, pred = output.max(1)
|
173 |
+
return self.pred2gender[pred.item()]
|
174 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchaudio
|