-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train_CNN_with_Hyper_Parameters_Tuning.py
195 lines (131 loc) · 6.06 KB
/
Train_CNN_with_Hyper_Parameters_Tuning.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
194
195
from keras.models import Sequential,load_model
from keras.layers import Conv2D,Activation, MaxPooling2D,Dense,Flatten
from keras.optimizers import SGD
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import accuracy_score,roc_curve,confusion_matrix,precision_score,recall_score,f1_score,roc_auc_score
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt_False_Positive_vs_True_Positive
def Build_CNN_Model():
# -------------------------------------------------------------------------
# Build CNN Model
# -------------------------------------------------------------------------
model = Sequential()
# First Block of CNN
model.add(Conv2D(8, (5, 5), padding='same', input_shape=(224, 224, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))
# Second Block of CNN
model.add(Conv2D(8, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))
# Third Block of CNN
model.add(Conv2D(16, (3, 3), padding='same'))
model.add(Activation('relu'))
# Forth Block of CNN
model.add(Conv2D(16, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))
# Flatten and Fully Connected Layer
model.add(Flatten())
model.add(Dense(10))
model.add(Activation('relu'))
# Softmax Classifier
model.add(Dense(2))
model.add(Activation('softmax'))
# Display model
model.summary()
# compile model
opt = SGD(learning_rate=0.001)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
return model
# train and evalluate cnn model
def Train_CNN_Model(model):
# -------------------------------------------------------------------------
# Train CNN Model
# -------------------------------------------------------------------------
# create data generators
train_datagen = ImageDataGenerator(
rescale=1.0/255.0,
featurewise_center= True,
featurewise_std_normalization = True)
valid_datagen = ImageDataGenerator(
rescale=1.0/255.0,
featurewise_center= True,
featurewise_std_normalization = True)
# prepare iterators
batch_size=32
train_it = train_datagen.flow_from_directory('Data/train/',classes =('normal','abnormal'),batch_size=batch_size, target_size=(224, 224))
valid_it = valid_datagen.flow_from_directory('Data/val/',classes =('normal','abnormal'),batch_size=batch_size, target_size=(224, 224))
epochs=100;
history = model.fit_generator(train_it, steps_per_epoch=len(train_it),
validation_data=valid_it, validation_steps=len(valid_it), epochs=epochs, verbose=1)
# "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', 'validation'], loc='upper left')
plt.show()
# "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', 'validation'], loc='upper left')
plt.show()
# save model
model.save('medical_diagnosis_cnn_model.h5')
def Evaluate_CNN_Model():
# -------------------------------------------------------------------------
# Evaluate CNN Model
# -------------------------------------------------------------------------
# load model
model = load_model('medical_diagnosis_cnn_model.h5')
# load test data
batch_size=32
test_datagen = ImageDataGenerator(
rescale=1.0/255.0,
featurewise_center= True,
featurewise_std_normalization = True)
test_it = test_datagen.flow_from_directory('Data/test/',classes =('normal','abnormal'),
shuffle=False,batch_size=batch_size, target_size=(224, 224))
y_true = test_it.classes;
y_pred = model.predict_generator(test_it, steps=len(test_it), verbose=1)
y_pred_prob = y_pred[:,1]
y_pred_binary = y_pred_prob > 0.5
#Confution Matrix
print('\nConfusion Matrix\n -------------------------')
print(confusion_matrix(y_true,y_pred_binary));
# accuracy: (tp + tn) / (p + n)
accuracy = accuracy_score(y_true, y_pred_binary)
print('Accuracy: %f' % accuracy)
# precision tp / (tp + fp)
precision = precision_score(y_true, y_pred_binary)
print('Precision: %f' % precision)
# recall: tp / (tp + fn)
recall = recall_score(y_true, y_pred_binary)
print('Recall: %f' % recall)
# f1: 2 tp / (2 tp + fp + fn)
f1 = f1_score(y_true, y_pred_binary)
print('F1 score: %f' % f1)
# ROC AUC
auc = roc_auc_score(y_true, y_pred_prob)
print('ROC AUC: %f' % auc)
# calculate roc curves
fpr, tpr, _ = roc_curve(y_true, y_pred_prob)
# plot the roc curve for the model
plt.figure()
plt_False_Positive_vs_True_Positive.plot(fpr, tpr, linestyle='--', label='')
# axis labels
plt_False_Positive_vs_True_Positive.xlabel('False Positive Rate')
plt_False_Positive_vs_True_Positive.ylabel('True Positive Rate')
# show the legend
plt_False_Positive_vs_True_Positive.legend()
# show the plot
plt_False_Positive_vs_True_Positive.show()
# main entry
model = Build_CNN_Model()
Train_CNN_Model(model)
Evaluate_CNN_Model()