-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlzheimerDisease_VGG16_3_Classes.py
382 lines (237 loc) · 15 KB
/
AlzheimerDisease_VGG16_3_Classes.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python
# coding: utf-8
# In[1]:
get_ipython().run_cell_magic('html', '', '\n<!-- Just run this cell to provide style to the notebook-->\n<!-- This is a code to add style to the notebook, it is based on a .css on GitHub http://bit.ly/1Bf5Hft -->\n\n<style>\n\nhtml {\n font-size: 62.5% !important; }\nbody {\n font-size: 1.5em !important; /* currently ems cause chrome bug misinterpreting rems on body element */\n line-height: 1.6 !important;\n font-weight: 400 !important;\n font-family: "Raleway", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif !important;\n color: #222 !important; }\n\ndiv{ border-radius: 0px !important; }\ndiv.CodeMirror-sizer{ background: rgb(244, 244, 248) !important; }\ndiv.input_area{ background: rgb(244, 244, 248) !important; }\n\ndiv.out_prompt_overlay:hover{ background: rgb(244, 244, 248) !important; }\ndiv.input_prompt:hover{ background: rgb(244, 244, 248) !important; }\n\nh1, h2, h3, h4, h5, h6 {\n color: #333 !important;\n margin-top: 0 !important;\n margin-bottom: 2rem !important;\n font-weight: 300 !important;\n text-decoration: underline;\n}\nh1 { font-size: 4.0rem !important; line-height: 1.2 !important; letter-spacing: -.1rem !important;}\nh2 { font-size: 3.6rem !important; line-height: 1.25 !important; letter-spacing: -.1rem !important; }\nh3 { font-size: 3.0rem !important; line-height: 1.3 !important; letter-spacing: -.1rem !important; }\nh4 { font-size: 2.4rem !important; line-height: 1.35 !important; letter-spacing: -.08rem !important; }\nh5 { font-size: 1.8rem !important; line-height: 1.5 !important; letter-spacing: -.05rem !important; }\nh6 { font-size: 1.5rem !important; line-height: 1.6 !important; letter-spacing: 0 !important; }\n \n@media (min-width: 550px) {\n h1 { font-size: 5.0rem !important; }\n h2 { font-size: 4.2rem !important; }\n h3 { font-size: 3.6rem !important; }\n h4 { font-size: 3.0rem !important; }\n h5 { font-size: 2.4rem !important; }\n h6 { font-size: 1.5rem !important; }\n}\n\np {\n margin-top: 0 !important;\n margin-bottom: 1rem !important;\n text-align: justify;\n text-justify: inter-word;\n line-height: 1.5 !important;\n font-size: 1.2em !important;\n font-family: "Raleway", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif !important;\n}\n \na {\n color: #1EAEDB !important; }\na:hover {\n color: #0FA0CE !important; }\n \ncode {\n padding: .2rem .5rem !important;\n margin: 0 .2rem !important;\n font-size: 90% !important;\n white-space: nowrap !important;\n background: #F1F1F1 !important;\n border: 1px solid #E1E1E1 !important;\n border-radius: 4px !important; }\npre > code {\n display: block !important;\n padding: 1rem 1.5rem !important;\n white-space: pre !important; }\n \nbutton{ border-radius: 0px !important; }\n.navbar-inner{ background-image: none !important; }\nselect, textarea{ border-radius: 0px !important; }\n \n#Top_Header {\n background-size: contain;\n background-repeat: no-repeat;\n background-position: left, right;\n}\n\n.output {\n display: flex;\n align-items: center;\n text-align: center;\n}\n \n</style>\n \n<script> \n$( document ).ready(function () {\n $("div#notebook-container").children().first().hide();\n});\n</script>')
# In[ ]:
# In[2]:
get_ipython().run_cell_magic('javascript', '', '//Execute this code to show the styling of the document\n$("div#notebook-container").children().first().show();')
#
# <div id="Top_Header">
# <center>
# <h1>Alzheimer Disease Identification</h1>
# <h3>Deep Learning: Transfer Learning</h3>
# <h6>Daniel Coll, TFM 2019</h6>
# </center>
# </div>
# <div id="Introduction">
# <h2>Introduction</h2>
# <p>This notebook proposes an approach to classify Structural Magnetic Resonance Imaging (MRI) for Alzheimer Disease identification using <i>Convolutional Neural Networks</i> and <i> Transfer Learning</i>.</p>
# <p><i> Transfer Learning</i> is an excellence approach for problems where there isn't a large amount of data. Using pre-trained models helps us increase our accuracy, since these models are trained on datasets with millions of images. Adding our own fully connected layers to adapt these models to this particular problem is the final step for this procedure. </p>
# </div>
# <h2> Importing </h2>
# In[1]:
import pandas as pd
import numpy as np
import os
import tensorflow as tf
import keras
import matplotlib.pyplot as plt
import pickle
import sklearn as skl
import itertools
from PIL import Image
from sklearn.metrics import confusion_matrix
from PIL import Image
from keras.layers import Dense,GlobalAveragePooling2D,Dropout, Flatten
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras.optimizers import Adam
from keras.applications.vgg16 import preprocess_input
# <h1> Auxiliar Functions </h1>
# <p> The following cell includes some useful functions. </p>
# <h1>Path and Labels </h1>
# <p> Labels are stored in the list named "classes" and are ordered according to the structure in the dataset folder where the images are stored. </p>
# <p>The dataset root paths for the training, validation and test sets are stored in four different dictionaries. </p>
# In[59]:
# Classes & datasets
classes = ['AD', "MCI", 'CN']
datasets = ['MPRAGE_3Cat', 'MPRAGE_3CatRed']
# In[3]:
# Dataset's Paths
for dataset in datasets:
vars()['dataset_{}_path'.format(dataset)] = {'train': '../data/Datasets/{}/TrainingSet/'.format(dataset,dataset),
'validation': '../data/Datasets/{}/CrossValSet/'.format(dataset,dataset),
'test':'../data/Datasets/{}/TestSet/'.format(dataset,dataset) }
print('Path to training dataset 1: ', dataset_MPRAGE_3Cat_path.get('train'))
print('Path to validation dataset 2: ', dataset_MPRAGE_3CatRed_path.get('validation'))
print('Path to test dataset 3: ', dataset_MPRAGE_3CatRed_path.get('test'))
# <h1>Neural Network Architecture </h1>
# <p>In Keras, pre-trained models are stored in <i>keras.applications</i> and are treated as layers when designing a network. As we call <i>model.add(Convolution(...)</i>, we would call <i>model.add(Xception(...))</i>. Paremeters for that method are: weights, include_top and input_shape. </p>
#
# <p> We can decide whether to use the pretrained weights by calling <i>weights = 'imagenet' </i> or just stick with the structure and randomly initialize them. Include_top makes reference to include the fully connected layers in the model or not. Usually, in transfer learning, these layers are not included because the output layer differs and we are predicting new classes. Finally, input_shape is the dimension of your images in the classic format [height, width, channels]. There are default dimensions in which the models were trained, but any dimension can be used. The layer would automatically infer changes on the convolutions, poolings etc... </p>
#
# <p> We opted for using default dimensions for each model. First of all, we define a function that returns the model we will use. Once we get the model, since the output shape is a 3D tensor we flatten it. Then, an undefined number of fully connected layers are added, each one with dropout, batch normalization and leaky relu as activation function. The output layer is the final one, and since this is classification problem we use softmax. Finally, we compile it, and we use categorical cross entropy as our loss function since we are using the softmax activation. </p>
# <h2>Importing pre-trained model and adding dense layers. </h2>
# <p> In this section we import InceptionV3 model without its last layer and add some dense layers and a softmax layer.</p>
# In[26]:
base_model=keras.applications.vgg16.VGG16(include_top=False, weights='imagenet', input_tensor=None, input_shape=(255,255,3))
x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(20,activation='relu')(x)
x=Dense(128, activation='relu')(x)
x=Dropout(0.5)(x)
preds=Dense(3,activation='softmax')(x)
# In[27]:
len(base_model.layers)
# In[28]:
base_model.summary()
# <p> Next we reduce the complexity of the model based removing last layers.</p>
# In[29]:
def remove_layers(num_layers):
for iterations in list(range(num_layers-1)):
base_model.layers.pop()
# In[30]:
# remove_layers(5)
# In[31]:
len(base_model.layers)
# In[32]:
base_model.summary()
# <p> Next we make a model based on the architecture we have provided for every dataset.</p>
# In[33]:
model_VGG16 = Model(inputs=base_model.input,outputs=preds)
# In[34]:
model_VGG16.summary()
# <p1> Since we do not have a large dataset, we will set all the base model's parameters as non trainable and use the pre-trained network weights as initialisers </p1>
# In[35]:
def set_nontrainable_layers(models,num_layers):
for model in models:
for layer in model.layers[:num_layers]:
layer.trainable=False
for layer in model.layers[num_layers:]:
layer.trainable=True
# In[36]:
set_nontrainable_layers([model_VGG16],len(base_model.layers))
# <h2> Loading train and validation data into ImageDataGenerators</h2>
# In[37]:
datagen = ImageDataGenerator(preprocessing_function= preprocess_input,
samplewise_center=True,
samplewise_std_normalization=True)
for dataset in datasets:
dataset_path = vars()['dataset_{}_path'.format(dataset)]
print(dataset_path)
vars()['train_generator_{}'.format(dataset)]=datagen.flow_from_directory(
dataset_path.get('train'),
target_size=(255,255),
color_mode='rgb',
batch_size=64,
class_mode='categorical',
shuffle=True)
vars()['val_generator_{}'.format(dataset)]=datagen.flow_from_directory(
dataset_path.get('validation'),
target_size=(255,255),
color_mode='rgb',
batch_size=64,
class_mode='categorical',
shuffle=True)
vars()['test_generator_{}'.format(dataset)]=datagen.flow_from_directory(
dataset_path.get('test'),
target_size=(255,255),
color_mode='rgb',
batch_size=64,
class_mode='categorical',
shuffle=True)
# <h2> Training and Evaluating model</h2>
# <p1> We compile the models using adam as optimizer, categorical cross entropy as loss function and accuracy as evaluation metric </p1>
# In[38]:
opt = keras.optimizers.RMSprop(lr=0.0001)
model_VGG16.compile(optimizer=opt,loss='categorical_crossentropy',metrics=['accuracy'])
# In[39]:
# # Dataset MPRAGE_2CatRed
# step_size_train=train_generator_MPRAGE_2CatRed.n//train_generator_MPRAGE_2CatRed.batch_size
# print(step_size_train)
# history = model_VGG16.fit_generator(generator=train_generator_MPRAGE_2CatRed, validation_data=val_generator_MPRAGE_2CatRed, validation_steps=10,
# steps_per_epoch=step_size_train, epochs=5, verbose=1)
# In[40]:
# Dataset MPRAGE_2Cat
step_size_train=32
print(step_size_train)
history = model_VGG16.fit_generator(generator=train_generator_MPRAGE_3Cat, validation_data=val_generator_MPRAGE_3Cat,
validation_steps=10,steps_per_epoch=step_size_train,
epochs=25)
# ## Training Curve
# In[41]:
# list all data in history
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()
# summarize history for 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', 'val'], loc='upper left')
plt.show()
# ## Singular prediction
# In[51]:
#Get image from to predict Post_Processada_2Cat dataset
img = Image.open('../data/Datasets/MPRAGE_2Cat/TestSet/CN/CN_51-frame000-slice131.jpg')
img = np.asarray(img.resize((255,255), Image.ANTIALIAS))/255
plt.imshow(img)
p = model_VGG16.predict(np.array([img]))
p
# In[42]:
#Get image from to predict Post_Processada_2Cat dataset
img = Image.open('../data/Datasets/MPRAGE_2Cat/TestSet/AD/AD_51-frame000-slice127.jpg')
img = np.asarray(img.resize((255,255), Image.ANTIALIAS))/255
plt.imshow(img)
p = model_VGG16.predict(np.array([img]))
p
# ## Multiple prediction
# In[60]:
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
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')
# In[61]:
# Get images and corresponding label
images_test, y_labels = next(test_generator_MPRAGE_3Cat)
images_test = np.round(images_test)
# Get predictions
y_pred = model_VGG16.predict_generator(test_generator_MPRAGE_3Cat, steps=1, verbose=0)
y_pred = y_pred.argmax(axis=1)
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_labels.argmax(axis=1), y_pred)
np.set_printoptions(precision=2)
# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=classes,
title='Confusion matrix, without normalization')
# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=classes, normalize=True,
title='Normalized confusion matrix')
plt.show()
# In[ ]: