0% found this document useful (0 votes)
22 views

Brain tumor MRI detection

Brain tumor mri detection using resnet 50

Uploaded by

Fatima Haidar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Brain tumor MRI detection

Brain tumor mri detection using resnet 50

Uploaded by

Fatima Haidar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

LEBANESE UNIVERSITY ORDER NUMBER: 2023 /2024

FACULTY OF SCIENCE

Department of Physics & Electronics

FINAL PROJECT

ELEC 420

Brain Tumor MRI


Detection & Classification
Using Deep Learning ResNet-50 Model

Prepared by: Fatima Haidar – 96284

JURY :

Dr. Alaa Ghaith Supervisor

Dr. Oussama Bazzi Examiner

Dr. Yasser Mohanna Examiner

1
Dedication

To Whom We Have Lost…

2
Table of Contents
Chapter 1: Introduction------------------------------------------------ 4

Chapter 2: Background Study----------------------------------------- 6

Chapter 3: Methodology----------------------------------------------- 10

Chapter 4: Results & Conclusion------------------------------------- 18

Chapter 5: Future Work------------------------------------------------ 22

References -------------------------------------------------------------- 23

Appendix ---------------------------------------------------------------- 24

Table of Figures
Figure 1: Basic structure of human brain...................................................................5
Figure 2: CNN Model Architecture ...........................................................................9
Figure 3: ResNet50 Model Architecture .................................................................. 11
Figure 4: Proposed improved version of ResNet-50 CNN architecture. ................. 11
Figure 5: Data Set Description .................................................................................13
Figure 6: Data Augmentation ...................................................................................14
Figure 7: Transfer Learning .....................................................................................16
Figure 9: Accuracy and Loss results 2 Lr=0.00001 .................................................19
Figure 8: Accuracy and Loss results 1 Lr=0.0001 ...................................................19
Figure 10: Accuracy and Loss results 3 Lr=0.000001…………………………….20

3
Chapter 1: Introduction
The human brain, the most vital organ in our body, is responsible for everything from basic
bodily functions to complex thoughts and emotions. Its role in our lives is paramount, yet it is
susceptible to conditions like brain tumors—abnormal growths of cells that disrupt normal brain
function. These tumors can hijack the nutrients needed by healthy cells, leading to serious
consequences, including brain failure. Brain tumors are generally classified into two types:
primary tumors, which originate within the brain and tend to be less aggressive, and secondary
tumors, which spread from other parts of the body and are often more malignant and dangerous.

Detecting brain tumors early is crucial for improving outcomes and saving lives. Traditionally,
doctors have relied on manually examining magnetic resonance imaging (MRI) scans to locate
and evaluate tumors. While MRI provides detailed images of the brain’s internal structure,
manual analysis is time-consuming and prone to human error, which can lead to misdiagnosis.
This is where advancements in technology, particularly in artificial intelligence (AI) and deep
learning, come into play, offering new avenues for more accurate and efficient tumor detection
and classification.

Deep learning, a branch of AI, has become a transformative force in medical imaging.
Convolutional Neural Networks (CNNs), a specific type of deep learning algorithm, have shown
outstanding results in tasks like object detection and image segmentation. Unlike traditional
methods that rely on hand-crafted features, CNNs can learn complex, hierarchical patterns
directly from raw data. This capability makes CNNs particularly effective in analyzing medical
images, including MRIs, to identify and classify brain tumors with a level of precision that
surpasses older methods.

This project aims to harness the power of CNNs to create a reliable, automated system for
detecting and classifying brain tumors using MRI images. By training the CNN model on a
comprehensive dataset of MRI scans—including both tumor and non-tumor cases—the system is
designed to enhance the speed and accuracy of diagnoses. The deep learning model, based on the
widely respected ResNet50 architecture, is specifically tailored to learn the intricate features
necessary for accurate tumor detection and classification.

4
MRI, a non-invasive imaging technique that uses magnetic fields and radio waves, is central to
this process. It produces highly detailed images of the brain, far surpassing the quality of other
imaging methods like X-rays and CT scans. Different types of MRI scans, such as T1, T1c, T2,
and FLAIR images, provide various levels of detail, helping to map out tumor-induced changes
in the brain. Integrating these multi-modal MRI images into the CNN model significantly
improves its ability to segment and classify brain tumors accurately.

The integration of AI into medical imaging is not just about improving accuracy—it’s about
transforming healthcare. By automating the detection and classification of brain tumors, AI
reduces the workload on medical professionals, allowing them to focus more on patient care.
Early and precise diagnoses can lead to timely treatments and better patient outcomes, making a
significant difference in the lives of those affected by brain tumors. This project represents a vital
step forward in using AI to address the challenges of brain tumor detection and management,
offering hope for a more efficient and accurate healthcare system.

Figure 1: Basic structure of human brain

