/
TMI_CNN.py
276 lines (227 loc) · 10.2 KB
/
TMI_CNN.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 24 10:32:17 2020
@author: Alokparna
"""
import tensorflow as tf
import tensorflow.keras as K
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import CategoricalCrossentropy
from tensorflow.keras.utils import plot_model
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix
from skimage.transform import resize
import itertools
import scipy.io
import time
import warnings
warnings.filterwarnings("ignore")
"""
-----------------------------------------------------------------------------------------------
Create a CNN class inheriting the Keras Model
-----------------------------------------------------------------------------------------------
"""
class CNN(Model):
def __init__(self):
super(CNN, self).__init__()
"""
---------------------------------------------------------------------------------------
Add the Layeys
---------------------------------------------------------------------------------------
"""
self.conv1 = Conv2D(filters=128, kernel_size=(3, 3), padding='same', strides=(1, 1), activation=tf.nn.relu)
self.maxpool2 = MaxPooling2D(pool_size=(2, 2), padding='valid', strides=(1, 1))
self.conv3 = Conv2D(filters=256, kernel_size=(3, 3), padding='same', strides=(1, 1), activation=tf.nn.relu)
self.maxpool4 = MaxPooling2D(pool_size=(2, 2), padding='valid', strides=(1, 1))
self.flatten = Flatten()
self.dense1 = Dense(units=512, activation=tf.nn.relu)
self.dense2 = Dense(units=128, activation=tf.nn.relu)
self.classification = Dense(units=2, activation=tf.nn.sigmoid)
def call(self, x):
x = self.conv1(x)
x = self.maxpool2(x)
x = self.conv3(x)
x = self.maxpool4(x)
x = self.flatten(x)
x = self.dense1(x)
x = self.dense2(x)
return self.classification(x)
"""
-----------------------------------------------------------------------------------------------
Function for loading TMI dataset and Data-Preprocessing
-----------------------------------------------------------------------------------------------
"""
def load_TMI_data():
"""
-------------------------------------------------------------------------------------------
Load the dataset
-------------------------------------------------------------------------------------------
"""
dataset = scipy.io.loadmat('Dataset/TMI2015/training/training.mat')
"""
-------------------------------------------------------------------------------------------
Split into train and test. Values are in range [0..1] as float64
-------------------------------------------------------------------------------------------
"""
X_train = np.transpose(dataset['train_x'], (3, 0, 1, 2))
y_train = list(dataset['train_y'][0])
X_test = np.transpose(dataset['test_x'], (3, 0, 1, 2))
y_test = list(dataset['test_y'][0])
"""
-------------------------------------------------------------------------------------------
Change shape and range.
-------------------------------------------------------------------------------------------
"""
y_train = np.asarray(y_train).reshape(-1, 1)
y_test = np.asarray(y_test).reshape(-1, 1)
"""
-------------------------------------------------------------------------------------------
1-> 0 : Non-nucleus. 2 -> 1: Nucleus
-------------------------------------------------------------------------------------------
"""
y_test -= 1
y_train -= 1
"""
-------------------------------------------------------------------------------------------
Resize to 32x32
-------------------------------------------------------------------------------------------
"""
X_train_resized = np.empty([X_train.shape[0], 32, 32, X_train.shape[3]])
for i in range(X_train.shape[0]):
X_train_resized[i] = resize(X_train[i], (32, 32, 3), mode='reflect')
X_test_resized = np.empty([X_test.shape[0], 32, 32, X_test.shape[3]])
for i in range(X_test.shape[0]):
X_test_resized[i] = resize(X_test[i], (32, 32, 3), mode='reflect')
"""
-------------------------------------------------------------------------------------------
Normalize images from [0..1] to [-1..1]
-------------------------------------------------------------------------------------------
"""
X_train_resized = 2 * X_train_resized - 1
X_test_resized = 2 * X_test_resized - 1
"""
-------------------------------------------------------------------------------------------
One-hot encoding the train and test labels
-------------------------------------------------------------------------------------------
"""
num_classes = 2
y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)
return X_train_resized, y_train, X_test_resized, y_test
"""
-----------------------------------------------------------------------------------------------
Function to plot the Confusion Matrix
-----------------------------------------------------------------------------------------------
"""
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
"""
-----------------------------------------------------------------------------------------------
Load the TMI dataset and Data-Preprocess the data
-----------------------------------------------------------------------------------------------
"""
x_train, y_train, x_test, y_test = load_TMI_data()
"""
-----------------------------------------------------------------------------------------------
Instantiate a CNN Model
-----------------------------------------------------------------------------------------------
"""
cnn_model = CNN()
"""
-----------------------------------------------------------------------------------------------
Compile the CNN Model
Loss = categorical_crossentropy
Optimizer = Adam
-----------------------------------------------------------------------------------------------
"""
cnn_model.compile(loss=['categorical_crossentropy'],
optimizer = Adam(learning_rate=0.0002, beta_1=0.5, beta_2=0.1),
metrics = ['accuracy'])
"""
-----------------------------------------------------------------------------------------------
Train the CNN Model
-----------------------------------------------------------------------------------------------
"""
start = time.time()
history = cnn_model.fit(x_train, y_train, batch_size=128, epochs=200, verbose=1, validation_data=(x_test, y_test))
end = time.time()
print ("\nTraining time: %0.1f minutes \n" % ((end-start) / 60))
"""
-----------------------------------------------------------------------------------------------
Evaluate the CNN Model
-----------------------------------------------------------------------------------------------
"""
score = cnn_model.evaluate(x_test, y_test, verbose=0)
loss = score[0]
acc = score[1]
print('[INFO] Test loss: {:5.2f}%'.format(100*loss))
print('[INFO] Test accuracy: {:5.2f}%'.format(100*acc))
"""
-----------------------------------------------------------------------------------------------
Save the Trained CNN Model
-----------------------------------------------------------------------------------------------
"""
cnn_model.save('TMI_CNN.model', save_format='tf')
"""
-----------------------------------------------------------------------------------------------
Plot Training History - Accuracies and Losses
-----------------------------------------------------------------------------------------------
"""
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend()
plt.show()
plt.plot(epochs, loss, 'r', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend()
plt.show()
"""
-----------------------------------------------------------------------------------------------
Create a Classification Report
-----------------------------------------------------------------------------------------------
"""
y_pred = np.round(cnn_model.predict(x_test),0)
class_names = ['Non-Nuclei', 'Nuclei']
print("Classification report:\n %s\n"
% (classification_report(y_test, y_pred, target_names=class_names)))
"""
-----------------------------------------------------------------------------------------------
Plot the Confusion Matrix
-----------------------------------------------------------------------------------------------
"""
categorical_test_labels = pd.DataFrame(y_test).idxmax(axis=1)
categorical_preds = pd.DataFrame(y_pred).idxmax(axis=1)
confusion_matrix= confusion_matrix(categorical_test_labels, categorical_preds)
plt.figure()
plot_confusion_matrix(confusion_matrix, class_names, title='Confusion matrix, without normalization')
plt.figure()
plot_confusion_matrix(confusion_matrix, class_names, normalize=True, title='Normalized confusion matrix')