Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<init_hyperparams>
from pathlib import Path
import numpy as np
import pandas as pd
import cv2
%%time
RUN_N_TRAIN_SAMPLES = 50
if os.path.exists(TRAIN_DATA):
names,preds = [],[]
with torch.no_grad() :
for i, samples in tqdm(enumerate(zip(*dls))):
meta_model_inputs = np.zeros(( bs,len(dls)))
 len_models = 0
for j,(x,y)in enumerate(samples):
x = x.cuda().float()
x = torch.stack([x,x.flip(-1),x.flip(-2),x.flip(-1,-2),
x.transpose(-1,-2),x.transpose(-1,-2 ).flip(-1),
x.transpose(-1,-2 ).flip(-2),x.transpose(-1,-2 ).flip(-1,-2)],1)
 x = x.view(-1,N[j],3,sz[j],sz[j])
 if is_ordinal[j]:
p = [tensor(ordinalRegs2cat(( torch.sigmoid(model(x)) > 0.5 ).cpu().numpy())).float().cuda() for model in models[j]]
elif regr[j]:
p = [regr2cat(model(x)) for model in models[j]]
else:
p = [model(x ).argmax(-1 ).float() for model in models[j]]
p = torch.stack(p,1 ).view(bs,len(models[j]), 8, -1)
 p = p.mean(axis=2)
 for idx,preds_item in enumerate(p):
softmax_sum = np.zeros(( 6), np.float)
 for preds_fold, cm in zip(preds_item, cms[j]):
if not regr and not is_ordinal:
pred_cl = int(preds_fold.argmax(-1))
else:
if preds_fold < coef[0]: pred_cl = 0
elif preds_fold < coef[1]: pred_cl = 1
elif preds_fold < coef[2]: pred_cl = 2
elif preds_fold < coef[3]: pred_cl = 3
elif preds_fold < coef[4]: pred_cl = 4
else: pred_cl = 5
cl_probs = cm[:,pred_cl]
softmax_probs = cl_probs / np.sum(cl_probs)
 softmax_sum += softmax_probs
meta_model_inputs[idx,j] = np.argmax(softmax_sum)
 del x
ps = np.round(np.average(meta_model_inputs.reshape(bs,-1), axis=1, weights=model_weights))
names.append(y)
 preds.append(torch.tensor(ps))
if i >=(RUN_N_TRAIN_SAMPLES//bs - 1):
print("Unit test succeeded.")
 break
gt_grades = np.array([train_df.loc[pred_ids]['isup_grade'].values for pred_ids in names] ).flatten()
pred_grades = regr2cat(np.array([pred.numpy() for pred in preds] ).flatten())
 qwk = cohen_kappa_score(gt_grades, pred_grades, weights="quadratic")
 print("QWK: {0:.4f}".format(qwk))
assert qwk>0.7, "QWK test gave low scores.Something is probably wrong."
names = np.concatenate(names)
 preds = regr2cat(np.array([pred.numpy() for pred in preds] ).flatten() ).astype(np.int32)
 sub_df = pd.DataFrame({'image_id': names, 'isup_grade': preds})
 print(sub_df.head() )<load_from_csv>
test_csv_path = ".. /input/aptos2019-blindness-detection/test.csv"
df = pd.read_csv(test_csv_path )
sub_df = pd.read_csv(SAMPLE)
 if os.path.exists(DATA):
dls = []
for i, _ in enumerate(models):
ds = PandaDataset(
 DATA,
TEST,
level=level[i],
tile_size=tile_size[i],
sz=sz[i],
mosaic_grid=mosaic_grid[i],
mean=mean[i],
std=std[i],
tissue_th=tissue_th,
sampling_method=sampling_method[i],
seed=seed 
)
 dl = DataLoader(ds, batch_size=bs, num_workers=nworkers, shuffle=False)
 dls.append(dl)
 names,preds = [],[]
with torch.no_grad() :
for i, samples in tqdm(enumerate(zip(*dls))):
meta_model_inputs = np.zeros(( bs,len(dls)))
 len_models = 0
for j,(x,y)in enumerate(samples):
x = x.cuda().float()
x = torch.stack([x,x.flip(-1),x.flip(-2),x.flip(-1,-2),
x.transpose(-1,-2),x.transpose(-1,-2 ).flip(-1),
x.transpose(-1,-2 ).flip(-2),x.transpose(-1,-2 ).flip(-1,-2)],1)
 x = x.view(-1,N[j],3,sz[j],sz[j])
 if is_ordinal[j]:
p = [tensor(ordinalRegs2cat(( torch.sigmoid(model(x)) > 0.5 ).cpu().numpy())).float().cuda() for model in models[j]]
elif regr[j]:
p = [regr2cat(model(x)) for model in models[j]]
else:
p = [model(x ).argmax(-1 ).float() for model in models[j]]
p = torch.stack(p,1 ).view(bs,len(models[j]), 8, -1)
 p = p.mean(axis=2)
 for idx,preds_item in enumerate(p):
softmax_sum = np.zeros(( 6), np.float)
 for preds_fold, cm in zip(preds_item, cms[j]):
if not regr and not is_ordinal:
pred_cl = int(preds_fold.argmax(-1))
else:
if preds_fold < coef[0]: pred_cl = 0
elif preds_fold < coef[1]: pred_cl = 1
elif preds_fold < coef[2]: pred_cl = 2
elif preds_fold < coef[3]: pred_cl = 3
elif preds_fold < coef[4]: pred_cl = 4
else: pred_cl = 5
cl_probs = cm[:,pred_cl]
softmax_probs = cl_probs / np.sum(cl_probs)
 softmax_sum += softmax_probs
meta_model_inputs[idx,j] = np.argmax(softmax_sum)
 del x
ps = np.round(np.average(meta_model_inputs.reshape(bs,-1), axis=1, weights=model_weights))
names.append(y)
 preds.append(torch.tensor(ps))
names = np.concatenate(names)
 preds = regr2cat(np.array([pred.numpy() for pred in preds] ).flatten() ).astype(np.int32)
 sub_df = pd.DataFrame({'image_id': names, 'isup_grade': preds})
 sub_df.to_csv('submission.csv', index=False)
 sub_df.head()<save_to_csv>
submissions = [
"submission_0.683.csv",
"submission_0.694.csv",
"submission_0.709.csv",
"submission_0.711.csv",
"submission_0.739.csv",
"submission_0.751.csv",
"submission_0.755.csv",
"submission_0.766.csv",
"submission_0.768.csv",
"submission_0.785.csv"
]
pub_test_csv_path = ".. /input/aptos10submissions/" + submissions[target_ratio_code]
pub_df = pd.read_csv(pub_test_csv_path)
 id_to_diagnosis = {id_code: diag for id_code, diag in zip(pub_df.id_code, pub_df.diagnosis)}
sub_df.to_csv("submission.csv", index=False)
 sub_df.head()<init_hyperparams>
all_diagnosis = []
for _, row in df.iterrows() :
id_code = row["id_code"]
if id_code in id_to_diagnosis:
all_diagnosis.append(id_to_diagnosis[id_code])
 else:
all_diagnosis.append(0 )
MODEL_DICT = {
'reg':{
'model1':{'ckpt':f'.. /input/pandachallengemodel/tiles_eb0_netvlad_reg_36_256_best_kappa_f0.pth',
'backbone':'efficientnet-b0','threshold':[0.5433,1.5323,2.4829,3.484,4.2806],'num_tiles':36},
},
'reg_with_attention':{
'model3':{'atten_ckpt':'.. /input/pandachallengemodel/newcv_tiles_eb0_attention_reg_64_256_f1.pth','atten_output':1,
'ckpt':'.. /input/pandachallengemodel/newcv_tiles_eb4_netvlad_attention_reg_16_256_f1.pth',
'backbone':'efficientnet-b4','threshold':[0.5445,1.4968,2.4163,3.4254,4.3853],'num_tiles':16}
},
'ordinal_reg':{
'model1':{'atten_ckpt':'.. /input/pandachallengemodel/newcv_tiles_eb0_attention_reg_64_256_f1.pth','atten_output':1,
'ckpt':'.. /input/pandachallengemodel/bag_eb0_attention_ordinal_reg_16_256_f1.pth',
'backbone':'efficientnet-b0','num_tiles':16},
},
'stitch_reg':{
'model1':{'atten_ckpt':'.. /input/pandachallengemodel/newcv_tiles_eb0_attention_reg_64_256_f1.pth','atten_output':1,
'ckpt':'.. /input/pandachallengemodel/stitched_regnety_800m_attention_reg_16_256_f1.pth',
'threshold':[0.5124,1.5636,2.4927,3.4076,4.4082],'num_tiles':16}}
}<define_variables>
id_codes = df.id_code.values
new_df = pd.DataFrame.from_dict(data={"id_code": df.id_code.values, "diagnosis": all_diagnosis})
 new_df.to_csv("submission.csv", index=False )