Skin Cancer Classification: Malignant vs. Benign

Jan 21, 2025

Skin cancer is one of the most common types of cancer worldwide, with early detection being crucial for effective treatment. Distinguishing between malignant (cancerous) and benign (non-cancerous) skin lesions is a critical step in diagnosis, but it requires expertise and precision. Traditionally, this process involved manual examination and biopsies by dermatologists. However, artificial intelligence (AI) is now making skin cancer classification more accurate and accessible by leveraging advanced image classification models.

This blog discusses the importance of skin cancer classification, the role of AI in distinguishing malignant and benign lesions, and the impact on patient outcomes.


1. Why Skin Cancer Classification is Essential

The two primary categories of skin lesions are:

  • Benign Lesions: Non-cancerous and typically harmless. Examples include moles, warts, and other non-cancerous skin growths.

  • Malignant Lesions: Cancerous and potentially dangerous, such as melanoma, basal cell carcinoma, and squamous cell carcinoma.

Detecting malignant lesions early increases the chances of successful treatment, while accurately identifying benign lesions helps avoid unnecessary biopsies, reducing patient anxiety and healthcare costs.

2. Benefits of AI in Skin Cancer Classification

Using AI for skin cancer classification offers several advantages:

  • Early Detection: AI can detect malignant patterns at an early stage, even before visible signs, which can improve survival rates.

  • Enhanced Accuracy: By reducing human error, AI offers more consistent and accurate results, helping to avoid both missed cancers and false alarms.

  • Time and Cost Savings: Automated classification reduces the time needed for diagnoses, potentially allowing dermatologists to see more patients and lowering healthcare costs.

  • Remote Access: AI-powered tools can provide skin cancer assessments in remote areas, making diagnostics more accessible to underserved populations.

3. Implementing Skin Cancer Classification with Matrice

  1. Dataset Preparation

  2. Model Training

  3. Model Evaluation

  4. Model Inference

  5. Model Deployment

Dataset Preparation

This dataset contains a balanced dataset of images of benign skin moles and malignant skin moles. The data consists of two folders with each 1800 pictures (224x244) of the two types of moles.

Model Training

The model was trained using the following experiment parameters:

Parameter

Value

Description

Model

efficientnet_v2_s

EfficientNetV2 Small variant - optimized for accuracy and efficiency

Batch Size

4

Number of samples processed in each training iteration

Epochs

50

Number of complete passes through the training dataset

Learning Rate

0.001

Initial learning rate for model optimization

LR Gamma

0.1

Multiplicative factor for learning rate decay

LR Min

0.00001

Minimum learning rate threshold

LR Scheduler

StepLR

Step learning rate scheduling for controlled decay

LR Step Size

10

Number of epochs between learning rate updates

Min Delta

0.0001

Minimum change in monitored quantity for early stopping

Momentum

0.95

Momentum coefficient for optimizer

Optimizer

AdamW

AdamW optimizer with weight decay regularization

Patience

5

Number of epochs with no improvement before early stopping

Primary Metric

acc@1

Top-1 accuracy used for model evaluation

Weight Decay

0.0001

L2 regularization factor to prevent overfitting

Model training graph from Matrice platform

SkinCancerClassificationTrainGraph

Model Evaluation

Once training was complete, we evaluated the model using key performance metrics to ensure its effectiveness:

  • Accuracy@1: Measures the model’s top-1 prediction accuracy

  • Accuracy@5: Measures if the correct class appears in the model’s top 5 predictions

  • Precision: Indicates the proportion of correct positive predictions

  • Recall: Measures the proportion of actual positives correctly identified

  • Specificity: Measures the proportion of actual negatives correctly identified

  • F1 Score: The harmonic mean of precision and recall

Validation Results:

Metric

All Categories

Benign

Malignant

Accuracy@1

0.839

0.839

0.839

Precision

0.832

0.819

0.869

Recall

0.844

0.906

0.758

F1 Score

0.835

0.860

0.810

Specificity

0.839

0.758

0.906

Test Results:

Metric

All Categories

Benign

Malignant

Accuracy@1

0.842

0.842

0.842

Precision

0.834

0.817

0.882

Recall

0.849

0.917

0.752

F1 Score

0.838

0.864

0.812

Specificity

0.842

0.752

0.917

These metrics validate the model’s strong performance across both skin lesion categories, with particularly high recall for benign lesions (0.917) and high precision for malignant lesions (0.882) in the test set. The model shows good balance between precision and recall, as reflected in the F1 scores, while maintaining strong accuracy and specificity across all categories. These results indicate the model will perform reliably in real-world skin cancer classification scenarios.

Model Inference Optimization

A unique feature of our platform is the ability to export trained models to a variety of formats. For this use case, the model can be exported from PyTorch (.pt) format to formats like ONNX, TensorRT, and OpenVINO. This is particularly valuable for on-the-edge deployments, where processing power may be limited.

By offering flexibility in model format, we ensure that models can be deployed in real-time settings without requiring extensive computational resources.

Model Deployment

Once the model is trained and optimized, deploying it is seamless with Matrice. Our platform supports real-time inference and allows integration via APIs for use in various applications.

You can use our pre-built API integration code for various programming languages, making it easy to computer vision functionality into web services, mobile apps, or custom applications.

4. Real-World Applications and Impact

AI-based skin cancer classification is already making an impact in healthcare:

  • Teledermatology: Patients can upload images to remote diagnostic systems, where AI algorithms assess the risk of malignancy and suggest follow-ups.

  • Clinical Decision Support: Dermatologists use AI as a tool for second opinions, increasing diagnostic confidence and improving patient management.

  • Screening Programs: AI-driven classification can streamline large-scale skin cancer screening, identifying high-risk patients for further testing.

These applications are contributing to faster, more accurate diagnoses and improving patient outcomes by ensuring timely intervention.

Conclusion

AI-based skin cancer classification is transforming how dermatologists detect and diagnose skin cancer. By accurately distinguishing between malignant and benign lesions, AI tools support early detection, efficient diagnosis, and better patient outcomes. As AI technology continues to evolve, it will play an even more integral role in dermatology, helping save lives through improved skin cancer care.

Think CV, Think Matrice

Experience 40% faster deployment and slash development costs by 80%