text_plain_output_1.png
from keras.callbacks import ReduceLROnPlateau

from keras.layers import Dense, Dropout, Flatten , Conv2D , MaxPool2D

from keras.models import Sequential

from keras.optimizers import Adam

from keras.preprocessing.image import ImageDataGenerator

import cv2

import numpy as np

import os

import pandas as pd

in_part1 = set(os.listdir('../input/skin-cancer-mnist-ham10000/ham10000_images_part_1'))

df = pd.read_csv('../input/skin-cancer-mnist-ham10000/HAM10000_metadata.csv')

x_train = []
y_train = []
lesions = set()
for index, row in df.iterrows():
    if row['lesion_id'] in lesions:
        continue
    lesions.add(row['lesion_id'])
    path = '../input/skin-cancer-mnist-ham10000/'
    if row['image_id'] + '.jpg' in in_part1:
        path += 'ham10000_images_part_1/' + row['image_id'] + '.jpg'
    else:
        path += 'ham10000_images_part_2/' + row['image_id'] + '.jpg'
    img = cv2.imread(path)
    img = cv2.resize(img, (100, 75))
    x_train.append(img)
    y_train.append(row['dx'])

y_train = pd.get_dummies(y_train)

y_train = np.asarray(y_train)
mean = np.mean(x_train)
std = np.std(x_train)
x_train = (x_train - mean) / std

input_shape = (75, 100, 3)
num_classes = 7
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(512, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.summary()

optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', patience=3, verbose=1, factor=0.5, min_lr=1e-05)

datagen = ImageDataGenerator(rotation_range=10, zoom_range=0.1, width_shift_range=0.1, height_shift_range=0.1)
datagen.fit(x_train)

epochs = 50
batch_size = 128
history = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, validation_data=(x_validate, y_validate), verbose=1, callbacks=[learning_rate_reduction], shuffle=True)