This document explains each cell in the Jupyter notebook for the MRI brain tumor detection project, written in simple, easy-to-understand language.
This cell introduces the project. It's about using artificial intelligence to detect brain tumors in MRI scans. The project uses deep learning with two main parts: a classifier to check if a tumor exists, and a segmenter to find exactly where the tumor is. They use transfer learning with ResNet models to make training faster.
This cell talks about the libraries and tools needed for the project. It mentions that you need to install packages like pandas, numpy, tensorflow, etc., using pip if they're not already installed.
This cell loads all the necessary Python libraries for the project. It includes libraries for data handling (pandas, numpy), machine learning (tensorflow, keras), image processing (opencv), and visualization (matplotlib, seaborn). It also sets up random seeds for consistent results and checks if a GPU is available for faster training.
Output: Shows that TensorFlow version 2.20.0 is loaded, no GPU is available, and mixed precision is enabled for faster processing.
This cell indicates that the next code will load the dataset containing MRI images and their corresponding tumor masks.
This cell reads a CSV file called 'data_mask.csv' into a pandas dataframe called brain_df. This CSV contains paths to MRI images and their mask files. It then displays the first few rows to show the data structure.
Output: Shows a table with columns: patient_id, image_path, mask_path, and mask (0 or 1 indicating if tumor exists).
This cell displays information about the brain_df dataframe, including the number of entries, column types, and if there are any missing values.
Output: Shows there are 3929 MRI scans, 4 columns, all with complete data.
This cell explains that the next code will show how many images have tumors (mask=1) versus don't have tumors (mask=0).
This cell shows the count of images for each mask value (0 = no tumor, 1 = tumor).
Output: Shows there are more images without tumors than with tumors.
This cell indicates that the next code will create an interactive bar chart showing the distribution of tumor vs non-tumor images.
This cell creates an interactive bar chart using Plotly to visualize how many images have tumors versus don't have tumors.
Output: An interactive bar chart showing the class distribution.
This cell displays the mask_path column from the dataframe, showing the file paths to the mask images.
Output: Lists the paths to mask files for all 3929 images.
This cell loads and displays a mask image from index 623 in the dataset to show what a tumor mask looks like.
Output: Shows the mask image (black and white, where white indicates tumor area).
This cell loads and displays the corresponding MRI image from index 623.
Output: Shows the brain MRI scan.
This cell checks the maximum pixel value in the mask image (should be 255 for white areas).
Output: 255 (indicating the mask uses standard grayscale values).
This cell checks the minimum pixel value in the mask image (should be 0 for black areas).
Output: 0 (background pixels are black).
This cell creates a grid showing 6 random MRI images alongside their corresponding masks, with titles indicating if each has a tumor (mask=1) or not (mask=0).
Output: A 6x2 grid of images showing random brain MRIs and their masks.
This cell shows 12 examples of MRIs that have tumors (mask=1). For each, it displays: the original MRI, the mask, and the MRI with the tumor area highlighted in red.
Output: A 12x3 grid showing tumor examples with overlays.
This cell removes the patient_id column from the dataframe (not needed for training) and shows the new shape.
Output: (3929, 3) - now 3 columns instead of 4.
This cell converts the mask column from numbers to strings because Keras requires categorical labels to be strings for proper classification.
Output: Shows the dataframe info with mask as object type.
This cell splits the dataset into training and testing sets, with 15% of data reserved for testing.
No output.
This cell sets up data generators that will load images from the dataframe and apply preprocessing. For training, it includes data augmentation (rotations, flips, zooms) to create more training examples. For validation, it only rescales images.
No output.
This cell creates the actual data generators for training, validation, and testing. It specifies batch size (16), image size (256x256), and other parameters.
Output: Shows number of batches found for training (2839 images), validation (500), and testing (590).
This cell loads the pre-trained ResNet50 model from ImageNet, but removes the top classification layers so we can add our own for tumor detection.
No output.
This cell displays the architecture of the ResNet50 model, showing all its layers and parameters.
Output: Detailed model summary with ~23.5 million parameters.
This cell freezes all the pre-trained ResNet layers so they won't be updated during initial training. Only the new layers we add will be trained.
Output: Shows 175 layers frozen, 0 trainable.
This cell adds new layers on top of ResNet for tumor classification. It includes dense layers with regularization (dropout, L2) to prevent overfitting, and ends with 2 output neurons for binary classification.
Output: Shows the complete model has ~24.8 million parameters.
This cell shows the full model architecture including the new classification layers.
Output: Model summary with trainable and non-trainable parameters.
This cell compiles the model with Adam optimizer, categorical crossentropy loss with label smoothing, and multiple metrics (accuracy, precision, recall, AUC).
Output: Confirmation that model is compiled.
This cell sets up training callbacks: early stopping (stop if no improvement), model checkpointing (save best model), and learning rate reduction on plateau.
Output: Confirmation of configured callbacks.
This cell trains the model for the first phase with frozen ResNet layers. Only the new classification head is trained. Uses 1 epoch for demo (would use 30-50 in real training).
Output: Training progress showing loss, accuracy, and other metrics improving.
This cell unfreezes the last 50 layers of ResNet and continues training with a lower learning rate to fine-tune the pre-trained features for tumor detection.
Output: Fine-tuning progress (may be interrupted in demo).
This cell saves the trained model's architecture to a JSON file for later use.
No output.
This cell loads a pre-trained classification model (simpler version) and its weights for making predictions.
Output: Confirmation model loaded with ~25.7 million parameters.
This cell uses the loaded classification model to predict tumor presence on the test set.
Output: Progress bar showing prediction on 36 batches.
This cell shows the raw prediction probabilities from the model.
Output: Array of shape (576, 2) with probabilities for each class.
This cell converts the prediction probabilities to actual class labels (0 or 1) by taking the highest probability.
No output.
This cell shows the predicted class labels.
Output: Array of '0's and '1's for 576 test images.
This cell gets the true labels from the test dataframe, matching the prediction length.
Output: 576 (number of test samples).
This cell calculates how accurate the model's predictions are compared to the true labels.
Output: 0.979 (97.9% accuracy).
This cell creates a confusion matrix heatmap showing true positives, false positives, true negatives, and false negatives.
Output: Heatmap visualization of the confusion matrix.
This cell generates a detailed report with precision, recall, and F1-score for each class.
Output: Report showing high performance metrics for both classes.
This cell creates a subset of the dataframe containing only images that have tumors (mask=1) for segmentation training.
Output: (1373, 4) - 1373 images with tumors.
This cell splits the tumor images into training, validation, and test sets for the segmentation model.
No output.
This cell extracts the image and mask file paths for training and validation.
No output.
This cell creates custom data generators for segmentation training and validation, with augmentation enabled for training.
Output: Shows 72 training batches and 6 validation batches.
This cell defines a residual block with squeeze-and-excitation attention for better feature learning in the segmentation model.
Output: Confirmation that the block is defined.
This cell defines attention gates and upsampling functions for the U-Net decoder, helping focus on relevant features during upsampling.
Output: Confirmation that attention upsampling is defined.
This cell constructs the complete ResUNet architecture for tumor segmentation with attention mechanisms and residual blocks.
Output: Shows model built with ~7.4 million parameters.
This cell displays the architecture of the segmentation model.
Output: Detailed model summary.
This cell imports custom loss functions (focal tversky) and other metrics from the utilities file.
No output.
This cell reloads the utilities module to ensure the latest functions are available.
Output: Confirmation utilities reloaded.
This cell compiles the segmentation model with focal tversky loss and multiple segmentation metrics (dice, IoU, etc.).
Output: Confirmation of compilation with metrics.
This cell sets up callbacks for segmentation training: early stopping, model checkpointing, and learning rate scheduling.
Output: Confirmation of configured callbacks.
This cell trains the ResUNet segmentation model. Uses 1 epoch for demo (would use 50-100 in real training).
Output: Training progress with segmentation metrics.
This cell saves the segmentation model's architecture to a JSON file.
No output.
This cell rebuilds and loads a pre-trained segmentation model with its weights.
Output: Confirmation model loaded with parameters.
This cell uses both classification and segmentation models to make complete predictions on test data.
No output.
This cell creates a dataframe with the prediction results from the segmentation model.
Output: DataFrame with image paths, predicted masks, and mask presence.
This cell merges the prediction results with the original test dataframe.
Output: First few rows of the merged dataframe.
This cell creates a comprehensive visualization showing 10 examples with: original MRI, ground truth mask, predicted mask, MRI with ground truth overlay (red), and MRI with prediction overlay (green).
Output: 10x5 grid of comparison images.
This cell saves the prediction comparison visualization to an image file for documentation.
Output: Confirmation image saved.
This cell creates another visualization showing 12 examples of tumor MRIs with their masks and overlays.
Output: 12x3 grid of tumor examples.
This cell saves the dataset visualization to an image file.
Output: Confirmation image saved.
This is the final cell marker in the notebook.
This notebook implements a complete brain tumor detection system with two main components:
-
Classification Model (ResNet-based): Detects whether a brain MRI contains a tumor or not, achieving ~98% accuracy.
-
Segmentation Model (ResUNet-based): Precisely locates and outlines tumor boundaries in MRIs that contain tumors.
The pipeline uses transfer learning, data augmentation, advanced loss functions, and attention mechanisms to achieve high performance. The final visualizations show excellent tumor detection and segmentation capabilities.