Thursday, February 18, 2021

Brain Tumor Detection Using CNN with Python Keras and Tensorflow

 Brain tumor

Brain Tumor is a very rare condition that very few people usually affected by it. Of any aging brain, tumors can occur, It usually occurs in fifth or sixth decades of the patients i.e., after fifty years. To improve human judgement in diagnosis advent of new technology into health care can be witnessed. An entrance of computer vision into diagnosis would reduce human error in judgment. The MRI scans are capable of identifying even the smallest aberrations in the human body.  

Brain Tumor Types 

In the brain, you can get two types of tumors. They are

1) Benign Tumors:

Benign tumor is completely removed from the brain, can be completely removed. Usually, do not have any problem in their future life.

2) Malignant Tumors:

Malignant tumor can be removed or it can cause recurrence of the brain, but not spread outside of the brain. It recurs into the brain.

Brain Tumor Diagnosis

Diagnosis of Brain Tumor can be done by either doing the CT scan of the brain or MRI of the Brain. MRI usually cannot miss any brain tumor, CT scan sometimes misses brain tumors.

Deep learning

Here we train a model to specifically identify these tiny aberrations from MRIs and predict presence of a tumor with high accuracy. Convolutional Neural Network (CNN) is one of the most effective techniques for this problem statement. Thus using image preprocessing and deep learning using keras and tensorflow, we built a highly reliant and robust model to solve this problem. 

The source code for Convolutional Neural Network is 

Initially the modeules such as tensorflow, keras, numpy and matplotlib are imported.

  1. import tensorflow as tf
  2. import numpy as np
  3. from keras.preprocessing import image
  4. from keras.preprocessing.image import ImageDataGenerator
  5. from matplotlib import pyplot as plt
  6. tf.__version__
Then Data Preprocessing is done with the image training dataset
  1. train_datagen = ImageDataGenerator(rescale = 1./255, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True)
  2. training_set = train_datagen.flow_from_directory('dataset/training_set',  target_size = (64, 64), batch_size = 32, class_mode = 'binary')
Alternatively the preprocessing is done with the image test dataset
  1. test_datagen = ImageDataGenerator(rescale = 1./255)
  2. test_set = test_datagen.flow_from_directory('dataset/test_set', target_size = (64, 64),                batch_size = 32, class_mode = 'binary')
Followed by training and testing the CNN is build with differnt cnn layers such as the convolution layer, maxpool layer, flatten layer, connection layer and the output layer. 
  1. cnn = tf.keras.models.Sequential()
  2. cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[64, 64, 3]))
  3. cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
  4. cnn.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, activation='relu'))
  5. cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
  6. cnn.add(tf.keras.layers.Flatten())
  7. cnn.add(tf.keras.layers.Dense(units=128, activation='relu'))
  8. cnn.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
Once all the layers of CNN are added training is done. For training the optimizer used is 'adam' and 'accuracy' is used as an metrics.
  1. cnn.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
  2. cnn.fit(x = training_set, validation_data = test_set, epochs = 15)

Finally after training CNN, testing is done with an brain tumor image. This final testing code is meant to predict whether the given MRI image is affected y brain tumor or not.

  1. test_image = image.load_img('dataset/2.jpg', target_size = (64, 64))
  2. plt.imshow(test_image)
  3. plt.title('Test Brain Image'), plt.xticks([]), plt.yticks([])
  4. plt.show()
  5. test_image = image.img_to_array(test_image)
  6. test_image = np.expand_dims(test_image, axis = 0)
  7. result = cnn.predict(test_image)
  8. training_set.class_indices
  9. if result[0][0] == 1:
  10.     prediction = 'brain tumor present'
  11. else:
  12.     prediction = 'no brain tumor'
  13. print(prediction)

The output for this code is:

Input test image

Predicted Image



The demo of this code is given in the following link:

For queries contact : researchscholarguidance@gmail.com

No comments:

Post a Comment