Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<categorify>
warnings.filterwarnings('ignore' )
with ThreadPoolExecutor(4)as e: e.map(resize_mask, train_names_png )<categorify>
def seed_everything(seed=0):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed )
with ThreadPoolExecutor(4)as e: e.map(resize_mask, masks_names_png )<categorify>
def make_predictions(tr_df, tt_df, features_columns, target, lgb_params, NFOLDS=2):
folds = KFold(n_splits=NFOLDS, shuffle=True, random_state=SEED)
 X,y = tr_df[features_columns], tr_df[target]
P,P_y = tt_df[features_columns], tt_df[target]
tt_df = tt_df[['TransactionID',target]]
predictions = np.zeros(len(tt_df))
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X, y)) :
print('Fold:',fold_)
 tr_x, tr_y = X.iloc[trn_idx,:], y[trn_idx]
vl_x, vl_y = X.iloc[val_idx,:], y[val_idx]
print(len(tr_x),len(vl_x))
tr_data = lgb.Dataset(tr_x, label=tr_y)
 if LOCAL_TEST:
vl_data = lgb.Dataset(P, label=P_y)
 else:
vl_data = lgb.Dataset(vl_x, label=vl_y)
 estimator = lgb.train(
 lgb_params,
tr_data,
valid_sets = [tr_data, vl_data],
verbose_eval = 200, 
)
 pp_p = estimator.predict(P)
 predictions += pp_p/NFOLDS
if LOCAL_TEST:
feature_imp = pd.DataFrame(sorted(zip(estimator.feature_importance() ,X.columns)) , columns=['Value','Feature'])
 print(feature_imp)
 del tr_x, tr_y, vl_x, vl_y, tr_data, vl_data
gc.collect()
tt_df['prediction'] = predictions
return tt_df
with ThreadPoolExecutor(4)as e: e.map(resize_mask, test_names_png )<prepare_x_and_y>
SEED = 42
seed_everything(SEED)
 LOCAL_TEST = False
TARGET = 'isFraud'
START_DATE = datetime.datetime.strptime('2017-11-30', '%Y-%m-%d' )
class CustomDataset(FilesDataset):
def __init__(self, fnames, y, transform, path):
self.y=y
assert(len(fnames)==len(y))
super().__init__(fnames, transform, path)
 def get_x(self, i):
return open_image(os.path.join(self.path,self.fnames[i]))
def get_y(self, i):
return open_image(os.path.join(self.path,self.y[i]))
def get_c(self): return 0<prepare_x_and_y>
print('Load Data')
 train_df = pd.read_pickle('.. /input/ieee-data-minification/train_transaction.pkl')
 if LOCAL_TEST:
