Vaibhav Mali's other Models Reports

Major Concepts

 

Sign-Up/Login to access Several ML Models and also Deploy & Monetize your own ML solutions for free

Models Home » Domain Usecases » Health Care and Pharmaceuticals » 22 Skin Disease Image Classification Using CNN

22 Skin Disease Image Classification Using CNN

Models Status

Model Overview

Skin diseases are one of the most common types of health illnesses faced by the people for ages. The identification of skin disease mostly relies on the expertise of the doctors and skin biopsy results, which is a time-consuming process.
An automated computer-based system for skin disease identification and classification through images is needed to improve the diagnostic accuracy as well as to handle the scarcity of human experts.
Classification of skin disease from an image is a crucial task and highly depends on the features of the diseases considered in order to classify it correctly. Many skin diseases have highly similar
visual characteristics, which add more challenges to the selection of useful features from the image. The accurate analysis of such diseases from the image would improve the diagnosis, accelerates the diagnostic time and leads to better and cost-effective treatment for patients.

The 22 types of skin diseases are :
1) Acne and Rosacea Photos: 0
2) Actinic Keratosis Basal Cell Carcinoma and other Malignant Lesions: 1
3)Atopic Dermatitis Photos: 2
4)Bullous Disease Photos: 3
5)Cellulitis Impetigo and other Bacterial Infections: 4
6)Eczema Photos: 5
7)Exanthems and Drug Eruptions: 6
8)Hair Loss Photos Alopecia and other Hair Diseases: 7
9)Herpes HPV and other STDs Photos: 8
10)Light Diseases and Disorders of Pigmentation: 9
11)Lupus and other Connective Tissue diseases: 10
12)Melanoma Skin Cancer Nevi and Moles: 11
13)Nail Fungus and other Nail Disease: 12
14)Poison Ivy Photos and other Contact Dermatitis: 13
15)Psoriasis pictures Lichen Planus and related diseases: 14
16)Scabies Lyme Disease and other Infestations and Bites: 15
17)Seborrheic Keratoses and other Benign Tumors: 16
18)Systemic Disease: 17
19)Urticaria Hives: 18
20)Vascular Tumors': 19
21)Vasculitis Photos: 20
22)Warts Molluscum and other Viral Infections': 21
number of classes : 22

Dataset Link: https://www.kaggle.com/datasets/shubhamgoel27/dermnet




Import Necessary Library
import matplotlib.pyplot as plt
import cv2
import numpy as np

from pathlib import Path

from PIL import Image


Load the path of the images and stored each variable image classes in a dictionary and also give the classes names.


my_path = Path(r'C:\Users\vaibhav1.mali\PycharmProjects\skin\train')

