-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn_model.py
More file actions
109 lines (93 loc) · 3.87 KB
/
cnn_model.py
File metadata and controls
109 lines (93 loc) · 3.87 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
import os
import numpy as np
import librosa
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense, Dropout
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
# Function to extract features
def extract_features(file_path):
try:
y, sr = librosa.load(file_path, sr=None)
# Extract MFCC
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
# Extract Chroma
chroma = librosa.feature.chroma_stft(y=y, sr=sr)
# Extract Spectral Contrast
spectral_contrast = librosa.feature.spectral_contrast(y=y, sr=sr)
# Combine features
features = np.concatenate([
np.mean(mfcc, axis=1),
np.mean(chroma, axis=1),
np.mean(spectral_contrast, axis=1)
])
return features
except Exception as e:
print(f"Error processing {file_path}: {e}")
return None
# Base path for dataset
base_path = r"C:\Users\Hp\PycharmProjects\Sitar_Script\Trimmed"
features_list = []
labels_list = []
# Loop through folders and process audio files
for folder_name in os.listdir(base_path):
folder_path = os.path.join(base_path, folder_name)
if os.path.isdir(folder_path):
label = folder_name # Use full folder name as label
for file_name in os.listdir(folder_path):
if file_name.endswith(".wav"):
file_path = os.path.join(folder_path, file_name)
features = extract_features(file_path)
if features is not None:
features_list.append(features)
labels_list.append(label)
# Convert features and labels to numpy arrays
X = np.array(features_list)
y = np.array(labels_list)
# Encode labels
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)
y_categorical = to_categorical(y_encoded)
# Normalize features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_categorical, test_size=0.2, random_state=42, stratify=y_categorical)
# Reshape for CNN input
X_train = X_train[..., np.newaxis]
X_test = X_test[..., np.newaxis]
# Build CNN Model
model = Sequential([
Conv1D(64, 3, activation='relu', input_shape=(X_train.shape[1], 1)),
MaxPooling1D(pool_size=2),
Conv1D(128, 3, activation='relu'),
MaxPooling1D(pool_size=2),
Flatten(),
Dense(128, activation='relu'),
Dropout(0.3),
Dense(64, activation='relu'),
Dropout(0.3),
Dense(y_categorical.shape[1], activation='softmax')
])
# Compile model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Callbacks
early_stop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
model_checkpoint = ModelCheckpoint('best_model.keras', save_best_only=True)
# Train the model
history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_data=(X_test, y_test),
callbacks=[early_stop, model_checkpoint])
# Evaluate the model
test_loss, test_accuracy = model.evaluate(X_test, y_test)
print(f"Test Accuracy: {test_accuracy * 100:.2f}%")
# Save the model (choose one of the formats)
model.save('sitar_openl3_classifier_cnn.h5')
# Predict on test data
predictions = model.predict(X_test)
predicted_labels = label_encoder.inverse_transform(np.argmax(predictions, axis=1))
# Print predicted and true labels for all samples
true_labels = label_encoder.inverse_transform(np.argmax(y_test, axis=1))
for true, pred in zip(true_labels, predicted_labels):
print(f"True Label: {true}, Predicted Label: {pred}")