Kaggle competition: APTOS 2019 Blindness Detection
def model_lstm_atten(embedding_matrix):
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x = Bidirectional(CuDNNLSTM(128, return_sequences=True))(x)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(x)
 x = Attention(maxlen )(x)
 x = Dense(64, activation="relu" )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model<choose_model_class>
import numpy as np
import pandas as pd
import scipy as sp
from keras.layers import Input, Dense, Dropout, GlobalAveragePooling2D, GlobalMaxPooling2D, Concatenate, BatchNormalization,PReLU
from keras import backend as K
from keras.models import Model, load_model
from keras.utils import Sequence
from keras.regularizers import l2
from keras.optimizers import Adam
from keras.callbacks import ReduceLROnPlateau, EarlyStopping
from tqdm import tqdm
import glob
from functools import partial
from multiprocessing import Pool
from matplotlib import pyplot as plt
from efficientnet import EfficientNetB3 , preprocess_input
from sklearn.metrics import cohen_kappa_score, mean_squared_error
from albumentations import HorizontalFlip, Compose, RandomRotate90, RandomBrightness, Resize,OneOf, VerticalFlip,Rotate, RandomBrightnessContrast
from sklearn.model_selection import StratifiedKFold
import cv2
import os
def model_gru_srk_atten(embedding_matrix):
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
 x = Attention(maxlen )(x)
 x = Dense(16, activation="relu" )(x)
 x = Dropout(0.1 )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model 
<choose_model_class>
IMAGE_SIZE = 299
batch_size = 32
TEST_DIR = 'test'
def model_lstm_du(embedding_matrix):
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
 avg_pool = GlobalAveragePooling1D()(x)
 max_pool = GlobalMaxPooling1D()(x)
 conc = concatenate([avg_pool, max_pool])
 conc = Dense(64, activation="relu" )(conc)
 conc = Dropout(0.1 )(conc)
 outp = Dense(1, activation="sigmoid" )(conc)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model<choose_model_class>
data=pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 subm = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 data.head()
def model_gru_atten_3(embedding_matrix):
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x = Bidirectional(CuDNNGRU(128, return_sequences=True))(x)
 x = Bidirectional(CuDNNGRU(100, return_sequences=True))(x)
 x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
 x = Attention(maxlen )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model<find_best_params>
if len(data)% 32 :
split_count = len(data)//32 + 1
else :
split_count = len(data)// 32
split_batch = np.array_split(np.arange(split_count), 4)
 batch_end =[min(len(data),(split_batch[i].max() +1)*batch_size)for i in range(len(split_batch)) ]
batch_start = [0] + batch_end[:-1]
batch_start, batch_end
def train_pred(model, epochs=2):
for e in range(epochs):
model.fit(train_X, train_y, batch_size=512, epochs=1, validation_data=(val_X, val_y))
pred_val_y = model.predict([val_X], batch_size=1024, verbose=0)
 best_thresh = 0.5
best_score = 0.0
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 score = metrics.f1_score(val_y,(pred_val_y > thresh ).astype(int))
if score > best_score:
best_thresh = thresh
best_score = score
print("Val F1 Score: {:.4f}".format(best_score))
print("Val best_thresh: {:.2f}".format(best_thresh))
pred_test_y = model.predict([test_X], batch_size=1024, verbose=0)
 return pred_val_y, pred_test_y, best_score<load_pretrained>
def get_head(linear_size = [512,256,128] , probs_dropout = 0.5):
inp = Input(shape =(None,None,base_model.output_shape[-1]))
x_avg = GlobalAveragePooling2D()(inp)
 x_max = GlobalMaxPooling2D()(inp)
 x = Concatenate()([x_avg,x_max])
 x = BatchNormalization()(x)
 x = Dropout(rate = probs_dropout )(x)
 x = Dense(linear_size[0], activation = 'tanh', kernel_regularizer = l2(1e-5))(x)
 for n in linear_size[1:] :
x = BatchNormalization()(x)
 x = Dropout(rate = probs_dropout )(x)
 x = Dense(n, kernel_regularizer = l2(1e-4))(x)
 x = PReLU()(x)
 x = Dense(1 , kernel_regularizer = l2(1e-4))(x)
 return Model(inp,x )
train_X, val_X, test_X, train_y, val_y, word_index = load_and_prec()
vocab = []
for w,k in word_index.items() :
vocab.append(w)
 if k >= max_features:
break
embedding_matrix_1 = load_glove(word_index)
 embedding_matrix_3 = load_para(word_index )<compute_test_metric>
class ImageLoader(Sequence):
def __init__(self , image_list,
image_cat = None,
batch_size = 32,
shuffle = True,
include_last = True,
transforms = None,
use_ben = True,
sigma_ben = 15,
randomized_sigma_ben_range = None,
crop = True,
resized = True):
self.image_list = np.array(image_list)
 self.crop = crop
self.use_ben = use_ben
self.have_transform = False
if self.use_ben :
if not(randomized_sigma_ben_range is None):
self.sigma_ben_range = randomized_sigma_ben_range
self.random_ben = True
else :
self.random_ben = False
self.sigma_ben = sigma_ben
if transforms is None :
self.have_transform = False
if not(resized):
self.have_transform = True
self.transforms = Resize(IMAGE_SIZE,IMAGE_SIZE)
 else :
