Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<drop_column>
sys.path.append(os.path.abspath('.. /input/efficientnet/efficientnet-master/efficientnet-master/'))
TRAIN_DATA_PATH = ".. /input/aptos2019-blindness-detection"
TEST_DATA_PATH = ".. /input/aptos2019-blindness-detection"
PREPROCESSED_IMAGE_PATH = "./preprocessed"
MODEL_PATH = "./models"
MODEL_FILE_NAME = "effnet_b3_single.h5"
TRAIN_CSV_FILE_PATH = os.path.join(TRAIN_DATA_PATH, "train.csv")
 TRAIN_IMAGE_FILE_PATH = ".. /input/aptos2019-blindness-detection/train_images"
TEST_CSV_FILE_PATH = ".. /input/aptos2019-blindness-detection/test.csv"
TEST_IMAGE_FILE_PATH = ".. /input/aptos2019-blindness-detection/test_images"

 CHECK_KERNEL_VALID = False
IMG_WIDTH = 456
IMG_HEIGHT = 456
IMG_CHANNELS = 3
BATCH_SIZE = 4
NUM_FOLDS = 6
EPOCHS = 30
if CHECK_KERNEL_VALID:
EPOCHS = 1
GENERATE_WEIGHTS = True
TRAIN_OVER_PRETRAINED = True
RESCALE_DN = 128.0
train_data.drop(['Id'], axis=1, inplace=True)
 test_data.drop('ForecastId', axis=1, inplace=True )<count_missing_values>
df_train = pd.read_csv(TRAIN_CSV_FILE_PATH)
 df_train['id_code'] = df_train['id_code'] + ".png"
df_test = pd.read_csv(TEST_CSV_FILE_PATH)
 df_train.head()
missing_val_count_by_column =(train_data.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column>0] )<import_modules>
def get_preds_and_labels(model, generator):

 preds = []
labels = []
for _ in range(int(np.ceil(generator.samples / BATCH_SIZE))):
x, y = next(generator)
 preds.append(model.predict(x))
labels.append(y)
 return np.concatenate(preds ).ravel() , np.concatenate(labels ).ravel()
from xgboost import XGBRegressor<prepare_x_and_y>
class Metrics(Callback):

 def __init__(self, model, val_generator, model_save_filepath):
self.model = model
self.val_generator = val_generator
self.model_save_filepath = model_save_filepath
def on_train_begin(self, logs={}):

 self.val_kappas = []
def on_epoch_end(self, epoch, logs={}):

 y_pred, labels = get_preds_and_labels(self.model, self.val_generator)
 y_pred = np.rint(y_pred ).astype(np.uint8 ).clip(0, 4)
 _val_kappa = cohen_kappa_score(labels, y_pred, weights='quadratic')
 self.val_kappas.append(_val_kappa)
 print(f"val_kappa: {round(_val_kappa, 4)}")
 if _val_kappa == max(self.val_kappas):
print("Validation Kappa has improved.Saving model.")
 self.model.save(self.model_save_filepath)
 return
X_train = train_data[['Country_Region','Date']]
y_train = train_data[['ConfirmedCases', 'Fatalities']]<split>
def get_callbacks(model, val_generator, model_save_filepath):
es = EarlyStopping(monitor='val_loss', mode='auto', verbose=1, patience=12)
 lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=4,
verbose=1, mode='auto', min_delta=0.0001)
 km = Metrics(model, val_generator, model_save_filepath)
 return [es, lr, km]
x_train = X_train.iloc[:,:].values
x_test = test_data.iloc[:,:].values<split>
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
<train_on_grid>
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
error_list = []
def return_error(estimator, x_train,x_test,y_train):
model = MultiOutputRegressor(XGBRegressor(n_estimators=estimator, random_state=42, max_depth=40))
model.fit(x_train, y_train)
 predict = MultiOutputRegressor(model.predict(x_test))
return predict<compute_test_metric>
def clear_memory() :
clear_session()
for i in range(20):
gc.collect()
predict = return_error(2000,x_train,x_test,y_train )<save_to_csv>
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()
df_sub = pd.DataFrame()
df_sub['ForecastId'] = Test_id
df_sub['ConfirmedCases'] = np.round(predict.estimator[:,0],0)
 df_sub['Fatalities'] = np.round(predict.estimator[:,1],0)
 df_sub.to_csv('submission.csv', index=False )<load_from_disk>
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']
<define_variables><EOS> 
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()