Kaggle competition: APTOS 2019 Blindness Detection
def get_total_batch(num_samples, batch_size):
if(num_samples % batch_size)> 0 :
return(num_samples // batch_size)+ 1
else :
return num_samples // batch_size<choose_model_class>
!pip install -U '.. /input/install/efficientnet-0.0.3-py2.py3-none-any.whl'
def build_model_effnet_b3(load_weights = True):

 effnet_b3 = EfficientNetB3(weights=None,
include_top=False,
input_shape=(IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS))
if load_weights == True:
effnet_b3.load_weights('.. /input/efficientnet-keras-weights-b0b5/efficientnet-b3_imagenet_1000_notop.h5')
 model = Sequential()
model.add(effnet_b3)
 model.add(GlobalAveragePooling2D())
 model.add(Dropout(0.5))
model.add(Dense(5, activation=elu))
model.add(Dense(1, activation="linear"))
model.compile(loss='mse',
optimizer=Adam(lr=0.00005),
metrics=['mse', 'acc'])
 print(model.summary())
 return model
def get_model(load_weight):
return build_model_effnet_b3(load_weight)
 def get_pretrained_model(model_filepath):
model = get_model(load_weight = False)
 model.load_weights(model_filepath)
 return model 
<set_options>
HEIGHT = 256
WIDTH = 256
COEFF = [0.52022015, 1.46022145, 2.49058373, 3.30146459]
def clear_memory() :
clear_session()
for i in range(20):
gc.collect()<load_pretrained>
def build_model() :
efficientnetb3 = EfficientNetB4(
 weights=None,
input_shape=(HEIGHT,WIDTH,3),
include_top=False 
)
 model = Sequential()
model.add(efficientnetb3)
 model.add(layers.GlobalAveragePooling2D())
 model.add(layers.Dropout(0.5))
model.add(layers.BatchNormalization())
 model.add(layers.Dense(5, activation=elu))
model.add(layers.Dense(1, activation="linear"))
return model
def train_single() :
model_save_filepath = os.path.join(MODEL_PATH, MODEL_FILE_NAME)
 model = None
if TRAIN_OVER_PRETRAINED:
print("train with existing weight :", pre_model_filepath)
 model = get_pretrained_model(pre_model_filepath)
 else:
model = get_model()
train_datagen = ImageDataGenerator(rescale = 1./RESCALE_DN,
preprocessing_function=preprocess_image,
rotation_range=360,
horizontal_flip=True,
validation_split=0.15,
vertical_flip=True)
 train_generator = train_datagen.flow_from_dataframe(df_train,
x_col='id_code',
y_col='diagnosis',
directory = TRAIN_IMAGE_FILE_PATH,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other',
subset='training')
 val_generator = train_datagen.flow_from_dataframe(df_train,
x_col='id_code',
y_col='diagnosis',
directory = TRAIN_IMAGE_FILE_PATH,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other',
subset='validation')
 if TRAIN_OVER_PRETRAINED == False:
if GENERATE_WEIGHTS == True:
if os.path.exists(model_save_filepath)== True:
os.remove(model_save_filepath)
 if os.path.exists(model_save_filepath)== True:
print(">>>>>>>>>>", model_save_filepath, " already trained...skip!")
 return
train_steps = get_total_batch(train_generator.samples, BATCH_SIZE)
 val_steps = get_total_batch(val_generator.samples, BATCH_SIZE)
 print("Steps : train=", train_steps, " validation=", val_steps)
 callbacks = get_callbacks(model=model, val_generator=val_generator, model_save_filepath=model_save_filepath)
 model.fit_generator(train_generator,
steps_per_epoch = train_steps,
epochs = EPOCHS,
validation_data = val_generator,
validation_steps = val_steps,
callbacks = callbacks)
 def train_models() :
clear_memory()
train_single()
clear_memory()
train_models()<compute_train_metric>
model1 = build_model()
model1.load_weights('.. /input/fork-of-efficientnetb3-regression-keras-2/val_model.h5')
 model1.summary()
class OptimizedRounder(object):

 def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):

 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
ll = cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):

 loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):

 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
def coefficients(self):
return self.coef_['x']<load_from_csv>
TEST_IMG_PATH = '.. /input/aptos2019-blindness-detection/test_images/'
test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 print(test_df.shape)
 original_names = test_df['id_code'].values
test_df['id_code'] = test_df['id_code'] + ".png"
test_df['diagnosis'] = np.zeros(test_df.shape[0])
 display(test_df.head() )
clear_memory()

 df_train = pd.read_csv(TRAIN_CSV_FILE_PATH)
 df_train['id_code'] = df_train['id_code'] + ".png"
