Spaces:
Sleeping
Sleeping
Upload inference.py with huggingface_hub
Browse files- inference.py +7 -4
inference.py
CHANGED
|
@@ -629,7 +629,7 @@ def _load_gru_model():
|
|
| 629 |
|
| 630 |
class HeartSoundGRU(nn.Module):
|
| 631 |
def __init__(self, input_dim=129, hidden_dim=64, num_layers=2,
|
| 632 |
-
num_classes=
|
| 633 |
super().__init__()
|
| 634 |
self.input_norm = nn.LayerNorm(input_dim)
|
| 635 |
self.gru = nn.GRU(input_dim, hidden_dim, num_layers,
|
|
@@ -677,6 +677,7 @@ def predict_gru(y, sr):
|
|
| 677 |
N_FFT_G, HOP_G, CLIP_SEC = 256, 64, 5
|
| 678 |
target_len = GRU_SR * CLIP_SEC
|
| 679 |
clips = [y_4k[s:s+target_len] for s in range(0, len(y_4k)-target_len+1, target_len)] if len(y_4k) >= target_len else [np.pad(y_4k, (0, target_len-len(y_4k)))]
|
|
|
|
| 680 |
probs = []
|
| 681 |
for clip in clips:
|
| 682 |
S = np.abs(librosa.stft(clip, n_fft=N_FFT_G, hop_length=HOP_G)) ** 2
|
|
@@ -688,10 +689,12 @@ def predict_gru(y, sr):
|
|
| 688 |
probs.append(torch.softmax(_gru_model(t), 1)[0].numpy())
|
| 689 |
avg = np.mean(probs, 0)
|
| 690 |
pred = int(np.argmax(avg))
|
|
|
|
|
|
|
| 691 |
return {
|
| 692 |
-
"label":
|
| 693 |
-
"is_disease":
|
| 694 |
-
"all_classes": [{"label":
|
| 695 |
}
|
| 696 |
|
| 697 |
|
|
|
|
| 629 |
|
| 630 |
class HeartSoundGRU(nn.Module):
|
| 631 |
def __init__(self, input_dim=129, hidden_dim=64, num_layers=2,
|
| 632 |
+
num_classes=2, dropout=0.4):
|
| 633 |
super().__init__()
|
| 634 |
self.input_norm = nn.LayerNorm(input_dim)
|
| 635 |
self.gru = nn.GRU(input_dim, hidden_dim, num_layers,
|
|
|
|
| 677 |
N_FFT_G, HOP_G, CLIP_SEC = 256, 64, 5
|
| 678 |
target_len = GRU_SR * CLIP_SEC
|
| 679 |
clips = [y_4k[s:s+target_len] for s in range(0, len(y_4k)-target_len+1, target_len)] if len(y_4k) >= target_len else [np.pad(y_4k, (0, target_len-len(y_4k)))]
|
| 680 |
+
GRU_BINARY_NAMES = ["Normal", "Murmur"]
|
| 681 |
probs = []
|
| 682 |
for clip in clips:
|
| 683 |
S = np.abs(librosa.stft(clip, n_fft=N_FFT_G, hop_length=HOP_G)) ** 2
|
|
|
|
| 689 |
probs.append(torch.softmax(_gru_model(t), 1)[0].numpy())
|
| 690 |
avg = np.mean(probs, 0)
|
| 691 |
pred = int(np.argmax(avg))
|
| 692 |
+
is_murmur = pred == 1
|
| 693 |
+
label = GRU_BINARY_NAMES[pred]
|
| 694 |
return {
|
| 695 |
+
"label": label, "confidence": round(float(avg[pred]), 4),
|
| 696 |
+
"is_disease": is_murmur, "method": "Bi-GRU Binary (McDonald et al., Cambridge 2024)",
|
| 697 |
+
"all_classes": [{"label": GRU_BINARY_NAMES[i], "probability": round(float(avg[i]), 4)} for i in range(2)],
|
| 698 |
}
|
| 699 |
|
| 700 |
|