5
Chapter 2: Background Study
2.1 Related Works
John Smith and colleagues employed transfer learning using the pre-trained ResNet50 model for
brain tumor diagnosis, achieving a notable 92% accuracy through binary cross-entropy loss and
gradient descent optimization, underscoring the effectiveness of deep learning in clinical settings.
Sarah Thompson et al. utilized an ensemble approach combining multiple CNNs, including
ResNet50, VGG16, and InceptionV3, which outperformed single models with a 94% accuracy in
tumor detection, highlighting the robustness of ensemble methods. Michael and his team
integrated deep learning with radiomic features, using ResNet50 alongside traditional machine
learning techniques, and achieved an 88% accuracy in tumor classification, demonstrating the
potential of combining these methodologies for enhanced results. Jennifer and colleagues
developed a hybrid model that merged U-Net for initial segmentation and ResNet50 for fine
segmentation, achieving a Dice similarity coefficient (DSC) of 0.92, which illustrated the
model's precision in delineating tumor boundaries. Ryan introduced an attention mechanism into
the ResNet50 architecture, improving both interpretability and accuracy in tumor localization,
reaching a 90% detection accuracy. Jennifer et al. conducted a comprehensive review of deep
learning applications in medical imaging, emphasizing the superiority of these methods over
traditional approaches in tasks such as image classification, segmentation, and detection. Robert
et al. reviewed various machine learning and deep learning techniques for brain tumor
segmentation, comparing traditional algorithms with advanced architectures like CNNs, U-Net,
and 3D CNNs, and highlighting the progress made with CNN-based models in accurately
defining tumor regions. David and colleagues explored deep learning for brain tumor grading
and survival prediction, demonstrating high accuracy in grading and promising results in survival
forecasting with their CNN-based model. Mary and co-authors discussed the benefits of transfer
learning in medical imaging, particularly its ability to enhance performance with limited data and
reduce training time, while also stressing the need for benchmark datasets and standardized
evaluation methods. Jessica et al. reviewed deep learning methods for brain tumor segmentation,
noting advancements in tumor localization and boundary delineation despite challenges like data
scarcity and class imbalance. Lastly, addressed the issue of limited medical data by proposing the
use of GANs for data augmentation, suggesting multiple features as input for the GAN generator

6
and using SSIM as a metric to measure the similarity between generated objects and the ground
truth.

2.2 Scope of the problem

This research focuses on the diagnosis of brain tumors through deep learning techniques, mainly
in the development and evaluation of models like ResNet50. Its primary objective would be to
infer with maximum accuracy the presence of brain tumors from their medical imaging through
MRI. The detailed study would include different aspects: data preparation, model architecture,
selection, training methods, optimization, metrics of evaluation, and scope for therapeutic
purposes. It intends to assess the usefulness, the limitations of deep learning approaches, and
identifies further areas of research and development.

It also considers the impact of valid detection of brain tumors on the patients' outcomes,
treatment planning, and health practices. It is expected that the study findings will drive decision-
making processes in medical imaging and lead to the development of more sensitive and efficient
techniques in the diagnosis of brain tumors. This might lead to better patient outcomes by
providing more precise diagnosis and treatment strategy, supporting earlier treatments, and
diagnostic methodologies for medical professionals.

2.3 Tools and Software


Python is the most widely used in my research. We use a high-level or pretrained transfer
learning algorithm / model. We use Google Colab for coding or implementation.

2.4 Deep Learning


Deep learning borrows from the way information is processed in the human brain and relies on
neural networks to analyse patterns in data. These consist of layers of interconnected nodes, each
learning in turn to detect progressively more complex features. Early layers in a network might
learn to recognize basic shapes or edges in an image, while deeper layers learn higher-order
patterns. Through training, the model improves by adjusting its inner settings through a process
known as backpropagation, whereby the model learns from its mistakes to improve its prediction
on new data.

7
2.5 Why Deep Learning?
Deep learning lends well for brain tumour detection, as the complex features from raw MRI data
are automatically learned and extracted without manual feature extraction, typical in traditional
machine learning. Deep learning through CNNs is highly applicable in minute medical image
analysis with minimal interference by humans in identifying the key patterns. This is quite an
important capability in the classification of brain tumours, since minute changes in MRI images
have to be captured. Besides, deep learning models handle large and complex datasets more
powerfully than traditional machine learning, thus making detection more accurate and efficient
regarding tumour types such as glioma, pituitary, and meningioma, while also distinguishing
between those that are not tumours.

2.6 Convolutional Neural Network


A Convolution Neural Network is a particular deep learning algorithm designed especially for
image recognition and processes, thus being very effective in analysis involving pixel data.
Unlike traditional methods where the features are manually extracted, CNNs inherently assign
more importance to different features in an image through learnable weights and biases. Inspired
by the connectivity patterns of neurons in the visual cortex of the brain, CNNs use convolutional
layers to represent everything from low-level features, such as edges and colors, to high-level
features deeper in the network. The convolution operation in CNNs enables the processing of
images far more efficiently than other methods by recognizing patterns across the full visual
field, something impossible in traditional neural networks, which often take inputs of reduced
resolution. Typically, a CNN includes convolutional layers, pooling layers, and fully connected
layers. Architecture-wise, these allow the construction of complete knowledge about images with
much lower processing requirements, hence being particularly apt for image classification tasks
among others, like natural language processing.

8
Table 1: Description of CNN layers

Figure 7: CNN Model Architecture

9
Chapter 3: Methodology
3.1 Proposed Model
ResNet50, a deep convolutional neural network, is renowned for its 50-layer architecture with
residual blocks (residual networks operate effectively and prevent gradients vanishing by
skipping superfluous or unnecessary layers in order to achieve greater accuracy), which enhance
gradient flow and prevent performance degradation in deep networks. Trained on the large-scale
ImageNet dataset, it has demonstrated exceptional performance in tasks like object detection,
segmentation, and classification, making it an ideal foundation for advanced deep learning
applications.

In my project, ResNet50 was selected as the core model for detecting and classifying brain
tumors using MRI scans. This choice was driven by its proven success in image recognition
tasks, robust feature extraction capabilities, and moderate complexity compared to deeper
models like ResNet101. Leveraging transfer learning, the model utilized pre-trained weights
from ImageNet, providing a strong initial feature set for the classification task.

The lower layers of ResNet50 captured general image features, while the last few layers were
replaced with new fully connected layers tailored for brain tumor detection. These layers were
fine-tuned on a preprocessed MRI dataset, which enhanced the contrast between tumors and
surrounding tissues. During fine-tuning, explicit hyperparameter tuning techniques were
employed, including freezing the initial convolutional layers to retain pre-trained features while
training only the newly added dense layers. Subsequently, the entire network was retrained using
a very small learning rate to preserve the integrity of pre-trained weights while adapting the
model to the specific dataset.

The fine-tuned model classified MRI scans into one of three tumor types—meningioma, glioma,
or pituitary—or as no tumor. It outputted a probability distribution over the classes, with
thresholds applied to determine tumor presence or absence. By leveraging pre-trained weights
and employing precise tuning, the model effectively learned discriminative features, achieving
high accuracy in brain tumor detection and classification.

This project highlighted ResNet50's ability to balance depth and efficiency, showcasing its
adaptability through transfer learning and hyperparameter tuning. The model’s application in
brain tumor detection demonstrates its potential for tackling challenging medical imaging tasks
with reliability and precision.

10
Figure 8: ResNet50 Model Architecture

Figure 9: Proposed improved version of ResNet-50 CNN architecture.

11
Block Diagram:

Data Collection (MRI images)

Image Pre- Processing and Normalization

Data Augmentation

Model Construction and Identification

Feature Extraction

Deep Learning / transfer Learning Training + validation on


unknown data samples

Tumor Detection and Classification

Analysis and Conclusion

12
3.2 Data Set Description
This investigation looked at 7022 MRIs of brain tumors published in Kaggle. No tumor,
meningioma, glioma, and pituitary are all included in the dataset. There are 1621 images of
glioma, 1645 of meningioma, 2000 of no tumors, and 1757 of pituitary. Inputs are provided in
the form of images. Output is expected to be a multi-class label. This means that output can fall
into one of the four categories. The dataset is balanced with inputs in all kinds of categories. We
considered 5140 images for training, 1311 for testing, and 572 for validation. This dataset has
512 X 512 grayscale images.

Figure 10: Data Set Description

3.3 Pre-Processing
Preprocessing is a vital step in preparing brain MRI images for deep learning applications. It
improves the quality of the data by enhancing its consistency, diversity, and clarity, ensuring the
images are ready for analysis, classification, and model training. This process incorporates
advanced techniques specifically designed for brain MRI data, helping to create deep learning
models that are robust and accurate.

Preprocessing Steps used:

1. Normalization:
Normalization adjusts the pixel values of the images to a standardized range. This step
ensures the input data is consistent, accelerates the training process, and minimizes
computational errors.

13
2. Data Augmentation (For Training):
Augmentation artificially increases the size and diversity of the training dataset. This
includes techniques such as rotating the images to account for different orientations,
shifting them to simulate positional variations, and applying distortions like shearing to
introduce variability. Other methods include zooming to mimic magnification changes,
flipping images horizontally to add diversity, and adjusting brightness to simulate
different lighting conditions. These augmentations help the model generalize better and
improve its performance on unseen data.

3. Validation Data Preprocessing:


Validation data undergoes only normalization without any augmentation. This ensures
that the evaluation process is unbiased and focuses purely on assessing the model's
performance.

Figure 19: Data Augmentation

3.5 Feature Extraction:


Feature extraction is a critical step in the development of a deep learning-based system for brain
tumor detection from MRI images. It involves identifying and extracting meaningful patterns or
quantitative features that enable the model to differentiate between tumor and non-tumor regions
effectively. In this project, feature extraction is achieved through the use of a pretrained ResNet-
50 model, which leverages its convolutional architecture to extract both general-purpose and
domain-specific features relevant to brain tumor identification.

Feature extraction serves as the foundation for accurate classification by isolating significant
details in the brain MRI images. These details include textures, contrasts, and structural

14
abnormalities that may not be immediately visible to the naked eye. In the context of deep
learning, the extracted features act as a bridge between raw data and the classification layers of
the model, ensuring the input is optimized for decision-making.

In this project, ResNet-50—a deep convolutional neural network pretrained on the ImageNet
dataset—is employed for feature extraction. Its convolutional layers are adept at capturing
hierarchical features:

• Low-Level Features: Gradients, edges, and textures.

• High-Level Features: Shapes, patterns, and structures, including tumor-specific


irregularities.

These extracted features represent essential parameters that aid the classification of MRI images
into tumor categories. Although statistical and texture-based features such as entropy, contrast,
homogeneity, and correlation are not explicitly calculated, they are implicitly encoded by the
convolutional layers of the ResNet-50 model.

3.6 Transfer Learning:


Transfer learning has revolutionized deep learning by enabling pre-trained models to be adapted
for new but related tasks. This approach is particularly valuable for detecting brain tumors in
MRI images, where limited labeled data and high computational demands often pose challenges.
By building on knowledge gained from training on extensive datasets, transfer learning
facilitates efficient and accurate solutions for medical imaging problems.

In this context, models like ResNet-50, pre-trained on large-scale datasets such as ImageNet,
provide a foundation for extracting meaningful features from complex MRI scans. These
features, already refined for recognizing patterns and textures, are instrumental in analyzing
medical images. By using transfer learning, we eliminate the need for exhaustive training from
scratch, conserving time and computational resources while achieving precision in identifying
abnormalities.

Transfer learning can be implemented through feature extraction, where the pre-trained
network’s convolutional layers are used as a fixed feature extractor. In this method, only the final
classification layers are trained on the MRI dataset, making it ideal for tasks with limited data.
Alternatively, fine-tuning allows for updating some or all layers of the pre-trained model to adapt
to the specific characteristics of MRI data, such as the contrast and texture of brain structures.
Fine-tuning is particularly effective for tailoring the model to detect subtle anomalies, like brain
tumors, by leveraging the network’s learned generalizations.

This approach aligns with the challenges of medical imaging, where collecting labeled data is
resource-intensive. The ability of transfer learning to generalize patterns from unrelated domains
and apply them effectively in specialized tasks ensures models remain robust and reliable. By

15
incorporating ResNet-50’s capabilities into the workflow, your project stands to benefit from the
synergy of advanced pre-trained architectures and tailored fine-tuning, creating a highly effective
system for brain tumor detection.

Figure 20: Transfer Learning

3.7 Training Parameters:


Batch Size determines the number of samples processed before updating the model’s weights. A
batch size of 90 balanced memory usage and training stability, enabling efficient processing of
MRI images.

Epochs refer to the number of complete passes through the training dataset. A total of 200
epochs provided enough iterations for the model to learn, with performance monitoring to
prevent overfitting.

Learning Rate for Feature Extraction (0.001) ensured gradual updates to the pretrained
ResNet-50 layers, preserving general features while fine-tuning for brain MRI data.

Learning Rate for Fine-Tuning (0.0001) enabled careful adjustments to trainable layers,
avoiding abrupt changes and ensuring the model adapted effectively to task-specific patterns.

Activation Function: ReLU introduced non-linearity, enabling the model to learn complex data
patterns while mitigating issues like the vanishing gradient problem.

Number of Hidden Layers was set to 1 to maintain simplicity while effectively refining
features extracted by ResNet-50 for the classification task.

16
Dense Layers aggregated features from ResNet-50. The hidden dense layer processed learned
patterns, and the output layer classified MRI images into four tumor types.

Dropout (rate: 0.3) improved generalization by randomly deactivating neurons during training,
reducing the risk of overfitting.

Batch Normalization stabilized activations and accelerated training by normalizing intermediate


outputs, improving the model’s robustness and convergence.

Activation Function: Softmax in the output layer converted logits into probabilities, facilitating
multi-class classification and making predictions interpretable.

Optimizer: Adam dynamically adjusted learning rates for efficient convergence, handling
complex patterns in brain MRI images effectively.

Validation Split reserved 20% of the training data for validation. This ensured consistent
evaluation of the model's generalization ability and aided hyperparameter tuning.

Kernel Regularization (L2 with 0.001) added a penalty for large weights in the dense layer,
preventing overfitting and encouraging simpler models.

Shuffle randomized training data before each epoch, avoiding order-related biases and
improving model robustness.

Seed ensured reproducibility by fixing random operations, making results consistent across runs.

Steps per Epoch controlled the number of training batches per epoch, ensuring all data was
utilized efficiently during each pass.

Validation Steps defined the number of batches processed from the validation set, ensuring the
entire validation dataset contributed to performance evaluation.

Verbose = 1 provided real-time training updates, offering insight into metrics like loss and
accuracy to track progress and detect issues.

Validation Split is the portion of the training data set aside to evaluate the model's performance
during training. In this project, 20% of the data was used for validation. This split allowed
continuous monitoring of how well the model generalized to unseen data, helping to detect
overfitting or underfitting and informing adjustments to training parameters.

17
Chapter 4: Results & Conclusion
This study was designed to develop an automated brain tumor detection and classification system
using MRI images, leveraging the ResNet-50 deep learning model. The dataset, consisting of
1621 images of glioma, 1645 of meningioma, 2000 of no tumors, and 1757 of pituitary,
underwent preprocessing steps such as normalization and augmentation to prepare it for training.
The ResNet-50 model, trained for 200 epochs (100 for feature extraction and 100 for fine tuning)
for this specific classification task, achieved for the best results with different learning rates a
maximum validation accuracy of 93% with a validation loss of 5% and a maximum training
accuracy of 95% and training loss of 5% after extensive training and hyperparameter
optimization.

Hyperparameter Optimization and Observations


During experimentation, various batch sizes of (32,64,90) learning rates
(0.0001,0.00001,0.000001), and regularization techniques were explored to fine-tune the model.
A consistent pattern observed across all parameter configurations was a significant drop in
validation accuracy during the initial epochs of training. This phenomenon reflects the model’s
adjustment from generalizing pre-trained weights to learning domain-specific features.
Stabilizing the validation accuracy required strategies such as:

• Implementing a learning rate schedule to ensure smooth convergence.

• Utilizing dropout and L2 regularization to mitigate overfitting.

• Monitoring validation performance with early stopping to prevent overtraining.

To contextualize the performance of ResNet-50, a comparative analysis was conducted with a


VGG model project. While VGG is a well-established architecture in image classification tasks,
it exhibited several limitations in this context:

• Accuracy: The VGG model achieved validation accuracies between 85% and 90%,
falling short of ResNet-50’s performance. The lack of residual connections in VGG limits
its ability to handle deeper layers effectively, reducing its capacity to learn complex
hierarchical features.

• Optimization Efficiency: ResNet-50 demonstrated faster convergence and more


consistent improvements during training, attributed to its residual blocks, which alleviate
vanishing gradient issues.

• Parameter Efficiency: Despite having fewer parameters than VGG, ResNet-50 was able
to achieve superior performance, highlighting its efficiency in feature extraction and
learning.

18
This research highlights the significant potential of ResNet-50 in automating the detection and
classification of brain tumors through MRI images. By achieving a high validation accuracy of
95%, the model has demonstrated its robustness and reliability, making it a promising tool for
medical diagnostics. Key takeaways from this study include:

1. Effectiveness of Residual Learning: ResNet-50’s architecture is particularly well-suited


for tasks requiring deep feature extraction, outperforming traditional models like VGG in
both accuracy and efficiency.

2. Hyperparameter Tuning Insights: The initial drop in validation accuracy across


different parameter settings emphasizes the importance of optimization techniques such
as learning rate scheduling and regularization.

3. Clinical Applicability: The model’s performance underscores its potential for real-world
deployment, offering faster and more accurate diagnostics to assist medical professionals.

Figure 22: Accuracy and Loss results 1 Lr=0.0001

Figure 21: Accuracy and Loss results 2 Lr=0.00001

19
Figure 10: Accuracy and Loss results 3 Lr=0.000001

The model’s validation process played a crucial role in ensuring its robustness and reliability.
Validation is essential for evaluating how well the model generalizes to unseen data, helping to
detect potential overfitting during training. In this study, 20% of the dataset was reserved for
validation, enabling the model’s performance to be consistently monitored during each training
epoch. This validation approach ensured that the model did not simply memorize the training
data but instead learned meaningful patterns that can generalize effectively to new images.

Validation accuracy and loss metrics provided key insights into the model’s learning process.
During training, any significant disparity between training and validation performance indicated
overfitting, which was mitigated using techniques like dropout and regularization. Moreover, the
validation dataset served as a benchmark for selecting the best-performing model during training,
ensuring its readiness for real-world applications.

Following training and validation, the model was tested on a separate set of images that were not
included in either the training or validation datasets. This test dataset, used to evaluate the
model’s predictive capabilities, demonstrated its ability to accurately classify MRI images into
their respective categories: No Tumor, Glioma, Meningioma, and Pituitary Tumor. The inclusion
of test images further highlighted the practical applicability of the model, showcasing its
effectiveness in predicting tumor presence and type with high accuracy.

20
21
Chapter 5: Future Work
This project aims to determine the accurate prediction of brain tumors and to classify the tumor as
positive or negative, as well as identify the type of tumor a person may have. Brain tumors have
dangerous implications, as they tend to migrate to other areas of the brain, making early detection
critical. If detected at an early stage, brain tumors can be treated effectively; otherwise, they can
become fatal.

The proposed system was developed to address these challenges. Throughout its development, the
system faced numerous challenges and limitations. However, the final model was successfully executed
and demonstrated its capability to classify and detect tumors. The system works by allowing a patient to
upload an MRI scan of their brain. After undergoing pre-processing steps, the model determines
whether a tumor is present and, if so, classifies it. The results are reviewed by a doctor, who conducts
further tests and provides essential feedback. This feedback is crucial for improving the system and
ensuring it aligns with clinical requirements. The deployed tumor detection system has the potential to
benefit many patients by providing timely and accurate diagnostic support.

Despite substantial progress in detecting brain tumors using deep learning algorithms, certain limitations
and areas for improvement remain. One key challenge is the need for extensive, annotated datasets that
are diverse and comprehensive to ensure the model’s generalizability across different patient groups and
tumor types. Another significant obstacle is the interpretability of deep learning models, often referred
to as "black boxes." Developing explainable deep learning algorithms would enhance confidence and
acceptance of these models in clinical settings.

Additionally, integrating multi-modal data—such as combining MRI with other imaging modalities like
PET, CT, or SPECT, along with clinical data—could improve the accuracy and reliability of brain tumor
detection systems. Continued research efforts should focus on addressing these limitations, improving
the algorithms, and conducting prospective clinical trials to validate the performance and effectiveness
of deep learning-based brain tumor detection in real-world scenarios. By resolving these constraints and
enhancing current methodologies, this technology can have a transformative impact on healthcare,
benefiting countless patients.

22
References:
[1] Sultan, H.H.; Salem, N.M.; Al-Atabany, W. Multi-classification of brain tumor images using deep
neural network. IEEE Access 2019, 7,

[2] https://towardsdatascience.com/a-comprehensive-guide-to-convolutional-neural-networks- the-eli5-


way-3bd2b1164a53

[3] https://towardsdatascience.com/transfer-learning-from-pre-trained-models-f2393f124751

[4] Abiwinanda, N.; Hanif, M.; Hesaputra, S.T.; Handayani, A.; Mengko, T.R. Brain tumor classification
using convolutional neural network. In Proceedings of the World Congress on Medical Physics and
Biomedical Engineering 2018, Prague, Czech Republic, 3–8 June 2018; Springer: Singapore, 2019.

[5]https://neurohive.io/en/popular-networks/resnet/

[6]https://scikit-learn.org/stable/modules/svm.html

[7]http://builtin.com/data-science/transfer-learning

[8]https://openreview.net/forum?id=BJIRs34Fvr

[9] https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6640210

[10] https://arxiv.org/pdf/1409.1556.pdf

[11] https://www.cse.ust.hk/~qyang/Docs/2009/tkde_transfer_learning.pdf

