-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRNN_FeatureBased.py
193 lines (140 loc) · 5.49 KB
/
CRNN_FeatureBased.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import numpy as np
import keras
import pickle
import sys
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from sklearn.metrics import confusion_matrix
from keras.layers import Dense, Dropout, Flatten, Lambda, Reshape, Bidirectional, LSTM, GaussianNoise
from keras.layers import Conv1D, MaxPooling1D, BatchNormalization, Activation
from keras.models import Model
from keras.callbacks import ModelCheckpoint
from keras import backend as K
from keras import Input
from sklearn.metrics import precision_recall_fscore_support, roc_curve, auc, accuracy_score, precision_recall_curve
import matplotlib.pyplot as plt
def LoadTrainingSet(filename):
with open(filename, 'rb') as f:
xTrain, yTrain = pickle.load(f)
return xTrain, yTrain
def LoadTestSet(filename):
with open(filename, 'rb') as f:
xTest, yTest = pickle.load(f)
return xTest, yTest
def generator(x, y, batchSize = 32):
offset = 0
while True:
if(offset > len(x)):
offset = 0
xBatch = []
yBatch = []
rightLim = offset + batchSize
xSamples = x[offset : rightLim]
ySamples = y[offset : rightLim]
for i, xSamp in enumerate(xSamples):
xTemp = xSamp
yTemp = ySamples[i]
#x = np.expand_dims(x, axis=-1)
xBatch.append(xTemp)
yBatch.append(yTemp)
offset += batchSize - 1
xBatch = np.array(xBatch)
yBatch = np.array(yBatch)
yield xBatch, yBatch
def CRNN(input_shape):
model = Sequential()
# CNN
model.add(Conv1D(8, 4, padding='same', name='conv1', input_shape = input_shape))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling1D(pool_size=2, name='max1'))
model.add(Conv1D(16, 4, padding='same', name='conv2'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling1D(pool_size=2, name='max2'))
# CNN to RNN
model.add(Reshape((1, 16)))
# RNN
model.add(LSTM(200))
model.add(BatchNormalization())
model.add(Dropout(0.5))
# Adding noise
model.add(GaussianNoise(0.2))
# Activation
model.add(Dense(4, name='dense'))
model.add(Activation('softmax', name='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adam(),
metrics=['accuracy'])
print(model.summary())
return model
def TrainCRNN(model, epochs):
xTrain, yTrain = LoadTrainingSet('./TrainingSignalFeatures.pk1')
xTest, yTest = LoadTestSet('./TestSignalFeatures.pk1')
trainGen = generator(xTrain, yTrain)
testGen = generator(xTest, yTest)
# Checkpoint
filepath="./model/weights-crnn-feat-{epoch:02d}-{val_acc:.2f}.h5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
model.fit_generator(trainGen, validation_data=testGen,
steps_per_epoch = np.ceil(len(xTrain) / 32),
validation_steps=np.ceil(len(xTest) / 32),
epochs=epochs, callbacks=callbacks_list, verbose=1)
model.save('./crnn_feat_model.h5')
def EvaluateCRNN(model, weightsFile):
xTest, yTest = LoadTestSet('./TestSignalFeatures.pk1')
model.load_weights(weightsFile)
xTest = np.array(xTest)
yTest = np.array(yTest)
print('Evaluation...')
yPredictedProbs = model.predict(xTest)
yMaxPredictedProbs = np.amax(yPredictedProbs, axis=1)
yPredicted = yPredictedProbs.argmax(axis = 1)
yTest = yTest.argmax(axis=1)
# Evaluate accuracy
accuracy = accuracy_score(yTest, yPredicted)
# Evaluate precision, recall and fscore
precision, recall, fscore, _ = precision_recall_fscore_support(yTest, yPredicted, average='macro')
precisions = []
recalls = []
f1Scores = []
for i in range(4):
yMaxPredictedProbsForClass = yMaxPredictedProbs
# 1 * casts to int.
maskTest = 1 * (yTest == i)
maskPred = 1 * (yPredicted == i)
precision, recall, fscore, _ = precision_recall_fscore_support(maskTest, maskPred, average='binary')
precisions.append(precision)
recalls.append(recall)
f1Scores.append(fscore)
print('Class ' , str(i))
print('Precision: ', str(precision))
print('Recall: ', str(recall))
print('F-Score: ', str(fscore))
fpr, tpr, _ = roc_curve(maskTest, yMaxPredictedProbsForClass)
roc_auc = auc(fpr, tpr)
# ROC
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic (ROC) for class' )
plt.legend(loc="lower right")
plt.savefig("./ROC_" + str(i))
# PROC
prec, rec, _ = precision_recall_curve(maskTest, yMaxPredictedProbsForClass)
plt.figure()
plt.plot(prec, rec, color='darkorange', lw=2)
plt.xlabel('Precision')
plt.ylabel('Recall')
plt.title('Precision-Recall Curve (PRC) for class')
plt.savefig("./PRC_" + str(i))
precision = sum(precisions) / 4.0
recall = sum(recalls) / 4.0
f1 = sum(f1Scores) / 4.0
print('Overall precision: ', str(precision))
print('Overall recall: ', str(recall))
print('Overall F-Score: ', str(f1))