Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<train_model>
IMG_DIM = 256
BATCH_SIZE = 32
CHANNELS = 3
NUM_CLASSES = 5
print(os.listdir("."))
print(os.listdir(".. /"))
print(os.listdir(".. /input/"))
print(os.listdir(".. /input/aptos2019-blindness-detection"))
print(os.listdir(".. /input/densenetmulti"))
INPUT_FOLDER = '.. /input/aptos2019-blindness-detection/'
xgb_model = XGBRegressor(
 max_depth=8,
n_estimators=1000,
min_child_weight=400,
colsample_bytree=0.6,
subsample=0.6,
eta=0.2,
seed=0,
learning_rate = 0.1,
n_jobs=-1)
 xgb_model.fit(
 X_train_xgb,
y_train,
eval_metric="rmse",
eval_set=[(X_train_xgb, y_train),(X_val_xgb, y_val)],
verbose=10,
early_stopping_rounds = 10 )<predict_on_test>
def dataGenerator(jitter=0.1):
datagen = image.ImageDataGenerator(rescale=1./255,
horizontal_flip = True and(jitter > 0.01),
vertical_flip = True and(jitter > 0.01),
zoom_range = [max(0.8, 1-5*jitter), 1],
rotation_range = int(600*jitter),
brightness_range = [1-jitter/3, 1+jitter/3],
fill_mode = "mirror",
channel_shift_range=int(30*jitter), 
)
 return datagen
xgb_train_pred = xgb_model.predict(X_train_xgb)
 xgb_val_pred = xgb_model.predict(X_val_xgb)
 xgb_test_pred = xgb_model.predict(X_test_xgb )<define_variables>
def load_network(network_name):
weights = f".. /input/densenetmulti/{network_name}.h5"
if network_name == "normal":
weights = f".. /input/densenetmulti/dense-0.800.h5"
model = Sequential()
model.add(DenseNet121(weights=None, include_top=False, input_shape=(IMG_DIM, IMG_DIM, CHANNELS)))
 model.add(GlobalAveragePooling2D())
 model.add(Dropout(0.5))
model.add(Dense(NUM_CLASSES, activation='sigmoid'))
model.load_weights(weights)
 model.compile(optimizer=Adam(lr=0.00005), loss='binary_crossentropy', metrics=['accuracy'])
 return model
rf_features = ['date_block_num',
'shop_id',
'item_id',
'item_category_id',
'big_category_id',
'sub_category_id',
'city_id',
'shop_category_id',
'item_cnt_month_lag_1',
'item_cnt_month_lag_2',
'item_cnt_month_lag_3',
'date_block_item_cnt_mean_lag_1',
'shop_item_cnt_mean_lag_1',
'shop_item_cnt_mean_lag_2',
'item_item_cnt_mean_lag_1',
'item_category_item_cnt_mean_lag_1',
'big_category_item_cnt_mean_lag_1',
'sub_category_item_cnt_mean_lag_1',
'shop_category_item_cnt_mean_lag_1',
'item_category_item_item_cnt_mean_lag_1',
'shop_item_category_item_cnt_mean_lag_1',
'shop_item_category_item_cnt_mean_lag_2',
'shop_big_category_item_cnt_mean_lag_1',
'shop_sub_category_item_cnt_mean_lag_1',
'month',
'holidays_cnt',
'dollar_ruble_rate',
'days_cnt']<split>
def prediction_convert_highest(predictions, thresholds):
thresholded = np.zeros(predictions.shape)
 for i in range(NUM_CLASSES):
thresholded[:,i] = predictions[:,i] > thresholds[i]
y_val = np.zeros(( predictions.shape[0]), dtype=np.int)
 for i in range(predictions.shape[0]):
for j in range(4, -1, -1):
if thresholded[i][j]:
y_val[i] = j
break
return y_val
def make_predictions(d_set, models):
images_dir = f"{INPUT_FOLDER}{d_set}_images/"
df = pd.read_csv(f"{INPUT_FOLDER}{d_set}.csv")
 df.id_code = df.id_code.apply(lambda x: x + ".png")
 block_size = 512
total = df.index.size
jitter_amounts = [0, 0.02, 0.02, 0.02, 0.03, 0.03, 0.03, 0.1]
ensemble_predictions = np.zeros(( df.index.size, len(jitter_amounts)*len(models), NUM_CLASSES))
for m, model in enumerate(models):
print(f"Making predictions with the {model} model on the {d_set} dataset.")
 neural_net = load_network(model)
 for start in range(0, total, block_size):
end = start + block_size
if end > total:
end = total
img_block = np.empty(( end-start, IMG_DIM, IMG_DIM, CHANNELS))
for i, filename in enumerate(df[start:end].id_code):
try:
bgr = cv2.imread(images_dir + filename)
 img_block[i,:,:,:] = process(bgr, model)
 except:
print("Error opening or manipulating image")
 img_block[i,:,:,:] = 128.
 for i, jit in enumerate(jitter_amounts):
datagen = dataGenerator(jit ).flow(img_block, shuffle=False)
 ensemble_predictions[start:end, i + len(models)*m] = neural_net.predict_generator(generator=datagen,
steps=len(datagen), workers=4, verbose=1)
 print(f"{start} - {end} finished")
 gc.collect()
return np.median(ensemble_predictions, axis=1 )
X_train_rf = X_train.loc[:, rf_features]
X_val_rf = X_val.loc[:, rf_features]
X_test_rf = X_test.loc[:, rf_features]<train_model>
X_train_rf.fillna(0, inplace=True)
 X_val_rf.fillna(0, inplace=True)
 X_test_rf.fillna(0, inplace=True )<train_model>
<predict_on_test><EOS> 
thresholds = [0.5, 0.5, 0.4, 0.4, 0.3]
predictions = make_predictions("test", ["normal", "weird"])
 as_classes = prediction_convert_highest(predictions, thresholds)
 print(as_classes[:10])
 test_df = pd.read_csv(INPUT_FOLDER + 'test.csv')
 test_df['diagnosis'] = as_classes
test_df.to_csv('submission.csv', index=False )