Kaggle competition: TGS Salt Identification Challenge
for m_id, tmp_stgs in enumerate(stgs_list):
n_folds = settings["globals"]["n_folds_split"]
tmp_seed = tmp_stgs["globals"]["seed"]
train_val_indexs = [
drug_multilabel_stratified_kfold(
 fold_id, target_in_drug_trt, TARGET_COL, n_folds, tmp_seed)
 for fold_id in range(n_folds)]
train_all_dataset.reset_model_order()
train_all_dataset.shuffle_model_order(tmp_seed)
 print("[model {}'s order]: {}".format(m_id, train_all_dataset.model_order))
for fold_id in range(n_folds):
print("[fold: {} - model: {}]".format(fold_id, m_id))
set_random_seed(tmp_seed , True)
 model = eval(tmp_stgs["model"]["name"] )(**tmp_stgs["model"]["params"])
 run_train_one_fold(
 tmp_stgs, model, train_all_dataset, train_val_indexs[fold_id], device,
TRAIN_OUTPUT / "fold{}_model{}".format(fold_id, m_id))
del model
gc.collect()
best_eval_list = []
for fold_id in range(n_folds):
output_dir = TRAIN_OUTPUT / "fold{}_model{}".format(fold_id, m_id)
 log_df = pd.read_json(output_dir / "log")
 best_log = log_df.iloc[log_df["val/metric"].idxmin() ][["epoch", "train/loss", "val/loss", "val/metric"]]
best_eval_list.append(best_log)
 best_epoch = int(best_log["epoch"])
 model_path = output_dir / "snapshot_epoch_{}.pth".format(best_epoch)
 cp_path = WORK_DIR / "best_model_fold{}_model{}.pth".format(fold_id, m_id)
 shutil.copy(model_path, cp_path)
 for model_path in output_dir.glob("*.pth"):
model_path.unlink()
cv_score_df = pd.DataFrame(best_eval_list)
 cv_score_df.insert(0, "fold", range(settings["globals"]["n_folds_split"]))
cv_score_df.insert(0, "model", m_id)
 del best_eval_list
cv_score_df_list.append(cv_score_df )<sort_values>
def rle_encode(im):

 pixels = im.flatten()
pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs)
 def rle_decode(rle_mask):

 s = rle_mask.split()
starts, lengths = [np.asarray(x, dtype=int)for x in(s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(101*101, dtype=np.uint8)
 for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(101,101 )
def order_sub(sub):
return sub.sort_values('sig_id' ).reset_index(drop=True )<load_from_csv>
def merge_dataframes(dfs, merge_keys):
dfs_merged = reduce(lambda left,right: pd.merge(left, right, on=merge_keys), dfs)
 return dfs_merged
if DO_SUBMISSION or DO_VIRTUAL_SUBMISSION:
sub_list = [
pd.read_csv('./submission-sinchir0-nn.csv'),
pd.read_csv('./submission_sinchir0_tabnet.csv'),
pd.read_csv("./submission_takapy_tf-resnet.csv"),
pd.read_csv('./submission_tawara_thrnn_seed_cv.csv'),
pd.read_csv('./submission_tawara_thrnn_drug_seed_cv.csv'),
]
else:
sub_list = [
pd.read_csv('.. /input/nn-use-train-public/submission.csv'),
pd.read_csv('.. /input/tabnet-train-public-add-n-shared-1/submission.csv'),
pd.read_csv(".. /input/moa-takapy-tf-resnet-transfer/submission.csv"),
pd.read_csv('.. /input/moa-weight-thrnn-seed-cv/submission.csv'),
pd.read_csv('.. /input/moa-weight-thrnn-drug-seed-cv/submission.csv'),
]
for i, name in enumerate(["NN(drugCV)", "TabNet", "ResNet", "ThrNN", "ThrNN(drugCV)"]):
print(f"[{name}]:", sub_list[i].shape )<sort_values>
df1 = pd.read_csv('.. /input/submission/crf_c_1.csv' ).rename(columns={'rle_mask': 'rle_mask'})
 df2 = pd.read_csv('.. /input/submission/crf_c.csv' ).rename(columns={'rle_mask': 'rle_mask1'})
 df3 = pd.read_csv('.. /input/submission/crf_c_2.csv' ).rename(columns={'rle_mask': 'rle_mask2'})
 df4 = pd.read_csv('.. /input/submission/sub.csv' ).rename(columns={'rle_mask': 'rle_mask3'})
sub_list = [order_sub(sub_df)for sub_df in sub_list]<predict_on_test>
dfs = [df1,df2,df3,df4]
merge_keys=['id']
df = merge_dataframes(dfs, merge_keys=merge_keys)
 df.head()
def inference_function(settings, model, loader, device):
model.to(device)
 model.eval()
pred_list = []
with torch.no_grad() :
for x, t in loader:
y = model(x.to(device))
pred_list.append(y.sigmoid().detach().cpu().numpy())
 pred_arr = np.concatenate(pred_list)
 del pred_list
return pred_arr<define_variables>
res = df1.copy()
i = 0
while True:
if i == 18000:
break
if(str(df.loc[i,'rle_mask'])!=str(np.nan)) and(str(df.loc[i,'rle_mask1'])!=str(np.nan)) and(str(df.loc[i,'rle_mask2'])!=str(np.nan)) and(str(df.loc[i,'rle_mask3'])!=str(np.nan)) :
decoded_mask1 = rle_decode(df.loc[i,'rle_mask'])
 decoded_mask2 = rle_decode(df.loc[i,'rle_mask1'])
 decoded_mask3 = rle_decode(df.loc[i,'rle_mask2'])
 decoded_mask4 = rle_decode(df.loc[i,'rle_mask3'])
 decoded_mask_all1 = decoded_mask1 + decoded_mask2 + decoded_mask3 + decoded_mask4
decoded_mask_all1[decoded_mask_all1<=2] = 0
decoded_mask_all1[decoded_mask_all1 >2] = 1
mask = rle_encode(decoded_mask_all1)
 res.loc[i,'rle_mask'] = mask
else:
res.loc[i,'rle_mask'] = df1.loc[i,'rle_mask']
i = i + 1
<concatenate><EOS> 
res.to_csv('blend_ez.csv',index=False )