text_plain_output_2.png text_plain_output_1.png
from keras.optimizers import Adam

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 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])