Skip to content

Skin cancer classification using U-Net segmentation and DenseNet201 transfer learning on HAM10000 dermoscopic images.

Notifications You must be signed in to change notification settings

sopheakchan/segmentation-guided-classification-model-for-skin-cancer

Repository files navigation

Skin Cancer Classification using Segmented Lesion Images

An automated deep learning pipeline for classifying seven types of skin lesions using U-Net segmentation and DenseNet201 transfer learning on the HAM10000 dataset.

Project Overview

This project tackles automated skin cancer classification by combining sophisticated preprocessing with deep learning. We built a complete pipeline: first removing hair artifacts with the Dull Razor algorithm, then using U-Net to segment the actual lesion areas, and finally classifying them with DenseNet201. The key innovation is using segmentation to help the classifier focus only on the lesion itself, ignoring surrounding skin and artifacts.

Dataset - HAM10000

The HAM10000 (Human Against Machine with 10,000 training images) contains 10,015 dermatoscopic images collected from skin cancer practices in Australia and Austria. The dataset covers seven skin lesion types with significant class imbalance:

  • NV (Melanocytic nevus): 6,705 images
  • MEL (Melanoma): 1,113 images
  • BKL (Benign keratosis): 1,099 images
  • BCC (Basal cell carcinoma): 514 images
  • AKIEC (Actinic keratosis): 327 images
  • VASC (Vascular lesion): 142 images
  • DF (Dermatofibroma): 115 images

Note: The original 2GB image dataset is not included in this repo. Download from Kaggle HAM10000 or ISIC Archive.

Preprocessing Pipeline

Dull Razor Algorithm: We applied the Dull Razor technique to remove hair artifacts that can interfere with lesion analysis. The algorithm uses morphological blackhat filtering (11×11 kernel) to detect dark hair strands, creates a binary mask with Gaussian blur and dilation (9×9 kernel), then fills in the hair regions using TELEA inpainting. This preserves lesion boundaries while removing occlusions.

Data Splitting: We used stratified splitting to maintain class distribution across all sets (70% training with augmentation, 15% validation, 15% test).

U-Net Segmentation Model

We built a custom U-Net architecture to precisely segment skin lesions from dermoscopic images. The encoder path has four convolutional blocks (16→32→64→128 filters) with max pooling and dropout (0.1-0.2), a bottleneck with 256 filters and 0.3 dropout, then a decoder path with skip connections that upsamples back through 128→64→32→16 filters. Each block uses double 3×3 convolutions with batch normalization and ReLU activation. Training used 224×224 images, batch size 16, Adam optimizer, and a combined Binary Crossentropy + Dice Loss function. Data augmentation included random flips, rotations (±20°), brightness adjustments, and zoom (0.9-1.1×).

Performance: The U-Net achieved 91%+ accuracy, 0.91+ Dice coefficient, and 0.83+ IoU on the test set, successfully learning to segment lesions with high precision.

After training, we used the U-Net to generate binary masks for all images, then applied these masks to isolate only the lesion regions. This created a focused dataset where the classification model could concentrate on relevant features instead of background skin.

DenseNet201 Classification

We used transfer learning with DenseNet201 (pre-trained on ImageNet) as our classification backbone. The base model feeds into a custom head: GlobalAveragePooling2D → Dense(256, ReLU) → Dense(128, ReLU) → Dropout(0.5) → Dense(7, softmax) for our seven skin lesion classes.

Training Strategy: We trained in three phases. Phase 1 froze the DenseNet201 base and trained only the top layers on ground truth segmented images (Adam lr=1e-4, 10 epochs). Phase 2 unfroze all layers for fine-tuning with lower learning rate (Adam lr=1e-5, 10 epochs). Phase 3 transferred the learned weights to our U-Net segmented dataset and continued fine-tuning (Adam lr=5e-6, 30 epochs). All phases used 224×224 images, batch size 32, with callbacks for early stopping and learning rate reduction.

Results: The model achieved 85% overall accuracy on both ground truth and U-Net segmented datasets, with macro precision/recall/F1 of ~0.85 and macro AUC of 0.92+. Performance varied by class: NV (majority class) reached 90-95% metrics, while minority classes like DF and AKIEC showed 70-75% performance due to limited training samples.

Discussion

Current Challenges: The training curves show instability and volatility in later epochs, indicating the model hasn't fully converged to a stable optimum. Class imbalance remains an issue despite augmentation - minority classes (DF: 115, VASC: 142, AKIEC: 327 images) still underperform compared to NV (6,705 images).

Getting Started

Environment: Python 3.8+, 8GB+ RAM, GPU recommended

Installation:

git clone https://github.com/sopheakchan/skin-cancer-classification.git
cd skin-cancer-classification/segmented-classification-model
pip install -r requirements.txt

Download the HAM10000 dataset from Kaggle and extract to a data/ folder.

Usage:

  1. Run preprocessing-technique/mynotebook.ipynb for Dull Razor preprocessing
  2. Train U-Net with unet-model/unet-trained-model.ipynb (saves best model as best_model.h5)
  3. Generate segmentation masks using preprocessing-technique/unet_model_for-segmentation.ipynb
  4. Train classifier with classification/segmented-classification-model.ipynb for full metrics and visualizations

Acknowledgments

To ISIC Archive for the HAM10000 dataset.

About

Skin cancer classification using U-Net segmentation and DenseNet201 transfer learning on HAM10000 dermoscopic images.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published