Kaggle competition: IEEE-CIS Fraud Detection
f = resnet34
cut,lr_cut = model_meta[f]
def get_learner(md):
m_base = load_pretrained(get_base(f, cut),PRETRAINED)
 m = to_gpu(Unet34(m_base))
models = UnetModel(m)
 learn = ConvLearner(md, models, tmp_name=TMP, models_name=MODEL)
 learn.opt_fn=optim.Adam
learn.crit = nn.BCEWithLogitsLoss()
learn.metrics=[accuracy_thresh(0.5), IoU]
learn.clip = 0.25
return learn<define_search_space>
%%time
train_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_transaction.csv', index_col='TransactionID')
 test_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_transaction.csv', index_col='TransactionID')
 train_identity = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_identity.csv', index_col='TransactionID')
 test_identity = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_identity.csv', index_col='TransactionID')
n_folds = 5
val_size = 4000//n_folds
actual_its = []
lr=3e-3
wd=1e-7
lrs = np.array([lr/100,lr/10,lr/3])
 test_x = np.array(test_names_png)
 out=np.zeros(( 18000,sz,sz))
alpha = 0<train_model>
train = train_transaction.merge(train_identity, how='left', left_index=True, right_index=True)
 test = test_transaction.merge(test_identity, how='left', left_index=True, right_index=True )
for i in [0,1,2]:
print(f'Fold{i}------------------------------------')
 val_idxs=list(range(i*val_size,(i+1)*val_size))
md = get_data(val_idxs)
 learn = get_learner(md)
 learn.freeze_to(1)
 print(f'fit(lrs, wd): {lrs, wd}')
 learn.fit(lrs, 1, wds=wd, cycle_len=20,use_clr_beta=(10,10, 0.85, 0.9),best_save_name='best1')
 print('unfreezing')
 learn.unfreeze()
learn.load('best1')
 learn.crit = lovasz_hinge
learn.fit(lrs/10, 1, wds=wd, cycle_len=20,use_clr_beta=(10,10, 0.85, 0.9),best_save_name='best2')
 learn.load('best2')
 print(f'computing test set: {i}')
 p = learn.predict(is_test=True)
 learn.save(f'{i}-model')
 print('Computing optimal threshold')
 preds, targs = learn.predict_with_targs()
IoUs=[]
for a in np.arange(0, 1, 0.1):
IoUs.append(IoU_np(preds, targs, a))
IoU_max = np.array(IoUs ).argmax()
print(f'optimal Threshold: {IoU_max/10.0}')
 alpha+=IoU_max/10.0
print('TTA')
 md.test_dl.dataset = TestFilesDataset(test_x,test_x,tfms[1],PATH128)
 p_f = learn.predict(is_test=True)
 p_f = p_f[:,:,::-1]
out =(p+p_f)/2
actual_its.append(i )<define_search_space>
y_train = train['isFraud'].copy()
del train_transaction, train_identity, test_transaction, test_identity
print(f'Last Iteration group [0,1,2]: {i} ' )<train_model>
X_train = train.drop('isFraud', axis=1)
 X_test = test.copy()
for i in [3,4]:
print(f'Fold{i}------------------------------------')
 val_idxs=list(range(i*val_size,(i+1)*val_size))
md = get_data(val_idxs)
 learn = get_learner(md)
 learn.freeze_to(1)
 print(f'fit(lrs, wd): {lrs, wd}')
 learn.fit(lrs, 1, wds=wd, cycle_len=20,use_clr_beta=(10,10, 0.85, 0.9),best_save_name='best1')
 print('unfreezing')
 learn.unfreeze()
learn.load('best1')
 learn.crit = lovasz_hinge
learn.fit(lrs/10, 1, wds=wd, cycle_len=20,use_clr_beta=(10,10, 0.85, 0.9),best_save_name='best2')
 learn.load('best2')
 print(f'computing test set: {i}')
 p = learn.predict(is_test=True)
 learn.save(f'{i}-model')
 print('Computing optimal threshold')
 preds, targs = learn.predict_with_targs()
IoUs=[]
for a in np.arange(0, 1, 0.1):
IoUs.append(IoU_np(preds, targs, a))
IoU_max = np.array(IoUs ).argmax()
print(f'optimal Threshold: {IoU_max/10.0}')
 alpha+=IoU_max/10.0
print('TTA')
 md.test_dl.dataset = TestFilesDataset(test_x,test_x,tfms[1],PATH128)
 p_f = learn.predict(is_test=True)
 p_f = p_f[:,:,::-1]
out =(p+p_f)/2
actual_its.append(i )<normalization>
del train, test
print(actual_its)
 out = out/n_folds
alpha = alpha/n_folds<categorify>
X_train = X_train.fillna(-999)
 X_test = X_test.fillna(-999 )
def rle_encode(im):

 pixels = im.flatten(order='F')
 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 )<create_dataframe>
for f in X_train.columns:
if X_train[f].dtype=='object' or X_test[f].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(X_train[f].values)+ list(X_test[f].values))
X_train[f] = lbl.transform(list(X_train[f].values))
X_test[f] = lbl.transform(list(X_test[f].values))
sub = pd.DataFrame(list(zip(name_list, tmp_list)) , columns = ['id', 'rle_mask'] )<save_to_csv>
clf = xgb.XGBClassifier(
 n_estimators=500,
max_depth=9,
learning_rate=0.05,
subsample=0.9,
colsample_bytree=0.9,
missing=-999,
random_state=2019,
tree_method='gpu_hist'
)
sub.to_csv('submission.csv', index=False )<save_to_csv>
%time clf.fit(X_train, y_train )
sub.to_csv('submission.csv', index=False )<set_options>
sample_submission = pd.read_csv('/kaggle/input/ieee-fraud-detection/sample_submission.csv', index_col='TransactionID' )
<define_variables><EOS> 
sample_submission['isFraud'] = clf.predict_proba(X_test)[:,1]
sample_submission.to_csv('simple_xgboost.csv' )