-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_4class_classifier.py
More file actions
380 lines (307 loc) · 13 KB
/
Copy pathtrain_4class_classifier.py
File metadata and controls
380 lines (307 loc) · 13 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Train 4-Class Meteor Classifier: Overdense, Underdense, Aircraft, Noise
Handles class imbalance with balanced weights
"""
import os
import numpy as np
import pandas as pd
from scipy.io import wavfile
from scipy.signal import spectrogram
from scipy.stats import kurtosis, skew
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.metrics import classification_report, confusion_matrix
import pickle
# ----- CONFIGURATION -----
TRAINING_CSV = "/Users/ninatormann/Desktop/10mMixed/simple_classification.csv"
AUDIO_DIR = "/Users/ninatormann/Desktop/10mMixed"
OUTPUT_DIR = "/Users/ninatormann/Desktop/10mMixed"
# Model output
MODEL_FILE = "meteor_4class_classifier.pkl"
FEATURES_CSV = "training_features_4class.csv"
# Feature extraction settings
FREQ_MIN = 500
FREQ_MAX = 2000
WINDOW_DURATION = 0.072
class MeteorFeatureExtractor:
"""Extract features from audio files."""
def __init__(self, freq_min=500, freq_max=2000, window_duration=0.072):
self.freq_min = freq_min
self.freq_max = freq_max
self.window_duration = window_duration
def compute_amplitude_timeseries(self, data, fs):
"""Compute MSK144 amplitude over time."""
window_size = int(fs * self.window_duration)
num_windows = len(data) // window_size
times = []
amplitudes = []
for i in range(num_windows):
start = i * window_size
end = start + window_size
segment = data[start:end]
fft_result = np.fft.rfft(segment)
freqs = np.fft.rfftfreq(len(segment), d=1/fs)
amps = np.abs(fft_result)
mask = (freqs >= self.freq_min) & (freqs <= self.freq_max)
avg_amp = np.mean(amps[mask])
times.append(i * self.window_duration)
amplitudes.append(avg_amp)
return np.array(times), np.array(amplitudes)
def extract_features(self, filepath):
"""Extract comprehensive features from a WAV file."""
try:
# Read audio file
fs, data = wavfile.read(filepath)
if data.ndim > 1:
data = np.mean(data, axis=1).astype(data.dtype)
# Compute amplitude timeseries
times, amplitudes = self.compute_amplitude_timeseries(data, fs)
# Basic statistics
amp_mean = np.mean(amplitudes)
amp_median = np.median(amplitudes)
amp_std = np.std(amplitudes)
amp_max = np.max(amplitudes)
amp_min = np.min(amplitudes)
amp_range = amp_max - amp_min
# Peak detection
threshold = amp_median * 1.35
above_threshold = amplitudes > threshold
num_peaks = np.sum(np.diff(above_threshold.astype(int)) == 1)
# Time above threshold
time_above_threshold = np.sum(above_threshold) * self.window_duration
percent_above_threshold = np.sum(above_threshold) / len(amplitudes) * 100
# Peak characteristics
if np.any(above_threshold):
peak_height_ratio = (amp_max - amp_median) / amp_median
peak_indices = np.where(above_threshold)[0]
peak_duration = len(peak_indices) * self.window_duration
# Rise and fall time
first_peak_idx = peak_indices[0]
last_peak_idx = peak_indices[-1]
if first_peak_idx > 0:
rise_time = first_peak_idx * self.window_duration
rise_rate = (amp_max - amp_median) / (rise_time + 1e-10)
else:
rise_time = 0
rise_rate = 0
if last_peak_idx < len(amplitudes) - 1:
fall_time = (len(amplitudes) - last_peak_idx) * self.window_duration
fall_rate = (amp_max - amp_median) / (fall_time + 1e-10)
else:
fall_time = 0
fall_rate = 0
else:
peak_height_ratio = 0
peak_duration = 0
rise_time = 0
rise_rate = 0
fall_time = 0
fall_rate = 0
# Variability features
coefficient_of_variation = amp_std / amp_mean * 100 if amp_mean > 0 else 0
# Distribution shape
amp_skewness = skew(amplitudes)
amp_kurtosis = kurtosis(amplitudes)
# Temporal patterns
amp_diff = np.diff(amplitudes)
max_rise = np.max(amp_diff) if len(amp_diff) > 0 else 0
max_fall = abs(np.min(amp_diff)) if len(amp_diff) > 0 else 0
avg_change = np.mean(np.abs(amp_diff)) if len(amp_diff) > 0 else 0
# Spectral features
f, t, Sxx = spectrogram(data, fs=fs, nperseg=2048, noverlap=1024)
freq_mask = (f >= self.freq_min) & (f <= self.freq_max)
power_db = 10 * np.log10(Sxx[freq_mask, :] + 1e-12)
spectral_mean = np.mean(power_db)
spectral_max = np.max(power_db)
spectral_std = np.std(power_db)
# Compile features
features = {
'amp_mean': amp_mean,
'amp_median': amp_median,
'amp_std': amp_std,
'amp_max': amp_max,
'amp_range': amp_range,
'coefficient_of_variation': coefficient_of_variation,
'num_peaks': num_peaks,
'peak_height_ratio': peak_height_ratio,
'peak_duration': peak_duration,
'time_above_threshold': time_above_threshold,
'percent_above_threshold': percent_above_threshold,
'rise_time': rise_time,
'fall_time': fall_time,
'rise_rate': rise_rate,
'fall_rate': fall_rate,
'max_rise': max_rise,
'max_fall': max_fall,
'avg_change': avg_change,
'amp_skewness': amp_skewness,
'amp_kurtosis': amp_kurtosis,
'spectral_mean': spectral_mean,
'spectral_max': spectral_max,
'spectral_std': spectral_std,
}
return features
except Exception as e:
print(f"Error processing {filepath}: {e}")
return None
def main():
print("="*70)
print("TRAINING 4-CLASS METEOR CLASSIFIER")
print("Classes: Overdense, Underdense, Aircraft, Noise")
print("="*70)
# Load training data
if not os.path.exists(TRAINING_CSV):
print(f"\n❌ Error: Training CSV not found: {TRAINING_CSV}")
return
print(f"\n📊 Loading training data from: {TRAINING_CSV}")
df = pd.read_csv(TRAINING_CSV)
print(f"✅ Loaded {len(df)} labeled files")
# Show class distribution
print(f"\n📈 Class Distribution:")
counts = df['classification'].value_counts()
for cls, count in counts.items():
pct = count / len(df) * 100
print(f" {cls.capitalize():15s}: {count:4d} ({pct:5.1f}%)")
# Initialize feature extractor
extractor = MeteorFeatureExtractor()
# Extract features for all files
print(f"\n🔍 Extracting features from audio files...")
features_list = []
labels_list = []
filenames_list = []
for idx, row in df.iterrows():
if (idx + 1) % 100 == 0:
print(f" Progress: {idx + 1}/{len(df)}")
filepath = os.path.join(AUDIO_DIR, row['filename'])
if not os.path.exists(filepath):
print(f" ⚠️ File not found: {row['filename']}")
continue
features = extractor.extract_features(filepath)
if features is not None:
features_list.append(features)
labels_list.append(row['classification'])
filenames_list.append(row['filename'])
print(f"\n✅ Extracted features from {len(features_list)} files")
# Create features dataframe
features_df = pd.DataFrame(features_list)
features_df['filename'] = filenames_list
features_df['classification'] = labels_list
# Save features
os.makedirs(OUTPUT_DIR, exist_ok=True)
features_path = os.path.join(OUTPUT_DIR, FEATURES_CSV)
features_df.to_csv(features_path, index=False)
print(f"💾 Saved features to: {features_path}")
# Prepare data for training
feature_columns = [col for col in features_df.columns if col not in ['filename', 'classification']]
X = features_df[feature_columns].values
y = features_df['classification'].values
print(f"\n🔧 Preparing ML model...")
print(f" Features: {len(feature_columns)}")
print(f" Samples: {len(X)}")
# Encode labels
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)
print(f" Classes: {list(label_encoder.classes_)}")
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y_encoded, test_size=0.25, random_state=42, stratify=y_encoded
)
print(f"\n📊 Train/Test Split:")
print(f" Training: {len(X_train)} samples")
print(f" Testing: {len(X_test)} samples")
# Standardize features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Train Random Forest with class balancing
print(f"\n🤖 Training Random Forest Classifier...")
print(f" Using class_weight='balanced' to handle imbalanced data")
model = RandomForestClassifier(
n_estimators=100,
max_depth=15,
min_samples_split=5,
min_samples_leaf=2,
class_weight='balanced', # CRITICAL for handling imbalance
random_state=42,
n_jobs=-1
)
model.fit(X_train_scaled, y_train)
print(f"✅ Model trained!")
# Evaluate on test set
print(f"\n📊 Test Set Performance:")
y_pred = model.predict(X_test_scaled)
# Overall accuracy
accuracy = np.mean(y_pred == y_test)
print(f" Overall Accuracy: {accuracy:.1%}")
# Per-class metrics
print(f"\n Detailed Classification Report:")
report = classification_report(
y_test, y_pred,
target_names=label_encoder.classes_,
digits=3
)
print(report)
# Confusion matrix
print(f"\n Confusion Matrix:")
cm = confusion_matrix(y_test, y_pred)
print(f" {'':15s}", end="")
for cls in label_encoder.classes_:
print(f"{cls[:10]:>10s}", end="")
print()
for i, cls in enumerate(label_encoder.classes_):
print(f" {cls:15s}", end="")
for j in range(len(label_encoder.classes_)):
print(f"{cm[i,j]:10d}", end="")
print()
# Cross-validation
print(f"\n🔄 5-Fold Cross-Validation:")
cv_scores = cross_val_score(model, X_train_scaled, y_train, cv=5)
print(f" CV Scores: {[f'{s:.1%}' for s in cv_scores]}")
print(f" Mean: {cv_scores.mean():.1%} ± {cv_scores.std():.1%}")
# Feature importance
print(f"\n🔍 Top 10 Most Important Features:")
importances = model.feature_importances_
indices = np.argsort(importances)[::-1]
for i in range(min(10, len(feature_columns))):
idx = indices[i]
print(f" {i+1}. {feature_columns[idx]:30s} : {importances[idx]:.4f}")
# Package model
print(f"\n📦 Packaging model...")
model_package = {
'model': model,
'scaler': scaler,
'label_encoder': label_encoder,
'feature_columns': feature_columns,
'training_info': {
'n_samples': len(X),
'n_features': len(feature_columns),
'classes': list(label_encoder.classes_),
'test_accuracy': accuracy,
'cv_mean': cv_scores.mean(),
'cv_std': cv_scores.std()
}
}
# Save model
model_path = os.path.join(OUTPUT_DIR, MODEL_FILE)
with open(model_path, 'wb') as f:
pickle.dump(model_package, f)
print(f"💾 Model saved to: {model_path}")
# Final summary
print(f"\n{'='*70}")
print("TRAINING COMPLETE!")
print(f"{'='*70}")
print(f"\n✅ 4-Class Classifier Ready:")
print(f" 📊 Trained on {len(X)} samples")
print(f" 🎯 Test Accuracy: {accuracy:.1%}")
print(f" 🔄 CV Score: {cv_scores.mean():.1%}")
print(f"\n💡 Model can now classify:")
print(f" • Overdense meteors")
print(f" • Underdense meteors")
print(f" • Aircraft scatter")
print(f" • Noise/interference")
print(f"\n📁 Next step: Use predict_4class.py to classify new files!")
if __name__ == "__main__":
main()