-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source code
190 lines (160 loc) · 6.44 KB
/
Source code
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
!pip install opendatasets
import opendatasets as od
dataset1='https://www.kaggle.com/datasets/jakeshbohaju/brain-tumor'
dataset2='https://www.kaggle.com/datasets/navoneel/brain-mri-images-for-brain-tumor-detection'
od.download(dataset1)
od.download(dataset2)
import os
data_dir1='.\\brain-tumor'
data_dir2='.\\brain-mri-images-for-brain-tumor-detection'
os.listdir(data_dir1)
os.listdir(data_dir2)
import os
from os import listdir
paths=[]
for dirname, _, filenames in os.walk('.\\brain-mri-images-for-brain-tumor-detection'):
for filename in filenames:
paths.append(os.path.join(dirname, filename))
print(os.path.join(dirname, filename))
!pip install imutils
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Input, ZeroPadding2D, BatchNormalization, Activation, Dropout, MaxPooling2D, Flatten, Dense
from tensorflow.keras.models import Model, load_model, Sequential
from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from sklearn.utils import shuffle
import matplotlib.pyplot as plt
import time
import imutils
import cv2 #open cv
from PIL import Image
from sklearn.preprocessing import OneHotEncoder
def contour(image , plot = False):
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #grayscaling the images
grayscale = cv2.GaussianBlur(grayscale, (5,5),0) #blur the image to bring it under the threshold
threshold_image = cv2.threshold(grayscale, 50, 255, cv2.THRESH_BINARY)[1] #convert these grayscaled images to binary images
threshold_image = cv2.erode(threshold_image, None, iterations=2) #to remove the regions of noise
threshold_image = cv2.dilate(threshold_image, None, iterations=2) #to remove all the noises around the image
#Now we need to find the contour and clean it to get what is inside the image.
contour = cv2.findContours(threshold_image.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#we grab the largest contour using the max.
contour = imutils.grab_contours(contour)
c = max(contour, key=cv2.contourArea)
#Now we limit the image by finding it's extreme points.
ext_left = tuple(c[c[:,:,0].argmin()][0])
ext_right = tuple(c[c[:,:,0].argmax()][0])
ext_top = tuple(c[c[:,:,1].argmin()][0])
ext_bot = tuple(c[c[:,:,1].argmax()][0])
processed_image = image[ext_top[1]:ext_bot[1],ext_left[0]:ext_right[0]]
if plot:
plt.figure()
plt.subplot(1,2,1)
plt.imshow(image)
plt.tick_params(axis="both", which="both",
top= False, bottom= False,left= False,right= False,
labeltop= False, labelbottom= False,
labelleft= False,labelright= False)
plt.title("ORIGINAL")
plt.subplot(1,2,2)
plt.imshow(processed_image)
plt.tick_params(axis="both", which="both",
top= False, bottom= False,left= False,right= False,
labeltop= False, labelbottom= False,
labelleft= False,labelright= False)
plt.title("PROCESSED")
plt.show()
return processed_image
for path in paths:
img = Image.open(path)
img = img.resize((128,128))
#img.save()
break
Image.open(paths[0])
for path in paths:
img = cv2.imread(path)
img = contour(img, True)
"""plt.imsave(img,path)"""
encoder = OneHotEncoder()
encoder.fit([[0], [1]])
# This cell updates result list for images with tumor
data = []
paths = []
result = []
for r, d, f in os.walk(r'.\yes'):
for file in f:
if '.jpg' in file:
paths.append(os.path.join(r, file))
for path in paths:
img = Image.open(path)
img = img.resize((128,128))
img = np.array(img)
if(img.shape == (128,128,3)):
data.append(np.array(img))
result.append(encoder.transform([[0]]).toarray())
# This cell updates result list for images without tumor
paths = []
for r, d, f in os.walk(r".\no"):
for file in f:
if '.jpg' in file:
paths.append(os.path.join(r, file))
for path in paths:
img = Image.open(path)
img = img.resize((128,128))
img = np.array(img)
if(img.shape == (128,128,3)):
data.append(np.array(img))
result.append(encoder.transform([[1]]).toarray())
data = np.array(data)
data.shape
result = np.array(result)
result = result.reshape(139,2)
x_train,x_test,y_train,y_test = train_test_split(data, result, test_size=0.2, shuffle=True, random_state=0)
model = Sequential()
model.add(Conv2D(32, kernel_size=(2, 2), input_shape=(128, 128, 3), padding = 'Same'))
model.add(Conv2D(32, kernel_size=(2, 2), activation ='relu', padding = 'Same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size = (2,2), activation ='relu', padding = 'Same'))
model.add(Conv2D(64, kernel_size = (2,2), activation ='relu', padding = 'Same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
model.compile(loss = "categorical_crossentropy", optimizer='Adamax')
print(model.summary())
history = model.fit(x_train, y_train, epochs = 30, batch_size = 40, verbose = 1,validation_data = (x_test, y_test))
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Test', 'Validation'], loc='upper right')
plt.show()
def names(cat):
if cat==0:
return 'Tumor detected'
else:
return 'No detected tumor'
from matplotlib.pyplot import imshow
img = Image.open(r"./yes/Y69.jpg")
x = np.array(img.resize((128,128)))
x = x.reshape(1,128,128,3)
res = model.predict_on_batch(x)
classification = np.where(res == np.amax(res))[1][0]
imshow(img)
print(str(res[0][classification]*100) + '% Confidence This Is ' + names(classification))
from matplotlib.pyplot import imshow
img = Image.open(r"./no/N5.jpg")
x = np.array(img.resize((128,128)))
x = x.reshape(1,128,128,3)
res = model.predict_on_batch(x)
classification = np.where(res == np.amax(res))[1][0]
imshow(img)
print(str(res[0][classification]*100) + '% Confidence This Is A ' + names(classification))