Image

Skin Cancer Detection Using Deep Learning

Think about it if diagnosing skin cancer could be done by uploading a picture of the skin. In this project, deep learning is used to make that a possibility. Here we have incorporated state-of-the-art approaches of convolutional neural networks to design a system that would be able to classify skin cancer. It helps healthcare professionals detect skin cancer faster and more accurately. With pre-trained models such as EfficientNetB4 and DenseNet121 and basic CNN, we can infuse medical imaging with the ability to diagnose skin cancer and detect it during the early stages.

The potential impact is early detection of skin cancer may save lives. This project is not simply developing lines of code. It's about enhancing healthcare by using advanced technology as the key tool.

Project Overview

In this project, we have tried to make the challenge of creating a skin cancer detection system using deep learning easier. Based on the dataset of skin cancer images, we fine-tune two advanced pre-trained models of EfficientNetB4 and DenseNet121, as well as a basic CNN to classify skin lesions into different types such as melanoma, pigmented benign keratosis, etc. With EfficientNetB4, we obtained an accuracy of over 80%.

The outcome is forecasts that help doctors in the actual process of diagnosing and may help eliminate deadly diseases at an early stage. Whether you’re a young professional interested in healthcare or an everyday internet user wondering how AI can improve human life, this project demonstrates how machine learning can help.

Prerequisites

Before we jump into the code, here’s what you’ll need:

  • An understanding of Python programming and usage of Google Colab
  • Basic knowledge about deep learning and medical images.
  • Comfortable using frameworks like Tensorflow, Keras, Numpy, OpenCV, and Seaborn to handle data and build models and visualize data and performance of models
  • Skin cancer dataset.

Once you organize these tools, you will notice how almost all of them can be used in the following step. Also, do not stress if you are not a Python master—through the tutorial, you will understand every line of the code!

Approach

The approach involves building, training, and evaluating deep learning models on a skin cancer dataset. We use image-processing techniques and deep-learning architectures to classify skin lesions into different types of cancer. By using pre-trained models like DenseNet and EfficientNet, we enhance the performance of the classification system while also improving accuracy.

The major steps involve:

  • Obtaining and preparing data (augmentation, resizing, normalizing)
  • Training and measuring the performance of several architectures
  • Visualizing performance with confusion matrices and accuracy plots

Workflow and Methodology

This project can be divided into the following basic steps:

  • Data Collection: We collected the skin cancer dataset labeled with different cancer names from Kaggle
  • Data preprocess: To improve the model performance and achieve higher accuracy, we applied different preprocessing techniques. First, we augmented the dataset to create a balanced dataset. Then we resized and normalized the images in 0 to 1 pixel values.
  • Model Selection: In this project, there are three models used (Custom CNN, EfficientNetB4, and DenseNet21).
  • Training and Testing: Each of the Models has been trained on the preprocessed dataset and later, tested on the dataset that was not used during training.
  • Model Evaluation: The evaluation of the model's performance is done by evaluating accuracy, precision, recall, confusion matrix, etc.

The methodology includes

  • Data Preprocessing: The images are resized, normalized, and augmented to improve the performance of models based on them.

  • Model Training: Each model is trained with 100 epochs to enhance the level of performance.

  • Evaluation: Standard metrics (accuracy, precision, recall, f1-score, and confusion matrix) are applied to assess the efficiency of the models.

Dataset Collection

The dataset we used had 2,500 images which were scaled through augmentation to 4,500 images. The dataset was divided in the following manner 80/20 which means 80% of the data was used for training the model and 20% each for validation of the model.

Data Preparation

The dataset was pre-processed by resizing the images to a size of 128 * 128 pixels and scaling the pixels to the range 0 to 255. To increase the variability of the dataset, primarily data augmentation techniques were applied.

Data Preparation Workflow

  • Load Dataset from Google Drive
  • Rotation, flipping, and changes in contrast, among others, are employed to increase the diversity of the datasets.
  • Process and Resize as per Standards used in the model. This helps to standardize the input of the models.
  • Further, the collected dataset has to be split into training and validation sets.

Code Explanation

STEP 1:

Mounting Google Drive

This command mounts your Google Drive to the indicated folder path (/content/drive). After this step has been performed, you will need to allow access to your Google Drive account. After the access has been granted, reading and writing files will become straightforward as you can do this straight from your Drive, which is very helpful in loading datasets and saving the results of the models during the project.

from google.colab import drive
drive.mount('/content/drive')

Import the necessary libraries.

This code block imports all the required libraries for this project for creating, training, and evaluating models. It also imports image processing libraries like PIL and OpenCV for handling images, and matplotlib and seaborn for data visualization. Scikit-learn utilities facilitate model evaluation using metrics such as confusion matrices.

import os
import keras
import numpy as np
from tqdm import tqdm
from keras.models import Sequential
from keras.callbacks import ModelCheckpoint
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras import optimizers
from keras.preprocessing import image
from PIL import Image,ImageOps
import cv2
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix, classification_report
import matplotlib.pyplot as plt
import tensorflow
from tensorflow.keras import Model
from tensorflow.keras.layers import Input, BatchNormalization, ReLU, ELU, Dropout, Conv2D, Dense, MaxPool2D, AvgPool2D, GlobalAvgPool2D, Concatenate
import tensorflow as tf
import tensorflow.keras
from tensorflow.keras import models, layers
from tensorflow.keras.models import Model, model_from_json, Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, SeparableConv2D, UpSampling2D, BatchNormalization, Input, GlobalAveragePooling2D
from tensorflow.keras.regularizers import l2
from tensorflow.keras.optimizers import SGD, RMSprop
from tensorflow.keras.utils import to_categorical

STEP 2:

Data collection and preparation

Load Dataset

This section of code is mainly focused on arranging the paths of the dataset. It starts by guiding the program to the main folder containing the skin cancer Datasets located on Google Drive. After that, it defines two different paths. One for the training set and another for the validation set.

dataset='/content/drive/MyDrive/Skin_Cancer_Detection/Datasets'
train_folder = os.path.join(dataset,"training")
test_folder = os.path.join(dataset,"validation")

Listing categories

The code sets the size of the images, creates a list to hold the names of different classes, checks which classes are available in the training folder, and then prints those class names. This makes it easier to keep track of and understand the different types of images that will be used for training the model.

img_size = 128
categories = []
for i in os.listdir(train_folder):
    categories.append(i)
print(categories)

This function iterates over different folders containing categories of images. Where it also performs reading and resizing images. Also keeps count of images across all categories and stores the processed images alongside their corresponding class numbers in a list. This takes care of the image preparation needed for training a model afterward.
Code Editor