Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<split>
from scipy.stats import spearmanr, rankdata
import pandas as pd
import numpy as np
import pickle
DATA_DIR = '.. /input'
train_dir = os.path.join(DATA_DIR, 'train')
 test_dir = os.path.join(DATA_DIR, 'test')
 def train_validation_split(df, val_fraction=0.1):
val_ids = np.random.choice(df.id, size=int(len(df)* val_fraction))
val_df = df.query('id in @val_ids')
 train_df = df.query('id not in @val_ids')
 return train_df, val_df
train_label_df, val_label_df = train_validation_split(pd.read_csv(os.path.join(DATA_DIR, 'train_labels.csv')) ,
val_fraction=0.1 )<categorify>
data_folder = ".. /input/ieee-fraud-detection/"
sub = pd.read_csv(f'{data_folder}sample_submission.csv' )
def function_timer(function):
def wrapper(*args, **kwargs):
start = time.time()
result = function(*args, **kwargs)
 duration = time.time() - start
hours = int(duration // 60**2)
 minutes = int(( duration % 60**2)// 60)
 seconds = int(duration % 60)
 print(f'execution-time of function "{function.__name__}": {hours}h {minutes}m {seconds}s')
 return result
return wrapper
class HistoPatches(Dataset):
def __init__(self,
image_dir: str,
label_df=None,
transform=transforms.ToTensor() ,
sample_n=None,
in_memory=False):

 self.image_dir = image_dir
self.label_df = label_df
self.transform = transform
self.in_memory = in_memory
if label_df is not None:
if sample_n:
self.label_df = self.label_df.sample(n=sample_n)
 ids = set(self.label_df.id)
 self.img_files = [f for f in os.listdir(image_dir)if f.split('.')[0] in ids]
else:
if sample_n is not None:
print('subsampling is currently only implemented when a label-dataframe is provided.')
 return
self.img_files = os.listdir(image_dir)
 if in_memory:
self.id2image = self._load_images()
print(f'Initialized datatset with {len(self.img_files)} images.
 ')
 @function_timer
def _load_images(self):
print('loading images in memory...')
 id2image = {}
for file_name in self.img_files:
img = PIL.Image.open(os.path.join(self.image_dir, file_name))
X = self.transform(img)
 id_ = file_name.split('.')[0]
id2image[id_] = X
return id2image
def __getitem__(self, idx):
file_name = self.img_files[idx]
id_ = file_name.split('.')[0]
if self.in_memory:
X = self.id2image[id_]
else:
img = PIL.Image.open(os.path.join(self.image_dir, file_name))
X = self.transform(img)
 if self.label_df is not None:
y = float(self.label_df.query('id == @id_' ).label)
 return X, y
else:
return X, id_
def __len__(self):
return len(self.img_files)
 memory = False
batchsize = 64
image_trans = transforms.Compose([
transforms.ToTensor() ,
transforms.Normalize(mean=[0.70017236, 0.5436771, 0.6961061],
std=[0.22246036, 0.26757348, 0.19798167])
 ])
 train_trans = transforms.Compose([transforms.ToTensor() ,
transforms.Normalize(mean=[0.70017236, 0.5436771, 0.6961061],
std=[0.22246036, 0.26757348, 0.19798167]),
transforms.RandomRotation(( -180, 180)) ,
transforms.RandomHorizontalFlip() ])
 train = HistoPatches(train_dir,
train_label_df,
transform=image_trans,
in_memory=memory)
 val = HistoPatches(train_dir,
val_label_df,
transform=image_trans,
in_memory=memory)
 train_loader = DataLoader(train, batch_size=batchsize, shuffle=True)
 val_loader = DataLoader(val, batch_size=batchsize, shuffle=False)
 X, y = next(iter(train_loader))
print('batch-dimensions: ', X.shape, y.shape )<categorify>
with open(".. /input/ieee-sol1/test_uid_nocoverage.p", "rb")as f:
test_uid_nocoverage = pickle.load(f )
def conv_dim(in_dim, k=3, s=1, p=0, p_left=None, p_right=None):

 if p is not None:
p_left = p_right = p
assert p_left is not None and p_right is not None
tmp =(in_dim - k + p_left + p_right)/ s
out_dim = int(np.floor(tmp)+ 1)
 if tmp % 1 != 0:
print('no exact output-dim; using Gauss-brackets.')
 print(f'out-dim: {out_dim}')
 conv_dim(30, k=3, s=1, p=0 )<choose_model_class>
sub1 = rankdata(pd.read_csv(".. /input/ieee-submissions-and-uids/final_model_blend.csv" ).isFraud)
 sub1 /= len(sub1)
 sub2 = rankdata(pd.read_csv(".. /input/ieee-sol1/blend_of_blends_1.csv" ).isFraud)
 sub2 /= len(sub2 )
net = models.densenet121(pretrained=False)
 net.features = nn.Sequential(
 nn.Sequential(OrderedDict([ 
('conv0', nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1, bias=False)) , 
('norm0', nn.BatchNorm2d(64)) , 
('relu0', nn.ReLU(inplace=True))
])) ,
net.features[4:])
 net.classifier = nn.Sequential(
 nn.Linear(1024, 512),
nn.Dropout(p=0.1),
nn.ReLU() ,
nn.Linear(512, 1)
 )<compute_train_metric>
spearmanr(sub1, sub2 )
myloss = nn.BCEWithLogitsLoss()
net.cuda()
X, y = next(iter(train_loader))
X , y = X.cuda() , y.cuda()
print('X, y shapes: ', X.shape, y.shape)
 out = net(X ).squeeze()
print(f'loss: {myloss(y, out.type(torch.DoubleTensor ).cuda() ).item() }' )<train_on_grid>
sub3 = sub1
sub3[test_uid_nocoverage] = 0.5 * sub1[test_uid_nocoverage] + 0.5 * sub2[test_uid_nocoverage]
<train_model><EOS> 
sub['isFraud'] = sub3
sub.to_csv('submission.csv', index=False)
 sub.head()