Brain Tumor Classification in Deep Learning

Brain Tumor Classification in Deep Learning

Introduction

We are going to implement a brain tumor classification using Convolutional Neural Network in deep learning. We’ll train the model on the brain images and after training the model,  our model classifies whether the person has a brain tumor or not with the help of the given brain image. The steps for training the model are as follows :-

  1. Import all required libraries
  2. Visualize the data 
  3. Data preprocessing (Data Augmentation) : Augmentation is used to create multiple images from the existing images 
  4. Define our own CNN architecture
  5. Define checkpoint :  checkpoint is used to save the model at a pattern validation accuracy.
  6. Train the model
  7. Load the model
  8. Test on the custom dataset for testing the model 

In this project, I defined my own Convolutional Neural Network architecture and trained the model on the brain images and after training the model, it gave me a 91.66% validation accuracy. And A model saved at 91.66% accuracy with the help of a checkpoint. And I used the data set of brain tumors which was taken from kaggle. This Set contains 155 images who have brain tumors and 98 images who have no brain tumor.


Let’s start :-

Firstly, download the Brain Tumor dataset from the kaggle website. After downloading the dataset, my first step is to import all the required libraries like os, shutil, warnings, tensorflow, numpy etc.

Then I remove the warnings which were generated at the code execution.

Then the next step is to visualize the dataset of brain tumors. Firstly, I made a list of yes type and no type Images of  brain tumor. Then display the first top 5 images of Brain Tumor and display first top five images of no brain tumor with the help of the matplotlib and cv2 library.



Then the next step is to apply a data preprocessing ( data augmentation). Find the path of my data set then apply  data augmentation. Data augmentation is used to create multiple images from an existing image. For example: if we have one image of a person from the front side, so data augmentation creates multiple images from the one person image like it creates a zoom image, increase shear range of image, create zoom image, vertical flip the image, horizontal flip the image, rescale the image etc.


The most important step to define the CNN architecture are as follows : 

→ define Sequential model


→ 1 convolutional layer with 32 filters and relu activation function and input size is (224,224,3)

→ 1 MaxPool2D with pool size is 2


→ 1 convolutional layer with 32 filters and relu activation function and input size is (224,224,3)

→ 1 MaxPool2D with pool size is 2


→ 1 convolutional layer with 32 filters and relu activation function and input size is (224,224,3)

→ 1 MaxPool2D with pool size is 2 


→ Flatten layer to convert into vectors


→  dense layer with 128 units and relu activation function

→  dense layer with 1 units and sigmoid activation function and this is our output layer.


Then check the summary of my model.

Then compile my model with loss binary_crossentropy  because this is the binary classification problem. Then define the checkpoint. Checkpoints save the model at max validation accuracy.

Finally, I trained my Brain Tumor model for 50 epochs. And it gave me 91.66% validation accuracy.


Now it's time to test the model. So I loaded my model using load_model function, then I defined my test function for testing my model with the help of the custom images of the brain.

I gave a custom image to test my model, so I will give an image of a person who has a tumor and check that my model predicts correctly or not. So you can see in the image below that it predicts correctly that a person has a tumor.

Then I gave another image of a person who has no tumor and checked that my model predicts correctly or not. So you can see below image that it predict correctly that person has no tumor.


Source code and how to use:

1. Go to my GitHub  and download code : Brain Tumor Classification

2. Download Dataset : Brain Tumor Dataset

3. After download github repository, Extract the folder.

4. Then download dataset and mention dataset path for train model and run the .ipnyb file in google colab to train your own model and after training, just save the model and use it.

5. After train and save the model, you can use it anytime to classify Brain Tumor and you can use it in your application.

Video Tutorial

Thank You !!!!!!!!!


If you have any doubts, Please let me know

Post a Comment (0)
Previous Post Next Post