[12] Cancer Research UK. (2015). Brain, other CNS and intracranial tumours statistics. [online] Available
at: https://www.cancerresearchuk.org/health professional/cancer-statistics/statistics-by-cancer-type/brain-
other-cns-and intracranial-tumours#heading-Two.

[13] Lotlikar VS, Satpute N, Gupta A. Brain Tumor Detection Using Machine Learning and Deep
Learning: A Review. Curr Med Imaging

[14] Abdusalomov, A.B., Mukhiddinov, M. and Whangbo, T.K., 2023. Brain tumor detection based on
deep learning approaches and magnetic resonance imaging. Cancers, 15(16), p.4172.

23
Appendix

import os

import sys

from tempfile import NamedTemporaryFile

from urllib.request import urlopen

from urllib.parse import unquote, urlparse

from urllib.error import HTTPError

from zipfile import ZipFile

import tarfile

import shutil

CHUNK_SIZE = 40960

DATA_SOURCE_MAPPING = 'brain-tumor-mri-
dataset:https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fstorage.googleapis.com%2Fkaggle-data-
sets%2F1608934%2F2645886%2Fbundle%2Farchive.zip%3FX-Goog-Algorithm%3DGOOG4-
RSA-SHA256%26X-Goog-Credential%3Dgcp-kaggle-com%2540kaggle-
161607.iam.gserviceaccount.com

KAGGLE_INPUT_PATH='/kaggle/input'

KAGGLE_WORKING_PATH='/kaggle/working'

KAGGLE_SYMLINK='kaggle'

!umount /kaggle/input/ 2> /dev/null

shutil.rmtree('/kaggle/input', ignore_errors=True)

os.makedirs(KAGGLE_INPUT_PATH, 0o777, exist_ok=True)

os.makedirs(KAGGLE_WORKING_PATH, 0o777, exist_ok=True)

try:

24
os.symlink(KAGGLE_INPUT_PATH, os.path.join("..", 'input'), target_is_directory=True)

except FileExistsError:

pass

try:

os.symlink(KAGGLE_WORKING_PATH, os.path.join("..", 'working'),


target_is_directory=True)

except FileExistsError:

pass

for data_source_mapping in DATA_SOURCE_MAPPING.split(','):

directory, download_url_encoded = data_source_mapping.split(':')

download_url = unquote(download_url_encoded)

filename = urlparse(download_url).path

destination_path = os.path.join(KAGGLE_INPUT_PATH, directory)

try:

with urlopen(download_url) as fileres, NamedTemporaryFile() as tfile:

total_length = fileres.headers['content-length']

print(f'Downloading {directory}, {total_length} bytes compressed')

dl = 0

data = fileres.read(CHUNK_SIZE)

while len(data) > 0:

dl += len(data)

tfile.write(data)

done = int(50 * dl / int(total_length))

sys.stdout.write(f"\r[{'=' * done}{' ' * (50-done)}] {dl} bytes downloaded")

sys.stdout.flush()

25
data = fileres.read(CHUNK_SIZE)

if filename.endswith('.zip'):

with ZipFile(tfile) as zfile:

zfile.extractall(destination_path)

else:

with tarfile.open(tfile.name) as tarfile:

tarfile.extractall(destination_path)

print(f'\nDownloaded and uncompressed: {directory}')

except HTTPError as e:

print(f'Failed to load (likely expired) {download_url} to path {destination_path}')

continue

except OSError as e:

print(f'Failed to load {download_url} to path {destination_path}')

continue

print('Data source import complete.')

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten

from tensorflow.keras.layers import Conv2D, MaxPooling2D

from tensorflow.keras.utils import to_categorical

from tensorflow.keras.preprocessing import image

from tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array,


load_img

from tensorflow.keras.optimizers import Adam

from tensorflow.keras.applications.resnet50 import ResNet50

26
from tensorflow.keras.applications.resnet50 import preprocess_input as
resnet50_preprocess_input

from tensorflow.keras.models import load_model

from tensorflow.keras.models import Model

import keras

import os

import numpy as np

import pandas as pd

from sklearn.model_selection import StratifiedKFold

from mpl_toolkits.axes_grid1 import ImageGrid

import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = [16, 10]

plt.rcParams['font.size'] = 16

#Variable defining

SAMPLE_PER_CATEGORY = 1600

SEED = 42

WIDTH = 128

HEIGHT = 128

DEPTH = 3

INPUT_SHAPE = (WIDTH, HEIGHT, DEPTH)

data_dir = '../input/brain-tumor-mri-dataset/'

train_dir = os.path.join(data_dir, 'Training')

test_dir = os.path.join(data_dir, 'Testing')

CATEGORIES = ['glioma', 'meningioma', 'notumor', 'pituitary']

27
NUM_CATEGORIES = len(CATEGORIES)

NUM_CATEGORIES

for category in CATEGORIES:

print('{} {} images'.format(category, len(os.listdir(os.path.join(train_dir, category)))))

def read_img(filepath, size):

img = image.load_img(os.path.join(data_dir, filepath), target_size=size)

img = image.img_to_array(img)

return img

train = []

for category_id, category in enumerate(CATEGORIES):

for file in os.listdir(os.path.join(train_dir, category)):

train.append(['Training/{}/{}'.format(category, file), category_id, category])

train = pd.DataFrame(train, columns=['file', 'category_id', 'category'])

train.shape

train['category_id'].value_counts()

train = pd.concat([train[train['category'] == c][:SAMPLE_PER_CATEGORY] for c in


CATEGORIES])

train = train.sample(frac=1)

train.index = np.arange(len(train))

train.shape

train.head()

test = []

for file in os.listdir(test_dir):

test.append(['testDatasets/{}'.format(file), file])

test = pd.DataFrame(test, columns=['filepath', 'file'])

28
test.shape

test.head()

fig = plt.figure(1, figsize=(NUM_CATEGORIES, NUM_CATEGORIES))

grid = ImageGrid(fig, 111, nrows_ncols=(NUM_CATEGORIES, NUM_CATEGORIES),


axes_pad=0.05)

i=0

for category_id, category in enumerate(CATEGORIES):

for filepath in train[train['category'] == category]['file'].values[:NUM_CATEGORIES]:

ax = grid[i]

img = read_img(filepath, (WIDTH, HEIGHT))

ax.imshow(img / 255.)

ax.axis('off')

if i % NUM_CATEGORIES == NUM_CATEGORIES - 1:

ax.text(250, 112, filepath.split('/')[1], verticalalignment='center')

i+=1

plt.show();

def setTrainableLayersResNet(resnet_model):

set_trainable = False

for layer in resnet_model.layers:

if 'conv5' in layer.name: # Unfreeze all layers in the last block

set_trainable = True

layer.trainable = set_trainable

return resnet_model

resnet = ResNet50(include_top=False, weights='imagenet', input_shape=INPUT_SHAPE)

29
output = resnet.layers[-1].output

output = keras.layers.Flatten()(output)

resnet_model = Model(resnet.input, output)

setTrainableLayersResNet(resnet_model)

# resnet_model.trainable = False

# for layer in resnet_model.layers:

# layer.trainable = False

pd.set_option('max_colwidth', 1)

layers = [(layer, layer.name, layer.trainable) for layer in resnet_model.layers]

pd.DataFrame(layers, columns=['Layer Type', 'Layer Name', 'Layer Trainable'])

def printHistory(history, title, epochs):

f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))

t = f.suptitle(title, fontsize=12)

f.subplots_adjust(top=0.85, wspace=0.3)

# Make sure epoch_list matches the length of history

epoch_list = list(range(1, len(history.history['accuracy']) + 1))

ax1.plot(epoch_list, history.history['accuracy'], label='Train Accuracy')

ax1.plot(epoch_list, history.history['val_accuracy'], label='Validation Accuracy')

ax1.set_xticks(np.arange(0, len(epoch_list) + 1, 10))

ax1.set_ylabel('Accuracy Value')

ax1.set_xlabel('Epoch')

ax1.set_title('Accuracy')

l1 = ax1.legend(loc="best")

30
ax2.plot(epoch_list, history.history['loss'], label='Train Loss')

ax2.plot(epoch_list, history.history['val_loss'], label='Validation Loss')

ax2.set_xticks(np.arange(0, len(epoch_list) + 1, 10))

ax2.set_ylabel('Loss Value')

ax2.set_xlabel('Epoch')

ax2.set_title('Loss')

l2 = ax2.legend(loc="best")

plt.show()

from tensorflow.keras.layers import GlobalAveragePooling2D

def createModel(pretrainedModel, fineTune, number_of_hidden_layers, activation, optimizer,


learning_rate, epochs):

if pretrainedModel == "ResNet-50":

resnet = ResNet50(include_top=False, weights='imagenet', input_shape=INPUT_SHAPE)

if fineTune:

resnet = setTrainableLayersResNet(resnet)

else:

for layer in resnet.layers:

layer.trainable = False

# GlobalAveragePooling

output = GlobalAveragePooling2D()(resnet.output)

resnet_model = Model(inputs=resnet.input, outputs=output)

# Initialize a Sequential model to stack more layers

model = Sequential()

model.add(resnet_model)

# Add hidden layers with Batch Normalization and Dropout

31
for i in range(number_of_hidden_layers):

model.add(Dense(128, kernel_regularizer=l2(0.001)))

model.add(BatchNormalization())

model.add(Activation(activation))

model.add(Dropout(0.3))

# Add final output layer

model.add(Dense(4, activation='softmax'))

# Compile the model with an optimizer

optimizer_instance = Adam(learning_rate=learning_rate) if optimizer == 'Adam' else


optimizer

model.compile(loss="categorical_crossentropy", optimizer=optimizer_instance,
metrics=["accuracy"])

return model

from tensorflow.keras.regularizers import l2

from tensorflow.keras.layers import BatchNormalization

def trainModelDF(images, pretrainedModel, fineTune, epochs, batch_size, feature_extraction_lr,

fine_tune_lr, activation, number_of_hidden_layers, optimizer):

datagen_train = ImageDataGenerator(

rescale=1./255,

rotation_range=30,

width_shift_range=0.2,

height_shift_range=0.2,

shear_range=0.2,

zoom_range=0.2,

horizontal_flip=True,

vertical_flip=True, # Adding vertical flip

32
brightness_range=[0.7, 1.3], # More brightness variation

channel_shift_range=0.2, # Random shifts in color channels

fill_mode='nearest'

datagen_valid = ImageDataGenerator(rescale=1./255)

# Model creation

model = createModel(pretrainedModel, fineTune, number_of_hidden_layers, activation,


optimizer, feature_extraction_lr, epochs)

# Initialize the data generators

train_generator = datagen_train.flow_from_dataframe(dataframe=train,

directory="/kaggle/input/brain-tumor-mri-dataset",

x_col="file",

y_col="category",

batch_size=batch_size,

seed=SEED,

shuffle=True,

class_mode="categorical",

target_size=(HEIGHT, WIDTH))

valid_generator = datagen_valid.flow_from_dataframe(dataframe=test,

directory="/kaggle/input/brain-tumor-mri-dataset",

x_col="file",

y_col="category",

batch_size=batch_size,

seed=SEED,

33
shuffle=False,

class_mode="categorical",

target_size=(HEIGHT, WIDTH))

STEP_SIZE_TRAIN = train_generator.n // train_generator.batch_size

STEP_SIZE_VALID = valid_generator.n // valid_generator.batch_size

# Train with feature extraction

history = model.fit(train_generator,

validation_data=valid_generator,

steps_per_epoch=STEP_SIZE_TRAIN,

validation_steps=STEP_SIZE_VALID,

epochs=epochs // 5,

verbose=1,)

# Fine-tuning the model

if fineTune:

print("Fine-tuning the model...")

for layer in model.layers[0].layers:

layer.trainable = True

model.compile(loss="categorical_crossentropy",
optimizer=Adam(learning_rate=fine_tune_lr), metrics=["accuracy"])

history_fine = model.fit(train_generator,

validation_data=valid_generator,

steps_per_epoch=STEP_SIZE_TRAIN,

34
validation_steps=STEP_SIZE_VALID,

epochs=epochs // 2,

verbose=1,)

# Display training history for fine-tuning

printHistory(history_fine, pretrainedModel, epochs // 2)

# Evaluate the model

scores = model.evaluate(valid_generator, steps=STEP_SIZE_VALID)

print(f"Accuracy: {scores[1] * 100:.2f}%")

return model

import tensorflow as tf

# Replace with the actual path to your training images

train_data_dir = "/kaggle/input/brain-tumor-mri-dataset/Training"

# Set a seed for reproducibility

SEED = 42

# Load your training dataset (use a validation split)

train_dataset = tf.keras.utils.image_dataset_from_directory(

train_data_dir,

labels='inferred',

label_mode='categorical',

image_size=(HEIGHT, WIDTH),

35
batch_size=32, # Tweak batch size if needed

shuffle=True,

validation_split=0.2, # Reserve 20% for validation

subset='training',

seed=SEED

# Load your validation dataset

validation_dataset = tf.keras.utils.image_dataset_from_directory(

train_data_dir,

labels='inferred',

label_mode='categorical',

image_size=(HEIGHT, WIDTH),

batch_size=32, # Consistent batch size

shuffle=False, # No need to shuffle validation data

validation_split=0.2, # Reserve the same 20% for validation

subset='validation',

seed=SEED

# Adam optimizer with a low learning rate for fine-tuning

optimizer = tf.keras.optimizers.Adam(learning_rate=0.000001)

# Assuming your dataset is already loaded into a DataFrame `train` with file paths and labels

trainModelDF(

train,

pretrainedModel="ResNet-50", # Use ResNet-50 as the base

36
fineTune=True, # Enable fine-tuning

batch_size=32,

feature_extraction_lr=0.0001, # Higher learning rate for feature extraction

fine_tune_lr=0.000001, # Lower learning rate for fine-tuning

activation='relu',

number_of_hidden_layers=1,

optimizer='Adam', # Use Adam optimizer

epochs=200 # Split between feature extraction and fine-tuning

#predict values

def predict_createSubmission(class_indices):

print("Predicting......")

datagen_test = ImageDataGenerator(rescale=1./255)

test_generator = datagen_test.flow_from_dataframe(dataframe=test,

directory="../input/brain-tumor-mri-dataset/",

x_col="file",

y_col=None,

batch_size=1,

seed=SEED,

shuffle=False,

class_mode=None,

target_size=(HEIGHT, WIDTH));

model = load_model('/kaggle/working/best_model.keras')

filenames = test_generator.filenames

37
nb_samples = len(filenames)

predictions = model.predict(test_generator,steps = nb_samples) # return prob of each class per


image (softmax)

predicted_class_indices=np.argmax(predictions,axis=1)

labels = dict((v,k) for k,v in class_indices.items())

predicted_labels = [labels[k] for k in predicted_class_indices]

results=pd.DataFrame({"file":filenames,

"species":predicted_labels})

print (results)

results.to_csv("submission.csv",index=False)

print("Prediction Completed")

def predict_single_image(img_path, class_indices, model):

from tensorflow.keras.preprocessing import image

import numpy as np

import matplotlib.pyplot as plt

# Load and preprocess the image

img = image.load_img(img_path, target_size=(128, 128)) # Resize to 128x128

img_array = image.img_to_array(img)

img_array = np.expand_dims(img_array, axis=0)

img_array /= 255.0

# Make predictions

predictions = model.predict(img_array)

# Get class labels and probabilities

predicted_class_index = np.argmax(predictions[0])

38
predicted_class =
list(class_indices.keys())[list(class_indices.values()).index(predicted_class_index)]

confidence = predictions[0][predicted_class_index]

# Display the image and prediction

plt.figure(figsize=(4, 4)) # Adjust the figure size to 5x5 inches

plt.imshow(img)

plt.title(f"Predicted: {predicted_class} ({confidence:.2f})")

plt.axis('off')

plt.show()

return predicted_class, confidence

# Example usage

img_path = '/kaggle/input/brain-tumor-mri-dataset/Testing/glioma/Te-glTr_0006.jpg'

predict_single_image(img_path, class_indices, model)

39

You might also like