Instructions to use abink/Tumor_Classification with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use abink/Tumor_Classification with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://abink/Tumor_Classification") - Notebooks
- Google Colab
- Kaggle
Create README.md
Browse files# Brain MRI Tumor Classification Using Deep Learning

## 1. Introduction
Cancer remains one of the major global health challenges, and the increasing availability of medical imaging has created opportunities to support clinicians through computer-aided image analysis. In oncology, medical imaging plays an important role in the detection, characterization, treatment planning, and follow-up of tumors.
**Brain MRI** is particularly important in the assessment of brain tumors because MRI provides detailed soft-tissue contrast without exposing the patient to ionizing radiation. However, interpretation of large numbers of medical images can be time-consuming and requires considerable clinical expertise.
This project explores the use of **deep learning for automated brain MRI tumor classification**, combining concepts from **medical imaging, artificial intelligence, machine learning, and deep learning**.
The objective was not simply to train a CNN and report its accuracy, but to investigate how different pretrained deep-learning architectures perform on brain MRI data and how **transfer learning, layer selection, fine-tuning, optimization, and regularization** affect classification performance.
---
## 2. Medical Imaging Dataset
The project uses brain MRI images representing four classification categories:
1. **Glioma**
2. **Meningioma**
3. **Pituitary tumor**
4. **No Tumor**
Gliomas, meningiomas, and pituitary tumors represent three major tumor categories considered in this classification task, while the **No Tumor** class provides a non-tumor reference category.
The dataset was organized as a four-class image-classification problem with a balanced test set of **1,600 images (400 per class)**.
A consistent preprocessing and augmentation pipeline was maintained across the different architectures to make the model comparison more meaningful.
**Input image size:** `128 Γ 128 Γ 3`
---
## 3. Project Objective
The main objective was to investigate whether transfer-learning-based CNN architectures could effectively learn discriminative features from brain MRI images and determine which architecture provided the most reliable classification performance.
Rather than relying on a single neural network, multiple established architectures were evaluated:
* **VGG16**
* **ResNet50**
* **DenseNet121**
* **EfficientNetB0**
The models were first evaluated using their pretrained ImageNet features and subsequently investigated through **domain-specific fine-tuning**.
The project also examined how changing the classification head, trainable layers, learning rate, optimizer, regularization, and fine-tuning strategy affected performance.
---
# 4. Transfer Learning and Model Development
All models used **ImageNet-pretrained convolutional backbones**. The original classification layers were removed and replaced with task-specific classification heads.
The general workflow was:
```text
Brain MRI Dataset
β
Preprocessing & Augmentation
β
Train / Validation / Test Split
β
ImageNet-Pretrained CNN
β
Custom Classification Head
β
Initial Transfer Learning
β
Layer Unfreezing & Fine-Tuning
β
Performance Evaluation
β
Model Comparison
```
For the classification heads, architectures using:
```text
GlobalAveragePooling2D
β
Dense Layers
β
Batch Normalization
β
Dropout
β
4-Class Softmax
```
were investigated.
Fine-tuning was performed by selectively unfreezing deeper convolutional layers while keeping earlier feature-extraction layers frozen. Lower learning rates were used during fine-tuning to avoid excessively changing useful pretrained features.
---
# 5. Model Comparison
The four architectures showed substantially different behavior on the brain MRI classification task.
| Model | Initial Accuracy | Fine-Tuned Accuracy | Final Status |
| ------------------ | ---------------: | ------------------: | --------------- |
| **VGG16** | 83.44% | **91.50%** | β Selected |
| **DenseNet121** | 83.00% | **83.00%** | Strong baseline |
| **ResNet50** | 68.00% | **81.00%** | Baseline |
| **EfficientNetB0** | 33.00% | **35.00%** | Not selected |
### Best-performing model
**Fine-tuned VGG16**
* Test Accuracy: **91.50%**
* Macro F1: **0.91**
* Weighted F1: **0.91**
Fine-tuning improved VGG16 from **83.44% to 91.50%**, an improvement of **8.06 percentage points**.
---
# 6. VGG16
VGG16 was the strongest architecture evaluated in this project.
### Classification Architecture
```text
VGG16 (ImageNet pretrained)
β
GlobalAveragePooling2D
β
Dense(256) + BatchNorm + Dropout(0.4)
β
Dense(128) + BatchNorm + Dropout(0.3)
β
Dense(4, Softmax)
```
The initial VGG16 experiment used the pretrained backbone with a custom classification head.
The classification head was subsequently improved by replacing `Flatten()` with `GlobalAveragePooling2D`, reducing the number of parameters and helping control overfitting.
### Fine-Tuning
The final convolutional block was unfrozen while earlier layers remained frozen.
Training used:
* Adam optimizer
* Initial learning rate: `1 Γ 10β»β΄`
* Fine-tuning learning rate: `1 Γ 10β»β΅`
* Early stopping
* ReduceLROnPlateau
* Model checkpointing
### Performance
| Class | Precision | Recall | F1 |
| ---------- | --------: | -------: | -------: |
| Glioma | 0.95 | 0.98 | **0.97** |
| Meningioma | 0.92 | **1.00** | **0.96** |
| No Tumor | 0.84 | 0.93 | **0.88** |
| Pituitary | 0.97 | 0.75 | **0.84** |
**Final accuracy: 91.50%**
The strong performance across the tumor classes made VGG16 the selected architecture for subsequent stages of the project.
---
# 7. ResNet50
ResNet50 was evaluated as an alternative deep residual architecture.
### Architecture
```text
ResNet50 (ImageNet pretrained)
β
GlobalAveragePooling2D
β
Dense(256) + BatchNorm + Dropout(0.4)
β
Dense(128) + BatchNorm + Dropout(0.3)
β
Dense(4, Softmax)
```
The backbone was initially frozen and the classification head was trained. The final convolutional section was subsequently unfrozen for domain-specific fine-tuning.
### Results
| Class | Precision | Recall | F1 |
| ---------- | --------: | -----: | ---: |
| Glioma | 0.89 | 0.91 | 0.90 |
| Meningioma | 0.78 | 0.99 | 0.87 |
| No Tumor | 0.78 | 0.66 | 0.72 |
| Pituitary | 0.77 | 0.68 | 0.72 |
**Initial accuracy:** 68.00%
**Fine-tuned accuracy:** 81.00%
Fine-tuning produced a substantial improvement of approximately **13 percentage points**, demonstrating that adaptation of pretrained features to the MRI domain was beneficial.
However, ResNet50 remained below the performance of fine-tuned VGG16.
---
# 8. DenseNet121
DenseNet121 was included because dense feature reuse and hierarchical feature propagation make it an important architecture for medical-image classification.
### Architecture
```text
DenseNet121 (ImageNet pretrained)
β
GlobalAveragePooling2D
β
Dense(256) + BatchNorm + Dropout(0.4)
β
Dense(128) + BatchNorm + Dropout(0.3)
β
Dense(4, Softmax)
```
### Fine-Tuning Investigation
The initial fine-tuning strategy reduced performance from:
**83% β 79%**
A more conservative DenseNet-specific strategy was then investigated:
* Later convolutional layers selectively unfrozen
* Batch Normalization layers kept frozen
* AdamW optimizer
* Learning rate: `3 Γ 10β»βΆ`
* Weight decay: `1 Γ 10β»β΄`
* Early stopping
* ReduceLROnPlateau
The optimized configuration recovered the performance to **83%**, but did not provide a meaningful overall improvement over the original frozen-backbone configuration.
### Best Results
| Class | Precision | Recall | F1 |
| ---------- | --------: | -----: | ---: |
| Glioma | 0.85 | 0.96 | 0.90 |
| Meningioma | 0.83 | 0.97 | 0.90 |
| No Tumor | 0.77 | 0.72 | 0.74 |
| Pituitary | 0.88 | 0.66 | 0.76 |
**Accuracy: 83.00%**
**Macro F1: 0.82**
DenseNet121 therefore provided a strong baseline but did not outperform the fine-tuned VGG16.
---
# 9. EfficientNetB0
EfficientNetB0 was evaluated using the same dataset and general transfer-learning framework.
### Architecture
```text
EfficientNetB0 (ImageNet pretrained)
β
GlobalAveragePooling2D
β
Dense(256) + BatchNorm + Dropout(0.4)
β
Dense(128) + BatchNorm + Dropout(0.3)
β
Dense(4, Softmax)
```
The backbone was initially frozen and subsequently partially fine-tuned.
### Results
**Initial accuracy: 33%**
**Fine-tuned accuracy: 35%**
The model exhibited severe class-collapse behavior and failed to reliably identify some of the tumor categories.
Therefore, EfficientNetB0 was not selected for further development within this project.
---
# 10. Overall Findings
The experiments demonstrated that **architecture selection and fine-tuning strategy have a major effect on medical-image classification performance**.
The results were:
| Architecture | Initial | Fine-Tuned | Improvement |
| -------------- | ------: | ---------: | -----------: |
| **VGG16** | 83.44% | **91.50%** | **+8.06 pp** |
| ResNet50 | 68.00% | 81.00% | +13.00 pp |
| DenseNet121 | 83.00% | 83.00% | 0 pp |
| EfficientNetB0 | 33.00% | 35.00% | +2.00 pp |
An important observation was that **fine-tuning did not improve every architecture equally**.
* VGG16 responded strongly to domain-specific fine-tuning.
* ResNet50 showed substantial improvement.
* DenseNet121 required a more conservative fine-tuning strategy and ultimately remained around its baseline performance.
* EfficientNetB0 showed severe classification difficulties under the tested configuration.
This demonstrates that pretrained architectures cannot simply be treated as interchangea