-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeras_modeling.py
145 lines (109 loc) · 4.37 KB
/
keras_modeling.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, Conv2D, MaxPooling2D, GlobalAveragePooling2D
from keras.optimizers import Adam
from keras.utils import np_utils
from sklearn import metrics
import os
import numpy as np
import pandas as pd
import scipy.io.wavfile
import python_speech_features as psf
from sklearn.metrics import accuracy_score
from pathlib import Path
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.decomposition import PCA
from sklearn import preprocessing
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
def get_file_features(wav_fname, num_ceps=13):
"""
Extract mfcc features from a file.
"""
# read wave
fs, sig = scipy.io.wavfile.read(wav_fname)
# get mfccs
mfccs = psf.mfcc(sig, samplerate=fs, winlen=0.025, winstep=0.01,
numcep=num_ceps, nfilt=26, nfft=512, lowfreq=0,
highfreq=None, preemph=0.97, ceplifter=22,
appendEnergy=False)
# compute mfcc means
mfcc_means = np.round(mfccs.mean(axis=0), 3)
return mfcc_means
# features
features=[]
human_pathlist = Path('./human').rglob('*.wav')
for path in human_pathlist:
class_label='human'
# because path is object not string
path_in_str = str(path)
features.append([get_file_features(path_in_str), class_label])
computer_pathlist = Path('./computer').rglob('*.wav')
for path in computer_pathlist:
class_label='computer'
# because path is object not string
path_in_str = str(path)
features.append([get_file_features(path_in_str), class_label])
# Convert into a Panda dataframe
featuresdf = pd.DataFrame(features, columns=['feature','class_label'])
print('Finished feature extraction from ', len(featuresdf), ' files')
# Convert features and corresponding classification labels into numpy arrays
X = np.array(featuresdf.feature.tolist())
y = np.array(featuresdf.class_label.tolist())
# Print shapes
print("X.shape: " + str(X.shape))
print("y.shape: " + str(y.shape))
# Encode the classification labels
le = preprocessing.LabelEncoder()
yy = le.fit_transform(y)
# split the dataset
x_train, x_test, y_train, y_test = train_test_split(X, yy, test_size=0.2, random_state = 42)
# num_rows = 40
# num_columns = 174
# num_channels = 1
# x_train = x_train.reshape(x_train.shape[0], num_rows, num_columns, num_channels)
# x_test = x_test.reshape(x_test.shape[0], num_rows, num_columns, num_channels)
num_labels = 2
filter_size = 2
# Construct model
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=2, input_shape=(x_train.shape[0],2868, 13), activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))
# model.add(Conv2D(filters=32, kernel_size=2, activation='relu'))
# model.add(MaxPooling2D(pool_size=2))
# model.add(Dropout(0.2))
# model.add(Conv2D(filters=64, kernel_size=2, activation='relu'))
# model.add(MaxPooling2D(pool_size=2))
# model.add(Dropout(0.2))
# model.add(Conv2D(filters=128, kernel_size=2, activation='relu'))
# model.add(MaxPooling2D(pool_size=2))
# model.add(Dropout(0.2))
# model.add(GlobalAveragePooling2D())
model.add(Dense(num_labels, activation='softmax'))
# Compile the model
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
# Display model architecture summary
model.summary()
# Calculate pre-training accuracy
score = model.evaluate(x_test, y_test, verbose=1)
accuracy = 100*score[1]
print("Pre-training accuracy: %.4f%%" % accuracy)
from keras.callbacks import ModelCheckpoint
from datetime import datetime
num_epochs = 72
num_batch_size = 256
checkpointer = ModelCheckpoint(filepath='saved_models/weights.best.basic_cnn.hdf5',
verbose=1, save_best_only=True)
start = datetime.now()
model.fit(x_train, y_train, batch_size=num_batch_size, epochs=num_epochs, validation_data=(x_test, y_test), callbacks=[checkpointer], verbose=1)
duration = datetime.now() - start
print("Training completed in time: ", duration)
# Evaluating the model on the training and testing set
score = model.evaluate(x_train, y_train, verbose=0)
print("Training Accuracy: ", score[1])
score = model.evaluate(x_test, y_test, verbose=0)
print("Testing Accuracy: ", score[1])