train_df['DT_M'] = train_df['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 train_df['DT_M'] =(train_df['DT_M'].dt.year-2017)*12 + train_df['DT_M'].dt.month
test_df = train_df[train_df['DT_M']==train_df['DT_M'].max() ].reset_index(drop=True)
 train_df = train_df[train_df['DT_M']<(train_df['DT_M'].max() -1)].reset_index(drop=True)
 train_identity = pd.read_pickle('.. /input/ieee-data-minification/train_identity.pkl')
 test_identity = train_identity[train_identity['TransactionID'].isin(
 test_df['TransactionID'])].reset_index(drop=True)
 train_identity = train_identity[train_identity['TransactionID'].isin(
 train_df['TransactionID'])].reset_index(drop=True)
 del train_df['DT_M'], test_df['DT_M']
else:
test_df = pd.read_pickle('.. /input/ieee-data-minification/test_transaction.pkl')
 train_identity = pd.read_pickle('.. /input/ieee-data-minification/train_identity.pkl')
 test_identity = pd.read_pickle('.. /input/ieee-data-minification/test_identity.pkl')
 base_columns = list(train_df)+ list(train_identity)
 print('Shape control:', train_df.shape, test_df.shape )
class TestFilesDataset(FilesDataset):
def __init__(self, fnames, y, transform, path):
self.y=y
self.th = 1
super().__init__(fnames, transform, path)
 def get_x(self, i):
return np.fliplr(open_image(os.path.join(self.path, self.fnames[i])))
 def get_y(self, i):
return np.fliplr(open_image(os.path.join(self.path,self.y[i])))
 def get_c(self): return 0<compute_test_metric>
i_cols = ['card1']
for col in i_cols:
valid_card = pd.concat([train_df[[col]], test_df[[col]]])
 valid_card = valid_card[col].value_counts()
valid_card = valid_card[valid_card>2]
valid_card = list(valid_card.index)
 train_df[col] = np.where(train_df[col].isin(valid_card), train_df[col], np.nan)
 test_df[col] = np.where(test_df[col].isin(valid_card), test_df[col], np.nan )
def IoU_np(pred, targs, thres=0):
pred =(pred>thres)
 intersection =(pred*targs ).sum()
return intersection /(( pred+targs ).sum() - intersection + 1.0)
 def IoU(pred, targs, thres=0):
pred =(pred>thres ).float()
intersection =(pred*targs ).sum()
return intersection /(( pred+targs ).sum() - intersection + 1.0 )<load_pretrained>
i_cols = ['card1','card2','card3','card5',
'C1','C2','C3','C4','C5','C6','C7','C8','C9','C10','C11','C12','C13','C14',
'D1','D2','D3','D4','D5','D6','D7','D8','D9',
'addr1','addr2',
'dist1','dist2',
]
for col in i_cols:
temp_df = pd.concat([train_df[[col]], test_df[[col]]])
 valid_card = temp_df[col].value_counts().to_dict()
train_df[col+'_fq_enc'] = train_df[col].map(valid_card)
 test_df[col+'_fq_enc'] = test_df[col].map(valid_card )
def get_base(f, cut):
layers = cut_model(f(True), cut)
 return nn.Sequential(*layers)
 def load_pretrained(model, path):
weights = torch.load(PRETRAINED, map_location=lambda storage, loc: storage)
 model.load_state_dict(weights, strict=False)
 return model<categorify>
for col in ['ProductCD','M4']:
temp_dict = train_df.groupby([col])[TARGET].agg(['mean'] ).reset_index().rename(
 columns={'mean': col+'_target_mean'})
 temp_dict.index = temp_dict[col].values
temp_dict = temp_dict[col+'_target_mean'].to_dict()
train_df[col+'_target_mean'] = train_df[col].map(temp_dict)
 test_df[col+'_target_mean'] = test_df[col].map(temp_dict )
class SaveFeatures() :
features=None
def __init__(self, m): self.hook = m.register_forward_hook(self.hook_fn)
 def hook_fn(self, module, input, output): self.features = output
def remove(self): self.hook.remove()<concatenate>
i_cols = ['M1','M2','M3','M5','M6','M7','M8','M9']
for df in [train_df, test_df]:
df['M_sum'] = df[i_cols].sum(axis=1 ).astype(np.int8)
 df['M_na'] = df[i_cols].isna().sum(axis=1 ).astype(np.int8 )
class UnetBlock(nn.Module):
def __init__(self, up_in, x_in, n_out):
super().__init__()
up_out = x_out = n_out//2
self.x_conv = nn.Conv2d(x_in, x_out, 1)
 self.tr_conv = nn.ConvTranspose2d(up_in, up_out, 2, stride=2)
 self.bn = nn.BatchNorm2d(n_out)
 def forward(self, up_p, x_p):
up_p = self.tr_conv(up_p)
 x_p = self.x_conv(x_p)
 cat_p = torch.cat([up_p,x_p], dim=1)
 return self.bn(F.relu(cat_p))<define_search_space>
train_df['uid'] = train_df['card1'].astype(str)+'_'+train_df['card2'].astype(str)
 test_df['uid'] = test_df['card1'].astype(str)+'_'+test_df['card2'].astype(str)
 train_df['uid2'] = train_df['uid'].astype(str)+'_'+train_df['card3'].astype(str)+'_'+train_df['card5'].astype(str)
 test_df['uid2'] = test_df['uid'].astype(str)+'_'+test_df['card3'].astype(str)+'_'+test_df['card5'].astype(str)
 train_df['uid3'] = train_df['uid2'].astype(str)+'_'+train_df['addr1'].astype(str)+'_'+train_df['addr2'].astype(str)
 test_df['uid3'] = test_df['uid2'].astype(str)+'_'+test_df['addr1'].astype(str)+'_'+test_df['addr2'].astype(str)
 train_df['TransactionAmt_check'] = np.where(train_df['TransactionAmt'].isin(test_df['TransactionAmt']), 1, 0)
 test_df['TransactionAmt_check'] = np.where(test_df['TransactionAmt'].isin(train_df['TransactionAmt']), 1, 0)
 i_cols = ['card1','card2','card3','card5','uid','uid2','uid3']
for col in i_cols:
for agg_type in ['mean','std']:
new_col_name = col+'_TransactionAmt_'+agg_type
temp_df = pd.concat([train_df[[col, 'TransactionAmt']], test_df[[col,'TransactionAmt']]])
 temp_df = temp_df.groupby([col])['TransactionAmt'].agg([agg_type] ).reset_index().rename(
 columns={agg_type: new_col_name})
 temp_df.index = list(temp_df[col])
 temp_df = temp_df[new_col_name].to_dict()
train_df[new_col_name] = train_df[col].map(temp_df)
 test_df[new_col_name] = test_df[col].map(temp_df )
class Unet34(nn.Module):
def __init__(self, rn):
super().__init__()
self.rn = rn
self.sfs = [SaveFeatures(rn[i])for i in [2,4,5,6]]
self.up1 = UnetBlock(512,256,256)
 self.up2 = UnetBlock(256,128,256)
 self.up3 = UnetBlock(256,64,256)
 self.up4 = UnetBlock(256,64,256)
 self.cSE = nn.Sequential(nn.AdaptiveAvgPool2d(1),
nn.Conv2d(512,256,1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 512,1),
nn.Sigmoid() 
)
 self.sSE = nn.Sequential(nn.Conv2d(512,512,1),
nn.Sigmoid())
 self.up5 = nn.ConvTranspose2d(256, 1, 2, stride=2)
 def forward(self,x):
x = F.dropout(F.relu(self.rn(x)) ,0.3)
 x = torch.addcmul(x * self.cSE(x), 1, x, self.sSE(x))
x1 = self.up1(x, self.sfs[3].features)
 x2 = self.up2(x1, self.sfs[2].features)
 x = self.up3(x2, self.sfs[1].features)
 x = self.up4(x, self.sfs[0].features)
 x = self.up5(x)
 x = torch.cat(( x,
F.interpolate(( x2),scale_factor=8,mode='bilinear',align_corners=True),
F.interpolate(( x1),scale_factor=16,mode='bilinear', align_corners=True)
),1)
 return F.dropout(x[:,0],0.3)
 def close(self):
for sf in self.sfs: sf.remove()<choose_model_class>
for col in list(train_df):
if train_df[col].dtype=='O':
print(col)
 train_df[col] = train_df[col].fillna('unseen_before_label')
 test_df[col] = test_df[col].fillna('unseen_before_label')
 train_df[col] = train_df[col].astype(str)
 test_df[col] = test_df[col].astype(str)
 le = LabelEncoder()
le.fit(list(train_df[col])+list(test_df[col]))
train_df[col] = le.transform(train_df[col])
 test_df[col] = le.transform(test_df[col])
 train_df[col] = train_df[col].astype('category')
 test_df[col] = test_df[col].astype('category' )
class UnetModel() :
def __init__(self,model,name='unet'):
self.model,self.name = model,name
def get_layer_groups(self, precompute):
lgs = list(split_by_idxs(children(self.model.rn), [lr_cut]))
return lgs + [children(self.model)[1:]]<prepare_x_and_y>
rm_cols = [
'TransactionID','TransactionDT',
TARGET,
'uid','uid2','uid3',
]
features_columns = [col for col in list(train_df)if col not in rm_cols]
x_names = [f'{x}.png' for x in train_names]
x_names_path = np.array([str(TRAIN_DN/x)for x in x_names])
 y_names = [x for x in x_names]
y_names_path = np.array([str(MASKS_DN/x)for x in x_names] )<normalization>
lgb_params = {
'objective':'binary',
'boosting_type':'gbdt',
'metric':'auc',
'n_jobs':-1,
'learning_rate':0.01,
'num_leaves': 2**8,
'max_depth':-1,
'tree_learner':'serial',
'colsample_bytree': 0.7,
'subsample_freq':1,
'subsample':0.7,
'n_estimators':800,
'max_bin':255,
'verbose':-1,
'seed': SEED,
'early_stopping_rounds':100,
}
class RandomRotate2(CoordTransform):

 def __init__(self, deg, p=0.75, mode=cv2.BORDER_REFLECT, tfm_y=TfmType.NO):
super().__init__(tfm_y)
 self.deg,self.p = deg,p
if tfm_y == TfmType.COORD or tfm_y == TfmType.CLASS:
self.modes =(mode,cv2.BORDER_CONSTANT)
 else:
self.modes =(mode,mode)
 def set_state(self):
self.store.rdeg = rand0(self.deg)
 self.store.rp = random.random() <self.p
def do_transform(self, x, is_y):
if self.store.rp: x = rotate_cv(x, self.store.rdeg,
mode = self.modes[0],
interpolation=cv2.INTER_NEAREST if is_y else cv2.INTER_AREA)
 return x<split>
if LOCAL_TEST:
lgb_params['learning_rate'] = 0.01
lgb_params['n_estimators'] = 20000
lgb_params['early_stopping_rounds'] = 100
test_predictions = make_predictions(train_df, test_df, features_columns, TARGET, lgb_params)
 print(metrics.roc_auc_score(test_predictions[TARGET], test_predictions['prediction']))
else:
lgb_params['learning_rate'] = 0.005
lgb_params['n_estimators'] = 1800
lgb_params['early_stopping_rounds'] = 100
test_predictions = make_predictions(train_df, test_df, features_columns, TARGET, lgb_params, NFOLDS=10 )
aug_tfms = [RandomRotate2(4, tfm_y=TfmType.CLASS),
RandomFlip(tfm_y=TfmType.CLASS),
RandomStretch(0.2,tfm_y=TfmType.CLASS),
RandomLighting(0.05, 0.05, tfm_y=TfmType.CLASS)]
tfms = tfms_from_model(resnet34, sz=sz, pad=0, crop_type=CropType.NO, tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)
 def get_data(val_idxs): 
(( val_x,trn_x),(val_y,trn_y)) = split_by_idx(val_idxs, x_names_path, y_names_path)
 datasets = ImageData.get_ds(CustomDataset,(trn_x,trn_y),(val_x,val_y), tfms,(test_x, test_x), path=PATH128)
 return ImageData(PATH128, datasets, bs=32, num_workers=nw, classes=None)
<choose_model_class>
if not LOCAL_TEST:
test_predictions['isFraud'] = test_predictions['prediction']
test_predictions[['TransactionID','isFraud']].to_csv('submission.csv', index=False )