Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<train_model>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
package_dir = '.. /input/efficientnet/efficientnet-pytorch/EfficientNet-PyTorch/'
sys.path.insert(0, package_dir)
 start = time.time()
%%time
gc.collect()
evals_result = {}
model_lgb =lgb.train(lgb_params, lgb_train, num_boost_round=500,
early_stopping_rounds=200, categorical_feature=cat_features,
evals_result=evals_result,verbose_eval=50,valid_sets=[lgb_train, lgb_valid] )<predict_on_test>
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
SEED = 999
seed_everything(SEED )
pred_lgb = model_lgb.predict(X_test.values ).clip(*target_range)
 pickle.dump(model_lgb,open("firstlevel_lgb_model" , "wb"))<compute_test_metric>
deployment_dir = ".. /input/efficientnetstacking"
def qk(y_pred, y):
k = torch.tensor(cohen_kappa_score(torch.round(y_pred), y, weights='quadratic'), device='cuda:0')
 k[k != k] = 0
k[torch.isinf(k)] = 0
return k
test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv' )
pred_lgb_val = model_lgb.predict(X_val)
 print('Val RMSE for lgb is %f' % sqrt(mean_squared_error(y_val, pred_lgb_val.clip(*target_range))))
print('Val RMSE for lgb is %f' % sqrt(mean_squared_error(y_val, pred_lgb_val)) )<compute_test_metric>
inf1_t1 = time.time()
b3_models = ["efficientnet-b3_0901_16-45-51_stage2_f1", "efficientnet-b3_0901_16-45-51_stage2_f2",
"efficientnet-b3_0901_16-45-51_stage2_f3", "efficientnet-b3_0901_16-45-51_stage2_f4",
"efficientnet-b3_0901_16-45-51_stage2_f5"]
b3_test_logits_list = []
for m in b3_models:
learn = load_learner(deployment_dir, "{}.pkl".format(m))
learn.data.add_test(ImageList.from_df(test_df,
'.. /input',
folder='test_images_ben_preprocessing_sigmaX10',
suffix='.png'))
logits,_ = learn.get_preds(DatasetType.Test)
 logits = logits.numpy()
b3_test_logits_list.append(logits)
 print(logits.shape)
 b4_models = ["efficientnet-b4_0820_01-09-57_stage2_f1", "efficientnet-b4_0820_01-09-57_stage2_f2",
"efficientnet-b4_0820_01-09-57_stage2_f3", "efficientnet-b4_0820_01-09-57_stage2_f4",
"efficientnet-b4_0821_00-02-25_stage2_f5"]
b4_test_logits_list = []
for m in b4_models:
learn = load_learner(deployment_dir, "{}.pkl".format(m))
learn.data.add_test(ImageList.from_df(test_df,
'.. /input',
folder='test_images_ben_preprocessing_sigmaX10',
suffix='.png'))
logits,_ = learn.get_preds(DatasetType.Test)
 logits = logits.numpy()
b4_test_logits_list.append(logits)
 print(logits.shape)
 b5_models = ["efficientnet-b5_0820_01-32-30_stage2_f1", "efficientnet-b5_0903_01-03-41_stage2_f2",
"efficientnet-b5_0820_22-13-07_stage2_f3", "efficientnet-b5_0821_01-30-37_stage2_f4",
"efficientnet-b5_0821_00-26-51_stage2_f5"]
b5_test_logits_list = []
for m in b5_models:
learn = load_learner(deployment_dir, "{}.pkl".format(m))
learn.data.add_test(ImageList.from_df(test_df,
'.. /input',
folder='test_images_ben_preprocessing_sigmaX10',
suffix='.png'))
logits,_ = learn.get_preds(DatasetType.Test)
 logits = logits.numpy()
b5_test_logits_list.append(logits)
 print(logits.shape)
 with open(os.path.join(deployment_dir, "xgboost-0903_05-26-03.pkl"), "rb")as f:
xlf = pk.load(f)
 with open(os.path.join(deployment_dir, "svr-0903_05-26-03.pkl"), "rb")as f:
