-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfccrnn.py
173 lines (134 loc) · 6 KB
/
mfccrnn.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
from datetime import datetime
from pprint import pformat
import numpy as np
from keras.optimizers import RMSprop
from keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import StratifiedKFold
from sklearn.utils import shuffle
from mlutils.classbalancing import oversample
from pcg.datasets import PhysionetCinC as PCC
from pcg.metrics import ConfusionMatrix, TrainingHistory
from pcg.mfccrnn import models, mfcc
SCRIPT_START_TIME = datetime.now()
print('Starting at time', SCRIPT_START_TIME)
# Get the data for training-a
print('Reading Dataset...')
data = PCC.get_subset('a')
# Compute the spectrograms and add as a column to data
print('Computing Spectrograms...')
data['spectrogram'] = [pad_sequences(s, 800, 'float32').T
for s in mfcc.MFCC(y=data['waveform'],
sampling_rate=data['fs'][0],
nperseg=100,
stride=50,
n_mfcc=20)
.get()]
hyperparameter_vectors = [
(30, 10, 0.5, 0.4, (800, 20)),
(30, 10, 0.6, 0.4, (800, 20)),
(30, 10, 0.7, 0.4, (800, 20)),
(30, 10, 0.8, 0.4, (800, 20)),
(30, 10, 0.9, 0.4, (800, 20)),
(30, 10, 0.95, 0.4, (800, 20)),
(30, 10, 0.5, 0.6, (800, 20)),
(30, 10, 0.6, 0.6, (800, 20)),
(30, 10, 0.7, 0.6, (800, 20)),
(30, 10, 0.8, 0.6, (800, 20)),
(30, 10, 0.9, 0.6, (800, 20)),
(30, 10, 0.95, 0.6, (800, 20)),
(30, 10, 0.5, 0.8, (800, 20)),
(30, 10, 0.6, 0.8, (800, 20)),
(30, 10, 0.7, 0.8, (800, 20)),
(30, 10, 0.8, 0.8, (800, 20)),
(30, 10, 0.9, 0.8, (800, 20)),
(30, 10, 0.95, 0.8, (800, 20)),
(15, 10, 0.5, 0.5, (800, 20)),
(15, 10, 0.6, 0.5, (800, 20)),
(15, 10, 0.7, 0.5, (800, 20)),
(15, 10, 0.8, 0.5, (800, 20)),
(15, 10, 0.9, 0.5, (800, 20)),
(10, 10, 0.5, 0.5, (800, 20)),
(10, 10, 0.6, 0.5, (800, 20)),
(10, 10, 0.7, 0.5, (800, 20)),
(10, 10, 0.8, 0.5, (800, 20)),
(10, 10, 0.9, 0.5, (800, 20)),
(15, 5, 0.5, 0.4, (800, 20)),
(15, 5, 0.6, 0.4, (800, 20)),
(15, 5, 0.7, 0.4, (800, 20)),
(15, 5, 0.8, 0.4, (800, 20)),
(15, 5, 0.9, 0.4, (800, 20)),
(10, 5, 0.5, 0.4, (800, 20)),
(10, 5, 0.6, 0.4, (800, 20)),
(10, 5, 0.7, 0.4, (800, 20)),
(10, 5, 0.8, 0.4, (800, 20)),
(10, 5, 0.9, 0.4, (800, 20)),
(5, 3, 0, 0, (800, 20)),
]
# Shuffling the dataset
data = data.sample(frac=1)
for hyperparameter_vector in hyperparameter_vectors:
fold_id = 0
for data_train_ids, data_test_ids in StratifiedKFold(n_splits=5).split(data, data['label']):
start_time = datetime.now()
print('Starting at time', start_time)
print('For hyperparameters: ', hyperparameter_vector)
print('Training for fold', fold_id)
model = models.model_C(*hyperparameter_vector)
data_train = data.iloc[data_train_ids]
data_test = data.iloc[data_test_ids]
# Sanity Check on shuffles
print('Train Test Split:')
print('Train Set:')
print(data_train.head())
print('Test Set')
print(data_test.head())
# Sanity Check on distribution
print('Test Train Split Stats:\n')
print('Training size:', len(data_train))
print('Test size: :', len(data_test))
print('Class Distribution Training:', np.unique(data_train['label'], return_counts=True))
print('Class Distribution Test :', np.unique(data_test['label'], return_counts=True))
# Oversample train and test separately
print('Oversampling to balance classes...')
data_train = oversample(data_train, 'label')
data_test = oversample(data_test, 'label')
# Sanity Check
print('Test Train Split Stats after Oversampling:\n')
print('Training size:', len(data_train))
print('Test size: :', len(data_test))
print('Class Distribution Training:', np.unique(data_train['label'], return_counts=True))
print('Class Distribution Test :', np.unique(data_test['label'], return_counts=True))
# Training
print('Training the model...')
model.compile(loss='binary_crossentropy',
optimizer=RMSprop(0.0005),
metrics=['accuracy'])
history = model.fit(np.stack(data_train['spectrogram']), np.stack(data_train['label']),
batch_size=512,
epochs=300,
validation_data=(np.stack(data_test['spectrogram']), np.stack(data_test['label'].values)))
# Create directory for logging
formatted_dt = start_time.strftime('%y-%m-%d-%H-%m-%S')
log_path = '/home/agnivesh/model_logs/mfccrnn/a/C/' + "{}_{} {}_{} {}".format(*hyperparameter_vector) + '/' + 'fold-{fold_id}'.format(fold_id=fold_id) + '/'
os.makedirs(log_path)
# Training history visualization
TrainingHistory(history).save(log_path)
# Saving the model configuration
raw_json = open(log_path + 'model_architecture.json', 'w')
raw_json.write(model.to_json())
raw_json.close()
pretty_json = open(log_path + 'model_description.txt', 'w')
pretty_json.write(pformat(model.to_json()))
pretty_json.close()
# Save weights
model.save_weights(log_path + 'weights.h5')
# Save the model
model.save(log_path + 'model.h5')
# Confusion Matrix
print('Calculating the Confusion Matrices...')
train_preds = np.round(model.predict(np.stack(data_train['spectrogram']))).astype('int32')
test_preds = np.round(model.predict(np.stack(data_test['spectrogram']))).astype('int32')
ConfusionMatrix(data_train['label'].values, train_preds, ['Normal', 'Abnormal']).save('Train Confusion Matrix', log_path)
ConfusionMatrix(data_test['label'].values, test_preds, ['Normal', 'Abnormal']).save('Test Confusion Matrix', log_path)
fold_id += 1