Skip to content

The goal is to build a Convolutional Neural Network (CNN) for brain tumor classification using MRI images with four different classes, is to accurately classify MRI images into one of the four categories related to brain tumors.

Notifications You must be signed in to change notification settings

Aietham/Brain-Tumor-MRI_Classification

Repository files navigation

Brain Tunor MRI Classification

About the dataset

This dataset is a combination of the following three datasets:

figshare - https://figshare.com/articles/dataset/brain_tumor_dataset/1512427

SARTAJ dataset - https://www.kaggle.com/sartajbhuvaji/brain-tumor-classification-mri

Br35H - https://www.kaggle.com/datasets/ahmedhamada0/brain-tumor-detection?select=no

Training samples - 5712

Teting samples - 1311

alt text

Distribution of each class.

alt text

alt text

A brain tumor is a collection, or mass, of abnormal cells in our brain. our skull, which encloses our brain, is very rigid. Any growth inside such a restricted space can cause problems. Brain tumors can be cancerous (malignant) or noncancerous (benign). When benign or malignant tumors grow, they can cause the pressure inside our skull to increase. This can cause brain damage, and it can be life-threatening.

Goal

The goal is to build a Convolutional Neural Network (CNN) for brain tumor classification using MRI images with four different classes, is to accurately classify MRI images into one of the four categories related to brain tumors.

  1. No Tumor
  2. Glioma Tumor
  3. Meningioma Tumor
  4. Pituitary Tumor

Given an MRI image of a brain, the model aims to learn patterns and features in the images that are indicative of the presence of different types of brain tumors. By training the model on a dataset of labeled MRI images, it learns to differentiate between images that represent different tumor types, enabling it to classify new, unseen MRI images into the correct tumor type category.

The ultimate goal is to build a model that can assist medical professionals in the diagnosis and classification of brain tumors based on MRI images, potentially improving the speed and accuracy of diagnoses.

Process

1. Data Preparation:

  • X_train, X_test, y_train, and y_test are initialized as empty lists to store image data and labels.
  • image_size is set to 150, indicating the desired size for images.
  • For each label in labels, images from the "Training" and "Testing" directories are loaded using OpenCV (cv2.imread) and resized to image_size x image_size. The images are appended to X_train or X_test based on their directory and the corresponding label is appended to y_train or y_test.
  • X_train, y_train, X_test, and y_test are converted to NumPy arrays.

2. Data Splitting:

  • The training data (X_train, y_train) is split into training and validation sets (X_train, X_val, y_train, y_val) using train_test_split from sklearn.model_selection. The validation set size is set to 10% of the training data.

3. Label Encoding:

  • Labels (y_train, y_val, y_test) are converted to categorical format using tf.keras.utils.to_categorical.

4. Model Definition:

  • A sequential CNN model is defined using Sequential from tensorflow.keras.models.
  • The model consists of several convolutional (Conv2D), pooling (MaxPooling2D), dropout (Dropout), and dense (Dense) layers.
  • ReLU activation function is used for convolutional and dense layers, and softmax activation is used for the output layer.
  • The model is compiled using the Adam optimizer and categorical crossentropy loss.

5. Model Training:

  • The model is trained using model.fit.
  • The training data (X_train, y_train) is used for training.
  • The validation data (X_val, y_val) is used for validation during training.
  • The EarlyStopping callback is used to prevent overfitting by monitoring the validation loss and stopping training if the loss does not improve for a certain number of epochs (patience=5 in this case).
  • The training process is run for 50 epochs with a batch size of 16.

6. Training Output:

  • The history object contains training and validation loss and accuracy for each epoch.
  • This information can be used to analyze the training process and performance of the model.

7. Data Leakage:

  • In this scenario, where images of brain tumors are classified, data leakage could occur if images in the training set are too similar to those in the test set, leading to overestimation of model performance.
  • Strategies to mitigate data leakage include ensuring that images in the training and test sets are from different patients or different time periods (for time series datasets).
  • Model Selection: The CNN model architecture is selected for its effectiveness in handling image data and capturing spatial features.
  • Other models like Random Forest or Gradient Boosting might not be suitable for image data due to their inability to effectively process spatial information.

Thought Process for Trying Multiple Models:

The 1st model with 24 layers proved to be less accurate than the 2nd model with 9 layers. The 1st was taken, purely on a trail basis, as we thought more number of layers would prove to be more accurate. But, a new model with a less number of layers proved to be more accurate.

There are several factors that could contribute to the improved accuracy of the shallower model compared to the deeper one:

  • Data Size and Complexity: Deeper models generally require more data to learn complex patterns effectively. If our dataset is relatively small, a shallower model might generalize better and avoid overfitting.
  • Overfitting: Deeper models are more prone to overfitting, especially when the dataset is limited. By reducing the model's complexity, we may have reduced the risk of overfitting, leading to better generalization to unseen data.

CNN Model breakdown

The model includes:

  • 2 Conv2D layers
  • 2 MaxPooling2D layers
  • 2 Dropout layers
  • 1 Flatten layer
  • 2 Dense layers

Adding these up, the total number of layers in the model is 9.

  1. model = Sequential(): This creates a Sequential model, which is a linear stack of layers. This is the most common type of model in Keras.
  2. Input Layer:
  • Input shape: (image_size, image_size, 3), where image_size is the size of the input images (e.g., 150x150 pixels) and 3 represents the number of color channels (RGB).
  1. Convolutional Layers:
  • Two Conv2D layers with 32 and 64 filters respectively, each using a 3x3 kernel and ReLU activation function.
  • Two additional Conv2D layers with 64 filters each and ReLU activation.
  1. Pooling Layers:
  • Two MaxPooling2D layers with a pool size of 2x2 to downsample the feature maps.
  1. Dropout Layers:
  • Two Dropout layers with a dropout rate of 0.3 to prevent overfitting.
  1. Flatten Layer:
  • Flattens the 2D feature maps into a 1D vector for input to the dense layers.
  1. Dense Layers:
  • Two Dense layers with 512 units and ReLU activation.
  • One final Dense layer with len(labels) units (equal to the number of classes) and softmax activation for classification.

Metrics

In healthcare, recall is used to measure how well a model identifies patients with a certain condition (e.g., a disease) out of all patients who actually have the condition. High recall indicates that the model is good at capturing positive cases, which is important when the cost of missing positive cases is high (e.g., in medical diagnosis).

Recall of the model - 95%

Predicting 4 random MRIs from test set

alt text

Predicting a newly downloaded image of no tumor and editing it and again predicting it.

alt text

After editing the image

alt text

Deployment

Things to keep in mind when the model is deployed in production.

  • Input Data: Ensure that the input data is preprocessed consistently with the preprocessing steps used during model training.
  • Deployment Environment: Choose an environment that can support the computational requirements of our model, especially if it involves GPU acceleration. Consider using cloud services for scalability.
  • Model Monitoring: Implement monitoring to track the performance of our deployed model over time. This can include metrics like accuracy, recall, inference time, and resource utilization.

Precautions

It's crucial to take several precautions to ensure safe and effective use. Some important precautions include:

  • Medical Oversight: Always use the model under the supervision of qualified medical professionals. The model's predictions should be used as an aid to clinical decision-making, not as a replacement for professional judgment.
  • Data Quality: Ensure that the input data used to train and validate the model is of high quality and representative of the target population. Low-quality or biased data can lead to inaccurate predictions and potential harm to patients.
  • Interpretability: Deep learning models are often considered black boxes due to their complex nature. Consider using interpretable models or methods to explain the model's predictions to healthcare providers and patients.

What could make this model better?

To improve the brain tumor classification model further, several strategies can be considered:

  1. Model Architecture: Experiment with different neural network architectures, such as deeper or wider networks, to see if they improve performance. Consider using pre-trained models like VGG, ResNet, or EfficientNet and fine-tuning them for the specific task.

  2. Data Augmentation: Increase the diversity of the training data by applying various data augmentation techniques such as rotation, flipping, scaling, and shifting. This can help the model generalize better to unseen data.

  3. Transfer Learning: Utilize transfer learning by fine-tuning a pre-trained model on a large dataset (e.g., ImageNet) to the brain tumor classification task. This approach can leverage features learned from the large dataset and adapt them to the new task, requiring less training data.

  4. Hyperparameter Tuning: Perform a systematic search for optimal hyperparameters (e.g., learning rate, batch size, dropout rate) using techniques like grid search or random search to improve model performance.

  5. Advanced Image Processing: Incorporate advanced image processing techniques, such as segmentation or feature extraction, to extract more meaningful information from the input images before feeding them to the neural network.

About

The goal is to build a Convolutional Neural Network (CNN) for brain tumor classification using MRI images with four different classes, is to accurately classify MRI images into one of the four categories related to brain tumors.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published