Upload Train.py

parent b5d216a0
import os
import numpy as np
import librosa
import tensorflow as tf
from sklearn.model_selection import train_test_split
# Define a function to extract features from the audio file
def extract_features(audio_path):
# Load the audio file
y, sr = librosa.load(audio_path, sr=22050, duration=3)
# Extract features using Mel-frequency cepstral coefficients (MFCC)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=20)
# Compute the mean of each feature dimension
mfcc_mean = np.mean(mfcc, axis=1)
# Compute the first and second derivatives of the MFCC features
mfcc_delta = librosa.feature.delta(mfcc)
mfcc_delta2 = librosa.feature.delta(mfcc, order=2)
# Compute the mean of each derivative feature dimension
mfcc_delta_mean = np.mean(mfcc_delta, axis=1)
mfcc_delta2_mean = np.mean(mfcc_delta2, axis=1)
# Concatenate the feature vectors
features = np.concatenate((mfcc_mean, mfcc_delta_mean, mfcc_delta2_mean))
return features
# Define a function to load the audio files and their labels
def load_data(data_dir):
audio_files = []
labels = []
# Loop through all subdirectories in the data directory
for root, dirs, files in os.walk(data_dir):
for file in files:
if file.endswith('.wav'):
# Extract the label from the filename
label = file.split('-')[2]
# Convert the label to an integer
label = int(label)
# Map the label to an emotion label
if label == 1:
emotion = 'neutral'
elif label == 2:
emotion = 'calm'
elif label == 3:
emotion = 'happy'
elif label == 4:
emotion = 'sad'
elif label == 5:
emotion = 'angry'
elif label == 6:
emotion = 'fearful'
elif label == 7:
emotion = 'disgust'
# Add the audio file and its label to the lists
audio_files.append(os.path.join(root, file))
labels.append(emotion)
return audio_files, labels
# Load the data
data_dir = 'ravdess'
audio_files, labels = load_data(data_dir)
# Extract features from the audio files
features = []
for audio_file in audio_files:
feature = extract_features(audio_file)
features.append(feature)
features = np.array(features)
# Map the emotion labels to integer values
label_map = {'neutral': 0, 'calm': 1, 'happy': 2, 'sad': 3, 'angry': 4, 'fearful': 5, 'disgust': 6}
labels = np.array([label_map[label] for label in labels])
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(60,)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(7, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=50, batch_size=32)
model_path = 'model.h5'
model.save(model_path)
print('Model saved at:', model_path)
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)
print('Test accuracy:', test_acc)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment