Brain tumor MRI detection
Brain tumor MRI detection
FACULTY OF SCIENCE
FINAL PROJECT
ELEC 420
JURY :
1
Dedication
2
Table of Contents
Chapter 1: Introduction------------------------------------------------ 4
Chapter 3: Methodology----------------------------------------------- 10
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.
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.
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.
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.
8
Table 1: Description of CNN layers
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
11
Block Diagram:
Data Augmentation
Feature Extraction
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.
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.
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.
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:
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.
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.
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.
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.
• 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.
• 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:
3. Clinical Applicability: The model’s performance underscores its potential for real-world
deployment, offering faster and more accurate diagnostics to assist medical professionals.
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,
[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
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'
shutil.rmtree('/kaggle/input', ignore_errors=True)
try:
24
os.symlink(KAGGLE_INPUT_PATH, os.path.join("..", 'input'), target_is_directory=True)
except FileExistsError:
pass
try:
except FileExistsError:
pass
download_url = unquote(download_url_encoded)
filename = urlparse(download_url).path
try:
total_length = fileres.headers['content-length']
dl = 0
data = fileres.read(CHUNK_SIZE)
dl += len(data)
tfile.write(data)
sys.stdout.flush()
25
data = fileres.read(CHUNK_SIZE)
if filename.endswith('.zip'):
zfile.extractall(destination_path)
else:
tarfile.extractall(destination_path)
except HTTPError as e:
continue
except OSError as e:
continue
26
from tensorflow.keras.applications.resnet50 import preprocess_input as
resnet50_preprocess_input
import keras
import os
import numpy as np
import pandas as pd
plt.rcParams['font.size'] = 16
#Variable defining
SAMPLE_PER_CATEGORY = 1600
SEED = 42
WIDTH = 128
HEIGHT = 128
DEPTH = 3
data_dir = '../input/brain-tumor-mri-dataset/'
27
NUM_CATEGORIES = len(CATEGORIES)
NUM_CATEGORIES
img = image.img_to_array(img)
return img
train = []
train.shape
train['category_id'].value_counts()
train = train.sample(frac=1)
train.index = np.arange(len(train))
train.shape
train.head()
test = []
test.append(['testDatasets/{}'.format(file), file])
28
test.shape
test.head()
i=0
ax = grid[i]
ax.imshow(img / 255.)
ax.axis('off')
if i % NUM_CATEGORIES == NUM_CATEGORIES - 1:
i+=1
plt.show();
def setTrainableLayersResNet(resnet_model):
set_trainable = False
set_trainable = True
layer.trainable = set_trainable
return resnet_model
29
output = resnet.layers[-1].output
output = keras.layers.Flatten()(output)
setTrainableLayersResNet(resnet_model)
# resnet_model.trainable = False
# layer.trainable = False
pd.set_option('max_colwidth', 1)
t = f.suptitle(title, fontsize=12)
f.subplots_adjust(top=0.85, wspace=0.3)
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.set_ylabel('Loss Value')
ax2.set_xlabel('Epoch')
ax2.set_title('Loss')
l2 = ax2.legend(loc="best")
plt.show()
if pretrainedModel == "ResNet-50":
if fineTune:
resnet = setTrainableLayersResNet(resnet)
else:
layer.trainable = False
# GlobalAveragePooling
output = GlobalAveragePooling2D()(resnet.output)
model = Sequential()
model.add(resnet_model)
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))
model.add(Dense(4, activation='softmax'))
model.compile(loss="categorical_crossentropy", optimizer=optimizer_instance,
metrics=["accuracy"])
return model
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,
32
brightness_range=[0.7, 1.3], # More brightness variation
fill_mode='nearest'
datagen_valid = ImageDataGenerator(rescale=1./255)
# Model creation
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))
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,)
if fineTune:
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,)
return model
import tensorflow as tf
train_data_dir = "/kaggle/input/brain-tumor-mri-dataset/Training"
SEED = 42
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,
subset='training',
seed=SEED
validation_dataset = tf.keras.utils.image_dataset_from_directory(
train_data_dir,
labels='inferred',
label_mode='categorical',
image_size=(HEIGHT, WIDTH),
subset='validation',
seed=SEED
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,
36
fineTune=True, # Enable fine-tuning
batch_size=32,
activation='relu',
number_of_hidden_layers=1,
#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)
predicted_class_indices=np.argmax(predictions,axis=1)
results=pd.DataFrame({"file":filenames,
"species":predicted_labels})
print (results)
results.to_csv("submission.csv",index=False)
print("Prediction Completed")
import numpy as np
img_array = image.img_to_array(img)
img_array /= 255.0
# Make predictions
predictions = model.predict(img_array)
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]
plt.imshow(img)
plt.axis('off')
plt.show()
# Example usage
img_path = '/kaggle/input/brain-tumor-mri-dataset/Testing/glioma/Te-glTr_0006.jpg'
39