Professional Documents
Culture Documents
DOCUMENTATION (Brain Tumor1)
DOCUMENTATION (Brain Tumor1)
DOCUMENTATION (Brain Tumor1)
INTRODUCTION
1
1.1 OVERVIEW OF THE PROJECT
The Brain Tumor Detection project employs TensorFlow and Keras
to construct a convolutional neural network (CNN) for classifying brain MRI images as
tumorous or non-tumorous. Leveraging a dataset from Kaggle containing 253 images, data
augmentation techniques are applied to augment the dataset and mitigate class imbalance.
Preprocessing steps standardize image sizes and pixel values for improved model
performance. A simplified neural network architecture, tailored to computational constraints,
includes convolutional layers, batch normalization, and max-pooling, culminating in a dense
output layer for binary classification. Training over 24 epochs yields a model with 88.7%
accuracy and 0.88 F1 score on the test set. Visualization of loss and accuracy aids in
monitoring training progress. The project provides comprehensive documentation, including
model weights and instructions for usage, fostering collaboration and further improvement.
Overall, the project demonstrates effective utilization of CNNs in medical imaging for brain
tumor detection, showcasing techniques to address challenges such as small dataset size and
class imbalance.
Organizational Profile pioneers advanced medical diagnostics using
cutting-edge technology. Specializing in artificial intelligence, particularly deep learning
methods like convolutional neural networks, we aim to enhance patient outcomes and
streamline healthcare processes. Our dedicated team collaborates with healthcare institutions,
research organizations, and technology companies to develop tailored AI-driven solutions.
With a commitment to innovation and excellence, we strive to revolutionize medical
diagnostics and make a positive impact on global healthcare
2
2.SYSTEM STUDY
3
2.1. EXISING SYSTEM
2.2PROPOSED SYSTEM
To address the limitations of the existing system, the proposed system
introduces a novel approach to brain tumor detection using advanced machine learning
techniques, specifically convolutional neural networks (CNNs). CNNs are a type of deep
learning algorithm capable of automatically learning and extracting relevant features from
images, making them well-suited for tasks such as medical image analysis.
4
2.2.1. ADVANTAGES OF PROPOSED SYSTEM
AUTOMATION:
By leveraging CNNs, the proposed system automates the process of brain
tumor detection from MRI images, significantly reducing the time and effort required for
analysis. Automated analysis enables faster diagnosis and treatment planning, leading to
improved patient outcomes.
ACCURACY:
CNNs have demonstrated impressive performance in image recognition
tasks, achieving levels of accuracy and reliability that rival or surpass human experts. By
harnessing the power of deep learning, the proposed system enhances the accuracy of tumor
detection, reducing the likelihood of false positives or negatives.
SCALABILITY:
Unlike manual workflows, which are constrained by human capacity and
resources, the proposed system is highly scalable and efficient. CNNs can analyze large
volumes of medical imaging data quickly and accurately, allowing healthcare providers to
handle increasing demand without compromising quality or efficiency.
INTEGRATION:
The proposed system can be seamlessly integrated into existing healthcare
infrastructure, providing a seamless workflow for radiologists and clinicians. By integrating
with picture archiving and communication systems (PACS) and electronic health records
(EHRs), the system streamlines the diagnostic process and enhances collaboration among
healthcare professionals.
In conclusion, the proposed system represents a significant advancement in
the field of medical diagnostics, offering a more efficient, accurate, and scalable solution for
brain tumor detection from MRI images. By harnessing the power of machine learning,
particularly CNNs, the proposed system has the potential to revolutionize diagnostic
workflows and improve patient care in healthcare settings.
5
3.SYSTEM SPECIFICATION
6
3.SYSTEM SPECIFICATION
3.1HARDWARE SPECIFICATION
❖ Processor-AMD PRO A4
❖ Memory required:4GB
❖ Storage:1GB or higher
3.2.SOFTWARE SPECIFICATION
❖ Windows 10
❖ Google colab
❖ Python
7
3.2.1SOFTWARE USED:
1. Python:
Python serves as the primary programming language for the project
due to its simplicity, versatility, and extensive support for machine learning libraries. Python
provides access to powerful libraries such as TensorFlow, Keras, and OpenCV, which are
essential for building and training convolutional neural networks (CNNs) and performing
image processing tasks.
2. Tensorflow:
TensorFlow is an open-source machine learning framework developed
by Google that provides comprehensive support for building and training deep learning
models. It offers a flexible architecture for constructing neural networks and efficient
computation on both CPUs and GPUs. In this project, TensorFlow is used for defining the
CNN architecture, optimizing model parameters, and conducting model training and
evaluation.
3. Keras
Keras is a high-level neural networks API written in Python that serves
as a user-friendly interface for TensorFlow and other deep learning frameworks. It simplifies
the process of building and training neural networks by providing a clear and concise syntax
for defining network layers, loss functions, and optimizers. In this project, Keras is used in
conjunction with TensorFlow to construct and train the brain tumor detection model.
4.opencv:
OpenCV (Open Source Computer Vision Library) is a powerful library
for image processing and computer vision tasks. It provides a wide range of functions and
algorithms for image manipulation, feature extraction, and object detection. In this project,
OpenCV is employed for preprocessing the MRI images, including cropping, resizing, and
normalization, to prepare them for input to the neural network.
8
5. Numpy:
NumPy is a fundamental package for scientific computing in Python,
providing support for multidimensional arrays, mathematical functions, and linear algebra
operations. It is widely used in machine learning and data analysis applications for efficient
handling and manipulation of numerical data. In this project, NumPy is utilized for array
manipulation and mathematical operations, particularly during data preprocessing and model
evaluation.
6. Matplotlib:
Matplotlib is a plotting library for Python that enables the creation of
static, interactive, and animated visualizations. It offers a variety of plotting functions and
customization options for generating high-quality charts, graphs, and images. In this project,
Matplotlib is employed for visualizing training metrics, such as loss and accuracy, to monitor
the performance of the neural network during training.
9
3.2.2.PROJECT IMPLEMENTATION
ALGORITHM USED:
1. Input layer:
The input layer receives preprocessed MRI images as input. These images undergo
preprocessing steps such as cropping, resizing, and normalization to standardize their
dimensions and pixel values, ensuring compatibility with the CNN architecture.
2. Convolutional layers
The convolutional layers consist of multiple filters that convolve
across the input images, extracting features such as edges, textures, and patterns. Each
filter performs a dot product operation with a small region of the input image,
producing feature maps that highlight relevant spatial information.
3. Activation function(ReLU)
The rectified linear unit (ReLU) activation function introduces non-
linearity into the network, enabling the model to learn complex relationships between
input features. ReLU activation is applied after each convolutional layer to introduce
non-linearity and improve the model's representational power.
4. Pooling layers
The pooling layers downsample the feature maps generated by the
convolutional layers, reducing their spatial dimensions while retaining important
information. Max pooling is typically used to extract the most significant features
from each feature map, enhancing the model's robustness to spatial translations and
distortions.
5. Flatten layer
The flatten layer reshapes the output of the previous layers into a one-
dimensional vector, preparing it for input to the fully connected layers. This
transformation consolidates the extracted features from the convolutional layers into a
format suitable for classification.
6. Fully connected layers
The fully connected layers receive the flattened feature vector as input
and perform a series of matrix multiplications and activation functions to learn
complex patterns and relationships between features. These layers gradually reduce
the dimensionality of the input, ultimately producing a single output representing the
10
probability of tumor presence
7. Output layer
The output layer consists of a single neuron with a sigmoid activation
function, producing a binary classification output indicating the likelihood of tumor
presence. A sigmoid activation function is used to squash the output into the range [0,
1], representing the probability of a positive (tumorous) outcome.
8. Data preprocessing
Data preprocessing is a crucial step in machine learning pipelines,
particularly in the context of image classification tasks like brain tumor detection
from MRI images. It involves transforming raw data into a format suitable for input to
machine learning algorithms, improving model performance and efficiency. In the
Brain Tumor Detection project, several preprocessing steps are applied to the MRI
images before feeding them into the convolutional neural network (CNN) model.
1. Image loading
The MRI images are loaded into memory from the dataset, ensuring
that they can be accessed and manipulated by the preprocessing pipeline.
2. Cropping
Since MRI images often contain extraneous information surrounding
the brain region, cropping is performed to isolate the relevant part of the image
containing only the brain. This reduces the computational overhead and focuses the
model's attention on the area of interest.
11
3. Resizing
MRI images may come in various sizes and resolutions, which can
pose challenges for model training. Resizing is performed to standardize the
dimensions of all images to a consistent size, typically using techniques like
interpolation to preserve important features.
4. Normalization
Pixel values in the MRI images are normalized to ensure that they fall
within a specific range, typically [0, 1] or [-1, 1]. Normalization helps stabilize
training, prevents numerical instabilities, and ensures that the model learns from
consistent input data.
5. Data Augmentation
Data augmentation is a technique commonly used in machine learning
and computer vision to artificially increase the size and diversity of a dataset by
applying a variety of transformations to existing data samples. This approach is
particularly beneficial when working with limited or imbalanced datasets, as it helps
improve model generalization and performance by exposing the model to a wider
range of variations and scenarios.
In the context of the Brain Tumor Detection project, data augmentation
is applied to the MRI images to address the challenges posed by the relatively small
dataset size and class imbalance between tumorous and non-tumorous samples. By
generating additional synthetic examples through augmentation, the dataset becomes
more representative of real-world scenarios, thereby enhancing the model's ability to
accurately detect brain tumors.
12
1. Rotation:
Rotation involves rotating the image by a certain angle, typically in
degrees, to simulate different viewpoints and orientations. This technique is useful
for training machine learning models to recognize objects from various angles and
orientations. For example, if you're training a model to recognize handwritten
digits, rotating the images can help the model generalize better to different
handwriting styles and orientations. Rotation augmentation is often performed by
applying affine transformations to the image matrix.
2. Flipping
Flipping, also known as mirroring, involves creating variations of an
image by horizontally or vertically flipping it. Horizontal flipping involves
flipping the image along the vertical axis, while vertical flipping involves flipping
the image along the horizontal axis. This technique helps in creating variations in
object placement and symmetry within the image. For instance, if you're training a
model to detect cars on the road, flipping the images horizontally can help the
model learn to recognize cars facing in both directions Zooming
3. Zooming
Zooming involves scaling the image to focus on specific regions of
interest, mimicking different levels of zoom during image acquisition. This
technique is particularly useful when the position or size of the object of interest
varies in the dataset. By zooming in or out, the model can learn to recognize
objects at different scales and positions within the image. For example, in a
dataset of animal images, zooming can help the model focus on specific features
of the animals, such as their faces or patterns
4. Translation:
Translation involves shifting the image horizontally or vertically to
simulate changes in object position within the frame. This technique helps in
training models to recognize objects regardless of their location within the image.
For instance, if you're training a model to detect pedestrians in surveillance
footage, translating the images can help the model learn to identify pedestrians at
different positions within the frame, such as near the center or closer to the edges
5. Brightness and contrast adjustment:
Brightness and contrast adjustment involves altering the brightness and
13
contrast levels of the image to simulate variations in lighting conditions. This
technique helps in training models to be robust to changes in illumination. By
adjusting the brightness and contrast, the model can learn to recognize objects
under different lighting conditions, such as bright sunlight or low-light
environments
6. .Noise injection
Noise injection involves adding random noise to the image to simulate
imperfections in image acquisition or transmission. This technique helps in
training models to be robust to noise and artifacts in the input data. By exposing
the model to noisy images during training, it can learn to ignore irrelevant details
and focus on the underlying features of the objects. Noise injection is particularly
useful in scenarios where the input images are prone to noise, such as medical
imaging or satellite imagery.
6 . DENSE
In the context of neural networks, a dense layer, also known as a fully connected
layers , is a type of layer where each neuron is connected to every neuron in the preceding
layer. This connection enables the dense layer to learn complex patterns and relationship in
the input data. In a dense layer, each neuron applies a weighted sum of its input values,
followed by an activation function, to produce an output. Dense layers are commonly used in
neural network architectures for tasks such as classification and regression, providing
flexibility and expressive power in modeling nonlinear relationships within the data.
14
In the project, the input MRI images undergo preprocessing steps such as
cropping, resizing, and normalization before being fed into the CNN. The convolutional
layers apply a set of learnable filters across the input images, extracting features such as
edges, textures, and shapes. Activation functions like ReLU introduce non-linearity to the
network, enabling it to learn complex patterns. Pooling layers downsample the feature maps,
reducing computational complexity while retaining important information. The flatten layer
reshapes the output of the convolutional layers into a one-dimensional vector, which is then
fed into fully connected layers. These layers further process the features, gradually reducing
dimensionality until reaching the output layer.
The output layer typically consists of a single neuron with a sigmoid activation
function, producing a binary classification output indicating the likelihood of tumor presence.
During training, the CNN learns to minimize a loss function by adjusting its parameters
(weights and biases) through backpropagation and gradient descent optimization. The model
is trained on a labeled dataset consisting of both tumorous and non-tumorous MRI images,
with performance evaluated on a separate test set.
Overall, the CNN architecture enables the automatic extraction and learning
of discriminative features from MRI images, facilitating accurate and efficient brain tumor
detection. Through iterative training and optimization, the CNN learns to generalize from the
training data, achieving high accuracy and robustness in classifying unseen images.
15
4. SYSTEM DESIGN
16
4.1.DATAFLOW DIAGRAM
17
MRI Images
Data set
Model
Performance
metrics
18
19
5. TESTING
20
5.1.Testing methodology:
1. Dataset Split
Training Set: 70% of the dataset for training the model.
Validation Set: 15% of the dataset for tuning hyperparameters and monitoring training
progress.
Test Set: 15% of the dataset for evaluating the trained model's performance.
2. Preprocessing
Data preprocessing is a critical step in preparing raw data for machine learning models.
In the context of brain tumor detection using MRI images, the following data preprocessing
steps are essential:
Image Cropping:
Since MRI images often contain unnecessary background information, cropping the
images to focus only on the brain region is essential. This step improves the model's ability to
learn relevant features and reduces computational overhead by removing irrelevant data.
Image Resizing:
MRI images may come in varying sizes, which can pose challenges for model training.
Resizing the images to a uniform size, such as (240, 240, 3), ensures consistency and
compatibility across the dataset. This step enables efficient batch processing and model
training.
Normalization:
Pixel values in MRI images typically range from 0 to 255, representing grayscale
intensities. Normalizing these pixel values to a standardized range, such as 0 to 1, improves
model convergence and stability during training. Normalization also ensures that each feature
contributes proportionally to the model's learning process.
Data Augmentation :
In scenarios where the dataset is small or imbalanced, data augmentation techniques
can be applied to artificially increase the dataset size and improve model generalization.
Techniques such as rotation, flipping, and zooming can create additional training examples,
enhancing the model's robustness to variations in input data.
21
Shuffling:
It's important to shuffle the dataset to remove any inherent ordering bias, especially if
the data is organized by class labels. Shuffling ensures that the model sees a diverse range of
examples during training, preventing it from learning patterns specific to the order of the
data.
Train-Validation-Test Split:
Finally, the dataset is split into three subsets: training, validation, and testing. The
training set is used to train the model, the validation set is used to tune hyperparameters and
monitor model performance during training, and the testing set is reserved for evaluating the
final model performance on unseen data.
3. Model Evaluation
Load the trained CNN model weights.Feed the preprocessed test images into the model.
Calculate evaluation metrics:
Accuracy:
Proportion of correctly classified images.
Precision:
Proportion of true positive predictions among all positive predictions.
Recall:
Proportion of true positive predictions among all actual positive samples.
F1-score:
Harmonic mean of precision and recall.
Confusion Matrix:
Visualization of model's performance across different classes.
4. Interpretation of Results
Analyze the model's performance metrics on the test set.Compare the results with
those obtained during training and validation.Identify any patterns or trends in the model's
predictions.
Discuss the significance of the results in the context of brain tumor detection.
22
TESTING REPORT
23
5.2.Testing Report
1. Introduction
Brief overview of the testing process.Importance of evaluating the trained model's
performance on unseen data.
2.Testing Methodologies:
Unit Testing:
Unit testing involves verifying the functionality of individual components or functions
in isolation from the rest of the codebase. It ensures that each unit of code behaves as
expected and meets the specified requirements.
Example: In your Brain Tumor Detection project, unit testing can be used to validate
functions such as image cropping (crop_brain_contour) or F1 score calculation
(compute_f1_score). Each unit test focuses on a specific function or module, verifying its
correctness and robustness.
Integration Testing:
Integration testing assesses the interaction and integration of different modules,
components, or functions within the system. It ensures that these components work together
seamlessly and produce the expected outputs when combined.
Example: In your project, integration testing can involve validating the interaction between
data preprocessing functions (e.g., image cropping, resizing) and the neural network model
training process. It ensures that data preprocessing prepares the input data correctly for model
training and evaluation.
End-to-End Testing:
End-to-end testing evaluates the entire workflow or system from start to finish,
including all components, modules, and processes involved. It tests the system's behavior and
performance in real-world scenarios, simulating user interactions or data inputs.
Example: In your Brain Tumor Detection project, end-to-end testing would involve
evaluating the complete workflow from data preprocessing (e.g., loading, preprocessing, and
splitting the data) to model training and evaluation (e.g., training the CNN model, evaluating
accuracy and F1 score). It ensures that the entire system functions correctly and produces
accurate results for detecting brain tumors from MRI images.
24
Test Cases:
Data Preprocessing Test:
Input: Raw brain MRI images
Expected Output: Preprocessed images with correct resizing, cropping, and normalization.
Test Execution:
Data Preprocessing:
Images were cropped, resized, and normalized using OpenCV.
Test conducted: Verified output images for correctness of preprocessing steps.
Model Training:
CNN model architecture was defined and compiled using TensorFlow and Keras.
Training was performed on training data with validation on development data.
Test conducted: Model training duration, loss, and accuracy monitored.
Model Evaluation:
Trained model was evaluated on separate testing data to assess performance.
Accuracy and F1 score were computed to measure model effectiveness.
Test conducted: Comparing model predictions with ground truth labels for accuracy
assessment.
25
5.3.Test results:
Accuracy:
Accuracy measures the proportion of correctly classified instances (both true
positives and true negatives) out of the total number of instances in the dataset. It provides an
overall indication of how well the model is performing.
Usage: In your project, accuracy is used to evaluate the proportion of correctly classified
brain MRI images as either tumorous or non-tumorous by your convolutional neural network
(CNN) model.
Interpretation: For example, an accuracy of 88.7% on the test set indicates that the model
correctly classified 88.7% of the test images, which is a good performance metric.
F1 Score:
The F1 score is the harmonic mean of precision and recall, combining both
measures to provide a single score that balances both false positives and false negatives. It is
particularly useful in binary classification tasks with imbalanced datasets.
Usage: In your project, the F1 score is used to assess the balance between precision (the
ability of the model to correctly identify positive cases) and recall (the ability of the model to
capture all positive cases) in detecting brain tumors.
Interpretation: For example, an F1 score of 0.88 on the test set indicates a good balance
between precision and recall, suggesting that the model achieves accurate and reliable tumor
detection with minimal false positives and false negatives.
Validation:
Validation is the process of evaluating the performance of a machine learning
model on a separate dataset (validation set) that was not used during training. It helps assess
the model's generalization ability and prevents overfitting.
Usage: In your project, validation is used to monitor the model's performance during training
and tune hyperparameters to optimize its performance without overfitting to the training data.
Interpretation: For example, by monitoring validation accuracy and loss curves during
training, you can identify epochs where the model starts to overfit (validation loss increases
while training loss decreases), allowing you to stop training or apply regularization
techniques.
26
IMPLEMENTATION
27
6.1.SAMPLE CODING
# Data Augmentation
%matplotlib inline
def hms_string(sec_elapsed):
h = int(sec_elapsed / (60 * 60))
m = int((sec_elapsed % (60 * 60)) / 60)
s = sec_elapsed % 60
return f"{h}:{m}:{round(s,1)}"
def augment_data(file_dir, n_generated_samples, save_to_dir):
"""
"""
data_gen = ImageDataGenerator(rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
brightness_range=(0.3, 1.0),
28
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest'
)
# augment data for the examples with label equal to 'yes' representing tumurous examples
augment_data(file_dir=yes_path, n_generated_samples=6,
save_to_dir=augmented_data_path+'yes')
# augment data for the examples with label equal to 'no' representing non-tumurous examples
augment_data(file_dir=no_path, n_generated_samples=9,
save_to_dir=augmented_data_path+'no')
end_time = time.time()
execution_time = (end_time - start_time)
print(f"Elapsed time: {hms_string(execution_time)}")
29
Let's see how many tumorous and non-tumorous examples after performing data
augmentation:
def data_summary(main_path):
yes_path = main_path+'yes'
no_path = main_path+'no'
# number of files (images) that are in the the folder named 'yes' that represent tumorous
(positive) examples
m_pos = len(listdir(yes_path))
# number of files (images) that are in the the folder named 'no' that represent non-tumorous
(negative) examples
m_neg = len(listdir(no_path))
data_summary(augmented_data_path)
That's it for this notebook. Now, we can use the augmented data to train our convolutional
neural network.
30
# Brain Tumor Detection Using a Convolutional Neural Network
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Input, ZeroPadding2D, BatchNormalization,
Activation, MaxPooling2D, Flatten, Dense
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from sklearn.utils import shuffle
import cv2
import imutils
import numpy as np
import matplotlib.pyplot as plt
import time
from os import listdir
%matplotlib inline
#import imutils
#import cv2
#from matplotlib import pyplot as plt
31
# dilations to remove any small regions of noise
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)
# crop new image out of the original image using the four extreme points (left, right, top,
bottom)
if plot:
plt.figure()
plt.subplot(1, 2, 1)
plt.imshow(image)
plt.tick_params(axis='both', which='both',
top=False, bottom=False, left=False, right=False,
labelbottom=False, labeltop=False, labelleft=False, labelright=False)
32
plt.title('Original Image')
plt.subplot(1, 2, 2)
plt.imshow(new_image)
plt.tick_params(axis='both', which='both',
top=False, bottom=False, left=False, right=False,
labelbottom=False, labeltop=False, labelleft=False, labelright=False)
plt.title('Cropped Image')
plt.show()
return new_image
ex_img = cv2.imread('yes/Y1.jpg')
ex_new_img = crop_brain_contour(ex_img, True)
33
# resize image
image = cv2.resize(image, dsize=(image_width, image_height),
interpolation=cv2.INTER_CUBIC)
# normalize values
image = image / 255.
# convert image to numpy array and append it to X
X.append(image)
X = np.array(X)
y = np.array(y)
return X, y
# augmented data (yes and no) contains both the original and the new generated examples
augmented_yes = augmented_path + 'yes'
augmented_no = augmented_path + 'no'
34
X, y = load_data([augmented_yes, augmented_no], (IMG_WIDTH, IMG_HEIGHT))
As we see, we have 2065 images. Each images has a shape of **(240, 240, 3)=(image_width,
image_height, number_of_channels)**
### Plot sample images:
def plot_sample_images(X, y, n=50):
"""
Plots n sample images for both values of y (labels).
Arguments:
X: A numpy array with shape = (#_examples, image_width, image_height, #_channels)
y: A numpy array with shape = (#_examples, 1)
"""
columns_n = 10
rows_n = int(n/ columns_n)
plt.figure(figsize=(20, 10))
i = 1 # current plot
for image in n_images:
plt.subplot(rows_n, columns_n, i)
plt.imshow(image[0])
# remove ticks
plt.tick_params(axis='both', which='both',
top=False, bottom=False, left=False, right=False,
labelbottom=False, labeltop=False, labelleft=False, labelright=False)
35
i += 1
plot_sample_images(X, y)
### Split the data:
Split <i>X</i> and <i>y</i> into training, validation (development) and validation sets.
def split_data(X, y, test_size=0.2):
"""
Splits data into training, development and test sets.
Arguments:
X: A numpy array with shape = (#_examples, image_width, image_height, #_channels)
y: A numpy array with shape = (#_examples, 1)
Returns:
X_train: A numpy array with shape = (#_train_examples, image_width, image_height,
#_channels)
y_train: A numpy array with shape = (#_train_examples, 1)
X_val: A numpy array with shape = (#_val_examples, image_width, image_height,
#_channels)
y_val: A numpy array with shape = (#_val_examples, 1)
X_test: A numpy array with shape = (#_test_examples, image_width, image_height,
#_channels)
y_test: A numpy array with shape = (#_test_examples, 1)
X_train, X_test_val, y_train, y_test_val = train_test_split(X, y, test_size=test_size)
X_test, X_val, y_test, y_val = train_test_split(X_test_val, y_test_val, test_size=0.5)
36
2. 15% of the data for validation.
3. 15% of the data for testing.
X_train, y_train, X_val, y_val, X_test, y_test = split_data(X, y, test_size=0.3)
print ("number of training examples = " + str(X_train.shape[0]))
print ("number of development examples = " + str(X_val.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(y_train.shape))
print ("X_val (dev) shape: " + str(X_val.shape))
print ("Y_val (dev) shape: " + str(y_val.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(y_test.shape))
Some helper functions:
# Nicely formatted time string
def hms_string(sec_elapsed):
h = int(sec_elapsed / (60 * 60))
m = int((sec_elapsed % (60 * 60)) / 60)
s = sec_elapsed % 60
return f"{h}:{m}:{round(s,1)}"
def compute_f1_score(y_true, prob):
# convert the vector of probabilities to a target vector
y_pred = np.where(prob > 0.5, 1, 0)
return score
# Build the model
Let's build a convolutional neural network model:
<img src='convnet_architecture.jpg'>
def build_model(input_shape):
"""
Arugments:
input_shape: A tuple representing the shape of the input of the model.
37
shape=(image_width, image_height, #_channels)
Returns:
model: A Model object.
"""
# Define the input placeholder as a tensor with shape input_shape.
X_input = Input(input_shape) # shape=(?, 240, 240, 3)
# MAXPOOL
X = MaxPooling2D((4, 4), name='max_pool0')(X) # shape=(?, 59, 59, 32)
# MAXPOOL
X = MaxPooling2D((4, 4), name='max_pool1')(X) # shape=(?, 14, 14, 32)
# FLATTEN X
X = Flatten()(X) # shape=(?, 6272)
# FULLYCONNECTED
X = Dense(1, activation='sigmoid', name='fc')(X) # shape=(?, 1)
# Create model. This creates your Keras model instance, you'll use this instance to
train/test the model.
model = Model(inputs = X_input, outputs = X, name='BrainDetectionModel')
return model
38
Define the image shape:
IMG_SHAPE = (IMG_WIDTH, IMG_HEIGHT, 3)
model = build_model(IMG_SHAPE)
model.summary()
Compile the model:
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# tensorboard
log_file_name = f'brain_tumor_detection_cnn_{int(time.time())}'
tensorboard = TensorBoard(log_dir=f'logs/{log_file_name}')
# checkpoint
# unique file name that will include the epoch and the validation (development) accuracy
filepath="cnn-parameters-improvement-{epoch:02d}-{val_acc:.2f}"
# save the model with the best validation (development) accuracy till now
checkpoint = ModelCheckpoint("models/{}.model".format(filepath, monitor='val_acc',
verbose=1, save_best_only=True, mode='max'))
end_time = time.time()
execution_time = (end_time - start_time)
print(f"Elapsed time: {hms_string(execution_time)}")
Let's train for a few more epochs:
39
start_time = time.time()
end_time = time.time()
execution_time = (end_time - start_time)
print(f"Elapsed time: {hms_string(execution_time)}")
start_time = time.time()
end_time = time.time()
execution_time = (end_time - start_time)
print(f"Elapsed time: {hms_string(execution_time)}")
start_time = time.time()
end_time = time.time()
execution_time = (end_time - start_time)
print(f"Elapsed time: {hms_string(execution_time)}")
start_time = time.time()
end_time = time.time()
execution_time = (end_time - start_time)
print(f"Elapsed time: {hms_string(execution_time)}")
40
history = model.history.history
for key in history.keys():
print(key)
train_loss = history['loss']
val_loss = history['val_loss']
train_acc = history['acc']
val_acc = history['val_acc']
# Loss
plt.figure()
plt.plot(train_loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Loss')
plt.legend()
plt.show()
# Accuracy
plt.figure()
plt.plot(train_acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Accuracy')
plt.legend()
plt.show()
# Results
### Load the best model
best_model = load_model(filepath='models/cnn-parameters-improvement-23-0.91.model')
best_model.metrics_names
Evaluate the best model on the testing data:
loss, acc = best_model.evaluate(x=X_test, y=y_test)
41
### Accuracy of the best model on the testing data:
print (f"Test Loss = {loss}")
print (f"Test Accuracy = {acc}")
m=len(y)
n_positive = np.sum(y)
n_negative = m - n_positive
42
data_percentage(y_train)
print("Validation Data:")
data_percentage(y_val)
print("Testing Data:")
data_percentage(y_test)
As expectred, the percentage of positive examples are around 50%.
43
OUTPUT
44
6.2.OUTPUT:
Model: BrainDetectionModel
_______________________________________________________________
Layer (type) Output Shape Param #
=========================================================
input_1 (InputLayer) [(None, 240, 240, 3)] 0
=========================================================
45
46
Training Data:
Validation Data:
Testing Data:
47
CONCLUSION
48
7.CONCLUSION:
The Brain Tumor Detection project represents a significant advancement in the field of
medical imaging, leveraging convolutional neural networks (CNNs) to automate the detection
of brain tumors from MRI images. Through meticulous data preprocessing, model
development, and training, the project has successfully achieved its primary objective of
creating an accurate and reliable tool for assisting in the diagnosis of brain tumors.
Beyond the technical achievements, the Brain Tumor Detection project holds profound
implications for healthcare and patient outcomes. By providing clinicians with a reliable
automated tool for tumor detection, the project streamlines the diagnostic process, reduces the
burden on radiologists, and enables earlier detection and intervention for patients with brain
tumors. This, in turn, can lead to improved treatment outcomes, enhanced patient care, and
ultimately, saved lives.
Looking ahead, the project opens avenues for further research and development in
medical image analysis and deep learning. Future work may involve exploring more
sophisticated neural network architectures, integrating additional data sources or modalities,
and deploying the model in clinical settings for real-time diagnosis. Collaboration with
medical professionals and institutions, such as Madurai Kamaraj University, can facilitate the
translation of research findings into practical solutions that benefit healthcare practitioners
and patients alike.
49
REFERENCES
50
8.1.BIBILOGRAPHY
1.Dangi, S., Chandra, P. (2020). Brain MRI Images for Brain Tumor Detection. Kaggle.
[Dataset]. Available at: https://www.kaggle.com/navoneel/brain-mri-images-for-brain-tumor-
detection.
2.Goodfellow, I., Bengio, Y., Courville, A., & Bengio, Y. (2016). Deep learning (Vol. 1).
MIT press Cambridge.
3.Kermany, D. S., Goldbaum, M., Cai, W., Valentim, C. C. S., Liang, H., Baxter, S. L., ... &
Zhang, K. (2018). Identifying medical diagnoses and treatable diseases by image-based deep
learning. Cell, 172(5), 1122-1131.
4.Litjens, G., Kooi, T., Bejnordi, B. E., Setio, A. A. A., Ciompi, F., Ghafoorian, M., ... &
Sanchez, C. I. (2017). A survey on deep learning in medical image analysis. Medical image
analysis, 42, 60-88.
5.Russakovsky, O., Deng, J., Su, H., Krause, J., Satheesh, S., Ma, S., ... & Berg, A. C. (2015).
ImageNet Large Scale Visual Recognition Challenge. International Journal of Computer
Vision, 115(3), 211-252.
6.Szegedy, C., Vanhoucke, V., Ioffe, S., Shlens, J., & Wojna, Z. (2016). Rethinking the
inception architecture for computer vision. In Proceedings of the IEEE conference on
computer vision and pattern recognition (pp. 2818-2826).
7.Tustison, N. J., Avants, B. B., Cook, P. A., Zheng, Y., Egan, A., Yushkevich, P. A., & Gee,
J. C. (2010). N4ITK: improved N3 bias correction. IEEE transactions on medical imaging,
29(6), 1310-1320.
51