Skip to content

Repository files navigation

Leukemia Cell Image Classification

An educational computer-vision project that classifies microscopic blood-cell images as:

  • HEM - healthy cells (0)
  • ALL - acute lymphoblastic leukemia cells (1)

The project is implemented as a Jupyter notebook and explores classical machine-learning baselines alongside transfer learning with pretrained convolutional neural networks.

Medical-use notice: This project is for coursework and research demonstration only. It is not a clinical diagnostic tool and must not be used to make medical decisions.

Project contents

.
|-- Leukimia_Classification.ipynb   # Data preparation, experiments, evaluation, and demo
|-- best_classification_model.pth   # Saved weights of the top validation experiment
`-- C-NMC_Leukemia.zip              # Dataset archive

What the notebook does

  1. Loads the C-NMC leukemia dataset from its folder structure.
  2. Builds a stratified 80/20 training/validation split from training_data.
  3. Resizes RGB images to 128 x 128 and applies ImageNet normalization.
  4. Establishes baselines with logistic regression on flattened pixels and a simple MLP.
  5. Trains pretrained ResNet18 and MobileNetV2 classifiers using:
    • frozen feature extractors;
    • fine-tuning of the final feature block;
    • optional augmentation, dropout, and weight decay.
  6. Compares accuracy, precision, recall, and F1-score; saves the best experiment; and demonstrates predictions on validation or chosen images.

The notebook's conclusion identifies fine-tuned ResNet18 without additional regularization as the best-performing configuration in its recorded experiment.

Kaggle Dataset

https://www.kaggle.com/datasets/andrewmvd/leukemia-classification

Dataset layout

Extract C-NMC_Leukemia.zip so the project contains this structure:

C-NMC_Leukemia/
|-- training_data/
|   |-- fold_0/
|   |   |-- all/
|   |   `-- hem/
|   |-- fold_1/
|   `-- fold_2/
`-- testing_data/
    `-- C-NMC_test_final_phase_data/

The notebook expects C-NMC_Leukemia beside it. It uses all as the positive ALL/leukemia class and hem as the healthy class.

Setup

Use Python 3.10+ and create an isolated environment if desired.

python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install jupyter torch torchvision pandas numpy matplotlib pillow tqdm scikit-learn scikit-image

Then extract the dataset archive and start Jupyter:

Expand-Archive .\C-NMC_Leukemia.zip -DestinationPath .
jupyter notebook

Open Leukimia_Classification.ipynb and run the cells in order. PyTorch automatically uses CUDA when it is available; otherwise it trains on CPU. The first use of ResNet18 or MobileNetV2 may download ImageNet pretrained weights.

Training configuration

The default notebook configuration is:

Setting Value
Image size 128 x 128
Batch size 16
Frozen-feature epochs 3
Fine-tuning epochs 2
Validation split 20%, stratified, random seed 42
Frozen-head learning rate 0.001
Fine-tuning learning rate 0.0001

Regularized experiments add horizontal flips, rotations, colour jitter, small translations, dropout (0.4), and weight decay (1e-4).

Using the saved checkpoint

best_classification_model.pth contains only a PyTorch state_dict, not the model architecture. Recreate the matching architecture before loading it. The checkpoint was produced by the notebook's selected best model; based on the notebook conclusion, that is a fine-tuned ResNet18 without the regularized head.

For prediction, apply the same preprocessing used in the notebook: resize to 128 x 128, convert to a tensor, and normalize with ImageNet mean [0.485, 0.456, 0.406] and standard deviation [0.229, 0.224, 0.225].

Methods Used

This project combines classical machine learning and deep learning approaches for leukemia cell image classification. It begins with exploratory dataset inspection, visualization, and Histogram of Oriented Gradients (HOG) feature extraction to understand image patterns. Two baseline models are evaluated: Logistic Regression trained on flattened pixel values and a simple Multi-Layer Perceptron (MLP). The main experiments use transfer learning with pretrained ResNet18 and MobileNetV2 architectures, replacing their final layers for binary classification between healthy (HEM) and leukemia (ALL) cells. Both frozen-feature training and fine-tuning of the final network blocks are tested. To reduce overfitting and improve generalization, the project also evaluates data augmentation, dropout, and L2 weight decay. Models are compared using accuracy, precision, recall, F1-score, classification reports, and confusion matrices.

About

Leukemia cell image classification using classical machine learning and transfer learning with ResNet18 and MobileNetV2.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages