Open Shubhabrata08 opened 5 months ago
1. Reading h5py File
import h5py
with h5py.File('/content/UrbanSound8kMFCC.h5py', 'r') as hdf_file:
# List all the datasets in the HDF5 file/UrbanSound8kMFCC.h5py
print("Datasets in the HDF5 file:", list(hdf_file.keys()))
# Access the 'X' dataset
X_dataset = hdf_file['X']
# Access the 'y' dataset
y_dataset = hdf_file['y']
# Now, you can work with the datasets as NumPy arrays
X_data = X_dataset[:]
y_data = y_dataset[:]
print("Shape of the 'X' dataset:", X_data.shape)
print("Shape of the 'y' dataset:", y_data.shape)
2.Creating an ANN Model
import tensorflow as tf
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.2, random_state=42)
# Normalize the input features
X_train = tf.keras.utils.normalize(X_train, axis=1)
X_test = tf.keras.utils.normalize(X_test, axis=1)
# Define the model
model = tf.keras.models.Sequential([
tf.keras.layers.Input(shape=X_train.shape[1:]),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax') # Assuming 10 classes
])
3. Training the model and Evaluating Accuracy
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2)
# Evaluate the model on the test data
test_loss, test_accuracy = model.evaluate(X_test, y_test)
print("Test accuracy:", test_accuracy)
4. Model Summary
5. Accuracy and Loss Plot
import matplotlib.pyplot as plt
# Plot training and validation accuracy curves
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
# Plot training and validation loss curves
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
Output:
Details:
Create ANNs for classifying UrbanSound8K. MFCCs of the audio samples are present in the linked dataset below and are to be directly used in the classification input as features. Experiment on the ANNs, by modification of layers, addition/removal of layers, etc. Feel free to try out ML algos on the features as well. Do document the results in a text file with necessary details.
Dataset:
UrbanSound8KMFCCs
Tools and Libraries recommended:
Google Colab and Tensorflow.