Detecting Skin Cancer using Deep Learning

Using a Convolutional Neural Network to detect malignant tumours with the accuracy of human experts

Mishaal Lakhani
7 min readJan 30, 2019

Quick, think of five people you love. Your mom, dad, sister or brother, best friend and grandma perhaps?

Statistically, 1 of these 5 people will get skin cancer at some point in their life. Keeping true to cancer’s deadly reputation, most skin cancers either spread to other parts of the body and/or are fatal unless detected and treated early. And to rain harder on your parade, because of the inefficiency in our healthcare systems today, the diagnosis and treatment will begin in stages much later than it could (and should) be starting.

Skin cancer is the single most common malignancy affecting humankind, with over 5.4 million, in the USA alone, diagnosed on a yearly basis.

Yet, diagnoses is still a visual process, which relies on the long-winded procedure of clinical screenings, followed by dermoscopic analysis, and then a biopsy and finally a histopathological examination. This process easily takes months and the need for many medical professionals and still is only ~77% accurate.

The sheer amount of time and technicalities it takes when diagnosing the patient (let alone beginning their treatment) and the many opportunities for human error, leaves thousands dead annually.

Current methods using AI to diagnose lesions

But, with the development of artificial intelligence and machine learning capabilities, there is shining potential to spare time and mitigate errors- saving millions of lives in the long run.

In particular, Convolutional Neural Networks (CNNs) can automate most of the diagnosis process with equal or more accuracy than the current methods. For a refresher on neural nets, you can read an introductory article here (This article assumes you have a fundamental understanding of NNs and CNNs).

To see for myself, I replicated a CNN using 10,000 training images to compare the results to human experts and analyze the results to see how they contrast. This article goes through the process and explains how a basic CNN can be enhanced quite easily and match or surpass human capabilities.

An overview of the steps we’ll take

In 14, relatively simple steps, I’ll show how I built and tuned this model, as well as the final results and how they compare.

The dataset I used includes 7 major categories of skin cancers, Melanocytic nevi, Melanoma, Benign keratosis-like lesions, Basal cell carcinoma, Actinic keratoses, Vascular lesions, and Dermatofibroma. This model uses the HAM10000 dataset and is processed on a GPU.

Step 1: Importing Essential Libraries

We begin with data preprocessing steps.

These libraries include Matplotlib, Numpy, Pandas, Sklearn and Keras.

There is also a function to plot the validation loss/accuracy. (Validation loss is the error after running the validation set of data through the trained network- basically how accurate the model is).

Step 2: Making a Dictionary of images and labels

To have one source of data, both folders of the dataset are combined and a corresponding dictionary of simple labels is created for the different types.

Step 3: Reading and Processing Data

A path is created to join the image folder(base folder) with a clear folder and new columns are added for better organization of the data.

Can see a sample of what the data looks like with the newly added columns.

Step 4: Data Cleaning

The dataset is checked for missing values or data types for each field, and are filled in with null values (for age, in this case).

Step 5: Exploratory data analysis (EDA)

This step is to better visualize and understand the data for ourselves- seeing the different features of the dataset, how it is distributed, and some actual numbers.

Can see the distribution of the data by category

The data set is also distributed into 4 main categories of lesions — Histopathology, Confocal, follow up, and consensus

Other features such as the location of lesions and age/gender of the patient can also be visualized.

The back, lower extremity and trunk are compromised areas of skin cancer
Ages 30–60 are most prone

Step 6: Loading & Resizing of images

The images are loaded into the image column and resized so Tensorflow can handle the size.

Now, you can also view the pictures in the dataset organized by the type of skin cancer.

Step 7: Train Test Split

The data is split into training and testing set with 80 20 division respectively.

Step 8: Normalization

The x_train, x_test is normalized by subtracting from the mean values and then dividing by their deviation.

Step 9: Label Encoding

Labels have at this point are the 7 different classes of skin cancer types from numbers 0 to 6. These labels are encoded to one hot vectors.

Step 10: Train validation split

The training is split even more with a small fraction — 10%, kept for the validation set (which the model is evaluated on) and the remaining 90% is used to train the model.

Step 11: Model Building (CNN)

Here comes the fun part, using Keras sequential API and adding one layer at a time staring with input. The part that really separates CNN’s from other Neral Nets

A quick reminder of the 4 steps of CNNs:

Convolution -> pooling -> flatten -> full connection

Basically…

  • Start off with an input image.
  • Apply filters or feature maps to the image, which gives a convolutional layer.
  • Break up the linearity of the image using the rectifier function.
  • The image is ready for the pooling step (which is for “spatial invariance”)
  • After pooling is done, it ends up with a pooled feature map.
  • The pooled feature map is flattened before inserting it into an artificial neural network.

Step 12: Setting Optimizer & Annealing

Once our layers are added to the model, we need to set up

  • a score function
  • a loss function (error rate between the observed labels and the predicted ones) — helps to measure how poorly our model performs on images with known labels.
  • an optimization algorithm — To iterate and improve parameters (filters kernel values, weights and bias of neurons) to minimize the loss.

SURPRISE!!! Bonus step 🎉

Data Augmentation- It’s optional. But, to avoid overfitting problem, we can artificially expand the HAM 10000 dataset to be even larger. The idea is to adjust the training data with small transformations to reproduce the variations of the same picture with additions like grayscales, horizontal flips, vertical flips, random crops, translations, rotations and more.

Step 13: Fitting the model

The model is fit into x_train, y_train. A batch size of 10 and 50 epochs is chosen — It important to keep in mind, the smaller the batch size is, the more efficiently the model is trained.

A snapshot of some of the epochs- It would make the article A LOT longer if I added all of them

Step 14: Model Evaluation (Testing and validation accuracy, confusion matrix, analysis of misclassified instances)

The testing accuracy and validation accuracy of our model are checked, and the confusion matrix is plotted. The misclassified images count of each type is also determined.

And that’s it! Not too bad considering it is only a couple steps away from a binary classifier CNN.

In the end, the model had an accuracy of about 78% which and can be easily trained to go above 80%. In comparison to a human eye having 77.0344% accuracy, and the time and effort it takes, it’s safe to say our model is the winner in terms of efficiency.

If you enjoyed this article clap it and follow me!

Also, check out my LinkedIn!

--

--

Mishaal Lakhani

Modern day renaissance girl (in training). Learning things I find interesting about the world and life, and occasionally sharing them :)