An automated machine learning pipeline for extracting complex spatial, morphological, and textural features from digitized histopathology images to detect metastatic cancer.
- Project Overview
- Dataset
- Data Handling
- Feature Extraction Methodology
- Training Pipeline
- Model Performance
- Usage
The objective of this project is to automate the detection of metastatic cancer cells in small image patches extracted from larger digital pathology scans of lymph node sections. By translating raw Hematoxylin and Eosin (H&E) stained images into dense numerical representations, this pipeline leverages advanced computer vision algorithms and Classification model to identify malignant tissue characteristics.
CancerScope detection dashboard showing an 83.61% malignancy risk prediction with multi-feature extraction panel and group contribution analysis.
The data for this project is a modified version of the PatchCamelyon (PCam) benchmark dataset, provided via Kaggle. It consists of small 96x96 pixel microscopic images of lymph node tissue sections.
The overarching task is binary classification: algorithms must predict whether the center 32x32 pixel region of a given patch contains at least one pixel of metastatic tumor tissue. The outer boundary region of the image is deliberately included to allow fully-convolutional models to process the tissue without relying on zero-padding, ensuring consistent behavior when applied to whole-slide images. Unlike the original PCam dataset, this specific iteration has been scrubbed of duplicate images.
Rather than relying solely on abstract representations from deep neural networks, this pipeline explicitly calculates the underlying mathematical mechanics of the tissue's morphology and texture.
The grids below demonstrate how our pipeline transforms a standard H&E image into distinct mathematical spaces. Notice the structural chaos and high density in the malignant sample compared to the uniform structure of the normal sample.
The image_features.py module extracts 7 distinct families of hand-crafted features, resulting in a 51-dimensional vector per image:
DoG is an edge-enhancement algorithm used to detect blob-like structures, making it highly effective for isolating cell nuclei. It works by convolving the original image
where
- Biological Intuition: Quantifies pleomorphism (irregular nuclear size) and crowding, which are primary morphological hallmarks of malignancy.
To quantify staining variability and tissue density independently of illumination, the BGR image is transformed into the HSV (Hue, Saturation, Value) color space. Statistical moments are then computed for each channel
- Biological Intuition: Captures hyperchromasia; malignant nuclei absorb hematoxylin stain differently, causing distinct mathematical shifts in the Hue and Saturation distributions.
GLCM evaluates macro-texture by analyzing the spatial relationships of pixel intensities. It counts the frequency at which pairs of pixels with specific gray-level values (
- Biological Intuition: Measures structural chaos. Healthy tissue yields high homogeneity, while the fragmented nature of cancer tissue generates high contrast scores.
LBP is a micro-texture descriptor that labels every pixel by thresholding its circular neighborhood. For a center pixel
where
- Biological Intuition: Translates the microscopic cellular boundaries into a histogram of geometric shapes (edges, corners, flat spots), revealing irregular cellular membranes.
This approach captures highly complex, second-order spatial-texture relationships. It first applies the LBP transformation to encode local micro-textures across the image array. Next, a GLCM matrix is computed on top of the LBP output, evaluating the macro-spatial relationships of those micro-textures ($GLCM(LBP(I))$).
- Biological Intuition: Identifies complex meta-patterns, such as whether chaotic microscopic edges tend to cluster together in dense malignant tumors.
GLRLM extracts directional patterns by assessing consecutive sequences (runs) of identical pixel values. The matrix
where
- Biological Intuition: Normal connective tissue often presents as long, continuous pixel runs, whereas the erratic growth of cancer cells breaks these runs into short, high-frequency fragments.
SFTA evaluates fractal dimensions by decomposing the image into binary layers via multi-level thresholding. It computes the area, mean, and standard deviation for the resulting regions, and evaluates boundary complexity using the box-counting fractal dimension
where
- Biological Intuition: Measures the "jaggedness" of the tissue landscape; malignant boundaries exhibit high fractal complexity compared to the smooth boundaries of healthy cells.
The training architecture (train.py) utilizes a robust, scikit-learn-based pipeline designed for scalability and rigor for binary classification.
- Dimensionality Expansion: Numerical features are passed through
PolynomialFeaturesto capture non-linear, interactive relationships between the complex biological metrics. - Scaling: Data is standardized using
StandardScaler(or optionallyMinMaxScaler) to ensure uniform feature contribution. - Categorical Handling: Any categorical variables are parsed through a
OneHotEncoder.
The pipeline currently evaluates Random Forest Classifier, Support Vector Machines (SVR), and eXtreme Gradient Boosting (XGBoost). Training is optimized through three available strategies:
- Bypass Search: Direct training using base parameters (fastest approach, standard configurations).
- Randomized Search: Rapid, stochastic exploration of the hyperparameter grid using cross-validation.
- Bayesian Optimization: Utilizes
skopt.BayesSearchCVto efficiently navigate the hyperparameter space based on prior trial performance.
The current production model utilizes a Random Forest Classifier trained directly on the base parameters. It was evaluated on a hidden test dataset comprising 20% of the total data.
- Model: Random Forest Classifier (Base Parameters)
- Test Accuracy: 0.8876
- Test ROC AUC: 0.9542
Classification Report:
| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| 0 (Normal) | 0.89 | 0.92 | 0.91 | 26,182 |
| 1 (Cancer) | 0.88 | 0.84 | 0.86 | 17,823 |
| Accuracy | 0.89 | 44,005 | ||
| Macro Avg | 0.89 | 0.88 | 0.88 | 44,005 |
| Weighted Avg | 0.89 | 0.89 | 0.89 | 44,005 |
(Note: Configuration states are saved dynamically to Config/model_config.yaml and the optimal model is serialized to Models/model_rf.pkl.)
To run the extraction pipeline independently and generate the .npz dataset:
cd src
python data_handler.py \
--csv "path/to/train_labels.csv" \
--images "path/to/train/images" \
--output_dir "Data/" \
--features dog color glcm lbp lbglcm glrlm sfta \
--samples -1 (Set --samples to a specific integer to test on a smaller subset, or -1 for the full dataset).



