Spaces:
Sleeping
Sleeping
Update prediction.py
Browse files- prediction.py +34 -25
prediction.py
CHANGED
|
@@ -21,52 +21,60 @@ def aggregate_predictions(spectrogram_list, model, threshold=0.5):
|
|
| 21 |
return final_label, mean_prob, segment_probs
|
| 22 |
|
| 23 |
def predict_eeg_recording(edf_path, model_name='2DCNN', threshold=0.5):
|
| 24 |
-
print(f"\n=== [DEBUG] Loading model: {model_name} ===")
|
| 25 |
-
|
| 26 |
if model_name == '2DCNN':
|
| 27 |
model = tf.keras.models.load_model('model1_2dcnn.h5')
|
| 28 |
channels = ["EEG FP1-REF", "EEG FP2-REF", "EEG F3-REF", "EEG F4-REF", "EEG C3-REF"]
|
| 29 |
-
|
| 30 |
preprocessed_df = preprocess_eeg_file(edf_path, fmin=1.0, fmax=45.0, segment_lenght=5, overlap=2, desired=channels)
|
| 31 |
-
print(f"[2DCNN] Nombre de segments : {len(preprocessed_df)}")
|
| 32 |
|
| 33 |
if preprocessed_df is None or preprocessed_df.empty:
|
| 34 |
raise ValueError("EEG file could not be preprocessed or no valid segments found.")
|
| 35 |
-
|
| 36 |
spectrogram_list = preprocessed_df.apply(
|
| 37 |
lambda row: convert_epoch_to_spectrogram(row, channels, fs=250, nperseg=128, noverlap=64), axis=1
|
| 38 |
).tolist()
|
| 39 |
|
| 40 |
-
print(f"[2DCNN] Shape segment 0 : {spectrogram_list[0].shape}")
|
| 41 |
return aggregate_predictions(spectrogram_list, model, threshold)
|
| 42 |
|
| 43 |
elif model_name == 'EEGNet':
|
| 44 |
loaded = joblib.load("eegnet_model.joblib")
|
| 45 |
state_dict = loaded["model_state_dict"]
|
| 46 |
-
|
| 47 |
model = EEGNet(n_channels=21, n_samples=1250, num_classes=2)
|
| 48 |
model.load_state_dict(state_dict)
|
| 49 |
-
|
| 50 |
-
channels = [
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
if preprocessed_df is None or preprocessed_df.empty:
|
| 55 |
raise ValueError("EEG file could not be preprocessed or no valid segments found.")
|
| 56 |
-
|
| 57 |
timeseries_list = preprocessed_df.apply(
|
| 58 |
lambda row: convert_epoch_to_timeseries(row, channels), axis=1
|
| 59 |
).tolist()
|
| 60 |
|
| 61 |
-
print(f"[EEGNet] Shape segment 0 : {timeseries_list[0].shape}")
|
| 62 |
return aggregate_predictions_EEGNET(timeseries_list, model, threshold)
|
| 63 |
|
| 64 |
elif model_name == 'EpilepsyNet':
|
| 65 |
-
print("[EpilepsyNet] Chargement du fichier EDF brut avec MNE...")
|
| 66 |
raw = mne.io.read_raw_edf(edf_path, preload=True, verbose='ERROR')
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
X = process_raw_files(
|
| 72 |
raw_file=raw,
|
|
@@ -76,25 +84,26 @@ def predict_eeg_recording(edf_path, model_name='2DCNN', threshold=0.5):
|
|
| 76 |
random_state=parameters['random_state']
|
| 77 |
)
|
| 78 |
|
| 79 |
-
print(f"[EpilepsyNet] Shape X : {X.shape}")
|
| 80 |
-
|
| 81 |
X_std = standardize_data(X)
|
| 82 |
corr_matrix = compute_correlation_matrix(X_std)
|
| 83 |
upper_triangle_matrix = extract_upper_triangle(corr_matrix)
|
| 84 |
-
print(f"[EpilepsyNet] Shape upper_triangle_matrix : {upper_triangle_matrix.shape}")
|
| 85 |
|
| 86 |
-
X_tensor = torch.tensor(upper_triangle_matrix, dtype=torch.float32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
model = TimeSeriesAttentionClassifier(input_dim
|
| 89 |
model.load_state_dict(torch.load('EpilepsyNet.pth'))
|
| 90 |
model.eval()
|
| 91 |
|
| 92 |
outputs, _ = model(X_tensor)
|
| 93 |
predicted = (outputs >= 0.5).float()
|
| 94 |
-
|
| 95 |
-
print(f"[EpilepsyNet] Probabilité brute : {outputs.squeeze().item()}")
|
| 96 |
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
def convert_epoch_to_timeseries(epoch_row, channels):
|
| 100 |
ts_list = []
|
|
|
|
| 21 |
return final_label, mean_prob, segment_probs
|
| 22 |
|
| 23 |
def predict_eeg_recording(edf_path, model_name='2DCNN', threshold=0.5):
|
|
|
|
|
|
|
| 24 |
if model_name == '2DCNN':
|
| 25 |
model = tf.keras.models.load_model('model1_2dcnn.h5')
|
| 26 |
channels = ["EEG FP1-REF", "EEG FP2-REF", "EEG F3-REF", "EEG F4-REF", "EEG C3-REF"]
|
|
|
|
| 27 |
preprocessed_df = preprocess_eeg_file(edf_path, fmin=1.0, fmax=45.0, segment_lenght=5, overlap=2, desired=channels)
|
|
|
|
| 28 |
|
| 29 |
if preprocessed_df is None or preprocessed_df.empty:
|
| 30 |
raise ValueError("EEG file could not be preprocessed or no valid segments found.")
|
| 31 |
+
|
| 32 |
spectrogram_list = preprocessed_df.apply(
|
| 33 |
lambda row: convert_epoch_to_spectrogram(row, channels, fs=250, nperseg=128, noverlap=64), axis=1
|
| 34 |
).tolist()
|
| 35 |
|
|
|
|
| 36 |
return aggregate_predictions(spectrogram_list, model, threshold)
|
| 37 |
|
| 38 |
elif model_name == 'EEGNet':
|
| 39 |
loaded = joblib.load("eegnet_model.joblib")
|
| 40 |
state_dict = loaded["model_state_dict"]
|
| 41 |
+
|
| 42 |
model = EEGNet(n_channels=21, n_samples=1250, num_classes=2)
|
| 43 |
model.load_state_dict(state_dict)
|
| 44 |
+
|
| 45 |
+
channels = [
|
| 46 |
+
'EEG FP1-REF', 'EEG FP2-REF', 'EEG F3-REF', 'EEG F4-REF', 'EEG C3-REF', 'EEG C4-REF',
|
| 47 |
+
'EEG P3-REF', 'EEG P4-REF', 'EEG O1-REF', 'EEG O2-REF', 'EEG F7-REF', 'EEG F8-REF',
|
| 48 |
+
'EEG T3-REF', 'EEG T4-REF', 'EEG T5-REF', 'EEG T6-REF', 'EEG FZ-REF', 'EEG CZ-REF',
|
| 49 |
+
'EEG PZ-REF', 'EEG ROC-REF', 'EEG LOC-REF'
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
preprocessed_df = preprocess_eeg_file(
|
| 53 |
+
edf_path, fmin=1.0, fmax=45.0, segment_lenght=5, overlap=0, desired=channels
|
| 54 |
+
)
|
| 55 |
|
| 56 |
if preprocessed_df is None or preprocessed_df.empty:
|
| 57 |
raise ValueError("EEG file could not be preprocessed or no valid segments found.")
|
| 58 |
+
|
| 59 |
timeseries_list = preprocessed_df.apply(
|
| 60 |
lambda row: convert_epoch_to_timeseries(row, channels), axis=1
|
| 61 |
).tolist()
|
| 62 |
|
|
|
|
| 63 |
return aggregate_predictions_EEGNET(timeseries_list, model, threshold)
|
| 64 |
|
| 65 |
elif model_name == 'EpilepsyNet':
|
|
|
|
| 66 |
raw = mne.io.read_raw_edf(edf_path, preload=True, verbose='ERROR')
|
| 67 |
+
eeg_cols = ['EEG FP1', 'EEG FP2', 'EEG F3', 'EEG F4', 'EEG C3', 'EEG C4', 'EEG P3', 'EEG P4',
|
| 68 |
+
'EEG O1', 'EEG O2', 'EEG F7', 'EEG F8', 'EEG T3', 'EEG T4', 'EEG T5', 'EEG T6',
|
| 69 |
+
'EEG T1', 'EEG T2', 'EEG FZ', 'EEG CZ', 'EEG PZ']
|
| 70 |
+
|
| 71 |
+
parameters = {
|
| 72 |
+
'eeg_cols': eeg_cols,
|
| 73 |
+
'segment_duration': 60.0,
|
| 74 |
+
'n_segments_per_file': 12,
|
| 75 |
+
'samples_per_segment': 1250,
|
| 76 |
+
'random_state': 42
|
| 77 |
+
}
|
| 78 |
|
| 79 |
X = process_raw_files(
|
| 80 |
raw_file=raw,
|
|
|
|
| 84 |
random_state=parameters['random_state']
|
| 85 |
)
|
| 86 |
|
|
|
|
|
|
|
| 87 |
X_std = standardize_data(X)
|
| 88 |
corr_matrix = compute_correlation_matrix(X_std)
|
| 89 |
upper_triangle_matrix = extract_upper_triangle(corr_matrix)
|
|
|
|
| 90 |
|
| 91 |
+
X_tensor = torch.tensor(upper_triangle_matrix, dtype=torch.float32)
|
| 92 |
+
X_tensor = X_tensor.unsqueeze(0)
|
| 93 |
+
|
| 94 |
+
input_dim = 210
|
| 95 |
+
embed_dim = 256
|
| 96 |
+
num_heads = 16
|
| 97 |
|
| 98 |
+
model = TimeSeriesAttentionClassifier(input_dim, embed_dim, num_heads)
|
| 99 |
model.load_state_dict(torch.load('EpilepsyNet.pth'))
|
| 100 |
model.eval()
|
| 101 |
|
| 102 |
outputs, _ = model(X_tensor)
|
| 103 |
predicted = (outputs >= 0.5).float()
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
prob = outputs.float().squeeze().item()
|
| 106 |
+
return int(predicted), prob, [prob]
|
| 107 |
|
| 108 |
def convert_epoch_to_timeseries(epoch_row, channels):
|
| 109 |
ts_list = []
|