Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<choose_model_class>
import tensorflow
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import cv2
import os
def compile_new_model(cfg, model):
with strategy.scope() :
model = get_model(cfg, model)
 losses = tf.keras.losses.BinaryCrossentropy(label_smoothing = cfg['label_smooth_fac'])
 model.compile(
 optimizer = cfg['optimizer'],
loss = losses,
metrics = [tf.keras.metrics.AUC(name='auc')])
 return model<train_model>
train_df = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 train_df['id_code'] = train_df['id_code'].apply(lambda x:x+'.png')
 train_df['diagnosis'] = train_df['diagnosis'].astype(str)
 test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 test_df['id_code'] = test_df['id_code'].apply(lambda x:x+'.png' )
ds_train = get_dataset(files_train, CFG, augment=True, shuffle=True, repeat=True)
 ds_train = ds_train.map(lambda img, label:(img, tuple([label])))
 steps_train = count_data_items(files_train)/(CFG['batch_size'] * REPLICAS)
 model_B6 = compile_new_model(CFG, 'EfficientNetB6')
 model_B7 = compile_new_model(CFG, 'EfficientNetB7')
 print("
Begin Training Models")
 Done Training model_B5
")
 history_B6 = model_B6.fit(ds_train,
verbose = 1,
steps_per_epoch = steps_train,
epochs = CFG['epochs'],
callbacks = [get_lr_callback(CFG)])
 print("
Done Training model_B6
")
 history_B7 = model_B7.fit(ds_train,
verbose = 1,
steps_per_epoch = steps_train,
epochs = CFG['epochs'],
callbacks = [get_lr_callback(CFG)])
 print("
Done Training model_B7
" )<predict_on_test>
num_classes = train_df['diagnosis'].nunique()
TRAIN_DATA_ROOT = './train_images_preprocessed/'
TEST_DATA_ROOT = './test_images_preprocessed/'
BATCH_SIZE = 16
train_datagen = ImageDataGenerator(
 rescale = 1/255,
rotation_range = 360,
horizontal_flip = True,
vertical_flip = True,
zoom_range = [0.98, 1.02],
width_shift_range = 0.01,
height_shift_range = 0.01,
validation_split = 0.20)
 validation_datagen = ImageDataGenerator(
 rescale = 1/255,
validation_split = 0.20)
 train_generator = train_datagen.flow_from_dataframe(
 dataframe = train_df,
directory = TRAIN_DATA_ROOT,
x_col = 'id_code',
y_col = 'diagnosis',
batch_size = BATCH_SIZE,
shuffle = True,
class_mode = 'categorical',
target_size =(IMAGE_HEIGHT, IMAGE_WIDTH),
subset = 'training')
 validation_generator = validation_datagen.flow_from_dataframe(
 dataframe = train_df,
directory = TRAIN_DATA_ROOT,
x_col = 'id_code',
y_col = 'diagnosis',
batch_size = BATCH_SIZE,
shuffle = True,
class_mode = 'categorical',
target_size =(IMAGE_HEIGHT, IMAGE_WIDTH),
subset = 'validation' )
CFG['batch_size'] = 256
cnt_test = count_data_items(files_test)
 steps = cnt_test /(CFG['batch_size'] * REPLICAS)* CFG['tta_steps']
ds_testAug = get_dataset(files_test, CFG, augment=True, repeat=True,
labeled=False, return_image_names=False)
 preds_B6 = model_B6.predict(ds_testAug, verbose=1, steps=steps)
 preds_B7 = model_B7.predict(ds_testAug, verbose=1, steps=steps )<split>
sklearn_class_weights = class_weight.compute_class_weight(
 'balanced',
np.unique(train_generator.classes),
train_generator.classes)
 print(sklearn_class_weights )
preds = np.stack(preds_B6)
 preds = preds[:,:cnt_test* CFG['tta_steps']]
preds = preds[:df_test.shape[0]*CFG['tta_steps']]
preds = np.stack(np.split(preds, CFG['tta_steps']),axis=1)
 preds = np.mean(preds, axis=1)
 preds_B6 = preds.reshape(-1 )<split>
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/resnet50/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(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(n_out, activation = 'sigmoid'))
return model
preds = np.stack(preds_B7)
 preds = preds[:,:cnt_test* CFG['tta_steps']]
preds = preds[:df_test.shape[0]*CFG['tta_steps']]
preds = np.stack(np.split(preds, CFG['tta_steps']),axis=1)
 preds = np.mean(preds, axis=1)
 preds_B7 = preds.reshape(-1 )<prepare_output>
PRETRAINED_MODEL = '.. /input/pretrained_blindness_detector/blindness_detector.h5'
if(os.path.exists(PRETRAINED_MODEL)) :
print('Restoring model from ' + PRETRAINED_MODEL)
 model.load_weights(PRETRAINED_MODEL)
 else:
print('No pretrained model found.Using fresh model.')
 current_epoch = 0
preds = preds_B6*0.6 + preds_B7*0.4<create_dataframe>
WARMUP_EPOCHS = 2
WARMUP_LEARNING_RATE = 1e-3
for layer in model.layers:
layer.trainable = False
for i in range(-3, 0):
model.layers[i].trainable = True
model.compile(optimizer = Adam(lr = WARMUP_LEARNING_RATE),
loss = 'binary_crossentropy',
metrics = ['accuracy'])
 warmup_history = model.fit_generator(generator = train_generator,
class_weight = sklearn_class_weights,
steps_per_epoch = train_generator.n // train_generator.batch_size,
validation_data = validation_generator,
validation_steps = validation_generator.n // validation_generator.batch_size,
epochs = WARMUP_EPOCHS,
use_multiprocessing = True,
workers = 4,
verbose = 1 ).history
ds = get_dataset(files_test, CFG, augment=False, repeat=False,
labeled=False, return_image_names=True)
 image_names = np.array([img_name.numpy().decode("utf-8")
 for img, img_name in iter(ds.unbatch())] )<save_to_csv>
FINETUNING_EPOCHS = 20
FINETUNING_LEARNING_RATE = 1e-4
for layer in model.layers:
layer.trainable = True
model.compile(optimizer = Adam(lr = FINETUNING_LEARNING_RATE),
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
 stopping = EarlyStopping(
 monitor = 'val_loss',
mode = 'min',
patience = 5,
restore_best_weights = True,
verbose = 1)
 rlrop = ReduceLROnPlateau(
 monitor = 'val_loss',
mode = 'min',
patience = 8,
factor = 0.5,
min_lr = 1e-6,
verbose = 1)
 checkpoint = ModelCheckpoint(
 'blindness_detector_best.h5',
monitor = 'val_acc',
save_best_only = True,
save_weights_only = False,
mode = 'max',
verbose = 1)
 finetune_history = model.fit_generator(generator = train_generator,
class_weight = sklearn_class_weights,
steps_per_epoch = train_generator.n // train_generator.batch_size,
validation_data = validation_generator,
validation_steps = validation_generator.n // validation_generator.batch_size,
epochs = FINETUNING_EPOCHS,
callbacks = [stopping, rlrop, checkpoint],
use_multiprocessing = True,
workers = 4,
verbose = 1 ).history
<install_modules><EOS> 
!rm -rf /kaggle/working/train_images_preprocessed/
print("Preprocessing test images...")
 !mkdir -p 'test_images_preprocessed'
for i, image_id in enumerate(tqdm(test_df['id_code'])) :
image = preprocess_image(f'.. /input/aptos2019-blindness-detection/test_images/{image_id}')
 cv2.imwrite(f'./test_images_preprocessed/{image_id}', image)
 test_datagen = ImageDataGenerator(rescale = 1./255)
 test_generator = test_datagen.flow_from_dataframe(
 dataframe = test_df,
directory = TEST_DATA_ROOT,
x_col = 'id_code',
target_size =(IMAGE_HEIGHT, IMAGE_WIDTH),
batch_size = 1,
shuffle = False,
class_mode = None)
 predict = model.predict_generator(test_generator, steps = len(test_generator.filenames))
results=pd.DataFrame({'id_code':test_generator.filenames,
'diagnosis':np.argmax(predict, axis = 1)})
 results['id_code'] = results['id_code'].map(lambda x: str(x)[:-4])
 results.to_csv('submission.csv', index = False )