Brain Tumor Detection Using Deep Learning and Flask
Brain Tumor Detection Using Deep Learning and Flask
1
Abstract
Brain tumors are among the most critical medical conditions requiring
timely and accurate diagnosis to improve patient outcomes. This
project presents a deep learning-based solution for brain tumor
detection using Convolutional Neural Networks (CNNs). The model,
trained on a dataset of MRI images categorized as either No Tumor or
Yes Tumor, leverages advanced image processing techniques to
achieve high classification accuracy.
2
Table of Contents
Title Page
Abstract
Table of Contents
Introduction
Background
Objective
Problem Statement
Scope of the Project
Technologies Used
Literature Review
Brain Tumor Detection Methods
Deep Learning in Medical Imaging
CNNs for Image Classification
Previous Work on Brain Tumor Detection
Challenges in Medical Image Classification
Methodology
Data Collection and Preparation
Dataset Overview
Data Pre-processing
Model Development
CNN Architecture
Training the Model
Flask Application Development
Backend Code
Frontend Code
Model Evaluation
3
Challenges Faced
Dataset Limitations
Model Overfitting
Debugging and Integration Issues
Future Scope
Expanding the Dataset
Improving Model Accuracy
Cloud Deployment for Real-Time Usage
Multi-Class Tumor Classification
Conclusion
References
Appendices
Source Code
Example Inputs and Outputs
Additional Visualizations
4
Introduction
5.1 Background
This project explores the use of CNNs for automated brain tumor
detection. The goal is to create a deep learning-based system capable
5
of analyzing MRI scans, detecting brain tumors, and providing
doctors with reliable diagnostic support.
Objective
6
5.4 Scope of the Project
Literature Review
8
Brain tumor detection is a critical field of study in medical imaging.
The early and accurate detection of brain tumors can significantly
improve the chances of successful treatment and patient survival.
Traditionally, the identification of brain tumors has relied heavily on
medical imaging modalities such as Magnetic Resonance Imaging
(MRI) and Computed Tomography (CT) scans. These imaging
techniques provide detailed internal views of the brain, which can
help doctors identify abnormal growths, tumors, or other
abnormalities. However, the process of manually analyzing these
images is often time-consuming and requires a high level of expertise.
Over the years, many methods have been proposed to automate brain
tumor detection. These can broadly be classified into two categories:
9
learning-based approaches performed better than traditional
methods, they still suffered from challenges in generalization
and accuracy, especially when dealing with large datasets and
complex image structures.
10
highlighting the potential of CNNs to handle complex image
segmentation tasks.
11
For brain tumor detection, CNNs can be trained on MRI images to
learn distinguishing features of tumors, allowing them to classify
images into categories like "tumor" or "no tumor." The use of CNNs
for brain tumor detection has been demonstrated in several studies,
such as Umar et al. (2020), who used a CNN to detect brain tumors
in MRI scans, achieving an accuracy rate of over 90%.
12
6.5 Challenges in Medical Image Classification
Conclusion
The field of brain tumor detection has evolved significantly with the
advent of deep learning technologies. CNNs have proven to be a
powerful tool in medical image classification, including brain tumor
detection, by automatically learning relevant features from the data.
Despite challenges such as limited data and class imbalance, CNN-
based models have demonstrated high accuracy in detecting tumors in
MRI images. This progress highlights the potential for AI to assist in
13
healthcare, providing faster and more accurate diagnoses to aid in
patient care.
The following section of this report will detail the methodology used
to develop the brain tumor detection model, including data
preprocessing, model architecture, training procedures, and the
implementation of the web application.
7. Methodology
1. Dataset Source:
14
o The dataset used in this project was collected from
publicly available datasets of brain MRI scans. One such
dataset is the Brain MRI Images for Brain Tumor
Detection dataset, which contains labeled images
categorized into two classes:
No Tumor: MRI images that do not contain a tumor.
Yes Tumor: MRI images that show the presence of a
tumor.
o The images in the dataset are labeled and divided into
subfolders corresponding to the two categories.
o
2. Data Description:
o The dataset consists of 2000+ images in total, with an
equal distribution between the two classes. The images
have varying dimensions, ranging from 128x128 to
512x512 pixels.
o The image format is primarily JPEG, which is standard for
medical imaging data due to its balance between file size
and quality.
o
3. Data Augmentation:
o To improve the model’s generalization and prevent
overfitting, data augmentation techniques were applied
to the dataset. These techniques included:
Random rotations (15-30 degrees)
Horizontal flipping
Zooming and shifting the images.
o These augmentations increase the diversity of the training
data, making the model more robust.
o
4. Dataset Split:
o The dataset was split into two sets: a training set (80%)
and a test set (20%). The training set was used to train the
15
CNN model, while the test set was kept aside to evaluate
the model’s performance.
The dataset used in this project comprises labeled MRI images of the
brain, classified into two categories:
Class Distribution:
o The dataset is balanced, with approximately 50% of
images belonging to the "No Tumor" class and the other
50% belonging to the "Yes Tumor" class.
o
Image Quality:
o The MRI images were captured in various conditions, with
different scanner settings and angles. This provides a
diverse set of images that help train the model to
generalize well across different types of input data.
16
were applied to the dataset to ensure the images were suitable for
input into the CNN model:
1. Resizing:
o The original MRI images have different dimensions. Since
CNNs require input images to be of the same size, all
images were resized to 64x64 pixels, which is a common
input size for image classification tasks involving CNNs.
o
2. Normalization:
o The pixel values of the images were normalized to a range
between 0 and 1 by dividing each pixel value by 255. This
normalization ensures that the model can process the
images more efficiently and helps speed up the training
process.
o
3. Color Conversion:
o The MRI images were initially in the BGR color format (as
provided by OpenCV). However, to ensure consistency,
the images were converted to RGB format using
OpenCV’s cv2.cvtColor function.
o
4. Image Augmentation:
o To artificially increase the size of the dataset and prevent
overfitting, image augmentation was applied during
model training. This includes random transformations like
rotations, scaling, flipping, and translations. These
augmented images were created on-the-fly during
training to provide more diverse inputs to the model.
17
5. Image Labeling:
o The images were labeled according to the presence or
absence of a tumor. Tumor images were labeled as 1, and
non-tumor images were labeled as 0.
The architecture of the CNN model used for brain tumor detection
was designed as follows:
1. Convolutional Layer 1:
o The first convolutional layer applied 32 filters of size 3x3
on the input image. The activation function used was
ReLU (Rectified Linear Unit), which helps introduce non-
linearity into the model. This layer was followed by
MaxPooling with a pool size of 2x2, which reduces the
spatial dimensions of the output and retains the most
important features.
2. Convolutional Layer 2:
o The second convolutional layer applied 32 filters with a
kernel size of 3x3. Again, the ReLU activation function was
used, followed by a MaxPooling layer with a pool size of
2x2.
3. Convolutional Layer 3:
o The third convolutional layer had 64 filters of size 3x3,
followed by ReLU activation and MaxPooling.
18
4. Flatten Layer:
o After passing through the convolutional and pooling
layers, the feature maps were flattened into a 1D vector.
This step is required before feeding the data into the fully
connected layers.
6. Dropout Layer:
o A Dropout layer with a rate of 0.5 was added to prevent
overfitting. This layer randomly drops 50% of the neurons
during training to force the model to generalize better.
7. Output Layer:
o The output layer had 1 neuron with a sigmoid activation
function, as this is a binary classification problem (tumor
vs. no tumor). The sigmoid function outputs a value
between 0 and 1, indicating the likelihood of a tumor
being present.
After defining the architecture, the model was compiled with the
following configurations:
19
7.4.3 MODEL TRAINING
The model was trained using the training data and validated using the
test data. The following configurations were used during training:
Batch Size: 16
Epochs: 10 epochs
Validation Data: 20% of the data was set aside for validation.
Shuffling: Shuffling was enabled to ensure that the model did
not overfit to any specific sequence of the data.
To make the model accessible for practical use, a web application was
developed using the Flask framework. Flask is a lightweight web
framework for Python that is perfect for building small web
applications.
1. Backend Development:
o The backend of the Flask application loaded the trained
model (BrainTumor10Epochs.h5) and exposed a
REST API to accept user inputs (MRI images).
o The backend handled the image upload, preprocessing,
and passed the processed image to the model for
prediction.
o The result was then returned to the user in the form of a
classification ("No Brain Tumor" or "Yes, Brain Tumor
Detected").
2. Frontend Development:
o The frontend of the Flask application was built using
HTML, CSS, and JavaScript. It provided a simple user
interface where users could upload MRI images and view
the results of the prediction.
o The web application allowed for easy testing and real-
time classification of brain tumors from MRI images.
20
3. File Handling:
o The Flask application ensured that uploaded files were
securely stored in the server directory using Werkzeug's
secure filename function. The server handled files in the
JPEG format.
After training the model, its performance was evaluated on the test set
using several metrics:
The model achieved high accuracy on the test set, demonstrating its
capability in detecting brain tumors from MRI images. The results
were then used to improve the model further through
hyperparameter tuning, such as adjusting the learning rate and
adding more layers.
Conclusion
21
8. Flask Application Development
22
File Handling: Flask allows for the handling of file uploads and
secure storage.
lua
23
+---------------------+
| Frontend (UI) | <--- HTTP
Request/Response ---> Flask Backend
(Controller)
+---------------------+
| Upload Image
v
+----------------------+
| Flask Backend |
| (Controller) |
+----------------------+
| Process Image
v
+----------------------+
| Machine Learning Model|
+----------------------+
|
v
+----------------------+
| Results |
+----------------------+
1. User Input: The user accesses the Flask web interface and
uploads an MRI image of the brain for tumor detection.
2. Image Upload: The image is uploaded via a form and sent to
the Flask server.
24
3. Image Processing: Once the image is uploaded, the server
preprocesses the image, including resizing it to the required
dimensions (64x64) and normalizing the pixel values.
4. Prediction: The preprocessed image is passed through the
trained Convolutional Neural Network (CNN) model, which
outputs a prediction of whether a tumor is present or not.
5. Display Results: The result (either "No Tumor" or "Yes Tumor
Detected") is displayed on the webpage for the user to view.
6. Error Handling: If an error occurs during any part of the
process, such as an invalid image format or prediction failure,
an error message is returned to the user.
app = Flask(__name__)
25
# Load the pre-trained model
model = load_model('BrainTumor10Epochs.h5')
print('Model loaded.')
8.4.2 IMAGE UPLOAD AND FILE HANDLING
26
the image is saved in the uploads/ directory. The image is then
passed to the get_result function for processing and prediction.
In the above code, the image is first read using OpenCV, then resized
and normalized to fit the model input. The model.predict()
function is used to get the prediction, which is a probability that the
image contains a tumor.
27
8.4.4 ERROR HANDLING
The main interface consists of a form where the user can upload an
image. The result of the classification is displayed after the image is
processed. Here is an example HTML form:
html
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Brain Tumor Detection</title>
28
</head>
<body>
<h1>Brain Tumor Detection</h1>
<form action="/predict" method="POST"
enctype="multipart/form-data">
<input type="file" name="file"
required>
<button type="submit">Upload
Image</button>
</form>
{% if prediction %}
<h2>{{ prediction }}</h2>
{% endif %}
</body>
</html>
29
8.6 Deployment
Once the Flask application was developed and tested locally, it was
deployed on a production server using Heroku, a popular platform for
deploying Python web applications.
File Upload Limits: Flask has default file size limits for uploads,
which were increased to allow larger MRI images.
Model Prediction Time: The model's prediction time was
optimized by reducing image size and improving server
response times.
Security: Measures were implemented to secure file uploads,
ensuring that only valid image formats were processed.
30
8.8 Conclusion
31
9.1 Overview of Standalone Prediction Script
32
# Resize the image to match the input size
expected by the model
img = img.resize((64, 64))
Before feeding the image into the model for prediction, it must
undergo several preprocessing steps:
Reading the Image: The image is read using the OpenCV library
(cv2.imread()).
Color Conversion: The image is in BGR format (due to OpenCV)
and needs to be converted to RGB format, which the model
expects.
33
python
Copy code
image = cv2.imread(image_path)
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
34
predictions = model.predict(input_img)
35
9.4 Use Cases for Standalone Prediction Script
While the standalone script serves its purpose, there are several areas
for improvement:
9.6 Conclusion
36
relying on a web interface. It is an essential tool for batch predictions,
testing, and integrating the model into other workflows. By ensuring
proper preprocessing and using the trained model, the script is capable
of delivering accurate results in real-world scenarios, helping in early
diagnosis of brain tumors from MRI images.
This section presents the results of the brain tumor detection model,
its performance, and a detailed analysis of its strengths and
weaknesses. We will examine various aspects such as model
accuracy, performance on different image types, comparison with
traditional methods, and the usability of the deployed Flask web
application.
37
10.1 Model Performance Evaluation
10.1.1 ACCURACY
40
10.2.3 DIFFERENT TUMOR TYPES
Performance was tested with various users, and the application was
responsive with no significant delays during image uploads or
predictions. On average, the prediction process took 1-2 seconds,
depending on the server load and image size.
41
Asynchronous Predictions: To handle multiple concurrent users
without overloading the server.
42
operate as black boxes, which could be a limitation in medical
contexts where explanations are important.
43
11.1.1 LIMITED DATASET AVAILABILITY
In addition to the dataset size, the quality of the images was highly
variable. Some MRI scans were high resolution with clear tumor
structures, while others were blurry or noisy. This inconsistency in
image quality posed challenges during training, as the model might
learn to focus on irrelevant noise or miss important details in lower-
quality images.
44
11.2 Model Training Challenges
11.2.1 OVERFITTING
45
Impact on Model Performance: Incorrect hyperparameters
could lead to poor convergence, long training times, or
suboptimal performance.
Solution: A systematic approach to hyperparameter tuning was
adopted, where different combinations of hyperparameters were
tested, and their effect on training time and accuracy was
evaluated. The Adam optimizer with a learning rate of 0.001 and
batch size of 16 were selected as optimal for this project. The
model was trained for 10 epochs, and performance was
monitored using training and validation loss and accuracy.
Once the model was trained and optimized, the next challenge was
deploying it as a web application using Flask. Integrating the trained
deep learning model (BrainTumor10Epochs.h5) into a Flask backend
posed several challenges related to server performance, prediction
speed, and handling multiple requests.
46
o Caching: Caching mechanisms were implemented for
repeated predictions to reduce the computation time and
load on the server.
47
Impact on User Experience: Poor user interface design can
lead to confusion and a lack of trust in the system.
Solution: A simple and intuitive user interface was developed
with clear instructions for uploading MRI images and
interpreting the results. The use of clear buttons and informative
feedback helped improve user experience and make the
application accessible to non-technical users.
11.5 Conclusion
48
12. Future Scope (3 Pages)
The accuracy and performance of the model are closely tied to the
quality and diversity of the dataset used for training. While the current
dataset provides a starting point, a larger and more varied dataset
would significantly improve the model's ability to generalize across
different tumor types and image conditions.
49
include images from different MRI machines, as the resolution
and quality of scans can vary significantly.
Data Augmentation and Synthesis: Another avenue for
improvement is the use of synthetic data generation. Using
techniques like Generative Adversarial Networks (GANs), more
realistic synthetic brain tumor images could be generated to
augment the dataset. This would help the model better
generalize to unseen data by providing variations of tumor
appearances that may not be available in the original dataset.
50
classification approach and a dataset containing labeled images
for different tumor categories.
51
12.2.3 MODEL OPTIMIZATION FOR FASTER PROCESSING
To enhance performance and ensure that the system can handle large
volumes of data, more advanced optimization techniques could be
explored:
12.5 Conclusion
The future scope of the brain tumor detection system is vast, offering
numerous opportunities for enhancement and expansion. By
improving model accuracy, enabling real-time prediction, deploying
the system to edge devices, and adding more advanced features like
multi-user support and telemedicine integration, the system can
become an invaluable tool for early brain tumor detection in diverse
healthcare settings. Additionally, the methodologies and technologies
used in this project can be generalized to other medical domains,
further contributing to the advancement of AI in healthcare. As deep
learning and medical imaging technology continue to evolve, the
potential for creating more accurate, efficient, and accessible
diagnostic tools is limitless.
54
13. Conclusion (2 Pages)
55
Web Application Deployment: The model was integrated into
a Flask-based web application, allowing users to upload MRI
images and receive predictions on whether a tumor is present.
This deployment makes the model accessible to a broader
audience, including healthcare professionals, without requiring
direct interaction with the underlying code.
User-Friendly Interface: The web application was designed to
be intuitive and easy to use, providing a simple interface for
uploading MRI images and viewing the result of the
classification. This is an important feature for ensuring that the
system can be easily adopted in clinical environments.
Standalone Prediction Script: Additionally, a standalone script
for predicting brain tumor presence was developed. This allows
medical professionals to use the model in local environments
without the need for a web interface.
57
Accessibility and Scalability: The deployment of the system as
a web-based application ensures that the technology can be
accessed by healthcare professionals across the globe. This is
particularly important in low-resource settings, where medical
experts may be scarce, and patients may have limited access to
diagnostic services. The ability to upload images and receive
instant results could greatly reduce the need for specialist
consultations, improving diagnostic efficiency.
Assistive Technology for Medical Professionals: While the
system is designed to be used by healthcare professionals, it
should not replace doctors or radiologists. Rather, it acts as a
decision-support tool, helping medical professionals make
more informed decisions by providing quick insights based on
the model’s predictions.
Potential for Broader Healthcare Applications: The
techniques and methodologies used in this project are not
limited to brain tumor detection alone. The same principles can
be applied to other areas of medical imaging, such as the
detection of lung cancer, retinal diseases, or heart conditions.
This opens up the possibility for scaling the model to handle
various types of medical diagnoses.
58
Real-Time Processing and Edge Deployment: To make the
system more practical for real-world use, optimizations for real-
time processing and edge deployment (on devices with limited
resources) should be prioritized. This would allow the system to
provide instant predictions, even in environments with limited
internet connectivity.
Integration with Other Diagnostic Tools: Future work could
involve integrating the system with other medical tools, such as
tumor segmentation models, which can provide more detailed
insights into tumor characteristics and growth patterns.
Clinical Validation: Finally, clinical validation in real-world
healthcare settings is crucial. The system must undergo rigorous
testing with medical professionals to evaluate its reliability,
accuracy, and potential for adoption in clinical workflows.
59
4. References (2 Pages)
In this section, we provide a list of all the sources cited throughout the
project report. These references include academic papers, books,
online articles, and tutorials that contributed to the understanding and
development of this project. It is important to cite reliable and
authoritative sources to support the findings and methodologies
presented in the report.
6. Esteva, A., Kuprel, B., Novoa, R. A., Ko, J., Swetter, S. M.,
Blau, H. M., & Thrun, S. (2017). Dermatologist-level
classification of skin cancer with deep neural networks. Nature,
542(7639), 115-118.
https://doi.org/10.1038/nature21056
o This study demonstrates the use of deep learning for
dermatological image classification, providing insight into
the application of CNNs for medical diagnostics.
61
o The official Keras documentation provides a
comprehensive guide to using Keras for deep learning
tasks, including building and training convolutional neural
networks (CNNs).
62
This book is a practical guide to machine learning and deep
learning, with detailed explanations of various algorithms and
techniques, including those used in the project.
16. Li, X., & Zhang, D. (2018). Deep Learning for Medical
Image Analysis. Springer Series in Bio-/Neuroinformatics.
63
17. Gonzalez, R. C., & Woods, R. E. (2018). Digital Image
Processing (4th ed.). Pearson.
14.6 Conclusion
64
15. Appendices (5 Pages)
# Dataset directory
image_directory = 'datasets/'
65
for i, image_name in
enumerate(no_tumor_images):
if image_name.split('.')[-1] in ['jpg',
'jpeg']:
image = cv2.imread(image_directory +
'no/' + image_name)
image = cv2.cvtColor(image,
cv2.COLOR_BGR2RGB)
image = Image.fromarray(image)
image = image.resize((INPUT_SIZE,
INPUT_SIZE))
dataset.append(np.array(image))
label.append(0)
dataset = np.array(dataset)
label = np.array(label)
This section of the code handles the loading, resizing, and labeling of
images in the dataset, which are necessary steps in preprocessing
before training the model.
67
model.add(Activation('sigmoid')) # Sigmoid
activation for binary classification
68
return "Yes, Brain Tumor Detected"
# Prediction function
def get_result(img_path):
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Convert BGR to RGB
img = Image.fromarray(img)
img = img.resize((64, 64))
img = np.array(img)
input_img = np.expand_dims(img, axis=0)
predictions = model.predict(input_img)
result = np.round(predictions[0][0])
return int(result)
@app.route('/predict', methods=['POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
file_path = os.path.join('uploads',
secure_filename(f.filename))
f.save(file_path)
try:
value = get_result(file_path)
result = get_class_name(value)
return result
except Exception as e:
return f"Error: {str(e)}"
if __name__ == '__main__':
app.run(debug=True)
69
This snippet covers the backend code of the Flask application, which
allows users to upload images for prediction. It includes routes for
displaying the main interface and handling predictions.
The training log shows the loss and accuracy for each epoch during
the model training process. This log is useful for analyzing the
model's performance over time and ensuring that it converges
appropriately.
70
2. Prediction Results: The page displaying the result of the brain
tumor detection after an image has been processed.
Below is a sample of the types of images used in the dataset for both
classes (no tumor, and yes tumor):
These images are used to train the model and perform the
classification task.
71
15.6 Additional Information
Conclusion
72
Additional Notes
While the dataset used in this project was adequate for demonstrating
the basic functionality of the brain tumor detection system, there are
several limitations:
73
2. Overfitting Prevention Techniques
3. Model Optimization
5. Ethical Considerations
75
6. Future Enhancements
77
78