skin_images_dict = {
'Acne and Rosacea Photos': list(my_path.glob('Acne and Rosacea Photos/*')),
'Actinic Keratosis Basal Cell Carcinoma and other Malignant Lesions': list(my_path.glob('Actinic Keratosis Basal Cell Carcinoma and other Malignant Lesions/*')),
'Atopic Dermatitis Photos': list(my_path.glob('Atopic Dermatitis Photos/*')),
'Bullous Disease Photos': list(my_path.glob('Bullous Disease Photos/*')),
'Cellulitis Impetigo and other Bacterial Infections': list(my_path.glob('Cellulitis Impetigo and other Bacterial Infections/*')),
'Eczema Photos': list(my_path.glob('Eczema Photos/*')),
'Exanthems and Drug Eruptions': list(my_path.glob('Exanthems and Drug Eruptions/*')),
'Hair Loss Photos Alopecia and other Hair Diseases': list(my_path.glob('Hair Loss Photos Alopecia and other Hair Diseases/*')),
'Herpes HPV and other STDs Photos': list(my_path.glob('Herpes HPV and other STDs Photos/*')),
'Light Diseases and Disorders of Pigmentation': list(my_path.glob('Light Diseases and Disorders of Pigmentation/*')),
'Lupus and other Connective Tissue diseases': list(my_path.glob('Lupus and other Connective Tissue diseases/*')),
'Melanoma Skin Cancer Nevi and Moles': list(my_path.glob('Melanoma Skin Cancer Nevi and Moles/*')),
'Nail Fungus and other Nail Disease': list(my_path.glob('Nail Fungus and other Nail Disease/*')),
'Poison Ivy Photos and other Contact Dermatitis': list(my_path.glob('Poison Ivy Photos and other Contact Dermatitis/*')),
'Psoriasis pictures Lichen Planus and related diseases': list(my_path.glob('Psoriasis pictures Lichen Planus and related diseases/*')),
'Scabies Lyme Disease and other Infestations and Bites': list(my_path.glob('Scabies Lyme Disease and other Infestations and Bites/*')),
'Seborrheic Keratoses and other Benign Tumors': list(my_path.glob('Seborrheic Keratoses and other Benign Tumors/*')),
'Systemic Disease': list(my_path.glob('Systemic Disease/*')),
'Urticaria Hives': list(my_path.glob('Urticaria Hives/*')),
'Vascular Tumors': list(my_path.glob('Vascular Tumors/*')),
'Vasculitis Photos': list(my_path.glob('Vasculitis Photos/*')),
'Warts Molluscum and other Viral Infections': list(my_path.glob('Warts Molluscum and other Viral Infections/*'))
}
skin_labels_dict = {
'Acne and Rosacea Photos': 0,
'Actinic Keratosis Basal Cell Carcinoma and other Malignant Lesions': 1,
'Atopic Dermatitis Photos': 2,
'Bullous Disease Photos': 3,
'Cellulitis Impetigo and other Bacterial Infections': 4,
'Eczema Photos': 5,
'Exanthems and Drug Eruptions': 6,
'Hair Loss Photos Alopecia and other Hair Diseases': 7,
'Herpes HPV and other STDs Photos': 8,
'Light Diseases and Disorders of Pigmentation': 9,
'Lupus and other Connective Tissue diseases': 10,
'Melanoma Skin Cancer Nevi and Moles': 11,
'Nail Fungus and other Nail Disease': 12,
'Poison Ivy Photos and other Contact Dermatitis': 13,
'Psoriasis pictures Lichen Planus and related diseases': 14,
'Scabies Lyme Disease and other Infestations and Bites': 15,
'Seborrheic Keratoses and other Benign Tumors': 16,
'Systemic Disease': 17,
'Urticaria Hives': 18,
'Vascular Tumors': 19,
'Vasculitis Photos': 20,
'Warts Molluscum and other Viral Infections': 21
}

Read the image and resizing it using cv2 library. Convert it into array and also scaled it 


X = []
y = []
for tumor_name,images in skin_images_dict.items():
for image in images:
img = cv2.imread(str(image))
resize = cv2.resize(img,(200,200))
X.append(resize)
y.append(skin_labels_dict[tumor_name])

X = np.array(X)
y = np.array(y)

X_scaled = X/255

Load the CNN mode


from keras import  models,layers
model = models.Sequential([
layers.Conv2D(filters=32, kernel_size=(4, 4),
activation='relu', input_shape=(200, 200, 3)),
layers.MaxPooling2D(pool_size=(2, 2)),

layers.Conv2D(filters=64, kernel_size=(3, 3),
activation='relu'),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(filters=124, kernel_size=(3, 3),
activation='relu'),
layers.Dropout(0.5),
layers.Flatten(),
layers.Dense(22,activation='softmax')
]
)

The purpose of Early Stopping is to avoid overfitting by stopping the model before it happens using a defined condition. If you use it, and then you save the model when the training is stopped, you will get a model that is assumed to be good enough and not overfitted.
The purpose of the class model checkpoint is to save models several times while training. This can be useful to find at which epoch the model gets the best performance. So, if you use it, you will get several models that are saved at different epochs


from keras.callbacks import ModelCheckpoint, EarlyStopping

checkpoint = ModelCheckpoint('skin_disease.h5',
monitor='loss',
mode='min',
save_best_only=True,
verbose=1)

earlystop = EarlyStopping(monitor='loss',
min_delta=0,
patience=3,
verbose=1,
restore_best_weights=True)

Model Compile defines the loss function, the optimizer and the metrics. That's all. It has nothing to do with the weights and you can compile a model as many times as you want without causing any problem to pretrained weights and fit the model 


model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)

model.fit(X_scaled,y,epochs=1,callbacks = [checkpoint,earlystop])

Now loading the test dataset 


test_path = Path(r'C:\Users\vaibhav1.mali\PycharmProjects\skin\test')

test_images_dict = {
'Acne and Rosacea Photos': list(test_path.glob('Acne and Rosacea Photos/*')),
'Actinic Keratosis Basal Cell Carcinoma and other Malignant Lesions': list(test_path.glob('Actinic Keratosis Basal Cell Carcinoma and other Malignant Lesions/*')),
'Atopic Dermatitis Photos': list(test_path.glob('Atopic Dermatitis Photos/*')),
'Bullous Disease Photos': list(test_path.glob('Bullous Disease Photos/*')),
'Cellulitis Impetigo and other Bacterial Infections': list(test_path.glob('Cellulitis Impetigo and other Bacterial Infections/*')),
'Eczema Photos': list(test_path.glob('Eczema Photos/*')),
'Exanthems and Drug Eruptions': list(test_path.glob('Exanthems and Drug Eruptions/*')),
'Hair Loss Photos Alopecia and other Hair Diseases': list(test_path.glob('Hair Loss Photos Alopecia and other Hair Diseases/*')),
'Herpes HPV and other STDs Photos': list(test_path.glob('Herpes HPV and other STDs Photos/*')),
'Light Diseases and Disorders of Pigmentation': list(test_path.glob('Light Diseases and Disorders of Pigmentation/*')),
'Lupus and other Connective Tissue diseases': list(test_path.glob('Lupus and other Connective Tissue diseases/*')),
'Melanoma Skin Cancer Nevi and Moles': list(test_path.glob('Melanoma Skin Cancer Nevi and Moles/*')),
'Nail Fungus and other Nail Disease': list(test_path.glob('Nail Fungus and other Nail Disease/*')),
'Poison Ivy Photos and other Contact Dermatitis': list(test_path.glob('Poison Ivy Photos and other Contact Dermatitis/*')),
'Psoriasis pictures Lichen Planus and related diseases': list(test_path.glob('Psoriasis pictures Lichen Planus and related diseases/*')),
'Scabies Lyme Disease and other Infestations and Bites': list(test_path.glob('Scabies Lyme Disease and other Infestations and Bites/*')),
'Seborrheic Keratoses and other Benign Tumors': list(test_path.glob('Seborrheic Keratoses and other Benign Tumors/*')),
'Systemic Disease': list(test_path.glob('Systemic Disease/*')),
'Urticaria Hives': list(test_path.glob('Urticaria Hives/*')),
'Vascular Tumors': list(test_path.glob('Vascular Tumors/*')),
'Vasculitis Photos': list(test_path.glob('Vasculitis Photos/*')),
'Warts Molluscum and other Viral Infections': list(test_path.glob('Warts Molluscum and other Viral Infections/*'))
}

Now stored in a dictionary and load the image using cv2 library


X_test = []
y_test = []

for tumor_name,images in skin_images_dict.items():
for image in images:
img = cv2.imread(str(image))
resize = cv2.resize(img,(200,200))
X_test.append(resize)
y_test.append(skin_labels_dict[tumor_name])

X_test = np.array(X_test)
y_test = np.array(y_test)

X_test_scaled = X_test/255

Now evaluate the test images


model.evaluate(X_test_scaled,y_test)

Now check the confusion matrix and classification report of the model


y_pred = list()
for i in model.predict(X_test_scaled):
y_pred.append(np.argmax(i))
y_pred = np.array(y_pred)

from sklearn.metrics import classification_report,confusion_matrix
print(classification_report(y_test,y_pred))


import seaborn as sns
import pandas as pd
class_names = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
fig,ax = plt.subplots()
tick_marks = np.arange(len(class_names))
plt.xticks(tick_marks,class_names)
plt.yticks(tick_marks,class_names)
cnf_matrix = confusion_matrix(y_test,y_pred)
sns.heatmap(pd.DataFrame(cnf_matrix), annot = True, cmap = 'YlGnBu',
fmt = 'g')
ax.xaxis.set_label_position('top')
plt.tight_layout()
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
plt.show()





From the above classification report we see that 
Accuracy = 95.00%
F1-score = 97.00%

As we see the decent accuracy and f1-score. So our model perform well.


0 comments