import tensorflow as tf
from tensorflow.keras import models, layers
import matplotlib.pyplot as plt
from pprint import pprint
import cv2
import numpy as np
Load Dataset :
import pathlib
from pathlib import Path
my_path = Path(r'/content/Skin disease/Train ')
skin_images_dict = {
'Chickenpox': list(my_path.glob('Chickenpox /*')),
'Impetigo': list(my_path.glob('Impetigo /*')),
'Infectious erythema': list(my_path.glob('Infectious erythema /*')),
'Scabies': list(my_path.glob('Scabies /*')),
'Skin warts': list(my_path.glob('Skin warts /*'))
}
skin_labels_dict = {
'Chickenpox': 0,
'Impetigo': 1,
'Infectious erythema': 2,
'Scabies': 3,
'Skin warts':4
}
Dependent and Independent Variable:
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])
Perform some data preprocessing :
X = np.array(X)
y = np.array(y)
X_scaled = X/255
Model Creation :
from tensorflow.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(270,activation='relu'),
layers.Dense(5,activation='softmax')
]
)
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
Train Model :
model.fit(X_scaled,y,epochs=20)
Model Evaluation :
Load Test data :
test_path = Path(r'/content/Skin disease/Test ')
test_images_dict = {
'Chickenpox': list(test_path.glob('Chickenpox/*')),
'Impetigo': list(test_path.glob('Impetigo/*')),
'Infectious erythema': list(test_path.glob('Infectious erythema/*')),
'Scabies': list(test_path.glob('Scabies/*')),
'Skin warts': list(test_path.glob('Skin warts/*'))
}
Perform data processing operations on test data as we perform on train data.
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
Check Accuracy for test data :
model.evaluate(X_test_scaled,y_test)
Classification Report :
Class 0 : Chickenpox
Class 1 : Impetigo
Class 2 : Infectious erythema
Class 3 : Scabies
Class 4 : Skin warts
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))
Confusion Matrix :
import seaborn as sns
import pandas as pd
class_names = [0,1,2,3,4]
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()
Thank You ):