main.py 10.7 KB
Newer Older
Hotwani's avatar
Hotwani committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
Sintal's avatar
Sintal committed
16
17
18
import json
from openpyxl import Workbook
import time
Hotwani's avatar
Hotwani committed
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


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


Sintal's avatar
Sintal committed
94
def create_and_compile_model(X, hidden_layer_dimensions):
Hotwani's avatar
Hotwani committed
95
96
97
    print("Creating a Model")
    # creating a model
    model = models.Sequential()
Sintal's avatar
Sintal committed
98
99
100
101
102
103

    for i, layer_dimension in enumerate(hidden_layer_dimensions):
        if i == 0:
            model.add(layers.Dense(layer_dimension, activation=constants.ACTIVATION_RELU, input_shape=(X.shape[1],)))
        else:
            model.add(layers.Dense(layer_dimension, activation=constants.ACTIVATION_RELU))
Hotwani's avatar
Hotwani committed
104
105
106
107
108
109
110
111
112
113
114
115
116
    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
Sintal's avatar
Sintal committed
117
    #model.save(constants.TRAINED_MODEL)
Hotwani's avatar
Hotwani committed
118
119
    return history

Sintal's avatar
Sintal committed
120
121
122
123
124
125
126
127
128
def model_predict(model, X_test, y_test):
    test_loss, test_acc = model.evaluate(X_test, y_test)
    print('test_acc: ', test_acc)
    y_predicted = np.argmax(model.predict(X_test), axis=-1)
    accuracy = np.mean(y_test == y_predicted)
    print(accuracy)
    return accuracy


Hotwani's avatar
Hotwani committed
129

Sintal's avatar
Sintal committed
130
def predict(model, X_test, y_test):
Hotwani's avatar
Hotwani committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    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()

Sintal's avatar
Sintal committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
def construct_and_apply_network(hidden_layer_dimensions):
    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(X, hidden_layer_dimensions)
    history = train_and_save_model(model, X_train, y_train, X_test, y_test)
    history
    predict(model, X_test, y_test)
    accuracy = model_predict(model, X_test, y_test)
    #plot_model_accuracy(history)
    #plot_model_loss(history)
    return accuracy

def save_mfcc(trainingDataDir, trainingDataSubDirs, dataset_path, json_path, n_mfcc=13, n_fft=2048, hop_length=512):
    data = {
        "mapping": [],
        "mfcc": []
    }

    # 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_fft=n_fft, n_mfcc=n_mfcc, hop_length=hop_length)
                data["mfcc"].append(mfcc.tolist())

                to_append = f'{audioFile.name}'
                for g in mfcc:
                    to_append += f' {np.mean(g)}'
                if trainingDataSubDir == constants.CAR:
                    data["mapping"].append(constants.CAR)
                    to_append += f' {constants.LIGHT_WEIGHT}'
                elif trainingDataSubDir == constants.BUS:
                    data["mapping"].append(constants.BUS)
                    to_append += f' {constants.MEDIUM_WEIGHT}'
                elif trainingDataSubDir == constants.TRUCK:
                    data["mapping"].append(constants.TRUCK)
                    to_append += f' {constants.HEAVY_WEIGHT}'
                elif trainingDataSubDir == constants.MOTORCYCLE:
                    data["mapping"].append(constants.MOTORCYCLE)
                    to_append += f' {constants.TWO_WHEELED}'
                elif trainingDataSubDir == constants.TRAM:
                    data["mapping"].append(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())

    with open(json_path, "w") as fp:
        json.dump(data, fp, indent=4)


if __name__ == "__main__":
    # Changing Directory to Training Dataset Folder
    chdir(constants.TRAINING_DATA_DIRECTORY_NAME)
    trainingDataDir = Path.cwd()
    trainingDataSubDirs = os.listdir(trainingDataDir)
    chdir("..")
    if os.path.isfile(constants.FEATURES_CSV_NAME):
        print("already exists")
    else:
        extract_features(trainingDataDir, trainingDataSubDirs)

    max_accuracy = 0
    neurons_increment_by = 8
    start_neuron_value = 8
    max_neuron_value = 128
    hidden_layers = 5
    hidden_layer_dimensions = []

    book = Workbook()
    sheet = book.active

    # loop_count = int((max_neuron_value / neurons_increment_by) * 4)
    row_counter = 0
    for i in range(hidden_layers):
        hidden_layer_dimensions.append(0)
        for j in range(start_neuron_value, (max_neuron_value + 1), neurons_increment_by):
            row_counter += 1
            hidden_layer_dimensions[i] = j
            start = time.time()
            new_accuracy = construct_and_apply_network(hidden_layer_dimensions)
            end = time.time()
            elapsed_time = end - start
            sheet.cell(row=(row_counter), column=1).value = hidden_layer_dimensions.__str__()
            sheet.cell(row=(row_counter), column=2).value = new_accuracy
            sheet.cell(row=(row_counter), column=3).value = elapsed_time
Hotwani's avatar
Hotwani committed
251

Sintal's avatar
Sintal committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    '''
    for i in range (loop_count):
        start = time.time()
        new_accuracy = construct_and_apply_network(hidden_layer_dimensions)
        end = time.time()
        if max_accuracy < new_accuracy:
            max_accuracy = new_accuracy
        elapsed_time = end - start
        print("durchlauf: ", (i+1))
        print("\nmax accuracy: ", max_accuracy)
        print("\nnew accuracy: ", new_accuracy)
        print("\nlist: ", hidden_layer_dimensions)
        sheet.cell(row=(i+1), column=1).value = hidden_layer_dimensions.__str__()
        sheet.cell(row=(i + 1), column=2).value = new_accuracy
        sheet.cell(row=(i + 1), column=3).value = elapsed_time
Hotwani's avatar
Hotwani committed
267

Sintal's avatar
Sintal committed
268
269
270
271
272
273
274
275
        if neurons_count == max_neuron_value:
            neurons_count = start_neuron_value
            hidden_layer_dimensions.append(start_neuron_value)
            pointer += 1
        else:
            neurons_count += neurons_increment_by
            hidden_layer_dimensions[pointer] = neurons_count
    '''
Hotwani's avatar
Hotwani committed
276

Sintal's avatar
Sintal committed
277
    book.save("sample.xlsx")
Hotwani's avatar
Hotwani committed
278
279