svr = pk.load(f)
 with open(os.path.join(deployment_dir, "cb-0906_06-09-42.pkl"), "rb")as f:
cb = pk.load(f)
 results_xlf = []
results_svr = []
results_cb = []
for b3, b4, b5 in zip(b3_test_logits_list, b4_test_logits_list, b5_test_logits_list):
X_test = np.concatenate([b3, b4, b5], axis=1)
 res_xlf = xlf.predict(X_test)
 results_xlf.append(res_xlf)
 res_svr = svr.predict(X_test)
 results_svr.append(res_svr)
 res_cb = cb.predict(X_test)
 results_cb.append(res_cb)
 avg_res_xlf = np.average(results_xlf, axis=0)
 avg_res_svr = np.average(results_svr, axis=0)
 avg_res_cb = np.average(results_cb, axis=0)
 y_pred_xlf = np.round(avg_res_xlf ).astype(int)
 y_pred_svr = np.round(avg_res_svr ).astype(int)
 y_pred_cb = np.round(avg_res_cb ).astype(int)
 test_df.diagnosis = y_pred_xlf
test_df.hist()
plt.show()
test_df.diagnosis = y_pred_svr
test_df.hist()
plt.show()
test_df.diagnosis = y_pred_cb
test_df.hist()
plt.show()
inf1_t2 = time.time()
inf1_dur = inf1_t2 - inf1_t1
print('Inference duration ',inf1_dur, inf1_dur/3600 )
X_test_level2 = np.c_[pred_lr, pred_lgb]<prepare_x_and_y>
np.corrcoef([avg_res_xlf, avg_res_svr, avg_res_cb] )
dates_train_level2 = dates_train[dates_train.isin([27,28, 29, 30, 31, 32])]
y_train_level2 = y_train[dates_train.isin(dates_train_level2)]<feature_engineering>
np.corrcoef([y_pred_xlf, y_pred_svr, y_pred_cb] )
print(X_train_level2.mean(axis=0))<compute_test_metric>
def voting(labels, Tweights = None):
if isinstance(labels, list):
X = np.array(labels, dtype=np.int64)
 maj = np.argmax(np.bincount(X, Tweights))
return maj
def average(logits, Tweights = None):
avg = np.average(logits, axis=0, weights = Tweights)
 return avg
alphas_to_try = np.linspace(0, 1, 1001)
 r2_scores = np.array([r2_score(y_train_level2, np.dot(X_train_level2, [alpha, 1 - alpha])) for alpha in alphas_to_try])
 best_alpha = alphas_to_try[r2_scores.argmax() ]
r2_train_simple_mix = r2_scores.max()
print('Best alpha: %f; Corresponding r2 score on train: %f' %(best_alpha, r2_train_simple_mix))<save_to_csv>
vote_list = [voting([int(avg_res_xlf[idx] + 0.5),
int(avg_res_svr[idx] + 0.5),
int(avg_res_cb[idx] + 0.5)])
 for idx in range(len(avg_res_xlf)) ]
test_preds = best_alpha * pred_lr +(1 - best_alpha)* pred_lgb
test['item_cnt_month'] = test_preds.clip(*target_range)
 test[['ID', 'item_cnt_month']].to_csv('submission_averaging.csv', index=False )<save_to_csv>
test_df.diagnosis = vote_list
test_df.hist()
plt.show()
test_df.to_csv('submission.csv',index=False )
test_preds_lr_scaled = best_alpha * pred_lr_scaled +(1 - best_alpha)* pred_lgb
test['item_cnt_month'] = test_preds_lr_scaled.clip(*target_range)
 test[['ID', 'item_cnt_month']].to_csv('submission_averaging_lr_scaled.csv', index=False )<train_model>
test_df['diagnosis'].value_counts()
<save_to_csv><EOS> 
dur = time.time() -start
print('Whole procedure takes {} seconds long...'.format(dur))
print(dur/3600)
 print('done' )