Commit b41a0877 authored by Hotwani's avatar Hotwani
Browse files

Initial commit

parents
File added
from pandas import datetime
MFCC_FEATURE_START = 1
MFCC_FEATURE_END = 21
TRAINING_DATA_DIRECTORY_NAME = 'DemoTrainingDataset'
TESTING_DATA_DIRECTORY_NAME = 'TEST'
CAR = 'Car'
BUS = 'Bus'
TRUCK = 'Truck'
MOTORCYCLE = 'Motorcycle'
TRAM = 'Tram'
LIGHT_WEIGHT = 'Light-Weight'
MEDIUM_WEIGHT = 'Medium-Weight'
HEAVY_WEIGHT = 'Heavy-Weight'
TWO_WHEELED = 'Two-Wheeled'
RAIL_BOUND = 'Rail-Bound'
FEATURES_CSV_NAME = 'features.csv'
TEST_CSV_NAME = 'test.csv'
HIDDEN_LAYER_1_DIMENSIONS = 32
HIDDEN_LAYER_2_DIMENSIONS = 32
HIDDEN_LAYER_3_DIMENSIONS = 32
OUTPUT_LAYER_DIMENSIONS = 5
ACTIVATION_RELU = 'relu'
ACTIVATION_SOFTMAX = 'softmax'
OPTIMIZER_ADAM = 'adam'
LOSS_FUNCTION_SPARSE = 'sparse_categorical_crossentropy'
ACCURACY_METRICS = 'accuracy'
LOG_DIR_PATH = "logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S")
TRAINED_MODEL = 'trained_model.h5'
import librosa.feature
import pandas as pd
import numpy as np
from pathlib import Path
from os import chdir
import os
import csv
from tensorflow import keras
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from keras import models
from keras import layers
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report
import constants
def create_csv_header():
header = 'filename '
for i in range(constants.MFCC_FEATURE_START, constants.MFCC_FEATURE_END):
header += f' mfcc{i}'
header += ' label'
header = header.split()
file = open(constants.FEATURES_CSV_NAME, 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerow(header)
def extract_features(trainingDataDir, trainingDataSubDirs):
create_csv_header()
# Looping over every file inside the subdirectories for feature extraction
for trainingDataSubDir in trainingDataSubDirs:
for fileName in os.listdir(trainingDataDir/f'{trainingDataSubDir}'):
if fileName.endswith(".wav"):
audioFile = trainingDataDir/f'{trainingDataSubDir}/{fileName}'
print("Extracting Features from Directory "+trainingDataSubDir+" and file "+audioFile.name)
y, sr = librosa.load(audioFile, mono=True)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=(constants.MFCC_FEATURE_END - constants.MFCC_FEATURE_START))
to_append = f'{audioFile.name}'
for g in mfcc:
to_append += f' {np.mean(g)}'
if trainingDataSubDir == constants.CAR:
to_append += f' {constants.LIGHT_WEIGHT}'
elif trainingDataSubDir == constants.BUS:
to_append += f' {constants.MEDIUM_WEIGHT}'
elif trainingDataSubDir == constants.TRUCK:
to_append += f' {constants.HEAVY_WEIGHT}'
elif trainingDataSubDir == constants.MOTORCYCLE:
to_append += f' {constants.TWO_WHEELED}'
elif trainingDataSubDir == constants.TRAM:
to_append += f' {constants.RAIL_BOUND}'
file = open(constants.FEATURES_CSV_NAME, 'a', newline='')
with file:
writer = csv.writer(file)
writer.writerow(to_append.split())
def preprocessing_csv_data():
print("Reading Features... ")
data = pd.read_csv(constants.FEATURES_CSV_NAME)
data.head()
# Dropping unnecessary columns (Column Filename is dropped)
data = data.drop(['filename'], axis=1)
data.head()
return data
def encode_labels(data):
# Extracting classes/label column as y from csv and converting string labels to numbers using LabelEncoder
audio_list = data.iloc[:, -1]
encoder = LabelEncoder()
target_labels = encoder.fit_transform(audio_list)
return target_labels, encoder
def normalize_data(data):
# normalizing - Extracting Remaining Columns as X and normalizing them to a common scale
scaler = StandardScaler()
X = scaler.fit_transform(np.array(data.iloc[:, :-1], dtype=float))
return X
def train_test_data_split(X, y):
# splitting of dataset into train and test dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)
return X_train, X_test, y_train, y_test
def create_and_compile_model():
print("Creating a Model")
# creating a model
model = models.Sequential()
model.add(layers.Dense(constants.HIDDEN_LAYER_1_DIMENSIONS, activation=constants.ACTIVATION_RELU, input_shape=(X.shape[1],)))
model.add(layers.Dense(constants.HIDDEN_LAYER_2_DIMENSIONS, activation=constants.ACTIVATION_RELU))
model.add(layers.Dense(constants.HIDDEN_LAYER_3_DIMENSIONS, activation=constants.ACTIVATION_RELU))
model.add(layers.Dense(constants.OUTPUT_LAYER_DIMENSIONS, activation=constants.ACTIVATION_SOFTMAX))
print("Compiling a Model")
model.compile(optimizer= constants.OPTIMIZER_ADAM, loss= constants.LOSS_FUNCTION_SPARSE, metrics=[constants.ACCURACY_METRICS])
return model
def train_and_save_model(model, X_train, y_train, X_test, y_test):
logdir = constants.LOG_DIR_PATH
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)
print("Start Training...")
history = model.fit(X_train, y_train, epochs=35, validation_data=(X_test, y_test), callbacks=[tensorboard_callback])
# Saving the trained model to avoid re-training
model.save(constants.TRAINED_MODEL)
return history
def predict(X_test, y_test):
print("Predictions.....")
predictions = np.argmax(model.predict(X_test), axis=-1)
target_names = [constants.LIGHT_WEIGHT, constants.MEDIUM_WEIGHT, constants.HEAVY_WEIGHT,constants.TWO_WHEELED, constants.RAIL_BOUND]
print(classification_report(y_test, predictions, target_names=target_names))
def plot_model_accuracy(history):
# Plot graph Model Accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
def plot_model_loss(history):
# Plot graph Model Loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper right')
plt.show()
# Changing Directory to Training Dataset Folder
chdir(constants.TRAINING_DATA_DIRECTORY_NAME)
trainingDataDir = Path.cwd()
trainingDataSubDirs = os.listdir(trainingDataDir)
extract_features(trainingDataDir, trainingDataSubDirs)
data = preprocessing_csv_data()
target_labels, encoder = encode_labels(data)
X = normalize_data(data)
X_train, X_test, y_train, y_test = train_test_data_split(X, target_labels)
model = create_and_compile_model()
history = train_and_save_model(model, X_train, y_train, X_test, y_test)
predict(X_test, y_test)
plot_model_accuracy(history)
plot_model_loss(history)
absl-py==0.11.0
appdirs==1.4.4
astunparse==1.6.3
audioread==2.1.9
cached-property==1.5.2
cachetools==4.1.1
certifi==2020.6.20
cffi==1.14.3
chardet==3.0.4
cycler==0.10.0
decorator==4.4.2
gast==0.3.3
google-auth==1.23.0
google-auth-oauthlib==0.4.2
google-pasta==0.2.0
grpcio==1.33.2
h5py==2.10.0
idna==2.10
importlib-metadata==2.0.0
inline==0.0.1
joblib==0.17.0
Keras==2.4.3
Keras-Preprocessing==1.1.2
kiwisolver==1.2.0
librosa==0.8.0
llvmlite==0.34.0
Markdown==3.3.3
matplotlib==3.3.2
numba==0.51.2
numpy==1.18.5
oauthlib==3.1.0
opt-einsum==3.3.0
packaging==20.4
pandas==1.1.4
pathlib==1.0.1
Pillow==8.0.1
pooch==1.2.0
protobuf==3.13.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2020.4
PyYAML==5.3.1
requests==2.24.0
requests-oauthlib==1.3.0
resampy==0.2.2
rsa==4.6
scikit-learn==0.23.2
scipy==1.5.3
six==1.15.0
SoundFile==0.10.3.post1
tensorboard==2.3.0
tensorboard-plugin-wit==1.7.0
tensorflow==2.3.1
tensorflow-estimator==2.3.0
termcolor==1.1.0
threadpoolctl==2.1.0
urllib3==1.25.11
Werkzeug==1.0.1
wrapt==1.12.1
zipp==3.4.0
import librosa.feature
import pandas as pd
import numpy as np
import os
from pathlib import Path
import csv
from tensorflow import keras
from sklearn.preprocessing import LabelEncoder, StandardScaler
import constants
def create_csv_header():
header=''
for i in range(constants.MFCC_FEATURE_START, constants.MFCC_FEATURE_END):
header += f' mfcc{i}'
header = header.split()
file = open(constants.TEST_CSV_NAME, 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerow(header)
def extract_features(workingDir, subDirectories):
create_csv_header()
for subDirectory in subDirectories:
if subDirectory == constants.TESTING_DATA_DIRECTORY_NAME:
for fileName in os.listdir(workingDir/f'{subDirectory}'):
if fileName.endswith(".wav"):
audioFile = workingDir / f'{subDirectory}/{fileName}'
y, sr = librosa.load(audioFile, mono=True)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=(constants.MFCC_FEATURE_END - constants.MFCC_FEATURE_START))
to_append = ''
for g in mfcc:
to_append += f' {np.mean(g)}'
file = open(constants.TEST_CSV_NAME, 'a', newline='')
with file:
writer = csv.writer(file)
writer.writerow(to_append.split())
def preprocessing_csv_data():
# reading dataset from csv
print("Reading Features... ")
data = pd.read_csv(constants.TEST_CSV_NAME)
data.head()
return data
def normalize_data(data):
# # normalizing - Extracting Remaining Columns as X and normalizing them to a common scale
scaler = StandardScaler()
X = scaler.fit_transform(np.array(data.iloc[:, :], dtype=float))
print(X)
print(X.shape)
return X
WorkingDir = Path.cwd()
subDirectories = os.listdir(WorkingDir)
extract_features(WorkingDir, subDirectories)
data = preprocessing_csv_data()
X = normalize_data(data)
model = keras.models.load_model('./DemoTrainingDataset/trained_model.h5')
model.summary()
predictions = np.argmax(model.predict(X), axis=-1)
encoder = LabelEncoder()
labels = ['Light-Weight', 'Medium-Weight', 'Heavy-Weight', 'Two-Wheeled', 'Rail-Bound']
encoder.fit_transform(labels)
print(predictions)
print(encoder.inverse_transform(predictions))
Supports Markdown
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