asd
commited on
python + sample dataset
Browse files- .gitattributes +8 -0
- extract_weights_properly.py +251 -0
- fix_conv1d_classifier.py +194 -0
- main2.py +289 -0
- test/.DS_Store +0 -0
- test/ambient1.mp3 +3 -0
- test/ambient2.mp3 +3 -0
- test/ambient3.mp3 +3 -0
- test/ambient4.mp3 +3 -0
- test/ambient5.mp3 +3 -0
- test/human1.mp3 +3 -0
- test/human2.mp3 +0 -0
- test/human3.mp3 +3 -0
- test/human4.mp3 +0 -0
- test/human5.mp3 +3 -0
- vad_benchmark.py +641 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,11 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
test/ambient1.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
test/ambient2.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
test/ambient3.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
test/ambient4.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
test/ambient5.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
test/human1.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
test/human3.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
test/human5.mp3 filter=lfs diff=lfs merge=lfs -text
|
extract_weights_properly.py
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Extract Weights Properly from silero_vad.jit
|
| 4 |
+
|
| 5 |
+
This script extracts weights using state_dict approach instead of iterating over layers.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
import coremltools as ct
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
print("π Loading silero_vad.jit to extract original weights...")
|
| 14 |
+
model = torch.jit.load('silero_vad.jit')
|
| 15 |
+
model.eval()
|
| 16 |
+
|
| 17 |
+
print("β
Model loaded successfully!")
|
| 18 |
+
|
| 19 |
+
# Get all parameters from the model
|
| 20 |
+
print("\nπ Extracting all model parameters...")
|
| 21 |
+
all_params = {}
|
| 22 |
+
for name, param in model.named_parameters():
|
| 23 |
+
print(f"Parameter: {name}, shape: {param.shape}")
|
| 24 |
+
all_params[name] = param.detach().numpy()
|
| 25 |
+
|
| 26 |
+
# Save extracted weights
|
| 27 |
+
torch.save(all_params, 'extracted_silero_weights.pth')
|
| 28 |
+
print("β
Weights saved to extracted_silero_weights.pth")
|
| 29 |
+
|
| 30 |
+
print("\n" + "="*60)
|
| 31 |
+
print("ποΈ Creating Proper 128-Parameter RNN with Original Weights")
|
| 32 |
+
print("="*60)
|
| 33 |
+
|
| 34 |
+
class ProperRNN128(nn.Module):
|
| 35 |
+
"""128-parameter RNN using original silero weights"""
|
| 36 |
+
def __init__(self, all_params):
|
| 37 |
+
super().__init__()
|
| 38 |
+
self.hidden_size = 128
|
| 39 |
+
self.input_size = 64 # From encoder output
|
| 40 |
+
|
| 41 |
+
# Create LSTM cell
|
| 42 |
+
self.lstm_cell = nn.LSTMCell(self.input_size, self.hidden_size)
|
| 43 |
+
|
| 44 |
+
# Load original weights - find the RNN weights
|
| 45 |
+
rnn_weight_ih = None
|
| 46 |
+
rnn_weight_hh = None
|
| 47 |
+
rnn_bias_ih = None
|
| 48 |
+
rnn_bias_hh = None
|
| 49 |
+
|
| 50 |
+
for name, param in all_params.items():
|
| 51 |
+
if 'rnn' in name.lower() and 'weight_ih' in name:
|
| 52 |
+
rnn_weight_ih = param
|
| 53 |
+
print(f"Found RNN weight_ih: {name}, shape: {param.shape}")
|
| 54 |
+
elif 'rnn' in name.lower() and 'weight_hh' in name:
|
| 55 |
+
rnn_weight_hh = param
|
| 56 |
+
print(f"Found RNN weight_hh: {name}, shape: {param.shape}")
|
| 57 |
+
elif 'rnn' in name.lower() and 'bias_ih' in name:
|
| 58 |
+
rnn_bias_ih = param
|
| 59 |
+
print(f"Found RNN bias_ih: {name}, shape: {param.shape}")
|
| 60 |
+
elif 'rnn' in name.lower() and 'bias_hh' in name:
|
| 61 |
+
rnn_bias_hh = param
|
| 62 |
+
print(f"Found RNN bias_hh: {name}, shape: {param.shape}")
|
| 63 |
+
|
| 64 |
+
# The original has input size 128 (not 64), so we need to adapt
|
| 65 |
+
if rnn_weight_ih is not None:
|
| 66 |
+
orig_input_size = rnn_weight_ih.shape[1]
|
| 67 |
+
print(f"Original input size: {orig_input_size}")
|
| 68 |
+
|
| 69 |
+
# Recreate LSTM with correct input size
|
| 70 |
+
self.lstm_cell = nn.LSTMCell(orig_input_size, self.hidden_size)
|
| 71 |
+
self.input_size = orig_input_size
|
| 72 |
+
|
| 73 |
+
# Load weights
|
| 74 |
+
self.lstm_cell.weight_ih.data = torch.from_numpy(rnn_weight_ih)
|
| 75 |
+
self.lstm_cell.weight_hh.data = torch.from_numpy(rnn_weight_hh)
|
| 76 |
+
self.lstm_cell.bias_ih.data = torch.from_numpy(rnn_bias_ih)
|
| 77 |
+
self.lstm_cell.bias_hh.data = torch.from_numpy(rnn_bias_hh)
|
| 78 |
+
|
| 79 |
+
print(f"β
RNN created with {self.hidden_size} hidden units, {self.input_size} input size")
|
| 80 |
+
|
| 81 |
+
def forward(self, x, h_prev=None, c_prev=None):
|
| 82 |
+
# x shape: (batch, seq_len, input_size)
|
| 83 |
+
batch_size, seq_len, input_size = x.shape
|
| 84 |
+
|
| 85 |
+
# Adapt input size if needed
|
| 86 |
+
if input_size != self.input_size:
|
| 87 |
+
if input_size < self.input_size:
|
| 88 |
+
# Pad with zeros
|
| 89 |
+
padding = torch.zeros(batch_size, seq_len, self.input_size - input_size)
|
| 90 |
+
x = torch.cat([x, padding], dim=2)
|
| 91 |
+
else:
|
| 92 |
+
# Truncate
|
| 93 |
+
x = x[:, :, :self.input_size]
|
| 94 |
+
|
| 95 |
+
if h_prev is None:
|
| 96 |
+
h_prev = torch.zeros(batch_size, self.hidden_size)
|
| 97 |
+
if c_prev is None:
|
| 98 |
+
c_prev = torch.zeros(batch_size, self.hidden_size)
|
| 99 |
+
|
| 100 |
+
outputs = []
|
| 101 |
+
h, c = h_prev, c_prev
|
| 102 |
+
|
| 103 |
+
for t in range(seq_len):
|
| 104 |
+
h, c = self.lstm_cell(x[:, t, :], (h, c))
|
| 105 |
+
outputs.append(h.unsqueeze(1))
|
| 106 |
+
|
| 107 |
+
output = torch.cat(outputs, dim=1) # (batch, seq_len, hidden_size)
|
| 108 |
+
return output, h, c
|
| 109 |
+
|
| 110 |
+
# Create proper RNN with original weights
|
| 111 |
+
proper_rnn = ProperRNN128(all_params)
|
| 112 |
+
proper_rnn.eval()
|
| 113 |
+
|
| 114 |
+
print("\n" + "="*60)
|
| 115 |
+
print("π― Creating Proper Classifier with Original Weights")
|
| 116 |
+
print("="*60)
|
| 117 |
+
|
| 118 |
+
class ProperClassifier128(nn.Module):
|
| 119 |
+
"""Classifier using original silero weights"""
|
| 120 |
+
def __init__(self, all_params):
|
| 121 |
+
super().__init__()
|
| 122 |
+
|
| 123 |
+
# Find classifier weights
|
| 124 |
+
classifier_weight = None
|
| 125 |
+
classifier_bias = None
|
| 126 |
+
|
| 127 |
+
for name, param in all_params.items():
|
| 128 |
+
if 'decoder' in name.lower() and 'weight' in name and param.shape[0] == 1:
|
| 129 |
+
classifier_weight = param
|
| 130 |
+
print(f"Found classifier weight: {name}, shape: {param.shape}")
|
| 131 |
+
elif 'decoder' in name.lower() and 'bias' in name and param.shape[0] == 1:
|
| 132 |
+
classifier_bias = param
|
| 133 |
+
print(f"Found classifier bias: {name}, shape: {param.shape}")
|
| 134 |
+
|
| 135 |
+
# Create classifier layers
|
| 136 |
+
self.dropout = nn.Dropout(0.1)
|
| 137 |
+
self.activation = nn.ReLU()
|
| 138 |
+
|
| 139 |
+
if classifier_weight is not None:
|
| 140 |
+
input_size = classifier_weight.shape[1] if classifier_weight.ndim == 2 else classifier_weight.shape[1]
|
| 141 |
+
self.classifier = nn.Linear(input_size, 1)
|
| 142 |
+
|
| 143 |
+
# Load weights
|
| 144 |
+
if classifier_weight.ndim == 3: # Conv1d weight
|
| 145 |
+
self.classifier.weight.data = torch.from_numpy(classifier_weight.squeeze(-1))
|
| 146 |
+
else:
|
| 147 |
+
self.classifier.weight.data = torch.from_numpy(classifier_weight)
|
| 148 |
+
|
| 149 |
+
if classifier_bias is not None:
|
| 150 |
+
self.classifier.bias.data = torch.from_numpy(classifier_bias)
|
| 151 |
+
|
| 152 |
+
print(f"β
Classifier created with input size: {input_size}")
|
| 153 |
+
else:
|
| 154 |
+
# Fallback: create with 128 input size
|
| 155 |
+
self.classifier = nn.Linear(128, 1)
|
| 156 |
+
print("β οΈ Using fallback classifier (no weights found)")
|
| 157 |
+
|
| 158 |
+
self.sigmoid = nn.Sigmoid()
|
| 159 |
+
|
| 160 |
+
def forward(self, x):
|
| 161 |
+
# x shape: (batch, seq_len, features)
|
| 162 |
+
# Take the last timestep for classification
|
| 163 |
+
if x.dim() == 3:
|
| 164 |
+
x = x[:, -1, :] # (batch, features)
|
| 165 |
+
|
| 166 |
+
x = self.dropout(x)
|
| 167 |
+
x = self.activation(x)
|
| 168 |
+
x = self.classifier(x)
|
| 169 |
+
x = self.sigmoid(x)
|
| 170 |
+
return x
|
| 171 |
+
|
| 172 |
+
# Create proper classifier with original weights
|
| 173 |
+
proper_classifier = ProperClassifier128(all_params)
|
| 174 |
+
proper_classifier.eval()
|
| 175 |
+
|
| 176 |
+
# Test the models
|
| 177 |
+
print(f"\nπ§ͺ Testing models...")
|
| 178 |
+
test_input = torch.randn(1, 4, 128) # Use 128 input size
|
| 179 |
+
print(f"Test input shape: {test_input.shape}")
|
| 180 |
+
|
| 181 |
+
with torch.no_grad():
|
| 182 |
+
rnn_output, h_final, c_final = proper_rnn(test_input)
|
| 183 |
+
print(f"β
RNN output shape: {rnn_output.shape}")
|
| 184 |
+
|
| 185 |
+
classifier_output = proper_classifier(rnn_output)
|
| 186 |
+
print(f"β
Classifier output: {classifier_output.item():.4f}")
|
| 187 |
+
|
| 188 |
+
print("\nπ Converting to CoreML...")
|
| 189 |
+
|
| 190 |
+
# Convert RNN to CoreML
|
| 191 |
+
print("Converting RNN...")
|
| 192 |
+
try:
|
| 193 |
+
class RNNWrapper(nn.Module):
|
| 194 |
+
def __init__(self, rnn):
|
| 195 |
+
super().__init__()
|
| 196 |
+
self.rnn = rnn
|
| 197 |
+
|
| 198 |
+
def forward(self, x, h_prev, c_prev):
|
| 199 |
+
output, h, c = self.rnn(x, h_prev, c_prev)
|
| 200 |
+
return output, h, c
|
| 201 |
+
|
| 202 |
+
rnn_wrapper = RNNWrapper(proper_rnn)
|
| 203 |
+
|
| 204 |
+
# Trace the model
|
| 205 |
+
dummy_input = torch.randn(1, 4, proper_rnn.input_size)
|
| 206 |
+
dummy_h = torch.zeros(1, 128)
|
| 207 |
+
dummy_c = torch.zeros(1, 128)
|
| 208 |
+
|
| 209 |
+
traced_rnn = torch.jit.trace(rnn_wrapper, (dummy_input, dummy_h, dummy_c))
|
| 210 |
+
|
| 211 |
+
proper_rnn_coreml = ct.convert(
|
| 212 |
+
traced_rnn,
|
| 213 |
+
inputs=[
|
| 214 |
+
ct.TensorType(shape=(1, 4, proper_rnn.input_size), name="encoder_features"),
|
| 215 |
+
ct.TensorType(shape=(1, 128), name="h_in"),
|
| 216 |
+
ct.TensorType(shape=(1, 128), name="c_in")
|
| 217 |
+
],
|
| 218 |
+
convert_to="mlprogram"
|
| 219 |
+
)
|
| 220 |
+
proper_rnn_coreml.save("proper_rnn_128_original_weights.mlpackage")
|
| 221 |
+
print("β
RNN saved as proper_rnn_128_original_weights.mlpackage")
|
| 222 |
+
|
| 223 |
+
except Exception as e:
|
| 224 |
+
print(f"β RNN conversion failed: {e}")
|
| 225 |
+
|
| 226 |
+
# Convert Classifier to CoreML
|
| 227 |
+
print("\nConverting Classifier...")
|
| 228 |
+
try:
|
| 229 |
+
traced_classifier = torch.jit.trace(proper_classifier, rnn_output)
|
| 230 |
+
|
| 231 |
+
proper_classifier_coreml = ct.convert(
|
| 232 |
+
traced_classifier,
|
| 233 |
+
inputs=[ct.TensorType(shape=(1, 4, 128), name="rnn_features")],
|
| 234 |
+
convert_to="mlprogram"
|
| 235 |
+
)
|
| 236 |
+
proper_classifier_coreml.save("proper_classifier_128_original_weights.mlpackage")
|
| 237 |
+
print("β
Classifier saved as proper_classifier_128_original_weights.mlpackage")
|
| 238 |
+
|
| 239 |
+
except Exception as e:
|
| 240 |
+
print(f"β Classifier conversion failed: {e}")
|
| 241 |
+
|
| 242 |
+
print("\n" + "="*60)
|
| 243 |
+
print("π Proper Models Created with Original Weights!")
|
| 244 |
+
print("="*60)
|
| 245 |
+
print("β
proper_rnn_128_original_weights.mlpackage - Original LSTM weights")
|
| 246 |
+
print("β
proper_classifier_128_original_weights.mlpackage - Original classifier weights")
|
| 247 |
+
print("\nπ― These models:")
|
| 248 |
+
print(" - Use the ACTUAL weights from silero_vad.jit")
|
| 249 |
+
print(" - Have 128 parameters as required")
|
| 250 |
+
print(" - Should produce meaningful VAD results")
|
| 251 |
+
print(" - Are simple and focused on just RNN + Classifier")
|
fix_conv1d_classifier.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Fix Conv1d Classifier Issue
|
| 4 |
+
|
| 5 |
+
The Conv1d β Linear conversion is WRONG. This script creates a proper
|
| 6 |
+
classifier that maintains the Conv1d operation or does the conversion correctly.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.nn as nn
|
| 11 |
+
import coremltools as ct
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
print("π§ FIXING Conv1d β Linear CONVERSION ISSUE")
|
| 15 |
+
print("=" * 60)
|
| 16 |
+
|
| 17 |
+
# Load the extracted weights
|
| 18 |
+
all_params = torch.load('extracted_silero_weights.pth', weights_only=False)
|
| 19 |
+
conv_weight = all_params['_model.decoder.decoder.2.weight']
|
| 20 |
+
conv_bias = all_params['_model.decoder.decoder.2.bias']
|
| 21 |
+
|
| 22 |
+
print(f"Original Conv1d weight shape: {conv_weight.shape}") # (1, 128, 1)
|
| 23 |
+
print(f"Original Conv1d bias shape: {conv_bias.shape}") # (1,)
|
| 24 |
+
|
| 25 |
+
# ============================================================================
|
| 26 |
+
# 1. UNDERSTAND THE PROBLEM
|
| 27 |
+
# ============================================================================
|
| 28 |
+
print(f"\n1οΈβ£ THE PROBLEM")
|
| 29 |
+
print("-" * 40)
|
| 30 |
+
|
| 31 |
+
print("β WRONG conversion in current classifier:")
|
| 32 |
+
print(" Conv1d weight (1, 128, 1) β Linear weight (128, 1) with transpose")
|
| 33 |
+
print(" This creates dimension mismatch!")
|
| 34 |
+
|
| 35 |
+
print("\nβ
CORRECT approach - Option 1: Keep Conv1d")
|
| 36 |
+
print(" Don't convert to Linear, keep as Conv1d in CoreML")
|
| 37 |
+
|
| 38 |
+
print("\nβ
CORRECT approach - Option 2: Proper Linear conversion")
|
| 39 |
+
print(" Conv1d weight (1, 128, 1) β Linear weight (1, 128) WITHOUT transpose")
|
| 40 |
+
|
| 41 |
+
# ============================================================================
|
| 42 |
+
# 2. CREATE CORRECT CLASSIFIER WITH CONV1D
|
| 43 |
+
# ============================================================================
|
| 44 |
+
print(f"\n2οΈβ£ SOLUTION 1: Keep Conv1d (Recommended)")
|
| 45 |
+
print("-" * 40)
|
| 46 |
+
|
| 47 |
+
class CorrectClassifierConv1d(nn.Module):
|
| 48 |
+
"""Classifier that keeps Conv1d operation (exactly like original)"""
|
| 49 |
+
def __init__(self, conv_weight, conv_bias):
|
| 50 |
+
super().__init__()
|
| 51 |
+
self.dropout = nn.Dropout(0.1)
|
| 52 |
+
self.activation = nn.ReLU()
|
| 53 |
+
|
| 54 |
+
# Keep as Conv1d (exactly like original)
|
| 55 |
+
self.classifier = nn.Conv1d(in_channels=128, out_channels=1, kernel_size=1)
|
| 56 |
+
self.classifier.weight.data = torch.from_numpy(conv_weight)
|
| 57 |
+
self.classifier.bias.data = torch.from_numpy(conv_bias)
|
| 58 |
+
|
| 59 |
+
self.sigmoid = nn.Sigmoid()
|
| 60 |
+
|
| 61 |
+
def forward(self, x):
|
| 62 |
+
# x shape: (batch, seq_len, features) from RNN
|
| 63 |
+
# Convert to Conv1d format: (batch, features, seq_len)
|
| 64 |
+
x = x.transpose(1, 2) # (batch, 128, seq_len)
|
| 65 |
+
|
| 66 |
+
x = self.dropout(x)
|
| 67 |
+
x = self.activation(x)
|
| 68 |
+
x = self.classifier(x) # (batch, 1, seq_len)
|
| 69 |
+
x = x.squeeze(1) # (batch, seq_len)
|
| 70 |
+
x = x[:, -1:] # Take last timestep: (batch, 1)
|
| 71 |
+
x = self.sigmoid(x)
|
| 72 |
+
return x
|
| 73 |
+
|
| 74 |
+
# Create and test correct Conv1d classifier
|
| 75 |
+
correct_conv_classifier = CorrectClassifierConv1d(conv_weight, conv_bias)
|
| 76 |
+
correct_conv_classifier.eval()
|
| 77 |
+
|
| 78 |
+
# Test it
|
| 79 |
+
test_input = torch.randn(1, 4, 128)
|
| 80 |
+
print(f"\nπ§ͺ Testing correct Conv1d classifier:")
|
| 81 |
+
with torch.no_grad():
|
| 82 |
+
correct_output = correct_conv_classifier(test_input)
|
| 83 |
+
print(f"Input shape: {test_input.shape}")
|
| 84 |
+
print(f"Output shape: {correct_output.shape}")
|
| 85 |
+
print(f"Output value: {correct_output.item():.4f}")
|
| 86 |
+
|
| 87 |
+
# ============================================================================
|
| 88 |
+
# 3. CREATE CORRECT CLASSIFIER WITH LINEAR
|
| 89 |
+
# ============================================================================
|
| 90 |
+
print(f"\n3οΈβ£ SOLUTION 2: Correct Linear Conversion")
|
| 91 |
+
print("-" * 40)
|
| 92 |
+
|
| 93 |
+
class CorrectClassifierLinear(nn.Module):
|
| 94 |
+
"""Classifier with CORRECT Conv1d β Linear conversion"""
|
| 95 |
+
def __init__(self, conv_weight, conv_bias):
|
| 96 |
+
super().__init__()
|
| 97 |
+
self.dropout = nn.Dropout(0.1)
|
| 98 |
+
self.activation = nn.ReLU()
|
| 99 |
+
|
| 100 |
+
# CORRECT conversion: (1, 128, 1) β (1, 128) WITHOUT transpose
|
| 101 |
+
linear_weight = conv_weight.squeeze(-1) # (1, 128) - NO TRANSPOSE!
|
| 102 |
+
self.classifier = nn.Linear(128, 1)
|
| 103 |
+
self.classifier.weight.data = torch.from_numpy(linear_weight)
|
| 104 |
+
self.classifier.bias.data = torch.from_numpy(conv_bias)
|
| 105 |
+
|
| 106 |
+
self.sigmoid = nn.Sigmoid()
|
| 107 |
+
|
| 108 |
+
def forward(self, x):
|
| 109 |
+
# x shape: (batch, seq_len, features) from RNN
|
| 110 |
+
# Take last timestep
|
| 111 |
+
x = x[:, -1, :] # (batch, features)
|
| 112 |
+
|
| 113 |
+
x = self.dropout(x)
|
| 114 |
+
x = self.activation(x)
|
| 115 |
+
x = self.classifier(x) # (batch, 1)
|
| 116 |
+
x = self.sigmoid(x)
|
| 117 |
+
return x
|
| 118 |
+
|
| 119 |
+
# Create and test correct Linear classifier
|
| 120 |
+
correct_linear_classifier = CorrectClassifierLinear(conv_weight, conv_bias)
|
| 121 |
+
correct_linear_classifier.eval()
|
| 122 |
+
|
| 123 |
+
print(f"\nπ§ͺ Testing correct Linear classifier:")
|
| 124 |
+
with torch.no_grad():
|
| 125 |
+
linear_output = correct_linear_classifier(test_input)
|
| 126 |
+
print(f"Input shape: {test_input.shape}")
|
| 127 |
+
print(f"Output shape: {linear_output.shape}")
|
| 128 |
+
print(f"Output value: {linear_output.item():.4f}")
|
| 129 |
+
|
| 130 |
+
# ============================================================================
|
| 131 |
+
# 4. VERIFY EQUIVALENCE
|
| 132 |
+
# ============================================================================
|
| 133 |
+
print(f"\n4οΈβ£ VERIFYING EQUIVALENCE")
|
| 134 |
+
print("-" * 40)
|
| 135 |
+
|
| 136 |
+
print(f"Conv1d classifier output: {correct_output.item():.6f}")
|
| 137 |
+
print(f"Linear classifier output: {linear_output.item():.6f}")
|
| 138 |
+
diff = abs(correct_output.item() - linear_output.item())
|
| 139 |
+
print(f"Difference: {diff:.10f}")
|
| 140 |
+
|
| 141 |
+
if diff < 1e-6:
|
| 142 |
+
print("β
Conv1d and corrected Linear are EQUIVALENT!")
|
| 143 |
+
else:
|
| 144 |
+
print("β Still not equivalent - need further investigation")
|
| 145 |
+
|
| 146 |
+
# ============================================================================
|
| 147 |
+
# 5. CREATE NEW COREML MODEL
|
| 148 |
+
# ============================================================================
|
| 149 |
+
print(f"\n5οΈβ£ CREATING CORRECTED COREML MODEL")
|
| 150 |
+
print("-" * 40)
|
| 151 |
+
|
| 152 |
+
# Convert the correct Conv1d classifier
|
| 153 |
+
print("Converting correct Conv1d classifier...")
|
| 154 |
+
try:
|
| 155 |
+
traced_conv_classifier = torch.jit.trace(correct_conv_classifier, test_input)
|
| 156 |
+
|
| 157 |
+
conv_classifier_coreml = ct.convert(
|
| 158 |
+
traced_conv_classifier,
|
| 159 |
+
inputs=[ct.TensorType(shape=(1, 4, 128), name="rnn_features")],
|
| 160 |
+
outputs=[ct.TensorType(name="vad_probability")],
|
| 161 |
+
convert_to="mlprogram"
|
| 162 |
+
)
|
| 163 |
+
conv_classifier_coreml.save("correct_classifier_conv1d.mlpackage")
|
| 164 |
+
print("β
Correct Conv1d classifier saved as correct_classifier_conv1d.mlpackage")
|
| 165 |
+
|
| 166 |
+
except Exception as e:
|
| 167 |
+
print(f"β Conv1d classifier conversion failed: {e}")
|
| 168 |
+
|
| 169 |
+
# Convert the correct Linear classifier
|
| 170 |
+
print("\nConverting correct Linear classifier...")
|
| 171 |
+
try:
|
| 172 |
+
traced_linear_classifier = torch.jit.trace(correct_linear_classifier, test_input)
|
| 173 |
+
|
| 174 |
+
linear_classifier_coreml = ct.convert(
|
| 175 |
+
traced_linear_classifier,
|
| 176 |
+
inputs=[ct.TensorType(shape=(1, 4, 128), name="rnn_features")],
|
| 177 |
+
outputs=[ct.TensorType(name="vad_probability")],
|
| 178 |
+
convert_to="mlprogram"
|
| 179 |
+
)
|
| 180 |
+
linear_classifier_coreml.save("correct_classifier_linear.mlpackage")
|
| 181 |
+
print("β
Correct Linear classifier saved as correct_classifier_linear.mlpackage")
|
| 182 |
+
|
| 183 |
+
except Exception as e:
|
| 184 |
+
print(f"β Linear classifier conversion failed: {e}")
|
| 185 |
+
|
| 186 |
+
print(f"\n6οΈβ£ RECOMMENDATION")
|
| 187 |
+
print("-" * 40)
|
| 188 |
+
print("π― The original Conv1d β Linear conversion was WRONG!")
|
| 189 |
+
print("π Root cause: Incorrect weight transpose and dimension handling")
|
| 190 |
+
print("π§ Solutions created:")
|
| 191 |
+
print(" 1. correct_classifier_conv1d.mlpackage - Keeps Conv1d (recommended)")
|
| 192 |
+
print(" 2. correct_classifier_linear.mlpackage - Correct Linear conversion")
|
| 193 |
+
print("\nβ
Use these corrected models instead of the broken one!")
|
| 194 |
+
print("β
This should fix the accuracy issues in main2.py")
|
main2.py
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Optimal VAD Implementation using RNN Decoder + Fixed Classifier
|
| 4 |
+
|
| 5 |
+
This uses the best combination discovered:
|
| 6 |
+
- silero_rnn_decoder.mlmodel (proper output magnitudes)
|
| 7 |
+
- correct_classifier_conv1d.mlpackage (fixed Conv1d)
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import librosa
|
| 12 |
+
import coremltools as ct
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class OptimalCoreMLVAD:
|
| 17 |
+
"""
|
| 18 |
+
Optimal VAD using RNN Decoder + Fixed Classifier
|
| 19 |
+
"""
|
| 20 |
+
def __init__(self):
|
| 21 |
+
"""Initialize the VAD pipeline with optimal models"""
|
| 22 |
+
print("Loading Optimal CoreML models...")
|
| 23 |
+
|
| 24 |
+
# Load existing preprocessing models with explicit ANE preference
|
| 25 |
+
self.stft_model = ct.models.MLModel("silero_stft.mlmodel", compute_units=ct.ComputeUnit.ALL)
|
| 26 |
+
self.encoder_model = ct.models.MLModel("silero_encoder.mlmodel", compute_units=ct.ComputeUnit.ALL)
|
| 27 |
+
|
| 28 |
+
# Load OPTIMAL combination with ANE preference
|
| 29 |
+
self.rnn_model = ct.models.MLModel("silero_rnn_decoder.mlmodel", compute_units=ct.ComputeUnit.ALL)
|
| 30 |
+
self.classifier_model = ct.models.MLModel("correct_classifier_conv1d.mlpackage", compute_units=ct.ComputeUnit.ALL)
|
| 31 |
+
|
| 32 |
+
print("β
Optimal models loaded:")
|
| 33 |
+
print(" - STFT: silero_stft.mlmodel")
|
| 34 |
+
print(" - Encoder: silero_encoder.mlmodel")
|
| 35 |
+
print(" - RNN: silero_rnn_decoder.mlmodel (π₯ BEST)")
|
| 36 |
+
print(" - Classifier: correct_classifier_conv1d.mlpackage (π§ FIXED)")
|
| 37 |
+
print("π§ All models configured for Neural Engine (ANE) acceleration")
|
| 38 |
+
|
| 39 |
+
# Initialize state for RNN Decoder (requires 3D states)
|
| 40 |
+
self.h_state = np.zeros((1, 1, 128), dtype=np.float32)
|
| 41 |
+
self.c_state = np.zeros((1, 1, 128), dtype=np.float32)
|
| 42 |
+
|
| 43 |
+
# Initialize feature buffer for temporal context
|
| 44 |
+
self.feature_buffer = []
|
| 45 |
+
|
| 46 |
+
print("β
Optimal VAD loaded successfully!")
|
| 47 |
+
|
| 48 |
+
def reset_state(self):
|
| 49 |
+
"""Reset the RNN state and feature buffer"""
|
| 50 |
+
self.h_state = np.zeros((1, 1, 128), dtype=np.float32)
|
| 51 |
+
self.c_state = np.zeros((1, 1, 128), dtype=np.float32)
|
| 52 |
+
|
| 53 |
+
if hasattr(self, 'feature_buffer'):
|
| 54 |
+
self.feature_buffer = []
|
| 55 |
+
|
| 56 |
+
def process_chunk(self, audio_chunk):
|
| 57 |
+
"""Process audio chunk using optimal model combination"""
|
| 58 |
+
# Ensure correct shape
|
| 59 |
+
if audio_chunk.ndim == 1:
|
| 60 |
+
audio_chunk = audio_chunk.reshape(1, -1)
|
| 61 |
+
|
| 62 |
+
# STFT processing
|
| 63 |
+
stft_result = self.stft_model.predict({"audio_input": audio_chunk})
|
| 64 |
+
stft_output_key = list(stft_result.keys())[0]
|
| 65 |
+
stft_features = stft_result[stft_output_key]
|
| 66 |
+
|
| 67 |
+
# Temporal context management
|
| 68 |
+
if not hasattr(self, 'feature_buffer'):
|
| 69 |
+
self.feature_buffer = []
|
| 70 |
+
|
| 71 |
+
# Add current features to buffer
|
| 72 |
+
self.feature_buffer.append(stft_features)
|
| 73 |
+
|
| 74 |
+
# Keep only the last 4 frames for temporal context
|
| 75 |
+
if len(self.feature_buffer) > 4:
|
| 76 |
+
self.feature_buffer = self.feature_buffer[-4:]
|
| 77 |
+
|
| 78 |
+
# Pad with zeros if we have less than 4 frames
|
| 79 |
+
while len(self.feature_buffer) < 4:
|
| 80 |
+
self.feature_buffer.insert(0, np.zeros_like(stft_features))
|
| 81 |
+
|
| 82 |
+
# Concatenate along time dimension
|
| 83 |
+
stft_features = np.concatenate(self.feature_buffer, axis=-1)
|
| 84 |
+
|
| 85 |
+
# Encoder processing
|
| 86 |
+
encoder_result = self.encoder_model.predict({"stft_features": stft_features})
|
| 87 |
+
encoder_output_key = list(encoder_result.keys())[0]
|
| 88 |
+
encoder_features = encoder_result[encoder_output_key]
|
| 89 |
+
|
| 90 |
+
# Reshape encoder features for RNN
|
| 91 |
+
encoder_features = np.transpose(encoder_features, (0, 2, 1)) # (1, T, 64)
|
| 92 |
+
|
| 93 |
+
# Take only the last 4 timesteps
|
| 94 |
+
if encoder_features.shape[1] > 4:
|
| 95 |
+
encoder_features = encoder_features[:, -4:, :]
|
| 96 |
+
elif encoder_features.shape[1] < 4:
|
| 97 |
+
# Pad with zeros if needed
|
| 98 |
+
padding = 4 - encoder_features.shape[1]
|
| 99 |
+
pad_shape = (encoder_features.shape[0], padding, encoder_features.shape[2])
|
| 100 |
+
encoder_features = np.concatenate([np.zeros(pad_shape), encoder_features], axis=1)
|
| 101 |
+
|
| 102 |
+
# Ensure the feature dimension is 128 for RNN
|
| 103 |
+
if encoder_features.shape[2] != 128:
|
| 104 |
+
# Resize/pad to 128 dimensions
|
| 105 |
+
if encoder_features.shape[2] > 128:
|
| 106 |
+
encoder_features = encoder_features[:, :, :128]
|
| 107 |
+
else:
|
| 108 |
+
padding = 128 - encoder_features.shape[2]
|
| 109 |
+
pad_shape = (encoder_features.shape[0], encoder_features.shape[1], padding)
|
| 110 |
+
encoder_features = np.concatenate([encoder_features, np.zeros(pad_shape)], axis=2)
|
| 111 |
+
|
| 112 |
+
# RNN Decoder processing with proper state management
|
| 113 |
+
rnn_result = self.rnn_model.predict({
|
| 114 |
+
"encoder_features": encoder_features,
|
| 115 |
+
"h_in": self.h_state,
|
| 116 |
+
"c_in": self.c_state
|
| 117 |
+
})
|
| 118 |
+
|
| 119 |
+
# Extract RNN Decoder outputs properly
|
| 120 |
+
rnn_features = None
|
| 121 |
+
new_h_state = None
|
| 122 |
+
new_c_state = None
|
| 123 |
+
|
| 124 |
+
# RNN Decoder has specific output names - find them by shape
|
| 125 |
+
for key, value in rnn_result.items():
|
| 126 |
+
if len(value.shape) == 3 and value.shape[1] > 1: # Sequence output
|
| 127 |
+
rnn_features = value
|
| 128 |
+
elif len(value.shape) == 3 and value.shape == (1, 1, 128): # State outputs
|
| 129 |
+
if new_h_state is None:
|
| 130 |
+
new_h_state = value
|
| 131 |
+
else:
|
| 132 |
+
new_c_state = value
|
| 133 |
+
|
| 134 |
+
# Update states for next chunk
|
| 135 |
+
if new_h_state is not None:
|
| 136 |
+
self.h_state = new_h_state
|
| 137 |
+
if new_c_state is not None:
|
| 138 |
+
self.c_state = new_c_state
|
| 139 |
+
|
| 140 |
+
# Ensure we have the sequence output
|
| 141 |
+
if rnn_features is None:
|
| 142 |
+
raise RuntimeError("Could not find RNN sequence output")
|
| 143 |
+
|
| 144 |
+
# Ensure correct shape for classifier (1, 4, 128)
|
| 145 |
+
if rnn_features.shape != (1, 4, 128):
|
| 146 |
+
if rnn_features.shape[1] != 4:
|
| 147 |
+
if rnn_features.shape[1] > 4:
|
| 148 |
+
rnn_features = rnn_features[:, -4:, :]
|
| 149 |
+
else:
|
| 150 |
+
last_timestep = rnn_features[:, -1:, :]
|
| 151 |
+
padding_needed = 4 - rnn_features.shape[1]
|
| 152 |
+
padding = np.repeat(last_timestep, padding_needed, axis=1)
|
| 153 |
+
rnn_features = np.concatenate([rnn_features, padding], axis=1)
|
| 154 |
+
|
| 155 |
+
if rnn_features.shape[2] != 128:
|
| 156 |
+
if rnn_features.shape[2] > 128:
|
| 157 |
+
rnn_features = rnn_features[:, :, :128]
|
| 158 |
+
else:
|
| 159 |
+
padding = 128 - rnn_features.shape[2]
|
| 160 |
+
pad_shape = (rnn_features.shape[0], rnn_features.shape[1], padding)
|
| 161 |
+
rnn_features = np.concatenate([rnn_features, np.zeros(pad_shape)], axis=2)
|
| 162 |
+
|
| 163 |
+
# Classifier processing with fixed Conv1d model (clean output!)
|
| 164 |
+
classifier_result = self.classifier_model.predict({"rnn_features": rnn_features})
|
| 165 |
+
classifier_output_key = list(classifier_result.keys())[0]
|
| 166 |
+
vad_prob = float(classifier_result[classifier_output_key].squeeze())
|
| 167 |
+
|
| 168 |
+
return vad_prob
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def process_file(filename, vad, sample_rate=16000, chunk_size=512, threshold=0.5):
|
| 172 |
+
"""Process audio file with VAD and display results"""
|
| 173 |
+
print(f"\nπ§ Processing: {filename}")
|
| 174 |
+
|
| 175 |
+
# Reset state for new file
|
| 176 |
+
vad.reset_state()
|
| 177 |
+
|
| 178 |
+
# Load audio
|
| 179 |
+
y, _ = librosa.load(filename, sr=sample_rate)
|
| 180 |
+
if y.ndim > 1:
|
| 181 |
+
y = librosa.to_mono(y)
|
| 182 |
+
|
| 183 |
+
num_chunks = len(y) // chunk_size
|
| 184 |
+
vad_scores = []
|
| 185 |
+
|
| 186 |
+
for i in range(num_chunks):
|
| 187 |
+
start = i * chunk_size
|
| 188 |
+
end = start + chunk_size
|
| 189 |
+
chunk = y[start:end]
|
| 190 |
+
if len(chunk) < chunk_size:
|
| 191 |
+
break # Skip last short chunk
|
| 192 |
+
|
| 193 |
+
prob = vad.process_chunk(chunk.astype(np.float32))
|
| 194 |
+
vad_scores.append(prob)
|
| 195 |
+
|
| 196 |
+
# Average VAD probability across all chunks
|
| 197 |
+
avg_vad = np.mean(vad_scores) if vad_scores else 0.0
|
| 198 |
+
status = "π’ Speech" if avg_vad >= threshold else "β«οΈ Silence"
|
| 199 |
+
|
| 200 |
+
print(f"{os.path.basename(filename):<18} | Avg VAD: {avg_vad:.4f} | {status}")
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def test_optimal_vad():
|
| 204 |
+
"""Test the optimal VAD implementation"""
|
| 205 |
+
print("π Testing OPTIMAL VAD Implementation")
|
| 206 |
+
print("=" * 60)
|
| 207 |
+
print("π₯ Using BEST model combination:")
|
| 208 |
+
print(" - RNN: silero_rnn_decoder.mlmodel")
|
| 209 |
+
print(" - Classifier: correct_classifier_conv1d.mlpackage")
|
| 210 |
+
print()
|
| 211 |
+
|
| 212 |
+
vad = OptimalCoreMLVAD()
|
| 213 |
+
|
| 214 |
+
test_folder = "test"
|
| 215 |
+
if not os.path.exists(test_folder):
|
| 216 |
+
print(f"β Test folder '{test_folder}' not found!")
|
| 217 |
+
return
|
| 218 |
+
|
| 219 |
+
test_files = sorted(f for f in os.listdir(test_folder) if f.endswith(".mp3"))
|
| 220 |
+
|
| 221 |
+
if not test_files:
|
| 222 |
+
print(f"β No MP3 files found in '{test_folder}' folder!")
|
| 223 |
+
return
|
| 224 |
+
|
| 225 |
+
print(f"{'File':<18} | {'VAD Score':<9} | {'Result'}")
|
| 226 |
+
print("-" * 50)
|
| 227 |
+
|
| 228 |
+
human_scores = []
|
| 229 |
+
ambient_scores = []
|
| 230 |
+
|
| 231 |
+
for file in test_files:
|
| 232 |
+
full_path = os.path.join(test_folder, file)
|
| 233 |
+
|
| 234 |
+
# Capture the score for analysis
|
| 235 |
+
vad.reset_state()
|
| 236 |
+
y, _ = librosa.load(full_path, sr=16000)
|
| 237 |
+
if y.ndim > 1:
|
| 238 |
+
y = librosa.to_mono(y)
|
| 239 |
+
|
| 240 |
+
chunk_size = 512
|
| 241 |
+
num_chunks = min(10, len(y) // chunk_size)
|
| 242 |
+
vad_scores = []
|
| 243 |
+
|
| 244 |
+
for i in range(num_chunks):
|
| 245 |
+
start = i * chunk_size
|
| 246 |
+
end = start + chunk_size
|
| 247 |
+
chunk = y[start:end]
|
| 248 |
+
if len(chunk) < chunk_size:
|
| 249 |
+
break
|
| 250 |
+
prob = vad.process_chunk(chunk.astype(np.float32))
|
| 251 |
+
vad_scores.append(prob)
|
| 252 |
+
|
| 253 |
+
avg_vad = np.mean(vad_scores) if vad_scores else 0.0
|
| 254 |
+
|
| 255 |
+
# Categorize for analysis
|
| 256 |
+
if "human" in file:
|
| 257 |
+
human_scores.append(avg_vad)
|
| 258 |
+
elif "ambient" in file:
|
| 259 |
+
ambient_scores.append(avg_vad)
|
| 260 |
+
|
| 261 |
+
# Display result
|
| 262 |
+
status = "π’ Speech" if avg_vad >= 0.5 else "β«οΈ Silence"
|
| 263 |
+
print(f"{os.path.basename(file):<18} | {avg_vad:.4f} | {status}")
|
| 264 |
+
|
| 265 |
+
# Analysis
|
| 266 |
+
if human_scores and ambient_scores:
|
| 267 |
+
human_avg = np.mean(human_scores)
|
| 268 |
+
ambient_avg = np.mean(ambient_scores)
|
| 269 |
+
separation = human_avg - ambient_avg
|
| 270 |
+
|
| 271 |
+
print(f"\nπ PERFORMANCE ANALYSIS:")
|
| 272 |
+
print(f" π€ Human average: {human_avg:.4f}")
|
| 273 |
+
print(f" πΏ Ambient average: {ambient_avg:.4f}")
|
| 274 |
+
print(f" π Separation: {separation:.4f}")
|
| 275 |
+
|
| 276 |
+
if separation > 0.05:
|
| 277 |
+
print(f" β
EXCELLENT: Strong separation")
|
| 278 |
+
elif separation > 0.01:
|
| 279 |
+
print(f" β
GOOD: Clear separation")
|
| 280 |
+
elif separation > 0:
|
| 281 |
+
print(f" β οΈ WEAK: Small separation")
|
| 282 |
+
else:
|
| 283 |
+
print(f" β POOR: No separation or inverted")
|
| 284 |
+
|
| 285 |
+
print("\nβ
Optimal VAD testing completed!")
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
if __name__ == "__main__":
|
| 289 |
+
test_optimal_vad()
|
test/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
test/ambient1.mp3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7d155512adf55505ca4a5b05f224e6ca7673691c94adcf645d320f5090c1227e
|
| 3 |
+
size 3331970
|
test/ambient2.mp3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:823ca10d36c37e7a9b5c6b9248c052ab58269578760ac53764c53e93973aaf0d
|
| 3 |
+
size 336875
|
test/ambient3.mp3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:751ad0c5bcefe22ff484c527997f8725484dc03282b5a8caf7229fa74c1ac55d
|
| 3 |
+
size 202560
|
test/ambient4.mp3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a8becd73bf451763ebdb51a83d983146bed5383c3203ae67230d92ed93131874
|
| 3 |
+
size 217440
|
test/ambient5.mp3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:026673682227fbc0280e4fea3433a3c7386dce6527b4790a960498a019575567
|
| 3 |
+
size 147840
|
test/human1.mp3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d8532881ef95c16d2e0e8bc8f14ecf7e0a8011c5b914399899122c381babea0b
|
| 3 |
+
size 184320
|
test/human2.mp3
ADDED
|
Binary file (63 kB). View file
|
|
|
test/human3.mp3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:58a908c1383d651980732846ea5b2c1f5336d29fd7e98b33317e60c451f8d2bd
|
| 3 |
+
size 123840
|
test/human4.mp3
ADDED
|
Binary file (31.7 kB). View file
|
|
|
test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Test Suite
|
| 4 |
+
Comprehensive benchmarking for comparing Silero VAD PyTorch with CoreML implementation
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import time
|
| 9 |
+
import json
|
| 10 |
+
import numpy as np
|
| 11 |
+
import librosa
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
from sklearn.metrics import roc_curve, auc, accuracy_score, precision_recall_fscore_support
|
| 14 |
+
from dataclasses import dataclass
|
| 15 |
+
from typing import List, Dict, Tuple, Optional
|
| 16 |
+
try:
|
| 17 |
+
import torch
|
| 18 |
+
import torchaudio
|
| 19 |
+
TORCH_AVAILABLE = True
|
| 20 |
+
except ImportError:
|
| 21 |
+
TORCH_AVAILABLE = False
|
| 22 |
+
print("Warning: PyTorch not available for comparison")
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
from main2 import OptimalCoreMLVAD
|
| 26 |
+
COREML_AVAILABLE = True
|
| 27 |
+
except ImportError:
|
| 28 |
+
COREML_AVAILABLE = False
|
| 29 |
+
print("Warning: CoreML VAD not available")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@dataclass
|
| 33 |
+
class VADBenchmarkResult:
|
| 34 |
+
"""Results from VAD benchmark testing"""
|
| 35 |
+
model_name: str
|
| 36 |
+
accuracy: float
|
| 37 |
+
precision: float
|
| 38 |
+
recall: float
|
| 39 |
+
f1_score: float
|
| 40 |
+
auc_score: float
|
| 41 |
+
processing_time: float
|
| 42 |
+
fps: float # Frames per second processed
|
| 43 |
+
total_time_seconds: float # Total wall-clock time in seconds
|
| 44 |
+
predictions: List[float]
|
| 45 |
+
ground_truth: List[int]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class DummyVAD:
|
| 49 |
+
"""Dummy VAD for testing when no models are available"""
|
| 50 |
+
|
| 51 |
+
def __init__(self):
|
| 52 |
+
"""Initialize dummy VAD"""
|
| 53 |
+
self.name = "Dummy_VAD"
|
| 54 |
+
|
| 55 |
+
def process_chunk(self, audio_chunk: np.ndarray) -> float:
|
| 56 |
+
"""Return random VAD probability for testing"""
|
| 57 |
+
# Simple energy-based VAD
|
| 58 |
+
energy = np.mean(audio_chunk ** 2)
|
| 59 |
+
return min(1.0, energy * 10) # Scale energy to probability
|
| 60 |
+
|
| 61 |
+
def reset_state(self):
|
| 62 |
+
"""Reset state (no-op for dummy)"""
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class SileroVADPyTorch:
|
| 67 |
+
"""Silero VAD PyTorch implementation for comparison"""
|
| 68 |
+
|
| 69 |
+
def __init__(self, model_path: Optional[str] = None):
|
| 70 |
+
"""Initialize Silero VAD PyTorch model"""
|
| 71 |
+
if not TORCH_AVAILABLE:
|
| 72 |
+
raise ImportError("PyTorch not available")
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
# Try the updated API first
|
| 76 |
+
self.model, utils = torch.hub.load(
|
| 77 |
+
repo_or_dir='snakers4/silero-vad',
|
| 78 |
+
model='silero_vad',
|
| 79 |
+
force_reload=True
|
| 80 |
+
)
|
| 81 |
+
self.get_speech_timestamps = utils[0]
|
| 82 |
+
self.sample_rate = 16000
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"Error loading Silero VAD: {e}")
|
| 85 |
+
# Fallback to direct model loading
|
| 86 |
+
try:
|
| 87 |
+
import urllib.request
|
| 88 |
+
import os
|
| 89 |
+
|
| 90 |
+
model_url = 'https://models.silero.ai/models/vad/silero_vad.onnx'
|
| 91 |
+
model_path = 'silero_vad.onnx'
|
| 92 |
+
|
| 93 |
+
if not os.path.exists(model_path):
|
| 94 |
+
print("Downloading Silero VAD ONNX model...")
|
| 95 |
+
urllib.request.urlretrieve(model_url, model_path)
|
| 96 |
+
|
| 97 |
+
import onnxruntime as ort
|
| 98 |
+
self.model = ort.InferenceSession(model_path)
|
| 99 |
+
self.sample_rate = 16000
|
| 100 |
+
self.use_onnx = True
|
| 101 |
+
except Exception as e2:
|
| 102 |
+
print(f"Fallback ONNX loading also failed: {e2}")
|
| 103 |
+
raise e
|
| 104 |
+
|
| 105 |
+
def process_chunk(self, audio_chunk: np.ndarray) -> float:
|
| 106 |
+
"""Process audio chunk and return VAD probability"""
|
| 107 |
+
if not hasattr(self, 'model'):
|
| 108 |
+
return 0.0
|
| 109 |
+
|
| 110 |
+
try:
|
| 111 |
+
if hasattr(self, 'use_onnx') and self.use_onnx:
|
| 112 |
+
# ONNX model processing
|
| 113 |
+
input_tensor = audio_chunk.reshape(1, -1).astype(np.float32)
|
| 114 |
+
outputs = self.model.run(None, {'input': input_tensor})
|
| 115 |
+
return float(outputs[0][0])
|
| 116 |
+
else:
|
| 117 |
+
# PyTorch model processing
|
| 118 |
+
if audio_chunk.ndim == 1:
|
| 119 |
+
audio_tensor = torch.from_numpy(audio_chunk).float()
|
| 120 |
+
else:
|
| 121 |
+
audio_tensor = torch.from_numpy(audio_chunk.squeeze()).float()
|
| 122 |
+
|
| 123 |
+
with torch.no_grad():
|
| 124 |
+
speech_prob = self.model(audio_tensor, self.sample_rate).item()
|
| 125 |
+
|
| 126 |
+
return speech_prob
|
| 127 |
+
except Exception as e:
|
| 128 |
+
print(f"Error in process_chunk: {e}")
|
| 129 |
+
return 0.0
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
class VADBenchmarkSuite:
|
| 133 |
+
"""Comprehensive VAD benchmark testing suite"""
|
| 134 |
+
|
| 135 |
+
def __init__(self):
|
| 136 |
+
"""Initialize benchmark suite"""
|
| 137 |
+
self.test_datasets = {
|
| 138 |
+
'clean_speech': [],
|
| 139 |
+
'noisy_speech': [],
|
| 140 |
+
'silence': [],
|
| 141 |
+
'noise_only': [],
|
| 142 |
+
'music': []
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
def load_test_data(self, test_dir: str) -> Dict[str, List[Tuple[str, int]]]:
|
| 146 |
+
"""
|
| 147 |
+
Load test data with ground truth labels
|
| 148 |
+
|
| 149 |
+
Args:
|
| 150 |
+
test_dir: Directory containing test audio files
|
| 151 |
+
|
| 152 |
+
Returns:
|
| 153 |
+
Dictionary mapping categories to (filepath, label) tuples
|
| 154 |
+
"""
|
| 155 |
+
test_data = {}
|
| 156 |
+
|
| 157 |
+
# Define file patterns and their labels - comprehensive patterns
|
| 158 |
+
patterns = {
|
| 159 |
+
'clean_speech': (['human', 'speech', 'voice', 'example'], 1),
|
| 160 |
+
'noisy_speech': (['noisy_speech', 'speech_noise'], 1),
|
| 161 |
+
'silence': (['silence', 'quiet', 'ambient'], 0),
|
| 162 |
+
'noise_only': (['noise', 'background', 'free-sound'], 0),
|
| 163 |
+
'music': (['music', 'instrumental'], 0)
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
# Special handling for speech directories
|
| 167 |
+
if 'speech' in test_dir.lower():
|
| 168 |
+
# All files in speech directories are speech (label=1)
|
| 169 |
+
patterns = {'clean_speech': ([''], 1)} # Match all files
|
| 170 |
+
|
| 171 |
+
if not os.path.exists(test_dir):
|
| 172 |
+
print(f"Warning: Test directory '{test_dir}' not found")
|
| 173 |
+
return test_data
|
| 174 |
+
|
| 175 |
+
for category, (keywords, label) in patterns.items():
|
| 176 |
+
test_data[category] = []
|
| 177 |
+
|
| 178 |
+
files = [f for f in os.listdir(test_dir) if f.endswith(('.wav', '.mp3', '.m4a'))]
|
| 179 |
+
# Sample intelligently: take every Nth file to get good coverage
|
| 180 |
+
if len(files) > 100:
|
| 181 |
+
step = max(1, len(files) // 100) # Sample ~100 files per category
|
| 182 |
+
files = files[::step]
|
| 183 |
+
for filename in files:
|
| 184 |
+
file_lower = filename.lower()
|
| 185 |
+
if any(keyword in file_lower for keyword in keywords):
|
| 186 |
+
filepath = os.path.join(test_dir, filename)
|
| 187 |
+
test_data[category].append((filepath, label))
|
| 188 |
+
|
| 189 |
+
return test_data
|
| 190 |
+
|
| 191 |
+
def process_audio_enhanced(self, audio, model, chunk_size):
|
| 192 |
+
"""Enhanced audio processing with overlap and better chunking"""
|
| 193 |
+
|
| 194 |
+
chunk_predictions = []
|
| 195 |
+
overlap_ratio = 0.25 # 25% overlap for smoother predictions
|
| 196 |
+
hop_size = int(chunk_size * (1 - overlap_ratio))
|
| 197 |
+
|
| 198 |
+
# Process with overlapping chunks
|
| 199 |
+
for start in range(0, len(audio) - chunk_size + 1, hop_size):
|
| 200 |
+
end = start + chunk_size
|
| 201 |
+
chunk = audio[start:end]
|
| 202 |
+
|
| 203 |
+
if len(chunk) == chunk_size:
|
| 204 |
+
try:
|
| 205 |
+
start_time = time.time()
|
| 206 |
+
prediction = model.process_chunk(chunk.astype(np.float32))
|
| 207 |
+
processing_time = time.time() - start_time
|
| 208 |
+
|
| 209 |
+
chunk_predictions.append({
|
| 210 |
+
'prediction': prediction,
|
| 211 |
+
'position': start / len(audio), # Relative position in file
|
| 212 |
+
'processing_time': processing_time
|
| 213 |
+
})
|
| 214 |
+
except Exception as e:
|
| 215 |
+
print(f" Error processing chunk at {start}: {e}")
|
| 216 |
+
continue
|
| 217 |
+
|
| 218 |
+
return chunk_predictions
|
| 219 |
+
|
| 220 |
+
def aggregate_predictions(self, chunk_predictions, model_name):
|
| 221 |
+
"""Enhanced prediction aggregation using multiple methods"""
|
| 222 |
+
|
| 223 |
+
if not chunk_predictions:
|
| 224 |
+
return 0.0
|
| 225 |
+
|
| 226 |
+
# Extract prediction values
|
| 227 |
+
predictions = [cp['prediction'] for cp in chunk_predictions]
|
| 228 |
+
positions = [cp['position'] for cp in chunk_predictions]
|
| 229 |
+
|
| 230 |
+
if len(predictions) == 1:
|
| 231 |
+
return predictions[0]
|
| 232 |
+
|
| 233 |
+
# Method 1: Weighted average (more weight to middle chunks)
|
| 234 |
+
weights = []
|
| 235 |
+
for pos in positions:
|
| 236 |
+
# Give more weight to middle of file (0.5), less to edges
|
| 237 |
+
distance_from_center = abs(pos - 0.5)
|
| 238 |
+
weight = 1.0 - distance_from_center # Weight between 0.5 and 1.0
|
| 239 |
+
weights.append(weight)
|
| 240 |
+
|
| 241 |
+
weights = np.array(weights)
|
| 242 |
+
weights = weights / np.sum(weights) # Normalize
|
| 243 |
+
weighted_avg = np.average(predictions, weights=weights)
|
| 244 |
+
|
| 245 |
+
# Method 2: Confidence-based filtering
|
| 246 |
+
# Remove outlier predictions that are very different from the median
|
| 247 |
+
median_pred = np.median(predictions)
|
| 248 |
+
filtered_preds = []
|
| 249 |
+
for pred in predictions:
|
| 250 |
+
if abs(pred - median_pred) < 0.3: # Keep predictions within 0.3 of median
|
| 251 |
+
filtered_preds.append(pred)
|
| 252 |
+
|
| 253 |
+
if not filtered_preds:
|
| 254 |
+
filtered_preds = predictions # Fallback to all predictions
|
| 255 |
+
|
| 256 |
+
# Method 3: Model-specific aggregation
|
| 257 |
+
if 'PyTorch' in model_name:
|
| 258 |
+
# For PyTorch: Use maximum prediction (most confident detection)
|
| 259 |
+
# This works well because PyTorch has very low false positive rate
|
| 260 |
+
max_pred = np.max(predictions)
|
| 261 |
+
confidence_filtered_avg = np.mean(filtered_preds)
|
| 262 |
+
|
| 263 |
+
# Combine max and filtered average
|
| 264 |
+
final_pred = 0.7 * max_pred + 0.3 * confidence_filtered_avg
|
| 265 |
+
|
| 266 |
+
else: # CoreML
|
| 267 |
+
# For CoreML: Use more conservative approach
|
| 268 |
+
# Weighted average with confidence filtering
|
| 269 |
+
final_pred = 0.6 * weighted_avg + 0.4 * np.mean(filtered_preds)
|
| 270 |
+
|
| 271 |
+
return final_pred
|
| 272 |
+
|
| 273 |
+
def generate_synthetic_data(self, duration: int = 5, sample_rate: int = 16000) -> Dict[str, List[Tuple[np.ndarray, int]]]:
|
| 274 |
+
"""
|
| 275 |
+
Generate synthetic test data for comprehensive benchmarking
|
| 276 |
+
|
| 277 |
+
Args:
|
| 278 |
+
duration: Duration of each test signal in seconds
|
| 279 |
+
sample_rate: Sample rate for audio generation
|
| 280 |
+
|
| 281 |
+
Returns:
|
| 282 |
+
Dictionary mapping categories to (audio_array, label) tuples
|
| 283 |
+
"""
|
| 284 |
+
synthetic_data = {}
|
| 285 |
+
samples = duration * sample_rate
|
| 286 |
+
|
| 287 |
+
# Generate clean speech-like signals
|
| 288 |
+
clean_speech = []
|
| 289 |
+
for i in range(10):
|
| 290 |
+
# Simulate speech with varying frequency components
|
| 291 |
+
t = np.linspace(0, duration, samples)
|
| 292 |
+
speech = np.sin(2 * np.pi * 150 * t) + 0.5 * np.sin(2 * np.pi * 300 * t)
|
| 293 |
+
speech += 0.3 * np.random.randn(samples) # Add some noise
|
| 294 |
+
clean_speech.append((speech.astype(np.float32), 1))
|
| 295 |
+
|
| 296 |
+
synthetic_data['clean_speech'] = clean_speech
|
| 297 |
+
|
| 298 |
+
# Generate silence
|
| 299 |
+
silence = []
|
| 300 |
+
for i in range(10):
|
| 301 |
+
silence_signal = 0.01 * np.random.randn(samples) # Very quiet noise
|
| 302 |
+
silence.append((silence_signal.astype(np.float32), 0))
|
| 303 |
+
|
| 304 |
+
synthetic_data['silence'] = silence
|
| 305 |
+
|
| 306 |
+
# Generate noise only
|
| 307 |
+
noise_only = []
|
| 308 |
+
for i in range(10):
|
| 309 |
+
noise = 0.5 * np.random.randn(samples) # White noise
|
| 310 |
+
noise_only.append((noise.astype(np.float32), 0))
|
| 311 |
+
|
| 312 |
+
synthetic_data['noise_only'] = noise_only
|
| 313 |
+
|
| 314 |
+
return synthetic_data
|
| 315 |
+
|
| 316 |
+
def benchmark_model(self, model, test_data: Dict, chunk_size: int = 512) -> VADBenchmarkResult:
|
| 317 |
+
"""
|
| 318 |
+
Benchmark a VAD model on test data
|
| 319 |
+
|
| 320 |
+
Args:
|
| 321 |
+
model: VAD model to benchmark
|
| 322 |
+
test_data: Test data dictionary
|
| 323 |
+
chunk_size: Size of audio chunks for processing
|
| 324 |
+
|
| 325 |
+
Returns:
|
| 326 |
+
VADBenchmarkResult with performance metrics
|
| 327 |
+
"""
|
| 328 |
+
all_predictions = []
|
| 329 |
+
all_ground_truth = []
|
| 330 |
+
total_processing_time = 0
|
| 331 |
+
total_chunks = 0
|
| 332 |
+
|
| 333 |
+
model_name = model.__class__.__name__
|
| 334 |
+
|
| 335 |
+
print(f"\nπ Benchmarking {model_name}...")
|
| 336 |
+
|
| 337 |
+
# Start total timing
|
| 338 |
+
benchmark_start_time = time.time()
|
| 339 |
+
|
| 340 |
+
for category, data_list in test_data.items():
|
| 341 |
+
print(f" Testing {category}: {len(data_list)} samples")
|
| 342 |
+
|
| 343 |
+
file_count = 0
|
| 344 |
+
for data_item in data_list:
|
| 345 |
+
file_count += 1
|
| 346 |
+
if file_count % 10 == 0 or file_count == len(data_list):
|
| 347 |
+
print(f" Progress: {file_count}/{len(data_list)} files processed")
|
| 348 |
+
if isinstance(data_item, tuple) and len(data_item) == 2:
|
| 349 |
+
if isinstance(data_item[0], str): # File path
|
| 350 |
+
filepath, label = data_item
|
| 351 |
+
try:
|
| 352 |
+
audio, _ = librosa.load(filepath, sr=16000)
|
| 353 |
+
except Exception as e:
|
| 354 |
+
print(f" Error loading {filepath}: {e}")
|
| 355 |
+
continue
|
| 356 |
+
else: # Audio array
|
| 357 |
+
audio, label = data_item
|
| 358 |
+
else:
|
| 359 |
+
continue
|
| 360 |
+
|
| 361 |
+
# Reset model state if available
|
| 362 |
+
if hasattr(model, 'reset_state'):
|
| 363 |
+
model.reset_state()
|
| 364 |
+
|
| 365 |
+
# Enhanced chunk processing with overlap and better aggregation
|
| 366 |
+
chunk_predictions = self.process_audio_enhanced(audio, model, chunk_size)
|
| 367 |
+
|
| 368 |
+
# Update timing stats from actual measurements
|
| 369 |
+
if chunk_predictions:
|
| 370 |
+
chunk_times = [cp['processing_time'] for cp in chunk_predictions]
|
| 371 |
+
total_processing_time += sum(chunk_times)
|
| 372 |
+
total_chunks += len(chunk_predictions)
|
| 373 |
+
|
| 374 |
+
# Enhanced prediction aggregation
|
| 375 |
+
if chunk_predictions:
|
| 376 |
+
final_prediction = self.aggregate_predictions(chunk_predictions, model_name)
|
| 377 |
+
all_predictions.append(final_prediction)
|
| 378 |
+
all_ground_truth.append(label)
|
| 379 |
+
|
| 380 |
+
# End total timing
|
| 381 |
+
benchmark_end_time = time.time()
|
| 382 |
+
total_benchmark_time = benchmark_end_time - benchmark_start_time
|
| 383 |
+
|
| 384 |
+
# Calculate metrics
|
| 385 |
+
if not all_predictions:
|
| 386 |
+
print(f" β No valid predictions for {model_name}")
|
| 387 |
+
return VADBenchmarkResult(
|
| 388 |
+
model_name=model_name,
|
| 389 |
+
accuracy=0.0, precision=0.0, recall=0.0, f1_score=0.0,
|
| 390 |
+
auc_score=0.0, processing_time=0.0, fps=0.0, total_time_seconds=0.0,
|
| 391 |
+
predictions=[], ground_truth=[]
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
# Use optimal thresholds based on analysis
|
| 395 |
+
if 'CoreML' in model_name:
|
| 396 |
+
threshold = 0.3 # CoreML needs lower threshold
|
| 397 |
+
else: # PyTorch VAD
|
| 398 |
+
threshold = 0.10 # Optimal threshold found through analysis
|
| 399 |
+
|
| 400 |
+
binary_predictions = [1 if p >= threshold else 0 for p in all_predictions]
|
| 401 |
+
|
| 402 |
+
# Calculate metrics
|
| 403 |
+
accuracy = accuracy_score(all_ground_truth, binary_predictions)
|
| 404 |
+
precision, recall, f1, _ = precision_recall_fscore_support(
|
| 405 |
+
all_ground_truth, binary_predictions, average='binary'
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
# Calculate AUC
|
| 409 |
+
try:
|
| 410 |
+
fpr, tpr, _ = roc_curve(all_ground_truth, all_predictions)
|
| 411 |
+
auc_score = auc(fpr, tpr)
|
| 412 |
+
except:
|
| 413 |
+
auc_score = 0.0
|
| 414 |
+
|
| 415 |
+
# Calculate processing speed
|
| 416 |
+
avg_processing_time = total_processing_time / total_chunks if total_chunks > 0 else 0
|
| 417 |
+
fps = (chunk_size / 16000) / avg_processing_time if avg_processing_time > 0 else 0
|
| 418 |
+
|
| 419 |
+
return VADBenchmarkResult(
|
| 420 |
+
model_name=model_name,
|
| 421 |
+
accuracy=accuracy,
|
| 422 |
+
precision=precision,
|
| 423 |
+
recall=recall,
|
| 424 |
+
f1_score=f1,
|
| 425 |
+
auc_score=auc_score,
|
| 426 |
+
processing_time=avg_processing_time,
|
| 427 |
+
fps=fps,
|
| 428 |
+
total_time_seconds=total_benchmark_time,
|
| 429 |
+
predictions=all_predictions,
|
| 430 |
+
ground_truth=all_ground_truth
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
def run_comprehensive_benchmark(self, test_dirs: List[str] = None) -> Dict[str, VADBenchmarkResult]:
|
| 434 |
+
"""
|
| 435 |
+
Run comprehensive benchmark comparing CoreML and PyTorch implementations
|
| 436 |
+
|
| 437 |
+
Args:
|
| 438 |
+
test_dirs: List of directories containing test audio files
|
| 439 |
+
|
| 440 |
+
Returns:
|
| 441 |
+
Dictionary mapping model names to benchmark results
|
| 442 |
+
"""
|
| 443 |
+
print("π Starting Comprehensive VAD Benchmark")
|
| 444 |
+
print("=" * 60)
|
| 445 |
+
|
| 446 |
+
# Default test directories if none provided - include ALL audio directories
|
| 447 |
+
if test_dirs is None:
|
| 448 |
+
test_dirs = [
|
| 449 |
+
"test",
|
| 450 |
+
"VAD_Benchmark/dataset/test_data",
|
| 451 |
+
"VAD_Benchmark/samples",
|
| 452 |
+
"musan/musan/noise/free-sound",
|
| 453 |
+
"musan/musan/speech",
|
| 454 |
+
"musan/musan"
|
| 455 |
+
]
|
| 456 |
+
|
| 457 |
+
# Load test data from all directories
|
| 458 |
+
test_data = {}
|
| 459 |
+
for test_dir in test_dirs:
|
| 460 |
+
dir_data = self.load_test_data(test_dir)
|
| 461 |
+
for category, data in dir_data.items():
|
| 462 |
+
if category not in test_data:
|
| 463 |
+
test_data[category] = []
|
| 464 |
+
test_data[category].extend(data)
|
| 465 |
+
|
| 466 |
+
# Add synthetic data if real data is limited
|
| 467 |
+
synthetic_data = self.generate_synthetic_data()
|
| 468 |
+
for category, data in synthetic_data.items():
|
| 469 |
+
if category not in test_data:
|
| 470 |
+
test_data[category] = []
|
| 471 |
+
test_data[category].extend(data)
|
| 472 |
+
|
| 473 |
+
# Print test data summary
|
| 474 |
+
total_samples = sum(len(data) for data in test_data.values())
|
| 475 |
+
print(f"π Test Data Summary ({total_samples} total samples):")
|
| 476 |
+
for category, data in test_data.items():
|
| 477 |
+
print(f" {category}: {len(data)} samples")
|
| 478 |
+
print()
|
| 479 |
+
|
| 480 |
+
# Initialize models
|
| 481 |
+
models = {}
|
| 482 |
+
|
| 483 |
+
# CoreML model
|
| 484 |
+
if COREML_AVAILABLE:
|
| 485 |
+
try:
|
| 486 |
+
models['CoreML_VAD'] = OptimalCoreMLVAD()
|
| 487 |
+
print("β
CoreML VAD model loaded")
|
| 488 |
+
except Exception as e:
|
| 489 |
+
print(f"β Failed to load CoreML VAD: {e}")
|
| 490 |
+
else:
|
| 491 |
+
print("β CoreML not available - skipping CoreML VAD")
|
| 492 |
+
|
| 493 |
+
# PyTorch model
|
| 494 |
+
if TORCH_AVAILABLE:
|
| 495 |
+
try:
|
| 496 |
+
models['PyTorch_VAD'] = SileroVADPyTorch()
|
| 497 |
+
print("β
PyTorch VAD model loaded")
|
| 498 |
+
except Exception as e:
|
| 499 |
+
print(f"β Failed to load PyTorch VAD: {e}")
|
| 500 |
+
else:
|
| 501 |
+
print("β PyTorch not available - skipping PyTorch VAD")
|
| 502 |
+
|
| 503 |
+
# If no models loaded, create a dummy model for testing
|
| 504 |
+
if not models:
|
| 505 |
+
print("β οΈ No VAD models available - creating dummy model for testing")
|
| 506 |
+
models['Dummy_VAD'] = DummyVAD()
|
| 507 |
+
|
| 508 |
+
print()
|
| 509 |
+
|
| 510 |
+
# Benchmark each model
|
| 511 |
+
results = {}
|
| 512 |
+
for model_name, model in models.items():
|
| 513 |
+
try:
|
| 514 |
+
result = self.benchmark_model(model, test_data)
|
| 515 |
+
results[model_name] = result
|
| 516 |
+
print(f"β
{model_name} benchmarked successfully")
|
| 517 |
+
except Exception as e:
|
| 518 |
+
print(f"β Failed to benchmark {model_name}: {e}")
|
| 519 |
+
|
| 520 |
+
return results
|
| 521 |
+
|
| 522 |
+
def generate_report(self, results: Dict[str, VADBenchmarkResult], output_file: str = "vad_benchmark_report.json"):
|
| 523 |
+
"""
|
| 524 |
+
Generate comprehensive benchmark report
|
| 525 |
+
|
| 526 |
+
Args:
|
| 527 |
+
results: Dictionary mapping model names to benchmark results
|
| 528 |
+
output_file: Output file for the report
|
| 529 |
+
"""
|
| 530 |
+
print(f"\nπ Generating Benchmark Report")
|
| 531 |
+
print("=" * 60)
|
| 532 |
+
|
| 533 |
+
# Display results table
|
| 534 |
+
print(f"{'Model':<15} | {'Accuracy':<8} | {'Precision':<9} | {'Recall':<6} | {'F1':<6} | {'AUC':<6} | {'Total Time (s)':<12}")
|
| 535 |
+
print("-" * 90)
|
| 536 |
+
|
| 537 |
+
for model_name, result in results.items():
|
| 538 |
+
print(f"{model_name:<15} | {result.accuracy:.4f} | {result.precision:.4f} | {result.recall:.4f} | {result.f1_score:.4f} | {result.auc_score:.4f} | {result.total_time_seconds:.2f}")
|
| 539 |
+
|
| 540 |
+
# Find best model and speed comparison
|
| 541 |
+
if results:
|
| 542 |
+
best_model = max(results.items(), key=lambda x: x[1].f1_score)
|
| 543 |
+
print(f"\nπ Best Model: {best_model[0]} (F1: {best_model[1].f1_score:.4f})")
|
| 544 |
+
|
| 545 |
+
# Speed comparison
|
| 546 |
+
if len(results) == 2:
|
| 547 |
+
models = list(results.items())
|
| 548 |
+
model1_name, model1_result = models[0]
|
| 549 |
+
model2_name, model2_result = models[1]
|
| 550 |
+
|
| 551 |
+
if model1_result.total_time_seconds < model2_result.total_time_seconds:
|
| 552 |
+
speedup = model2_result.total_time_seconds / model1_result.total_time_seconds
|
| 553 |
+
print(f"β‘ {model1_name} is {speedup:.1f}x faster than {model2_name}")
|
| 554 |
+
print(f" {model1_name}: {model1_result.total_time_seconds:.2f}s | {model2_name}: {model2_result.total_time_seconds:.2f}s")
|
| 555 |
+
else:
|
| 556 |
+
speedup = model1_result.total_time_seconds / model2_result.total_time_seconds
|
| 557 |
+
print(f"β‘ {model2_name} is {speedup:.1f}x faster than {model1_name}")
|
| 558 |
+
print(f" {model2_name}: {model2_result.total_time_seconds:.2f}s | {model1_name}: {model1_result.total_time_seconds:.2f}s")
|
| 559 |
+
|
| 560 |
+
# Save detailed report
|
| 561 |
+
report_data = {}
|
| 562 |
+
for model_name, result in results.items():
|
| 563 |
+
report_data[model_name] = {
|
| 564 |
+
'accuracy': result.accuracy,
|
| 565 |
+
'precision': result.precision,
|
| 566 |
+
'recall': result.recall,
|
| 567 |
+
'f1_score': result.f1_score,
|
| 568 |
+
'auc_score': result.auc_score,
|
| 569 |
+
'processing_time': result.processing_time,
|
| 570 |
+
'fps': result.fps,
|
| 571 |
+
'total_time_seconds': result.total_time_seconds,
|
| 572 |
+
'predictions': result.predictions,
|
| 573 |
+
'ground_truth': result.ground_truth
|
| 574 |
+
}
|
| 575 |
+
|
| 576 |
+
with open(output_file, 'w') as f:
|
| 577 |
+
json.dump(report_data, f, indent=2)
|
| 578 |
+
|
| 579 |
+
print(f"πΎ Detailed report saved to: {output_file}")
|
| 580 |
+
|
| 581 |
+
# Generate ROC curve plot
|
| 582 |
+
self.plot_roc_curves(results)
|
| 583 |
+
|
| 584 |
+
def plot_roc_curves(self, results: Dict[str, VADBenchmarkResult]):
|
| 585 |
+
"""
|
| 586 |
+
Plot ROC curves for model comparison
|
| 587 |
+
|
| 588 |
+
Args:
|
| 589 |
+
results: Dictionary mapping model names to benchmark results
|
| 590 |
+
"""
|
| 591 |
+
try:
|
| 592 |
+
plt.figure(figsize=(10, 8))
|
| 593 |
+
|
| 594 |
+
plotted_any = False
|
| 595 |
+
for model_name, result in results.items():
|
| 596 |
+
if result.predictions and result.ground_truth:
|
| 597 |
+
try:
|
| 598 |
+
fpr, tpr, _ = roc_curve(result.ground_truth, result.predictions)
|
| 599 |
+
auc_score = auc(fpr, tpr)
|
| 600 |
+
plt.plot(fpr, tpr, label=f'{model_name} (AUC = {auc_score:.3f})')
|
| 601 |
+
plotted_any = True
|
| 602 |
+
except:
|
| 603 |
+
continue
|
| 604 |
+
|
| 605 |
+
if plotted_any:
|
| 606 |
+
plt.plot([0, 1], [0, 1], 'k--', label='Random Classifier')
|
| 607 |
+
plt.xlim([0.0, 1.0])
|
| 608 |
+
plt.ylim([0.0, 1.05])
|
| 609 |
+
plt.xlabel('False Positive Rate')
|
| 610 |
+
plt.ylabel('True Positive Rate')
|
| 611 |
+
plt.title('ROC Curves - VAD Model Comparison')
|
| 612 |
+
plt.legend()
|
| 613 |
+
plt.grid(True, alpha=0.3)
|
| 614 |
+
plt.tight_layout()
|
| 615 |
+
plt.savefig('vad_roc_curves.png', dpi=300, bbox_inches='tight')
|
| 616 |
+
# plt.show() # Skip interactive display
|
| 617 |
+
|
| 618 |
+
print("π ROC curves saved to: vad_roc_curves.png")
|
| 619 |
+
else:
|
| 620 |
+
print("β οΈ No valid results to plot")
|
| 621 |
+
|
| 622 |
+
except Exception as e:
|
| 623 |
+
print(f"β Error creating ROC plot: {e}")
|
| 624 |
+
print("π Skipping ROC curve generation")
|
| 625 |
+
|
| 626 |
+
|
| 627 |
+
def main():
|
| 628 |
+
"""Main function to run VAD benchmark"""
|
| 629 |
+
benchmark = VADBenchmarkSuite()
|
| 630 |
+
|
| 631 |
+
# Run comprehensive benchmark
|
| 632 |
+
results = benchmark.run_comprehensive_benchmark()
|
| 633 |
+
|
| 634 |
+
# Generate report
|
| 635 |
+
benchmark.generate_report(results)
|
| 636 |
+
|
| 637 |
+
print(f"\nπ VAD Benchmark Complete!")
|
| 638 |
+
|
| 639 |
+
|
| 640 |
+
if __name__ == "__main__":
|
| 641 |
+
main()
|