-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCnn_model.py
441 lines (336 loc) · 14.9 KB
/
Cnn_model.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# -*- coding: utf-8 -*-
"""bdmh_project_code.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1sm-VUhRRBpclgBlVKnldCh_aJ3-A3nbw
"""
"""!pip uninstall opencv-python -y
!pip install opencv-contrib-python==3.4.2.17 --force-reinstall
import zipfile
with zipfile.ZipFile("/content/drive/My Drive/BDMH_PROJECT/brain-mri-images-for-brain-tumor-detection.zip","r") as zip_ref:
zip_ref.extractall("/content/drive/My Drive/BDMH_PROJECT/dataset_bdmh_braintumor")
"""
from matplotlib import pyplot as plt
import numpy as np
import cv2
from skimage.feature import hog, local_binary_pattern
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import random
from sklearn.svm import SVC
import os
from keras.preprocessing.image import ImageDataGenerator
from sklearn.decomposition import PCA
from collections import Counter
from imblearn.over_sampling import SMOTE
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from xgboost import XGBClassifier
from imblearn.over_sampling import SMOTE
#input_path = "/content/drive/My Drive/BDMH_PROJECT/dataset_bdmh_braintumor"
input_path="Dataset"
CATEGORIES = ["no", "yes"]
def create_training_data():
training_data = []
for category in CATEGORIES:
path = os.path.join(input_path, category)
category_index = CATEGORIES.index(category)
for image in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path, image), cv2.IMREAD_GRAYSCALE)
#img_array = cv2.imread(os.path.join(path, image), cv2.IMREAD_REDUCED_GRAYSCALE_2)
img_array = img_array.astype(np.float32)
img_array = cv2.resize(img_array, (128,128))
training_data.append([img_array, category_index])
except Exception as e:
print(e)
return training_data
training_data=create_training_data()
random.shuffle(training_data)
#print(training_data[0])
data_set = []
data_label = []
for feature,label in training_data:
data_set.append(feature)
data_label.append(label)
plt.imshow(data_set[132])
data_set=np.array(data_set)
data_set=data_set/255.0
print(data_set.shape)
data_set_flatten=data_set.reshape(data_set.shape[0],data_set.shape[1]*data_set.shape[1])
oversample = SMOTE()
data_set_flatten, data_label = oversample.fit_resample(data_set_flatten, data_label)
data_set=data_set_flatten.reshape(data_set_flatten.shape[0],data_set.shape[1],data_set.shape[1])
counter=Counter(data_label)
print(counter)
print(data_set_flatten.shape)
print(data_set.shape)
def get_hog(train_images_temp):
hog_features_train=[]
for i in range(0,len(train_images_temp)):
fd = hog(train_images_temp[i],orientations=8,pixels_per_cell=(16,16),cells_per_block=(2,2),block_norm='L2')
hog_features_train.append(fd)
hog_features_train=np.array(hog_features_train)
return hog_features_train
def get_surf(images, name='surf', save=False):
# SURF descriptor for 1 image
def get_image_surf(image, vector_size=8):
image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX).astype('uint8')
alg = cv2.xfeatures2d.SURF_create()
kps = alg.detect(image, None)
kps = sorted(kps, key=lambda x: -x.response)[:vector_size]
# Making descriptor of same size
# Descriptor vector size is 64
needed_size = (vector_size * 128)
if len(kps) == 0:
return np.zeros(needed_size)
kps, dsc = alg.compute(image, kps)
dsc = dsc.flatten()
if dsc.size < needed_size:
# if we have less than 32 descriptors then just adding zeros at the
# end of our feature vector
dsc = np.concatenate([dsc, np.zeros(needed_size - dsc.size)])
return dsc
# SURF descriptor for all images
features = []
for i, img in enumerate(images):
dsc = get_image_surf(img)
features.append(dsc)
result = np.array(features)
return result
def get_sift(images, name='sift'):
# SIFT descriptor for 1 image
def get_image_sift(image, vector_size=8):
image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX).astype('uint8')
alg = cv2.xfeatures2d.SIFT_create()
kps = alg.detect(image, None)
kps = sorted(kps, key=lambda x: -x.response)[:vector_size]
# Making descriptor of same size
# Descriptor vector size is 128
needed_size = (vector_size * 128)
if len(kps) == 0:
return np.zeros(needed_size)
kps, dsc = alg.compute(image, kps)
dsc = dsc.flatten()
if dsc.size < needed_size:
# if we have less than 32 descriptors then just adding zeros at the
# end of our feature vector
dsc = np.concatenate([dsc, np.zeros(needed_size - dsc.size)])
return dsc
# SIFT descriptor for all images
features = []
for i, img in enumerate(images):
dsc = get_image_sift(img)
features.append(dsc)
result = np.array(features)
return result
def get_kaze(images, name='kaze', save=False):
# KAZE descriptor for 1 image
def get_image_kaze(image, vector_size=32):
alg = cv2.KAZE_create()
kps = alg.detect(image)
kps = sorted(kps, key=lambda x: -x.response)[:vector_size]
# Making descriptor of same size
# Descriptor vector size is 64
needed_size = (vector_size * 64)
if len(kps) == 0:
return np.zeros(needed_size)
kps, dsc = alg.compute(image, kps)
dsc = dsc.flatten()
if dsc.size < needed_size:
# if we have less than 32 descriptors then just adding zeros at the
# end of our feature vector
dsc = np.concatenate([dsc, np.zeros(needed_size - dsc.size)])
return dsc
# KAZE descriptor for all images
features = []
for i, img in enumerate(images):
dsc = get_image_kaze(img)
features.append(dsc)
result = np.array(features)
return result
train_data_hog=get_hog(data_set)
train_data_sift=get_sift(data_set)
train_data_surf=get_surf(data_set)
train_data_kaze=get_kaze(data_set)
print(train_data_hog.shape)
print(train_data_sift.shape)
print(train_data_surf.shape)
print(train_data_kaze.shape)
data_set_hog=get_hog(data_set)
data_set_hog.shape
X_train1,X_test1,y_train1,y_test1=train_test_split(train_data_hog,data_label,test_size=0.2,random_state=42)
X_train2,X_test2,y_train2,y_test2=train_test_split(train_data_sift,data_label,test_size=0.2,random_state=42)
X_train3,X_test3,y_train3,y_test3=train_test_split(train_data_surf,data_label,test_size=0.2,random_state=42)
X_train4,X_test4,y_train4,y_test4=train_test_split(train_data_kaze,data_label,test_size=0.2,random_state=42)
X_train5,X_test5,y_train5,y_test5=train_test_split(data_set_flatten,data_label,test_size=0.2,random_state=42)
random_forest_list=[]
random_forest_list1=[]
model1=RandomForestClassifier()
model2=RandomForestClassifier()
model3=RandomForestClassifier()
model4=RandomForestClassifier()
model5=RandomForestClassifier()
model1=model1.fit(X_train1,y_train1)
model2=model2.fit(X_train2,y_train2)
model3=model3.fit(X_train3,y_train3)
model4=model4.fit(X_train4,y_train4)
model5=model5.fit(X_train5,y_train5)
random_forest_list1.append(model1.score(X_train1,y_train1))
random_forest_list1.append(model2.score(X_train2,y_train2))
random_forest_list1.append(model3.score(X_train3,y_train3))
random_forest_list1.append(model4.score(X_train4,y_train4))
random_forest_list1.append(model5.score(X_train5,y_train5))
#print("Using Random Forest and hog Transformation",model1.score(X_test1,y_test1))
random_forest_list.append(model1.score(X_test1,y_test1))
random_forest_list.append(model2.score(X_test2,y_test2))
random_forest_list.append(model3.score(X_test3,y_test3))
random_forest_list.append(model4.score(X_test4,y_test4))
random_forest_list.append(model5.score(X_test5,y_test5))
print(random_forest_list)
print(random_forest_list1)
random_forest_list=[]
random_forest_list1=[]
model1=SVC()
model2=SVC()
model3=SVC()
model4=SVC()
model5=SVC()
model1=model1.fit(X_train1,y_train1)
model2=model2.fit(X_train2,y_train2)
model3=model3.fit(X_train3,y_train3)
model4=model4.fit(X_train4,y_train4)
model5=model5.fit(X_train5,y_train5)
random_forest_list1.append(model1.score(X_train1,y_train1))
random_forest_list1.append(model2.score(X_train2,y_train2))
random_forest_list1.append(model3.score(X_train3,y_train3))
random_forest_list1.append(model4.score(X_train4,y_train4))
random_forest_list1.append(model5.score(X_train5,y_train5))
#print("Using Random Forest and hog Transformation",model1.score(X_test1,y_test1))
random_forest_list.append(model1.score(X_test1,y_test1))
random_forest_list.append(model2.score(X_test2,y_test2))
random_forest_list.append(model3.score(X_test3,y_test3))
random_forest_list.append(model4.score(X_test4,y_test4))
random_forest_list.append(model5.score(X_test5,y_test5))
print(random_forest_list)
print(random_forest_list1)
random_forest_list=[]
random_forest_list1=[]
model1=XGBClassifier()
model2=XGBClassifier()
model3=XGBClassifier()
model4=XGBClassifier()
model5=XGBClassifier()
model1=model1.fit(X_train1,y_train1)
model2=model2.fit(X_train2,y_train2)
model3=model3.fit(X_train3,y_train3)
model4=model4.fit(X_train4,y_train4)
model5=model5.fit(X_train5,y_train5)
random_forest_list1.append(model1.score(X_train1,y_train1))
random_forest_list1.append(model2.score(X_train2,y_train2))
random_forest_list1.append(model3.score(X_train3,y_train3))
random_forest_list1.append(model4.score(X_train4,y_train4))
random_forest_list1.append(model5.score(X_train5,y_train5))
#print("Using Random Forest and hog Transformation",model1.score(X_test1,y_test1))
random_forest_list.append(model1.score(X_test1,y_test1))
random_forest_list.append(model2.score(X_test2,y_test2))
random_forest_list.append(model3.score(X_test3,y_test3))
random_forest_list.append(model4.score(X_test4,y_test4))
random_forest_list.append(model5.score(X_test5,y_test5))
print(random_forest_list)
print(random_forest_list1)
fig = plt.figure(figsize = (7, 4))
courses=['hog','sift','surf','kaze','NO']
# creating the bar plot
plt.bar(courses, random_forest_list, color ='maroon',
width = 0.5)
plt.xlabel("Feature Selection")
plt.ylabel("Testing accuracy for Random Forest")
plt.title("Testing accuracy vs Features selection technique")
plt.show()
#CNN
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from keras.utils import to_categorical
from keras.callbacks import ModelCheckpoint
from keras.callbacks import EarlyStopping
#VGG-16 CNN Model
def CNN_VGG16_model():
inputs=keras.Input(shape=(data_set.shape[1],data_set.shape[1],1),dtype=tf.float32)
x=inputs
x=keras.layers.Conv2D(16,(3,3),activation='relu')(x)
x=keras.layers.Conv2D(16,(3,3),activation='relu')(x)
x=keras.layers.MaxPooling2D((2,2))(x)
x=keras.layers.Conv2D(32,(3,3),activation='relu')(x)
x=keras.layers.Conv2D(32,(3,3),activation='relu')(x)
x=keras.layers.MaxPooling2D((2,2))(x)
x=keras.layers.Conv2D(64,(3,3),activation='relu')(x)
x=keras.layers.Conv2D(64,(3,3),activation='relu')(x)
x=keras.layers.Conv2D(64,(3,3),activation='relu')(x)
x=keras.layers.MaxPooling2D((2,2))(x)
x=keras.layers.Conv2D(128,(3,3),activation='relu')(x)
x=keras.layers.Conv2D(128,(3,3),activation='relu')(x)
x=keras.layers.Conv2D(128,(3,3),activation='relu')(x)
x=keras.layers.MaxPooling2D((2,2))(x)
# x=keras.layers.Conv2D(512,(3,3),activation='relu')(x)
# x=keras.layers.Conv2D(512,(3,3),activation='relu')(x)
# x=keras.layers.Conv2D(512,(3,3),activation='relu')(x)
# x=keras.layers.MaxPooling2D((2,2))(x)
x=keras.layers.Flatten()(x)
x=keras.layers.Dense(50,activation='relu')(x)
x=keras.layers.Dense(50,activation='relu')(x)
output=keras.layers.Dense(1,activation='sigmoid')(x)
model=keras.Model(inputs=inputs,outputs=output,name='CNN_VGG16_Model')
return model
data_label=np.array(data_label)
data_set=np.array(data_set)
vgg_model=CNN_VGG16_model()
vgg_model.summary()
es=tf.keras.callbacks.EarlyStopping(monitor='val_loss',mode='min',verbose=1,patience=6)
mc = tf.keras.callbacks.ModelCheckpoint('best_model.h5', monitor='val_accuracy', mode='max', verbose=1, save_best_only=True)
vgg_model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
first_nn_fitted=vgg_model.fit(data_set,data_label,validation_split=0.2,epochs=40,verbose=1,callbacks=[es,mc])
def base_model_func1():
inputs=keras.Input(shape=(data_set.shape[1],data_set.shape[1],1),dtype=tf.float32)
x=inputs
x=keras.layers.Conv2D(64,(3,3),activation='relu')(x)
x=keras.layers.Conv2D(64,(3,3),activation='relu')(x)
x=keras.layers.MaxPooling2D((2,2))(x)
#x=keras.layers.Dropout(0.4)(x)
x=keras.layers.Conv2D(128,(3,3),activation='relu')(x)
x=keras.layers.Conv2D(128,(3,3),activation='relu')(x)
x=keras.layers.MaxPooling2D((2,2))(x)
#x=keras.layers.Dropout(0.4)(x)
# x=keras.layers.Conv2D(128,(3,3),activation='relu')(x)
# x=keras.layers.Conv2D(128,(3,3),activation='relu')(x)
# x=keras.layers.MaxPooling2D((2,2))(x)
#x=keras.layers.Dropout(0.4)(x)
x=keras.layers.Flatten()(x)
x=keras.layers.Dense(20,activation='relu')(x)
# x=keras.layers.Dropout(0.2)(x)
# x=keras.layers.Dense(25,activation='relu')(x)
#x=keras.layers.Dense(8,activation='relu')(x)
output=keras.layers.Dense(1,activation='tanh')(x)
model=keras.Model(inputs=inputs,outputs=output,name='CNN_base_model')
return model
#data_set=data_set.reshape(data_set.shape[0],data_set.shape[1]*data_set.shape[1])
data_label=np.array(data_label)
base_model=base_model_func1()
base_model.summary()
es=tf.keras.callbacks.EarlyStopping(monitor='val_loss',mode='min',verbose=1,patience=10)
mc = tf.keras.callbacks.ModelCheckpoint('best_model.h5', monitor='val_accuracy', mode='max', verbose=1, save_best_only=True)
base_model.compile(optimizer='adam',loss='squared_hinge',metrics=['accuracy'])
first_nn_fitted=base_model.fit(data_set,data_label,validation_split=0.2,epochs=40,verbose=1,callbacks=[es,mc])
#PLOTTING EPOCHS VS LOSS AND ACCURCY FOR BASE MODELS
fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.plot(range(len(first_nn_fitted.history['loss'])), first_nn_fitted.history['loss'],linestyle='-', color='blue',label='Training', lw=2)
ax1.plot(range(len(first_nn_fitted.history['val_loss'])), first_nn_fitted.history['val_loss'], linestyle='-', color='green',label='Test', lw=2)
ax2.plot(range(len(first_nn_fitted.history['accuracy'])), first_nn_fitted.history['accuracy'],linestyle='-', color='blue',label='Training', lw=2)
ax2.plot(range(len(first_nn_fitted.history['val_accuracy'])), first_nn_fitted.history['val_accuracy'], linestyle='-', color='green',label='Test', lw=2)
leg = ax1.legend(bbox_to_anchor=(0.7, 0.9), loc=2, borderaxespad=0.,fontsize=13)
ax1.set_xticklabels('')
#ax1.set_yscale('log')
ax2.set_xlabel('# Epochs',fontsize=14)
ax1.set_ylabel('Loss',fontsize=14)
ax2.set_ylabel('Accuracy',fontsize=14)
plt.show()