text_plain_output_1.png
from efficientnet import EfficientNetB5

from keras.applications import DenseNet121, ResNet50, InceptionV3, Xception

from keras.layers import Dense, Dropout, GlobalAveragePooling2D, LeakyReLU

from keras.models import Model, Sequential

import cv2

import matplotlib.pyplot as plt

import numpy as np

import os

import pandas as pd

import sys

test_df = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')
test_df['id_code'] = test_df['id_code'].apply(lambda x: x + '.png')
diag_text = ['Normal', 'Mild', 'Moderate', 'Severe', 'Proliferative']
num_classes = 5

def display_raw_images(df, columns = 4, rows = 3):
    fig=plt.figure(figsize = (5 * columns, 4 * rows))
    for i in range(columns * rows):
        image_name = df.loc[i,'id_code']
        img = cv2.imread(f'../input/aptos2019-blindness-detection/test_images/{image_name}')[...,[2, 1, 0]]
        fig.add_subplot(rows, columns, i + 1)
        plt.imshow(img)
    plt.tight_layout()

display_raw_images(test_df)

sys.path.append(os.path.abspath('../input/efficientnet/efficientnet-master/efficientnet-master/'))
from efficientnet import EfficientNetB5

from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint, Callback
from keras.layers import Dense, Dropout, GlobalAveragePooling2D, LeakyReLU
from keras.applications import DenseNet121, ResNet50, InceptionV3, Xception
from keras.models import Model, Sequential
from keras.optimizers import Adam
def create_resnet50_model(input_shape, n_out):
    base_model = ResNet50(weights=None, include_top=False, input_shape=input_shape)
    base_model.load_weights('../input/keras-pretrained-models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
    model = Sequential()
    model.add(base_model)
    model.add(GlobalAveragePooling2D())
    model.add(Dropout(0.5))
    model.add(Dense(2048, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(n_out, activation='sigmoid'))
    return model
def create_inception_v3_model(input_shape, n_out):
    base_model = InceptionV3(weights=None, include_top=False, input_shape=input_shape)
    base_model.load_weights('../input/keras-pretrained-models/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5')
    model = Sequential()
    model.add(base_model)
    model.add(GlobalAveragePooling2D())
    model.add(Dropout(0.5))
    model.add(Dense(2048, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(n_out, activation='sigmoid'))
    return model
def create_xception_model(input_shape, n_out):
    base_model = Xception(weights=None, include_top=False, input_shape=input_shape)
    base_model.load_weights('../input/keras-pretrained-models/xception_weights_tf_dim_ordering_tf_kernels_notop.h5')
    model = Sequential()
    model.add(base_model)
    model.add(GlobalAveragePooling2D())
    model.add(Dropout(0.5))
    model.add(Dense(2048, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(n_out, activation='sigmoid'))
    return model
def create_densenet121_model(input_shape, n_out):
    base_model = DenseNet121(weights=None, include_top=False, input_shape=input_shape)
    base_model.load_weights('../input/densenet-keras/DenseNet-BC-121-32-no-top.h5')
    model = Sequential()
    model.add(base_model)
    model.add(GlobalAveragePooling2D())
    model.add(Dropout(0.5))
    model.add(Dense(2048, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(n_out, activation='sigmoid'))
    return model
def create_effnetB5_model(input_shape, n_out):
    base_model = EfficientNetB5(weights=sys.path.append(os.path.abspath('../input/efficientnet-keras-weights-b0b5/efficientnet-b5_imagenet_1000_notop.h5')), include_top=False, input_shape=input_shape)
    model = Sequential()
    model.add(base_model)
    model.add(Dropout(0.25))
    model.add(Dense(1024))
    model.add(LeakyReLU())
    model.add(GlobalAveragePooling2D())
    model.add(Dropout(0.5))
    model.add(Dense(n_out, activation='sigmoid'))
    return model

IMAGE_HEIGHT = 340
IMAGE_WIDTH = 340
model = create_effnetB5_model(input_shape=(IMAGE_HEIGHT, IMAGE_WIDTH, 3), n_out=num_classes)
model.summary()

PRETRAINED_MODEL = '../input/efficientnetb5-blindness-detector/blindness_detector_bestqwk.h5'
if os.path.exists(PRETRAINED_MODEL):
    model.load_weights(PRETRAINED_MODEL)

from tqdm import tqdm_notebook as tqdm
submit = pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')
predicted = []
for i, name in tqdm(enumerate(submit['id_code'])):
    path = os.path.join('../input/aptos2019-blindness-detection/test_images/', name + '.png')
    image = cv2.imread(path)
    image = cv2.resize(image, (IMAGE_HEIGHT, IMAGE_WIDTH))
    X = np.array(image[np.newaxis] / 255)
    raw_prediction = model.predict(X) > 0.5
    prediction = raw_prediction.astype(int).sum(axis=1) - 1
    predicted.append(prediction[0])

submit['diagnosis'] = predicted
submit.to_csv('submission.csv', index=False)
submit.head(10)