val_datagen = ImageDataGenerator(rescale = 1./RESCALE_DN,
preprocessing_function=preprocess_image)
 val_generator = val_datagen.flow_from_dataframe(df_train,
x_col='id_code',
y_col='diagnosis',
directory = TRAIN_IMAGE_FILE_PATH,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other')
 model_save_filepath = os.path.join(MODEL_PATH, MODEL_FILE_NAME)
 model = get_pretrained_model(model_save_filepath)
 y_val_preds, val_labels = get_preds_and_labels(model, val_generator)
 optR = OptimizedRounder()
optR.fit(y_val_preds, val_labels)
 coefficients = optR.coefficients()
clear_memory()
def make_submission() :
test_df = pd.read_csv(TEST_CSV_FILE_PATH)
 test_df['id_code'] = test_df['id_code'] + ".png"
test_df['diagnosis'] = np.zeros(test_df.shape[0])
 datagen = ImageDataGenerator(rescale = 1./RESCALE_DN,
preprocessing_function=preprocess_image)
 test_generator = datagen.flow_from_dataframe(
 test_df,
x_col='id_code',
y_col='diagnosis',
directory=TEST_IMAGE_FILE_PATH,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other',
shuffle=False)
 model = get_pretrained_model(model_save_filepath)
 y_test, _ = get_preds_and_labels(model, test_generator)
 steps = get_total_batch(test_df.shape[0], BATCH_SIZE)
 y_test = model.predict_generator(generator = test_generator,
steps = steps,
verbose = 0)
 y_test = optR.predict(y_test, coefficients ).astype(np.uint8)
 test_df['diagnosis'] = y_test
test_df['id_code'] = test_df['id_code'].str.replace(r'.png$', '')
 test_df.to_csv('submission.csv', index=False)
 make_submission()
clear_memory()<install_modules>
tta_steps = 10
predictions1 = []
for i in tqdm(range(tta_steps)) :
test_generator = ImageDataGenerator(rescale=1./255,
horizontal_flip=True,
rotation_range= 90,
vertical_flip=True,
brightness_range=(0.5,1.5),
zoom_range= 0.2,
fill_mode='constant',
preprocessing_function=preprocess_image,
cval = 0 ).flow_from_dataframe(test_df,
x_col='id_code',
y_col = 'diagnosis',
directory = TEST_IMG_PATH,
target_size=(WIDTH, HEIGHT),
batch_size=1,
class_mode='other',
shuffle = False)
 preds1 = model1.predict_generator(test_generator, steps = test_df.shape[0])
 predictions1.append(preds1)
 del test_generator
gc.collect()
pred_test1 = np.mean(predictions1, axis=0)
!pip install.. /input/efficientnet/efficientnet-master/efficientnet-master<set_options>
y_test = pred_test1
set_random_seed(2)
 np.random.seed(0)
<import_modules>
for i, pred in enumerate(y_test):
if pred < COEFF[0]:
y_test[i] = 0
elif pred >= COEFF[0] and pred < COEFF[1]:
y_test[i] = 1
elif pred >= COEFF[1] and pred < COEFF[2]:
y_test[i] = 2
elif pred >= COEFF[2] and pred < COEFF[3]:
y_test[i] = 3
else:
y_test[i] = 4
from efficientnet.keras import *<choose_model_class>
test_df['diagnosis'] = y_test.astype(int)
 test_df['id_code'] = test_df['id_code'].str.replace(r'.png$', '')
 test_df.head()
def output_relu(x):
return K.relu(x, max_value=4)
 def get_model(version, IMG_SIZE):
base_model = 0
if version == 0:
base_model = EfficientNetB0(weights=None, include_top=False, input_shape=(IMG_SIZE,IMG_SIZE,3))
elif version == 1:
base_model = EfficientNetB1(weights=None, include_top=False, input_shape=(IMG_SIZE,IMG_SIZE,3))
elif version == 2:
base_model = EfficientNetB2(weights=None, include_top=False, input_shape=(IMG_SIZE,IMG_SIZE,3))
elif version == 3:
base_model = EfficientNetB3(weights=None, include_top=False, input_shape=(IMG_SIZE,IMG_SIZE,3))
elif version == 4:
base_model = EfficientNetB4(weights=None, include_top=False, input_shape=(IMG_SIZE,IMG_SIZE,3))
else:
return None
x = base_model.output
x = GlobalAveragePooling2D()(x)
 x = Dense(1, activation=output_relu, kernel_initializer='he_normal' )(x)
 model = Model(inputs=base_model.input, outputs=x)
 return model<load_pretrained>
leaks = pd.read_csv('.. /input/aptos-trained-weights/leaks.csv', dtype= {'diagnosis': np.int32})
 codes = leaks['id_code'].values
diagnosis = leaks['diagnosis'].values
for i in range(codes.shape[0]):
test_df.loc[test_df['id_code'] == codes[i], 'diagnosis'] = int(diagnosis[i] )
model_b0_256 = get_model(0, 300)
 model_b0_256.load_weights('.. /input/pretrained-weights/model_b0_finetuned_20_epochs.h5')
 model_b1_240 = get_model(1, 300)
 model_b1_240.load_weights('.. /input/pretrained-weights/model_b1.h5')
 model_b3_300 = get_model(3, 300)
 model_b3_300.load_weights('.. /input/pretrained-weights/model_b3_epochs_30_300.h5' )<load_from_csv>
test_df.to_csv('submission.csv',index=False)
 print(round(test_df.diagnosis.value_counts() /len(test_df)*100,4))