-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_voice_model.py
More file actions
272 lines (222 loc) · 9.52 KB
/
Copy pathtrain_voice_model.py
File metadata and controls
272 lines (222 loc) · 9.52 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import joblib
import os
from audio_features import VoiceFeatureExtractor
class VoiceModelTrainer:
"""
Trains voice-based Parkinson's detection models.
Supports both ML (Random Forest, SVM) and DL (Neural Network) approaches.
"""
def __init__(self, model_type='neural_network'):
"""
model_type: 'random_forest', 'svm', or 'neural_network'
"""
self.model_type = model_type
self.model = None
self.scaler = StandardScaler()
self.feature_extractor = VoiceFeatureExtractor()
def prepare_dataset(self, audio_folder, labels_csv=None):
"""
Prepare dataset from audio files.
audio_folder structure:
audio_folder/
healthy/
audio1.wav
audio2.wav
parkinson/
audio1.wav
audio2.wav
OR provide labels_csv with columns: filename, label (0=healthy, 1=parkinson)
"""
features_list = []
labels_list = []
if labels_csv:
# Load from CSV
df = pd.read_csv(labels_csv)
for idx, row in df.iterrows():
audio_path = os.path.join(audio_folder, row['filename'])
if os.path.exists(audio_path):
print(f"Processing: {row['filename']}")
features = self.feature_extractor.extract_all_features(audio_path)
if features:
feature_array = self.feature_extractor.features_to_array(features)
features_list.append(feature_array[0])
labels_list.append(row['label'])
else:
# Load from folder structure
for class_name in ['healthy', 'parkinson']:
class_folder = os.path.join(audio_folder, class_name)
label = 0 if class_name == 'healthy' else 1
if os.path.exists(class_folder):
for audio_file in os.listdir(class_folder):
if audio_file.endswith(('.wav', '.mp3', '.ogg')):
audio_path = os.path.join(class_folder, audio_file)
print(f"Processing: {audio_file}")
features = self.feature_extractor.extract_all_features(audio_path)
if features:
feature_array = self.feature_extractor.features_to_array(features)
features_list.append(feature_array[0])
labels_list.append(label)
X = np.array(features_list)
y = np.array(labels_list)
print(f"\n✅ Dataset prepared: {X.shape[0]} samples, {X.shape[1]} features")
print(f" Healthy: {np.sum(y == 0)}, Parkinson: {np.sum(y == 1)}")
return X, y
def train(self, X, y, test_size=0.2, save_path='models/voice_model'):
"""Train the selected model"""
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=42, stratify=y
)
# Scale features
X_train_scaled = self.scaler.fit_transform(X_train)
X_test_scaled = self.scaler.transform(X_test)
print(f"\n🔥 Training {self.model_type} model...")
if self.model_type == 'random_forest':
self.model = self._train_random_forest(X_train_scaled, y_train)
elif self.model_type == 'svm':
self.model = self._train_svm(X_train_scaled, y_train)
elif self.model_type == 'neural_network':
self.model = self._train_neural_network(
X_train_scaled, y_train,
X_test_scaled, y_test
)
# Evaluate
y_pred = self.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"\n✅ Model trained successfully!")
print(f"📊 Test Accuracy: {accuracy * 100:.2f}%")
print(f"\n📋 Classification Report:")
print(classification_report(y_test, y_pred,
target_names=['Healthy', 'Parkinson']))
# Save model
self._save_model(save_path)
return accuracy
def _train_random_forest(self, X_train, y_train):
"""Train Random Forest classifier"""
model = RandomForestClassifier(
n_estimators=200,
max_depth=20,
min_samples_split=5,
min_samples_leaf=2,
random_state=42,
class_weight='balanced'
)
model.fit(X_train, y_train)
return model
def _train_svm(self, X_train, y_train):
"""Train SVM classifier"""
model = SVC(
kernel='rbf',
C=10,
gamma='scale',
probability=True,
random_state=42,
class_weight='balanced'
)
model.fit(X_train, y_train)
return model
def _train_neural_network(self, X_train, y_train, X_val, y_val):
"""Train Neural Network"""
model = keras.Sequential([
layers.Input(shape=(X_train.shape[1],)),
layers.Dense(128, activation='relu'),
layers.Dropout(0.3),
layers.BatchNormalization(),
layers.Dense(64, activation='relu'),
layers.Dropout(0.3),
layers.BatchNormalization(),
layers.Dense(32, activation='relu'),
layers.Dropout(0.2),
layers.Dense(1, activation='sigmoid')
])
model.compile(
optimizer=keras.optimizers.Adam(learning_rate=0.001),
loss='binary_crossentropy',
metrics=['accuracy']
)
early_stop = keras.callbacks.EarlyStopping(
monitor='val_loss',
patience=15,
restore_best_weights=True
)
history = model.fit(
X_train, y_train,
validation_data=(X_val, y_val),
epochs=100,
batch_size=16,
callbacks=[early_stop],
verbose=1
)
return model
def predict(self, X):
"""Make predictions"""
X_scaled = self.scaler.transform(X)
if self.model_type == 'neural_network':
predictions = (self.model.predict(X_scaled) > 0.5).astype(int).flatten()
else:
predictions = self.model.predict(X_scaled)
return predictions
def predict_proba(self, X):
"""Get prediction probabilities"""
X_scaled = self.scaler.transform(X)
if self.model_type == 'neural_network':
proba = self.model.predict(X_scaled).flatten()
else:
proba = self.model.predict_proba(X_scaled)[:, 1]
return proba
def _save_model(self, save_path):
"""Save model and scaler"""
os.makedirs(save_path, exist_ok=True)
# Save scaler
joblib.dump(self.scaler, os.path.join(save_path, 'scaler.pkl'))
# Save model
if self.model_type == 'neural_network':
self.model.save(os.path.join(save_path, 'voice_model.h5'))
else:
joblib.dump(self.model, os.path.join(save_path, 'voice_model.pkl'))
# Save model type
with open(os.path.join(save_path, 'model_type.txt'), 'w') as f:
f.write(self.model_type)
print(f"\n💾 Model saved to: {save_path}")
def load_model(self, save_path):
"""Load saved model"""
# Load model type
with open(os.path.join(save_path, 'model_type.txt'), 'r') as f:
self.model_type = f.read().strip()
# Load scaler
self.scaler = joblib.load(os.path.join(save_path, 'scaler.pkl'))
# Load model
if self.model_type == 'neural_network':
self.model = keras.models.load_model(
os.path.join(save_path, 'voice_model.h5')
)
else:
self.model = joblib.load(os.path.join(save_path, 'voice_model.pkl'))
print(f"✅ Model loaded from: {save_path}")
# Example usage
if __name__ == "__main__":
print("="*60)
print("Voice-Based Parkinson's Detection - Model Training")
print("="*60)
# Initialize trainer
trainer = VoiceModelTrainer(model_type='neural_network')
# Prepare dataset
# Option 1: From folder structure
X, y = trainer.prepare_dataset('voice_dataset/')
# Option 2: From CSV
# X, y = trainer.prepare_dataset('voice_data/', 'labels.csv')
# Train model
if len(X) > 0:
trainer.train(X, y, save_path='models/voice_model')
else:
print("❌ No data found. Please prepare your dataset first.")