-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_res.py
179 lines (136 loc) · 5.54 KB
/
train_res.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
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 8 16:06:59 2018
"""
import time
import pickle
from keras import Model
from keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout
from keras.utils import to_categorical
from keras import optimizers
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras import applications
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from sklearn.metrics import confusion_matrix
from matplotlib import pyplot
import matplotlib.pyplot as plt
#plt.style.use('seaborn-white')
#import seaborn as sns
#sns.set_style("white")
t1=time.time()
x_train = pickle.load(open("X_Train_224.pickle","rb"))
y_train = pickle.load(open("Y_Train_224.pickle","rb"))
y_train = to_categorical(y_train,2)
t2=time.time()
print("time to load :",(t2-t1))
t1=time.time()
x_test = pickle.load(open("X_Test_224.pickle","rb"))
y_test = pickle.load(open("Y_Test_224.pickle","rb"))
y_test_ROC = y_test
y_test = to_categorical(y_test,2)
t2=time.time()
print("time to load :",(t2-t1))
epoch = 50
#set early stopping criteria
pat = 10 #this is the number of epochs with no improvment after which the training will stop
early_stopping = EarlyStopping(monitor='val_loss', patience=pat, verbose=1)
#define the model checkpoint callback -> this will keep on saving the model as a physical file
model_checkpoint = ModelCheckpoint('Malaria_Res_Model_by_Machine.h5', verbose=1, save_best_only=True)
def final_model():
img_width, img_height = 224, 224
model = applications.ResNet50(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, 3))
# Freeze the layers which you don't want to train. Here I am freezing the first 5 layers.
for layer in model.layers[:5]:
layer.trainable = False
#Adding custom Layers
x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
predictions = Dense(2, activation="softmax")(x)
# creating the final model
model_final = Model(inputs = model.input, output = predictions)
# compile the model
model_final.compile(loss = "categorical_crossentropy", optimizer = optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"])
return model_final
def fit_and_evaluate(X_Train, X_Test, Y_Train, Y_Test, y_test_ROC):
model = None
model = final_model()
results = model.fit(X_Train, Y_Train, batch_size=8, epochs = epoch, callbacks=[early_stopping, model_checkpoint], verbose=1, validation_split=0.05)
model.save('Malaria_ResNET50_Model.h5')
################## Result Evaluation Start ###################
score = model.evaluate(X_Test, Y_Test, batch_size=8)
print(score)
# Make prediction from model
predictions = model.predict(X_Test)
Y_Pred = (predictions > 0.5)
Y_Test = (Y_Test > 0.5)
########### ROC Curve Generate
probs = predictions
# keep probabilities for the positive outcome only
probs = probs[:, 1]
# calculate AUC
auc = roc_auc_score(y_test_ROC, probs)
auc = '%.3f' % auc
print('AUC: ' + auc)
# calculate roc curve
fpr, tpr, thresholds = roc_curve(y_test_ROC, probs)
pyplot.figure()
# plot no skill
lw = 2
plt.plot(fpr[2], tpr[2], color='darkorange', lw=lw, label='ROC Curve ( Area = ' + auc +' )')
pyplot.plot([0, 1], [0, 1], linestyle='--')
# plot the roc curve for the model
pyplot.plot(fpr, tpr, marker='.')
# show the plot
pyplot.title('Receiver Operating Characteristic ( ROC )')
pyplot.xlabel('False Positive Rate')
pyplot.ylabel('True Positive Rate')
pyplot.legend()
pyplot.savefig('ROC_' + auc + '.png')
pyplot.show()
############################################
# Confusion Matrix #
############################################
file = open("Result_of_Model.txt","w")
Y_Pred_abn = Y_Pred
print(Y_Pred_abn.shape)
Y_Pred_abn = Y_Pred[:, 0]
Y_Test_abn = Y_Test
print(Y_Test_abn.shape)
Y_Test_abn = Y_Test_abn[:, 0]
con_mat = confusion_matrix(Y_Test_abn, Y_Pred_abn)
print("Confusion Matrix => \n" + str(con_mat))
file.write("Confusion Matrix => \n" + str(con_mat) + "\n\n")
file.close()
################## Result Evaluation END ###################
print('\n\n\n')
return results
model_history = []
model_history.append(fit_and_evaluate(x_train, x_test, y_train, y_test, y_test_ROC))
plt.figure()
plt.title('Accuracies vs Epochs On Training Data')
plt.plot(model_history[0].history['acc'], label='Training Data')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.savefig('Accuracies_vs_Epochs_training.png')
plt.show()
plt.figure()
plt.title('Accuracies vs Epochs On Validation Data')
plt.plot(model_history[0].history['val_acc'], label='Validation Data')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.savefig('Accuracies_vs_Epochs_validation.png')
plt.show()
plt.figure()
plt.title('Train Accuracy vs Validation Accuracy')
plt.plot(model_history[0].history['acc'], label='Training Accuracy', color='black')
plt.plot(model_history[0].history['val_acc'], label='Validation Accuracy', color='black', linestyle = "dashdot")
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.savefig('Train_vs_Val.png')
plt.show()