text_plain_output_1.png
from tensorflow.keras.layers import Input , Dense , Flatten , GlobalAveragePooling2D

from tensorflow.keras.models import Sequential

from tensorflow.keras.preprocessing import image_dataset_from_directory

import matplotlib.image as mping

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import os

import random

import random

import splitfolders

import tensorflow as tf

os.makedirs('output')
os.makedirs('output/train')
os.makedirs('output/val')
os.makedirs('output/test')
loc = '/kaggle/input/skin-diseases-image-dataset/IMG_CLASSES'
splitfolders.ratio(loc, output='output', ratio=(0.8, 0.1, 0.1))

def random_image(val_dir, val_class):
    folder = val_dir + val_class
    random_image = random.sample(os.listdir(folder), 1)
    img = mping.imread(folder + '/' + random_image[0])
    return img

fig = plt.figure(figsize=(10, 7))
#Add an Axes to the figure as part of a subplot arrangement(Three integers (nrows, ncols, index).)
fig.add_subplot(2,2,1)
img_1 = random_image(val_dir = "./output/val/",val_class = "2. Melanoma 15.75k")
fig.add_subplot(2,2,2)
img_2 = random_image(val_dir = "./output/val/",val_class = "4. Basal Cell Carcinoma (BCC) 3323")
fig.add_subplot(2,2,3)
img_3 = random_image(val_dir = "./output/val/",val_class = "5. Melanocytic Nevi (NV) - 7970")
fig.add_subplot(2,2,4)
img4 = random_image(val_dir = "./output/val/",val_class = "1. Eczema 1677")

fig = plt.figure(figsize=(10, 7))
#Add an Axes to the figure as part of a subplot arrangement(Three integers (nrows, ncols, index).)
fig.add_subplot(2,2,1)
img_1 = random_image(val_dir = "./output/val/",val_class = "3. Atopic Dermatitis - 1.25k")
fig.add_subplot(2,2,2)
img_2 = random_image(val_dir = "./output/val/",val_class = "6. Benign Keratosis-like Lesions (BKL) 2624")
fig.add_subplot(2,2,3)
img_3 = random_image(val_dir = "./output/val/",val_class = "7. Psoriasis pictures Lichen Planus and related diseases - 2k")
fig.add_subplot(2,2,4)
img4 = random_image(val_dir = "./output/val/",val_class = "10. Warts Molluscum and other Viral Infections - 2103")

from tensorflow.keras.preprocessing import image_dataset_from_directory
train_dir = './output/train'
test_dir = './output/test'
val_dir = './output/val'
train_data = image_dataset_from_directory(train_dir, label_mode='categorical', image_size=(299, 299), batch_size=32, shuffle=True, seed=42)
test_data = image_dataset_from_directory(test_dir, label_mode='categorical', image_size=(299, 299), batch_size=32, shuffle=False, seed=42)
val_data = image_dataset_from_directory(val_dir, label_mode='categorical', image_size=(299, 299), batch_size=32, shuffle=False, seed=42)

addModel = tf.keras.applications.xception.Xception(input_shape=(299, 299, 3), include_top=False, weights='imagenet')
model = Sequential()
model.add(addModel)
model.add(GlobalAveragePooling2D())
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax', name='classification'))

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005), loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

history_1 = model.fit(train_data, validation_data=val_data, epochs=10)

model.evaluate(val_data)
plt.figure()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['loss', 'val_loss'], loc='upper right')
plt.show()
plt.figure()
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['accuracy', 'val_accuracy'], loc='upper right')
plt.show()