text_plain_output_1.png
from keras.optimizers import Adam

from keras.utils import load_img, img_to_array

from mpl_toolkits.axes_grid1 import ImageGrid

from sklearn.model_selection import train_test_split

from tensorflow.keras.applications import EfficientNetB0,EfficientNetB3,vgg19

from tensorflow.keras.optimizers import Adam

from tensorflow.keras.preprocessing import image

import cv2

import cv2

import keras

import keras.utils as image

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import os

import tensorflow as tf

import numpy as np
import pandas as pd
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        os.path.join(dirname, filename)

np.random.seed(1234)
path = '/kaggle/input/skin-cancer/skin caner'
img_list = os.listdir(path)
lables = [i for i in range(len(img_list))]
label_dict = dict()
label_dict['Normal'] = 0
label_dict['melanoma'] = 1
label_dict['nevus'] = 2
label_dict['pigmented benign keratosis'] = 3

data = []
label = []
C = 0
for cat in img_list:
    C = 0
    pic_list = os.path.join(path, cat)
    for img in os.listdir(pic_list):
        image = os.path.join(pic_list, img)
        if image == '/kaggle/input/skin-cancer/skin caner/Normal/34.avif':
            continue
        else:
            image = cv2.imread(image)
            image = cv2.resize(image, (224, 224))
            data.append(image)
            label.append(label_dict[cat])
            C += 1

data = np.array(data)
data.shape

label = np.array(label)
label.shape

num_classes = 4
label = keras.utils.to_categorical(label, num_classes)

model = vgg19.VGG19(include_top=True, weights=None, input_shape=(224, 224, 3), classes=4)

model.compile(loss='categorical_crossentropy', optimizer=Adam(0.0001), metrics=['accuracy'])

acc = []
for i in range(5):
    x_train, x_test, y_train, y_test = train_test_split(data, label, test_size=0.2, random_state=np.random.randint(1, 1000, 1)[0])
    x_train, x_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size=0.1, random_state=np.random.randint(1, 1000, 1)[0])
    model.fit(x_train, y_train, validation_data=(x_valid, y_valid), epochs=15, batch_size=16, verbose=1)

y_pred = model.predict(x_test)
y_pred = np.argmax(y_pred, axis=1)
y_pred

y_test = np.argmax(y_test, axis=1)

img = cv2.imread('/kaggle/input/skin-cancer/skin caner/Normal/0_0_aidai_0029.jpg')
img = cv2.resize(img, (224, 224))
img = np.expand_dims(img, axis=0)
img_class = model.predict(img, batch_size=1)
score = tf.nn.softmax(img_class[0])

score = np.argmax(score)

label = ''
if score == 0:
    label = 'Normal'
elif score == 1:
    label = 'melanoma'
elif score == 2:
    label = 'nevus'
elif score == 3:
    label = 'pigmented benign keratosis'

from keras.utils import load_img, img_to_array
import keras.utils as image
def predict_image_class(image_path,true_value):

    img = cv2.imread(image_path)
    img = cv2.resize(img,(224,224))
    img = np.expand_dims(img, axis=0)
    img_class = model.predict(img, batch_size=1)
    score = tf.nn.softmax(img_class[0])
    score=np.argmax(score)
    
    label=''

    if score==0:
        
        label='Normal'
    elif score==1:
        
        label='melanoma'
    elif score==2:
        
        label='nevus'
    elif score==3:
        
        label='pigmented benign keratosis'
    print(
        "This image most likely belongs to {}"
        .format(label)
    )
    
    # for folder_name in our_folders:
    fig = plt.figure(1, figsize=(10, 10))
    grid = ImageGrid(fig, 111, nrows_ncols=(1, 1), axes_pad=0.05)
    ax = grid[0]
    
    img = load_img(image_path, (224, 224))
    img = np.array(img.convert('RGB'))
    img = image.img_to_array(img)
    ax.imshow(img / 255.)
    
    ax.text(10, 100, 'True Label: %s' % true_value.upper(), color='g', backgroundcolor='w',\
    alpha=0.8, size = 20)    
    ax.text(10, 150, 'Predicted Label: %s' % label.upper(), color='k', backgroundcolor='w',\
    alpha=0.8, size = 20)
    ax.axis('off')
    plt.show()

predict_image_class('/kaggle/input/skin-cancer/skin caner/pigmented benign keratosis/ISIC_0024495.jpg', 'pigmented')