Skip to content

dewangbhanushali-25/brain_tumor-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Brain Tumor Classification using ResNet50

A deep learning project for classifying brain tumors from MRI images using transfer learning with ResNet50 architecture.

Project Overview

This project implements a brain tumor classification system that can identify and classify brain tumors into four categories:

  • Class 0: No tumor
  • Class 1: Meningioma
  • Class 2: Glioma
  • Class 3: Pituitary tumor

The model uses transfer learning with a pre-trained ResNet50 architecture, enhanced with custom fully connected layers and extensive data augmentation to achieve high classification accuracy.

Dataset

Source: Brain Tumor Dataset from figshare (Dataset ID: 1512427)

Dataset Statistics:

  • Total images: 3,064 T1-weighted contrast-enhanced MRI scans
  • Image resolution: 512 × 512 pixels
  • Pixel dimensions: 0.49 × 0.49 mm²
  • Slice thickness: 6 mm
  • Slice gap: 1 mm
  • Patients: 233

Class Distribution:

  • Meningioma: 708 slices
  • Glioma: 1,426 slices
  • Pituitary tumor: 930 slices

Data Split:

  • Training: 2,144 samples (70%)
  • Validation: 460 samples (15%)
  • Testing: 460 samples (15%)

After 8x Augmentation:

  • Training: 17,152 samples
  • Validation: 3,680 samples
  • Testing: 3,680 samples

Model Architecture

Base Model

Pre-trained ResNet50 (ImageNet weights) with all layers trainable

Custom Classification Head

ResNet50 Base
    ↓
Linear(2048 → 2048)
    ↓
SELU Activation
    ↓
Dropout(p=0.4)
    ↓
Linear(2048 → 2048)
    ↓
SELU Activation
    ↓
Dropout(p=0.4)
    ↓
Linear(2048 → 4)
    ↓
LogSigmoid

Data Augmentation Strategy

The custom BrainTumorDataset class implements comprehensive data augmentation with 8 augmented versions per sample:

  1. Original image
  2. Random rotation (±45°)
  3. Random rotation (±90°)
  4. Random rotation (±120°)
  5. Random rotation (±180°)
  6. Random rotation (±270°)
  7. Random rotation (±300°)
  8. Random rotation (±330°)

Image Preprocessing:

  • Conversion to PIL Image
  • Random rotation augmentation
  • Tensor conversion
  • One-hot encoding of labels (4 classes)

Training Configuration

Hyperparameters

  • Optimizer: SGD with momentum (0.9)
  • Learning Rate: 3e-4
  • Loss Function: CrossEntropyLoss
  • Batch Size: 4
  • Epochs: 30 (17 completed in notebook)
  • Hardware: Google Colab with Tesla T4 GPU

DataLoader Settings

  • Training: batch_size=4, shuffle=True, pin_memory=True, num_workers=8
  • Validation: batch_size=4, shuffle=True, pin_memory=True, num_workers=8
  • Testing: batch_size=10, shuffle=True, pin_memory=True, num_workers=8

Training Results (17 Epochs)

Epoch Training Accuracy Validation Accuracy Training Loss Validation Loss Duration
1 55.79% 76.32% 0.2076 0.3480 13.06 min
5 89.73% 91.26% 0.0321 0.9825 13.05 min
8 94.09% 95.50% 0.0392 0.0114 13.05 min
10 95.04% 96.93% 0.5381 0.4361 13.05 min
14 97.73% 98.60% 0.3301 0.0039 13.05 min
17 98.19% 99.04% 0.2755 0.0038 13.05 min

Best Validation Accuracy: 99.04% (Epoch 17)

Data Preprocessing Pipeline

Dataset Extraction (Untitled0.ipynb)

# 1. Mount Google Drive
drive.mount('/content/drive')

# 2. Extract dataset from ZIP files
# 4 separate ZIP files combined into single dataset

# 3. Convert .mat files to .jpg images
# - Load MATLAB files containing MRI scans
# - Extract image data and labels
# - Normalize pixel values
# - Save as grayscale JPEG images

# 4. Extract metadata
# - Labels: tumor classification (1-3)
# - Patient ID
# - Tumor border coordinates
# - Tumor mask

# 5. Create training data pickle
# - Resize images to 512x512
# - Convert grayscale to RGB
# - Pair images with labels
# - Save as pickle file

Data Structure

dataset/
├── imageData/          # Original .mat files (3,064 files)
├── bt_images/          # Extracted JPEG images
├── labels.pickle       # Label array (3,064 labels)
└── training_data.pickle # Image-label pairs

File Structure

brain-tumor-classification/
├── Untitled.ipynb              # Main training notebook
├── Untitled0.ipynb             # Data preprocessing notebook
├── bt_resnet50_model.pt        # Final model weights
├── bt_resnet50_ckpt_v2.pth.tar # Best checkpoint
└── dataset/
    ├── imageData/              # Original MATLAB files
    ├── bt_images/              # Processed images
    ├── labels.pickle           # Labels
    └── training_data.pickle    # Training data

