Kaggle competition: HuBMAP - Hacking the Kidney
<SOS> metric: Dice Kaggle data source: hubmap-hacking-the-kidney<set_options>
import numpy as np
import pandas as pd
torch.cuda.empty_cache()<choose_model_class>
def rle2mask(mask_rle, shape=(1600,256)) :

 s = mask_rle.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(shape[0]*shape[1], dtype=np.uint8)
 for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape ).T
def rle_encode_less_memory(img):

 pixels = img.T.flatten()
pixels[0] = 0
pixels[-1] = 0
runs = np.where(pixels[1:] != pixels[:-1])[0] + 2
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs)
 def image_size_dict(img_id, x, y):
image_id = [thing[:-5] for thing in img_id]
x_y = [(x[i], y[i])for i in range(0, len(x)) ]
return dict(zip(image_id, x_y))
def global_shift_mask(maskpred1, y_shift, x_shift):

 if y_shift <0 and x_shift >=0:
maskpred2 = np.pad(maskpred1, [(0,abs(y_shift)) ,(abs(x_shift), 0)], mode='constant', constant_values=0)
 maskpred3 = maskpred2[abs(y_shift):, :maskpred1.shape[1]]
elif y_shift >=0 and x_shift <0:
maskpred2 = np.pad(maskpred1, [(abs(y_shift),0),(0, abs(x_shift)) ], mode='constant', constant_values=0)
 maskpred3 = maskpred2[:maskpred1.shape[0], abs(x_shift):]
elif y_shift >=0 and x_shift >=0:
maskpred2 = np.pad(maskpred1, [(abs(y_shift),0),(abs(x_shift), 0)], mode='constant', constant_values=0)
 maskpred3 = maskpred2[:maskpred1.shape[0], :maskpred1.shape[1]]
elif y_shift < 0 and x_shift < 0:
maskpred2 = np.pad(maskpred1, [(0, abs(y_shift)) ,(0, abs(x_shift)) ], mode='constant', constant_values=0)
 maskpred3 = maskpred2[abs(y_shift):, abs(x_shift):]
return maskpred3
net = get_net(WEIGHT_FILE1 )<train_model>
dfpred = pd.read_csv('.. /input/best-public-hubmap/submission_public_TPU.csv')
 TARGET_ID = 'afa5e8098'
y_shift = -40
x_shift = -24
run_training()<set_options>
dfinfo = pd.read_csv('.. /input/hubmap-kidney-segmentation/HuBMAP-20-dataset_information.csv')
 size_dict = image_size_dict(dfinfo.image_file, dfinfo.width_pixels, dfinfo.height_pixels)
 mask_shape = size_dict.get(TARGET_ID)
 taridx = dfpred[dfpred['id']==TARGET_ID].index.values[0]
del net
gc.collect()
torch.cuda.empty_cache()<load_from_csv>
maskpred = rle2mask(dfpred.iloc[taridx]['predicted'], mask_shape)
 maskpred1 = maskpred.copy()
maskpred1[maskpred1>0]=1
test_df = pd.read_csv('pseudo2.csv')
 bad_boxes = [3687, 117344,173,113947,52868,2159,2169,121633,121634,147504,118211,52727,147552]
marking = pd.concat([pd.read_csv('.. /input/global-wheat-detection/train.csv'),test_df])if len(glob(f'.. /input/global-wheat-detection/test/*.jpg')) != 10 else pd.concat([test_df, test_df])
 if len(glob(f'.. /input/global-wheat-detection/test/*.jpg')) != 10:
marking.drop(bad_boxes)
 bboxs = np.stack(marking['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
marking[column] = bboxs[:,i]
marking.drop(columns=['bbox'], inplace=True)
 targets = set(marking.source.values)
 if len(glob(f'.. /input/global-wheat-detection/test/*.jpg')) != 10:
targets.remove('arvalis_3')
 targets.remove('usask_1')
 skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 df_folds = marking[['image_id']].copy()
df_folds.loc[:, 'bbox_count'] = 1
df_folds = df_folds.groupby('image_id' ).count()
df_folds.loc[:, 'source'] = marking[['image_id', 'source']].groupby('image_id' ).min() ['source']
df_folds.loc[:, 'stratify_group'] = np.char.add(
 df_folds['source'].values.astype(str),
df_folds['bbox_count'].apply(lambda x: f'_{x // 15}' ).values.astype(str)
)
 df_folds.loc[:, 'fold'] = 0
print(set(df_folds.source.values))
for fold_number,(train_index, val_index)in enumerate(skf.split(X=df_folds.index, y=df_folds['stratify_group'])) :
df_folds.loc[df_folds.iloc[val_index].index, 'fold'] = fold_number
df_folds = pd.concat([ df_folds.loc[df_folds['source'] == i, :] for i in targets])
 df_folds.loc[df_folds['source']=='pseudo','fold'] = 2
df_folds.loc[df_folds['fold']==3,'fold'] = 2<create_dataframe>
mask_shifted = global_shift_mask(maskpred1, y_shift, x_shift )
<train_model><EOS> 
newrle = rle_encode_less_memory(mask_shifted)
 dfpred.at[taridx, 'predicted'] = newrle
dfsample = pd.read_csv('.. /input/hubmap-kidney-segmentation/sample_submission.csv')
 mydict = dict(zip(dfpred['id'], dfpred['predicted']))
dfsample['predicted'] = dfsample['id'].map(mydict ).fillna(dfsample['predicted'])
 dfsample = dfsample.replace(np.nan, '', regex=True)
 dfsample.to_csv('submission.csv',index=False)