Brain Tumour Detection Using Artificial Intelligence (AI)

How to build an AI model that effectively detects brain tumours in MRI scans with 99.4% accuracy.

Yameen Khurshid
7 min readMay 20, 2021

Brain tumours are the leading cause of cancer deaths in young patients. In fact, it kills more children and adults under the age of 40 than any other cancer.

Unfortunately, one of the main reasons for why it continues to exist is because our current methods of tumour detection are ineffective. It is very difficult to simply detect brain tumours through MRI scans.

Now, what if we took a different approach and leveraged a new tool to overcome this challenge?

Here’s how I built an AI model capable of detecting brain tumours…

Artificial Intelligence (AI)

“Artificial Intelligence refers to a machine’s ability to learn and act intelligently — meaning that they can make decisions, carry out tasks and even predict future outcomes based on what they learn from data.” - Credit: Tech Trends in Practice

AI refers to a machine’s ability to learn from data in order to make decisions/predictions about a certain topic.

In our case here, AI can be used to analyze MRI scans (the prediction) to detect brain tumours using datasets (the data) of tumour-negative scans vs. tumour-positive scans.

This will be explained more in-depth with the code in the sections below.

Deep Learning

We can use Deep Learning to enhance our ability to detect brain tumours. Deep learning replicates the functions of the human brain to perform several actions — one of them is image detection.

It does this in the form of Neural Networks — algorithms which help a machine to recognize relationships between large amounts of data. The best thing is, it does this analysis all by itself (with the help of a CNN)!

Convolutional Neural Networks (CNNs)

Now, what’s a CNN.

It’s a type of deep learning model which can be used to analyze images by extracting features from an input image. It does this by applying a filter.

This results in an activation. When this process is repeated, we get a map of activations (also known as a feature map). A feature map indicates the locations and strength of a detected feature in an input, such as an image.

When detecting brain tumours in MRI scans, we can use this principle to detect the opacity level in the scans (as seen below).

This image follows the general idea of how our neural network will work

The process above starts with the input image (MRI scan). After going through the CNN, it transforms into a feature map — the map of activations. From there, it will go through two places:

  1. The Region Proposal — this will over-segment our image by adding a clustering algorithm which helps to eliminate any unnecessary features, improve inter-object definition, smooth image textures and more.
  2. RoI Pooling — Region of Interest Pooling. This helps to determine an area of interest for our model to analyze (for tumours, we are analyzing the sections in the brain).

We repeat this process several times to train our model. As soon as that is completed, our model can finally detect brain tumours!

However, to do all that, we need to code it…

Setting Up

Before we begin coding, we need to follow two important steps.

The first is to download a dataset consisting of licence plate images (these images will be used to train and test our model):

Examples of images:

The next step would be to set up our environment.

For this project, we will be using Google Colaboratory.

If you know how to use Google Colab, feel free to scroll down to the code.

If not, follow this simple guide to learn the basics:

Building the Model

The first step in creating our model is to import the necessary libraries and dependencies:

When that is completed, we must import our dataset using the following code (only do so if your dataset is downloaded on your Google Drive):

After running that code, you will receive a link where you can get your authorization code.

Click on the link, select your Google account and paste your authorization code into the box:

Next, we use the following code to list the files of our dataset:

Output:

The three files of our dataset

We will then apply a one-hot encoding process which helps our ML algorithm(s) in making more accurate predictions:

Output:

Next, we use the following code to make changes to our data:

The code above will transform the “yes tumour” dataset based on several conditions (eg. resize, into array, etc) — allowing our model to analyze the images.

Similarly, we use the same method for our “no tumour” dataset:

Now, let’s determine the shape, dimensions and size of our dataset:

Output:

We can then reshape our dataset:

Output:

Once the data formatting is completed, we can move on to training our model.

However, before we get into that, we need to create the parameters for our train and test environments:

Similarly to what we did previously, we can determine the shape of our dataset (more specifically, the shapes of the files in our dataset):

Output:

Like many AI detection models, we will be using a convolutional neural network (CNN).

Here is how we will add the layers of the neural network to our model (and get a model summary at the end — helping to visualize the layers):

Output:

Now that we have created our CNN, we can move onto compiling our model:

The step above allows us to specify several amounts such as the loss function, optimizer, etc.

More amounts to be specified are below:

As you can see, we specifically specified the number of epochs, batch size.

This will begin our training of 40 epochs of a batch size of 128 (as already specified).

Output:

The final ~20 epochs

That’s not bad! As shown above, our final epoch (#40) ended with an accuracy of 99.4%.

With that completed, our model is now fully trained.

To add a visualization aspect, let’s plot the training history of our model:

Output:

Now we must define the models return for when a scan is tumor-positive/tumor-negative:

The outputs for when our model detects a tumour positive/negative MRI scan

Awesome, everything’s completed. Let’s see how our model predicts based on some images from our dataset:

Essentially, the what code above did was import and reshape the dataset, which allowed our model to output its accuracy + final prediction.

Output:

Scan detected as “Tumour Positive”

We can try this again on several other images.

Outputs:

Scan detected as “Tumour Negative”
Scan detected as “Tumour Negative”
Scan detected as “Tumour Positive”

There you have it! Our model is doing amazing — it’s detecting tumours with >99.4% accuracy!

Without a doubt, the use of AI models in medical settings (real-life brain tumour detection) will have a major role in helping to minimize the deaths involved with brain tumours.

Thanks to AI, tumour detection can finally be more effective and efficient.

Feel free to check out the GitHub repository below for the entire project’s code:

Final Words

It is without a doubt that AI will disrupt every aspect of our lives. Plus, with it’s capabilities increasing exponentially, there is an exciting future ahead involving the implementation of AI in the medical field.

Brain tumour detection is just the beginning…there is still a whole world out there waiting to be explored!

Let’s use these technologies to change the world! It all starts with you.

About the Author

If you have made it this far in the article, I’d like to thank you for your time. And if you didn’t know, I’m Yameen — a 14-year-old Innovator at The Knowledge Society fascinated by the convergence of AI and medicine!

Hopefully you were able to learn something new from this article, however, the best way to really understand a concept is to apply. That’s why I call on you to use the concepts taught in this article to create your own AI model (let me know how it goes!).

As always, if you have any questions, I’m only a message away. Good luck!

--

--

No responses yet