Dependencies

torch>=1.9.0
torchvision>=0.10.0
h5py>=3.0.0
opencv-python>=4.5.0
numpy>=1.19.0
pandas>=1.3.0
matplotlib>=3.4.0
seaborn>=0.11.0
scikit-learn>=0.24.0
Pillow>=8.3.0

Installation & Setup

1. Clone Repository

git clone <repository-url>
cd brain-tumor-classification

2. Install Dependencies

pip install torch torchvision h5py opencv-python numpy pandas matplotlib seaborn scikit-learn

3. Download Dataset

# Download from figshare: https://figshare.com/articles/dataset/1512427
# Extract to dataset/ directory

4. Preprocess Data

# Run Untitled0.ipynb to:
# - Extract .mat files to images
# - Create training_data.pickle

5. Train Model

# Run Untitled.ipynb
# Or use the pre-trained model: bt_resnet50_model.pt

Usage

Training

# Load preprocessed data
with open('dataset/training_data.pickle', 'rb') as file:
    training_data = pickle.load(file)

# Create datasets
train_set = BrainTumorDataset(X_train, y_train)
valid_set = BrainTumorDataset(X_valid, y_valid)

# Train model
for epoch in range(epochs):
    for batch, (y, X) in enumerate(train_gen):
        # Forward pass
        y_pred = resnet_model(X.view(-1, 3, 512, 512))
        loss = criterion(y_pred, y)
        
        # Backward pass
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

Inference

# Load trained model
model = models.resnet50()
model.fc = nn.Sequential(
    nn.Linear(2048, 2048),
    nn.SELU(),
    nn.Dropout(p=0.4),
    nn.Linear(2048, 2048),
    nn.SELU(),
    nn.Dropout(p=0.4),
    nn.Linear(2048, 4),
    nn.LogSigmoid()
)
model.load_state_dict(torch.load('bt_resnet50_model.pt'))
model.eval()

# Predict
with torch.no_grad():
    prediction = model(image_tensor)
    predicted_class = torch.argmax(prediction, dim=1)

Key Features

Transfer Learning: Leverages pre-trained ResNet50 knowledge
Extensive Augmentation: 8x data multiplication through rotations
High Accuracy: 99.04% validation accuracy achieved
Robust Architecture: Deep fully connected layers with dropout
GPU Accelerated: CUDA support for faster training
Checkpoint System: Saves best model based on validation loss
Medical-Grade: T1-weighted contrast-enhanced MRI compatibility

Model Performance Characteristics

Strengths

  • Excellent generalization (99%+ validation accuracy)
  • Handles rotational variance well
  • Stable training with minimal overfitting
  • Fast inference time

Training Insights

  • Rapid initial learning (76% accuracy in epoch 1)
  • Consistent ~13 minute epochs on Tesla T4
  • Validation accuracy exceeds training accuracy (good regularization)
  • Low final validation loss (0.0038)

Acquisition Protocol

MRI Specifications:

  • Modality: T1-weighted contrast-enhanced MRI
  • Contrast Agent: Gd-DTPA injection
  • Gd Dose: 0.1 mmol/kg at 2 ml/s
  • Acquisition Period: 2005-2010
  • Hospitals: Nanfang Hospital (Guangzhou) & General Hospital (Tianjin Medical University)

Citations

If you use this dataset, please cite:

  1. Cheng, Jun, et al. "Enhanced Performance of Brain Tumor Classification via Tumor Region Augmentation and Partition." PloS one 10.10 (2015).

  2. Cheng, Jun, et al. "Retrieval of Brain Tumors by Adaptive Spatial Pooling and Fisher Vector Representation." PloS one 11.6 (2016).

Hardware Requirements

Future Improvements

  • Implement Grad-CAM visualization for interpretability
  • Add ensemble methods with other architectures
  • Experiment with newer models (EfficientNet, Vision Transformers)
  • 5-fold cross-validation as provided in cvind.mat
  • Web application for clinical deployment
  • DICOM file support
  • Real-time inference optimization
  • Multi-view MRI analysis

License

Dataset: Original dataset from figshare (Dataset ID: 1512427)
Code: MIT License (check repository for details)

Credit to

For questions or collaboration:

  • Author: Jun Cheng
  • Affiliation: School of Biomedical Engineering, Shenzhen University
  • Email: chengjun583@qq.com

Acknowledgments

  • ResNet50 architecture by Microsoft Research
  • PyTorch team for the deep learning framework
  • Google Colab for providing free GPU resources
  • Original dataset authors and contributing hospitals

About

this repo is playing with resenet and image processing technique

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors