-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_audio_classification_lstm.py
79 lines (58 loc) · 2.93 KB
/
5_audio_classification_lstm.py
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
import pandas as pd
from sklearn.metrics import classification_report
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.utils import to_categorical
import os
# Load the training data from the Excel file
train_data = pd.read_excel("D:/MCA/4th sem/SER3/output/Actor_01/train_test_data.xlsx", sheet_name='Training')
# Encode the target variable
label_encoder = LabelEncoder()
train_data['Emotion'] = label_encoder.fit_transform(train_data['Emotion'])
# Store the mapping of encoded labels to original labels
label_mapping = dict(zip(range(len(label_encoder.classes_)), label_encoder.classes_))
# Separate features (MFCCs) and target variable (Emotion)
X_train = train_data.drop(columns=['Audio File', 'Emotion'])
y_train = train_data['Emotion']
# Reshape X_train to 3D tensor for LSTM input
X_train_lstm = X_train.values.reshape(X_train.shape[0], X_train.shape[1], 1)
# Convert target variable to categorical
y_train_categorical = to_categorical(y_train)
# Define the LSTM model
model = Sequential()
model.add(LSTM(128, input_shape=(X_train_lstm.shape[1], X_train_lstm.shape[2])))
model.add(Dense(len(label_encoder.classes_), activation='softmax'))
# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model to the training data
model.fit(X_train_lstm, y_train_categorical, epochs=10, batch_size=32)
# Load the testing data from the Excel file
test_data = pd.read_excel("D:/MCA/4th sem/SER3/output/Actor_01/train_test_data.xlsx", sheet_name='Testing')
# Encode the target variable
test_data['Emotion'] = label_encoder.transform(test_data['Emotion'])
# Separate features (MFCCs) and target variable (Emotion)
X_test = test_data.drop(columns=['Audio File', 'Emotion'])
y_test = test_data['Emotion']
# Reshape X_test to 3D tensor for LSTM input
X_test_lstm = X_test.values.reshape(X_test.shape[0], X_test.shape[1], 1)
# Convert target variable to categorical
y_test_categorical = to_categorical(y_test)
# Predict probabilities for each class for the testing data
y_pred_probabilities = model.predict(X_test_lstm)
# Get the predicted class labels
y_pred = y_pred_probabilities.argmax(axis=1)
# Convert the predicted and true labels back to their original emotion names
y_pred_emotions = [label_mapping[label] for label in y_pred]
y_true_emotions = [label_mapping[label] for label in y_test]
# Evaluate the classifier
report = classification_report(y_true_emotions, y_pred_emotions, output_dict=True)
# Convert report to DataFrame
report_df = pd.DataFrame(report).transpose()
# Specify the full path for the output Excel file
output_folder = "D:/MCA/4th sem/SER3/output/Actor_01"
output_excel_file = os.path.join(output_folder, "classify_report_lstm.xlsx")
# Save classification report to Excel file
report_df.to_excel(output_excel_file)
print("Classification report saved successfully.")