self.have_transform = True
self.transforms = transforms
if len(self.image_list)== 0 :
print('List is empty please recheck')
 else :
print('List contains {} images'.format(len(self.image_list)))
 if image_cat is None :
self.image_cat = None
else :
self.image_cat = np.array(image_cat)
 assert len(self.image_list)== len(self.image_cat), 'Image List and Cat Mismatch'
self.batch_size = batch_size
self.shuffle = shuffle
self.index = np.arange(len(self.image_list), dtype = int)
 if shuffle :
self.shuffle_index()
self.include_last = include_last
def __len__(self):
if self.include_last :
if len(self.image_list)% self.batch_size :
return len(self.image_list)// self.batch_size + 1
else :
return len(self.image_list)// self.batch_size
else :
return len(self.image_list)// self.batch_size
def __getitem__(self , index):
if self.include_last and(index ==(len(self.image_list)// self.batch_size)) :
batch_count = len(self.image_list)% self.batch_size
else :
batch_count = self.batch_size
X = np.empty(( batch_count,IMAGE_SIZE,IMAGE_SIZE,3))
idxs = np.arange(index*self.batch_size,(index*self.batch_size)+batch_count)
 if self.use_ben :
if self.random_ben :
sigma = np.random.randint(self.sigma_ben_range[0], self.sigma_ben_range[1] , size = batch_count)
 else :
sigma = [self.sigma_ben]*batch_count
for i , idx in enumerate(idxs):
img = cv2.imread(self.image_list[idx])
 img = cv2.cvtColor(img , cv2.COLOR_BGR2RGB)
 if self.crop :
img = crop_image_from_gray(img)
 if self.have_transform :
img = self.transforms(image=img)['image']
if self.use_ben :
img = cv2.addWeighted(img,4, cv2.GaussianBlur(img,(0,0), sigma[i]),-4 ,128)
 X[i] = img
X = preprocess_input(X)
 if not(self.image_cat is None):
return X, self.image_cat[idxs]
else :
return X
def shuffle_index(self):
np.random.shuffle(self.index)
 self.image_list = self.image_list[self.index]
if not(self.image_cat is None):
self.image_cat = self.image_cat[self.index]
def on_epoch_end(self):
if self.shuffle :
self.shuffle_index()
embedding_matrix = np.mean([embedding_matrix_1, embedding_matrix_3], axis = 0)
 np.shape(embedding_matrix )<define_variables>
models = []
K.clear_session()
for i in range(4):
base_model = EfficientNetB3(weights = None , include_top = False , input_shape =(IMAGE_SIZE,IMAGE_SIZE,3))
head_model = get_head()
model = Model(base_model.input, head_model(base_model.output))
model.load_weights('.. /input/finetune-effnet/model_{}.h5'.format(i))
models.append(model )
outputs = []<predict_on_test>
def get_path(path):
def corrector(x):
return os.path.join(path, x)+ '.png'
return corrector
def clean_images() :
if os.path.isdir('test'):
for f in glob.glob('test/*.png'):
os.remove(f)
 os.rmdir('test' )
pred_val_y, pred_test_y, best_score = train_pred(model_gru_atten_3(embedding_matrix), epochs = 3)
 outputs.append([pred_val_y, pred_test_y, best_score, '3 GRU w/ atten'] )<predict_on_test>
TTA_count = 8
preds = np.zeros([len(data),4])
 for i,(batch_s, batch_e)in enumerate(zip(batch_start,batch_end)) :
test_img_list = data.iloc[batch_s:batch_e,0].apply(get_path('.. /input/aptos2019-blindness-detection/test_images/')).tolist()
prepare_files(test_img_list, image_size =(IMAGE_SIZE,IMAGE_SIZE))
test_img_list = data.iloc[batch_s:batch_e,0].apply(get_path('test')).tolist()
loader = ImageLoader(test_img_list,
shuffle = False,
crop = False,
transforms=train_aug,
resized = True)
 for j in range(4):
for k in range(TTA_count):
preds[slice(batch_s,batch_e),j:j+1] += models[j].predict_generator(loader ,
use_multiprocessing = True,
workers = os.cpu_count())/TTA_count
clean_images()
try :
print(len(os.listdir('test')))
 except :
print('Batch {} Cleared'.format(i+1))
print()
pred_val_y, pred_test_y , best_score= train_pred(model_gru_srk_atten(embedding_matrix), epochs = 2)
 outputs.append([pred_val_y, pred_test_y, best_score, 'gru atten srk'])
<predict_on_test>
def get_labels(X , coef = [0.5,1.5,2.5,3.5]):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
coef = [0.5353928773986922, 1.5749586701298883, 2.448495965240568, 3.393477738062221]
pred_val_y, pred_test_y, best_score = train_pred(model_cnn(embedding_matrix_1), epochs = 2)
 outputs.append([pred_val_y, pred_test_y, best_score, '2d CNN GloVe'] )<predict_on_test>
subm['diagnosis'] = get_labels(preds.mean(axis = 1)+ 2, coef ).astype(int)
 subm.to_csv('submission.csv', index = False )
<predict_on_test><EOS> 
print('Kernel Runtime : {:.3f} minute'.format(( time.time() - s_time)/ 60))