This repository contains code for training and evaluating models for different MRI modalities (t1, t1ce, t2, flair, and stack) and performing explainability analysis using Grad-CAM, Guided Grad-CAM, LIME, and XRAI methods.
- Import the necessary libraries, including os, glob, numpy, nibabel, splitfolders, and pathlib.
- Define the
create_directory
function to create directories if they don't already exist. - Specify the
input_folder
andoutput_folder
paths for the dataset. - Use the
split_folders_with_ratio
function to split the dataset into train, validation, and test folders based on the specified ratio. - Define a list of file paths for training and testing, and create necessary directories for saving the MRI slices.
- Use the
extract_slices
function to extract and save individual MRI slices for each modality and data type. - Use the
stack_3_slices
function to extract, save, and stack three slices from the t2, t1ce, and flair modalities. - Use the
stack_4_slices
function to extract, save, and stack four slices from the t2, t1ce, flair, and t1 modalities.
- Import the necessary custom functions from the
Model_functions
andexplainabilty_functions
modules. - Set the
home_dir
andbase_dir
variables to the corresponding directory paths. - Specify the
modality
variable for the desired MRI modality. - Load the data for training, validation, and testing using the
load_from_dir
function. - Preprocess and combine the HGG and LGG data.
- Convert labels to numerical values and perform one-hot encoding.
- Convert data to arrays and shuffle the datasets.
- Generate class weights for imbalanced datasets.
- Perform data augmentation using the ImageDataGenerator.
- Set up hyperparameter tuning using the Hyperband tuner.
- Search for the best hyperparameters and models for each tuner.
- Define callbacks for early stopping, learning rate reduction, and model checkpointing.
- Specify the folder path for saving the training plots.
- Fit the best models from each tuner to the training data using the best hyperparameters.
- Load the pretrained models for explainability analysis.
- Plot ROC curves for the loaded models.
- Generate predictions and compute classification metrics.
- Plot a confusion matrix.
- Perform Grad-CAM and Guided Grad-CAM analysis.
- Perform LIME analysis.
- Perform XRAI analysis.
- Save the computed coefficients to CSV files for further analysis.
Note: Make sure to have the required dependencies installed before running the code.