Photo by National Cancer Institute on Unsplash

Enhanced Melanoma Classifier Model with CNN-VGG16

Mary Adewunmi
CodeX
Published in
8 min readAug 16, 2021

--

Having promised to increase the model accuracy from the last post, I have researched further. In addition to increasing the image size of the image dataset through augmentation, preprocessing the images with VGG16 prior to training the model with CNN can give higher accuracy. While looking for ways of boosting the accuracy, I ran into Keras preprocessing function, which is specific to the VGG16 model keras.applications.vgg16.preprocess_input and does additional steps by subtracting the average RGB channel values from the original ImageNet training set.

In this project, we present a Melanoma classifier model based on the Deep Neural Network(DNN) originated by the Visual Geometry Group with 16 layers — VGG16 (Simonyan, K., & Zisserman, A. (2014). First, we pre samples as byte plot pictures, in which each byte represents one grayscale pixel. Using the convolutional layers of VGG16 pre-trained on the ImageNet dataset, we extract the filter activation maps (also known as bottleneck features) by deducting the average RGB channel values of the original Image net and adjust the training set images.

Full code can be found here.

🌱Objective of this Project

It is to increase the accuracy of the Melanoma Classifier Model.

🌱The framework used in this project

This just illustrated how we arrive at a faster model with high accuracy for training melanoma images.

  • Collate the datasets from here
  • Preprocess the images with VGG16 by extracting the features and labels
  • Set the parameters or fine-tuning the images and environment for the CNN model
  • Train the images with CNN
  • Evaluate the model with accuracy as the metrics
The architecture of the VGG16 Preprocessed Model — Author

🌱Code Implementation

Import the necessary libraries especially Keras and Tensor flow using accuracy,precision and recall as the metrics.

🌱Load datasets

Load the datasets by using already splitter datasets, which was 2239 images to train and valid dataset batches and 118 to test batches dataset using an image data generator library.

We used TensorFlow-Keras, vgg16 image data generator, more on this can be found here

📍Note that this is where the images are preprocessed with VGG16.

👉Viewing the images, can you spot the difference in the input datasets?

Original images
VGG16 Preprocessed lesion images — Author

🌱Train the Images with CNN

Over the last decade, Convolutional Neural Networks have achieved breakthroughs in a range of pattern recognition domains, ranging from image processing to speech recognition. The most advantageous feature of CNNs is that they reduce the number of parameters in ANN. This accomplishment has inspired academics and developers to consider larger models to perform challenging tasks that were previously impossible to solve with traditional ANNs; The most significant assumption about issues solved by CNN is that they should not contain spatially dependent properties. To put it another way, in a face detection application, we don’t need to worry about where the faces are in the photos (S. Albawi et al.).

We used the Sequential model with convolutional 2D, kernel size (3,3), relu activation, padding with zeros, pool size, 2,2, learning rate, Adam optimizer with accuracy as metrics.

The layer’s activation function is called ‘Activation.’ Models using an activation function can account for nonlinear relationships. When forecasting diabetes in patients, for clarification, predicting a lesion image type out of 9 lesion types is a nonlinear model, we will use ReLU, or Rectified Linear Activation, as our activation function. Even though it is made up of two linear parts, it has been shown to operate effectively in neural networks.

Source

Padding is equal to the ‘same’ by adding zeros to the images in other to boost the image quality after reducing the size of the images to fit into the CNN model requirement.

The optimizer and loss parameters are used to compile the model.
The learning rate is controlled by the optimizer. As an optimizer, we’ll use the ‘adam.’ Throughout the training, the adam optimizer adjusts the learning rate. The learning rate controls how quickly the model’s optimal weights are calculated. A slower learning rate of 0.0001, yes, it may take longer to compute the weights but it can result in more accurate weights.

In the domain of image processing, pooling is useful for lowering the resolution and complexity. The number of filters is unaffected by pooling. One of the most frequent forms of pooling strategies is max-pooling. It divides the image into sub-region rectangles and only returns the maximum value of each sub-inside. region’s One of the most common sizes used in max-pooling is 2×2. with stride 2. Moreover, pooling can be used with non-equal filters and strides to improve the efficiency(D. Stutz and L. Beyer(2014))

The model looks like this👇

Model Summary

🌱Compile the model

Epochs and verbose The number of epochs determines how many times the model cycles over the data. The model will improve overtime as we run more epochs, up to a point. After that, the model will no longer improve with each epoch. Furthermore, the more epochs the model has, the longer it will take to run.

history = model.fit(x=train_batches,
steps_per_epoch=len(train_batches),
validation_data=valid_batches,
validation_steps=len(valid_batches),
epochs=10,
verbose=2
)

🌱 Performance Computation

Area under Curve(AUC) metric of 93% was achieved which indicated a very high accuracy melanoma classifier. The results also showed that there was no overfitting because the training and validation loss was declining, not increasing or with a weird-looking accuracy graph😂.

result_batch = model.predict(test_batches)labels_batch = label_names[np.argmax(result_batch, axis=-1)]
labels_batch

🎯Results👇

Remember, we have 118 test images for testing our classifier model after splitting our datasets using the image data generator code.

Are you thinking what I am thinking🤔

👌So, this model can actually be used for classifying other types of Skin cancer apart from Melanoma like Basal Cell Carcinoma, Dermatofibroma,Nevus, Squamous Cell Carcinoma and the rest.

array(['Basal Cell Carcinoma', 'Basal Cell Carcinoma', 'Nevus',
'Basal Cell Carcinoma', 'Melanoma', 'Basal Cell Carcinoma',
'Pigmented Benign Keratosis', 'Dermatofibroma', 'Nevus', Nevus',
'Nevus', 'Squamous Cell Carcinoma', 'Nevus',
'Pigmented Benign Keratosis', 'Pigmented Benign Keratosis',
'Nevus', 'Nevus', 'Nevus', 'Nevus', 'Basal Cell Carcinoma',
'Pigmented Benign Keratosis', 'Melanoma', 'Basal Cell Carcinoma',
'Melanoma', 'Pigmented Benign Keratosis', 'Melanoma',
'Basal Cell Carcinoma', 'Basal Cell Carcinoma',
'Pigmented Benign Keratosis', 'Pigmented Benign Keratosis',
'Basal Cell Carcinoma', 'Nevus', 'Squamous Cell Carcinoma',
'Basal Cell Carcinoma', 'Pigmented Benign Keratosis', 'Nevus',
'Melanoma', 'Nevus', 'Nevus', 'Basal Cell Carcinoma', 'Nevus',
'Melanoma', 'Melanoma', 'Nevus', 'Vascular Lesion', 'Nevus',
'Nevus', 'Melanoma', 'Nevus', 'Pigmented Benign Keratosis',
'Basal Cell Carcinoma', 'Pigmented Benign Keratosis', 'Nevus',
'Pigmented Benign Keratosis', 'Nevus', 'Nevus', 'Nevus',
'Melanoma', 'Basal Cell Carcinoma', 'Nevus', 'Nevus', 'Melanoma',
'Nevus', 'Melanoma', 'Basal Cell Carcinoma',
'Pigmented Benign Keratosis', 'Pigmented Benign Keratosis',
'Nevus', 'Nevus', 'Pigmented Benign Keratosis', 'Melanoma',
'Nevus', 'Nevus', 'Nevus', 'Basal Cell Carcinoma',
'Pigmented Benign Keratosis', 'Pigmented Benign Keratosis',
'Nevus', 'Pigmented Benign Keratosis', 'Squamous Cell Carcinoma',
'Basal Cell Carcinoma', 'Melanoma', 'Pigmented Benign Keratosis',
'Nevus', 'Pigmented Benign Keratosis', 'Melanoma',
'Vascular Lesion', 'Melanoma', 'Melanoma', 'Melanoma',
'Pigmented Benign Keratosis', 'Nevus',
'Pigmented Benign Keratosis', 'Pigmented Benign Keratosis',
'Melanoma', 'Nevus', 'Melanoma', 'Pigmented Benign Keratosis',
'Melanoma', 'Nevus', 'Basal Cell Carcinoma', 'Vascular Lesion',
'Pigmented Benign Keratosis', 'Pigmented Benign Keratosis',
'Basal Cell Carcinoma', 'Melanoma', 'Nevus',
'Pigmented Benign Keratosis', 'Pigmented Benign Keratosis',
'Nevus', 'Squamous Cell Carcinoma', 'Pigmented Benign Keratosis',
'Nevus', 'Nevus', 'Melanoma', 'Basal Cell Carcinoma',
'Pigmented Benign Keratosis', 'Nevus'], dtype='<U26')

The classifier also achieved a precision of 1, recall of 0.5 and an F1-score of 0.67.

🌱Save Classifier Model and Result Prediction

We saved the melanoma model using this simple code below for future reuse and transfer learning purposes.

saved_model = model.save("melanoma_model")

🎯Results prediction

Remember the data sets has been split into Melanoma and other classes of lesion images under the test directory. So, you can pick the images one after the other to classify whether it is Melanoma or not Melanoma. If it is Melanoma, then urgent action needs to be taken place and if otherwise, further investigation can be carried out.

from keras.preprocessing import image
img = image.load_img('../input/skin-cancer9-classesisic/Skin cancer ISIC The International Skin Imaging Collaboration/Test/dermatofibroma/ISIC_0001114.jpg',target_size=(224,224))
img = np.asarray(img)
plt.imshow(img)
img = np.expand_dims(img, axis=0)
from keras.models import load_model
saved_model = load_model("melanoma_model")
output = saved_model.predict(img)
if output[0][0] > output[0][1]:
print("melanoma")
else:
print('not melanoma')

Melanoma Classifier for images ISIC_0000002(Melanoma),ISIC_0000004.jpg(Melanoma) and ISIC_0001114.jpg(Not Melanoma)

ISIC_0000002.jpg
ISIC_0000004.jpg
ISIC_0001114.jpg

🌱Conclusion

This Melanoma Classifier achieved a performance accuracy of 93%, precision of 1 and recall of 0.5% with Melanoma images, through preprocessing images with VGG16. This indicated that training images need to be adjusted before they can be trained on deep learning models especially when aiming at higher performance accuracy.

Full code can be found on my here

For researchers, kindly reference and cite the work using this link https://www.scienceopen.com/document/read?id=bbdc162f-242b-41d4-a07a-6df03110f0bd

🌱References

📌Rotemberg, V., Kurtansky, N., Betz-Stablein, B., Caffery, L., Chousakos, E., Codella, N., Combalia, M., Dusza, S., Guitera, P., & Gutman, D. (2021). A patient-centric dataset of images and metadata for identifying melanomas using clinical context. Scientific Data, 8(1), 1–8.

📌Simonyan, K., & Zisserman, A. (2014). Very deep convolutional networks for large-scale image recognition. arXiv preprint arXiv:1409.1556.

📌https://www.kaggle.com/c/siim-isic-melanoma-classification/data

📌S. Albawi, T. A. Mohammed and S. Al-Zawi, “Understanding of a convolutional neural network,” 2017 International Conference on Engineering and Technology (ICET), 2017, pp. 1–6, doi: 10.1109/ICEngTechnol.2017.8308186.

📌D. Stutz and L. Beyer, “Understanding Convolutional Neural Networks,” 2014.

📌https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator

Congrats! You are now well on your way to building a Melanoma cancer classifier with VGG16-CNN👏👏

For referencing and citations, the scientific poster on this work can be found here

The Github repository for this tutorial can be found here.

Thanks for reading!

Happy coding😘

--

--

Mary Adewunmi
CodeX
Writer for

I am a Data scientist/Deep learning Researcher with focus on using Deep learning/Computer vision for medical image diagnosis.