Kaggle competition: Google Cloud & NCAA® ML Competition 2018-Men's
<SOS> metric: LogLoss Kaggle data source: mens-machine-learning-competition-2018<define_variables>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
from sklearn.preprocessing import Imputer
from sklearn import preprocessing
from sklearn.cross_validation import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
import re
version = 5
basic_name = f'Unet_resnet_v{version}'
save_model_name = basic_name + '.model'
submission_file = basic_name + '.csv'
print(save_model_name)
 print(submission_file )<train_model>
massey = pd.read_csv('.. /input/MasseyOrdinals.csv')
 cities = pd.read_csv('.. /input/Cities.csv')
 conferences = pd.read_csv('.. /input/Conferences.csv')
 conf_tourney = pd.read_csv('.. /input/ConferenceTourneyGames.csv')
 game_cities = pd.read_csv('.. /input/GameCities.csv')
 tourney_results = pd.read_csv('.. /input/NCAATourneyCompactResults.csv')
 detailed_tourney_results = pd.read_csv('.. /input/NCAATourneyDetailedResults.csv')
 tourney_all = pd.read_csv('.. /input/NCAATourneySeedRoundSlots.csv')
 tourney_seeds = pd.read_csv('.. /input/NCAATourneySeeds.csv')
 tourney_slots = pd.read_csv('.. /input/NCAATourneySlots.csv')
 reg_season = pd.read_csv('.. /input/RegularSeasonCompactResults.csv')
 detailed_reg_season = pd.read_csv('.. /input/RegularSeasonDetailedResults.csv')
 seasons = pd.read_csv('.. /input/Seasons.csv')
 second_tourney = pd.read_csv('.. /input/SecondaryTourneyCompactResults.csv')
 detailed_second_tourney = pd.read_csv('.. /input/SecondaryTourneyTeams.csv')
 team_coaches = pd.read_csv('.. /input/TeamCoaches.csv')
 team_conferences = pd.read_csv('.. /input/TeamConferences.csv')
 teams = pd.read_csv('.. /input/Teams.csv')
 teams_description = pd.read_csv('.. /input/Teams.csv' )
img_size_ori = 101
img_size_target = 101
def upsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_target, img_size_target), mode='constant', preserve_range=True)
 def downsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_ori, img_size_ori), mode='constant', preserve_range=True )<load_from_csv>
detailed_reg_season.groupby('Season' ).count()
train_df = pd.read_csv(".. /input/train.csv", index_col="id", usecols=[0])
 depths_df = pd.read_csv(".. /input/depths.csv", index_col="id")
 train_df = train_df.join(depths_df)
 test_df = depths_df[~depths_df.index.isin(train_df.index)]
len(train_df )<feature_engineering>
rs = pd.read_csv(".. /input/RegularSeasonCompactResults.csv")
 results_data = pd.read_csv(".. /input/RegularSeasonCompactResults.csv")
 K = 45
HOME_ADVANTAGE = 10
rs = results_data
rs.head(3)
 team_ids = set(rs.WTeamID ).union(set(rs.LTeamID))
len(team_ids)
 elo_dict = dict(zip(list(team_ids), [1500] * len(team_ids)))
 rs['margin'] = rs.WScore - rs.LScore
def elo_pred(elo1, elo2):
return(1./(10.**(-(elo1 - elo2)/ 400.) + 1.))
 def expected_margin(elo_diff):
return(( 7.5 + 0.006 * elo_diff))
def elo_update(w_elo, l_elo, margin):
elo_diff = w_elo - l_elo
pred = elo_pred(w_elo, l_elo)
 mult =(( margin + 3.) ** 0.8)/ expected_margin(elo_diff)
 update = K * mult *(1 - pred)
 return(pred, update)
 assert np.all(rs.index.values == np.array(range(rs.shape[0]))), "Index is out of order."
preds = []
w_elo = []
l_elo = []
for row in rs.itertuples() :
w = row.WTeamID
l = row.LTeamID
margin = row.margin
wloc = row.WLoc
w_ad, l_ad, = 0., 0.
 if wloc == "H":
w_ad += HOME_ADVANTAGE
elif wloc == "A":
l_ad += HOME_ADVANTAGE
pred, update = elo_update(elo_dict[w] + w_ad,
elo_dict[l] + l_ad,
margin)
 elo_dict[w] += update
elo_dict[l] -= update
preds.append(pred)
 w_elo.append(elo_dict[w])
 l_elo.append(elo_dict[l])
 rs['w_elo'] = w_elo
rs['l_elo'] = l_elo
rs['elo_dif'] =rs['w_elo']-rs['l_elo']
print(rs[['elo_dif','margin']].corr().iloc[0,1])
train_df["images"] = [np.array(load_img(".. /input/train/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]<feature_engineering>
rs_win=rs[['Season','DayNum','WTeamID','w_elo']]
rs_lose=rs[['Season','DayNum','LTeamID','l_elo']]
rs_win=rs_win.rename(columns={'WTeamID':'TeamID','w_elo':'elo'})
 rs_lose=rs_lose.rename(columns={'LTeamID':'TeamID','l_elo':'elo'})
 rs_all=rs_win.append(rs_lose)
 final_elo=pd.DataFrame()
for team in rs_all['TeamID'].unique() :
for season in rs_all[rs_all['TeamID']==team]['Season'].unique() :
append=rs_all[(rs_all['TeamID']==team)&(rs_all['Season']==season)].sort_values('DayNum', ascending=False ).iloc[0,:]
final_elo=final_elo.append(append)
 final_elo.head(2 )
train_df["masks"] = [np.array(load_img(".. /input/train/masks/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]<categorify>
tourney_seeds = pd.read_csv('.. /input/NCAATourneySeeds.csv' )
train_df["coverage"] = train_df.masks.map(np.sum)/ pow(img_size_ori, 2)
 def cov_to_class(val):
for i in range(0, 11):
if val * 10 <= i :
return i
train_df["coverage_class"] = train_df.coverage.map(cov_to_class)
<split>
def removeletters(x):
return re.sub('[^0-9]','', x )
ids_train, ids_valid, x_train, x_valid, y_train, y_valid, cov_train, cov_test, depth_train, depth_test = train_test_split(
 train_df.index.values,
np.array(train_df.images.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1),
np.array(train_df.masks.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1),
train_df.coverage.values,
train_df.z.values,
test_size=0.2, stratify=train_df.coverage_class, random_state= 1234 )<define_search_model>
tourney_seeds['Seed']=tourney_seeds['Seed'].apply(lambda x: removeletters(x)).apply(int )
def BatchActivate(x):
x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 return x
def convolution_block(x, filters, size, strides=(1,1), padding='same', activation=True):
x = Conv2D(filters, size, strides=strides, padding=padding )(x)
 if activation == True:
x = BatchActivate(x)
 return x
def residual_block(blockInput, num_filters=16, batch_activate = False):
x = BatchActivate(blockInput)
 x = convolution_block(x, num_filters,(3,3))
x = convolution_block(x, num_filters,(3,3), activation=False)
 x = Add()([x, blockInput])
 if batch_activate:
x = BatchActivate(x)
 return x 
<choose_model_class>
df1=tourney_seeds.merge(tourney_results, left_on= ['TeamID','Season'], right_on=['WTeamID','Season'], how='left' )
def build_model(input_layer, start_neurons, DropoutRatio = 0.5):
conv1 = Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same" )(input_layer)
 conv1 = residual_block(conv1,start_neurons * 1)
 conv1 = residual_block(conv1,start_neurons * 1, True)
 pool1 = MaxPooling2D(( 2, 2))(conv1)
 pool1 = Dropout(DropoutRatio/2 )(pool1)
 conv2 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(pool1)
 conv2 = residual_block(conv2,start_neurons * 2)
 conv2 = residual_block(conv2,start_neurons * 2, True)
 pool2 = MaxPooling2D(( 2, 2))(conv2)
 pool2 = Dropout(DropoutRatio )(pool2)
 conv3 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(pool2)
 conv3 = residual_block(conv3,start_neurons * 4)
 conv3 = residual_block(conv3,start_neurons * 4, True)
 pool3 = MaxPooling2D(( 2, 2))(conv3)
 pool3 = Dropout(DropoutRatio )(pool3)
 conv4 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(pool3)
 conv4 = residual_block(conv4,start_neurons * 8)
 conv4 = residual_block(conv4,start_neurons * 8, True)
 pool4 = MaxPooling2D(( 2, 2))(conv4)
 pool4 = Dropout(DropoutRatio )(pool4)
 convm = Conv2D(start_neurons * 16,(3, 3), activation=None, padding="same" )(pool4)
 convm = residual_block(convm,start_neurons * 16)
 convm = residual_block(convm,start_neurons * 16, True)
 deconv4 = Conv2DTranspose(start_neurons * 8,(3, 3), strides=(2, 2), padding="same" )(convm)
 uconv4 = concatenate([deconv4, conv4])
 uconv4 = Dropout(DropoutRatio )(uconv4)
 uconv4 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(uconv4)
 uconv4 = residual_block(uconv4,start_neurons * 8)
 uconv4 = residual_block(uconv4,start_neurons * 8, True)
 deconv3 = Conv2DTranspose(start_neurons * 4,(3, 3), strides=(2, 2), padding="valid" )(uconv4)
 uconv3 = concatenate([deconv3, conv3])
 uconv3 = Dropout(DropoutRatio )(uconv3)
 uconv3 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(uconv3)
 uconv3 = residual_block(uconv3,start_neurons * 4)
 uconv3 = residual_block(uconv3,start_neurons * 4, True)
 deconv2 = Conv2DTranspose(start_neurons * 2,(3, 3), strides=(2, 2), padding="same" )(uconv3)
 uconv2 = concatenate([deconv2, conv2])
 uconv2 = Dropout(DropoutRatio )(uconv2)
 uconv2 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(uconv2)
 uconv2 = residual_block(uconv2,start_neurons * 2)
 uconv2 = residual_block(uconv2,start_neurons * 2, True)
 deconv1 = Conv2DTranspose(start_neurons * 1,(3, 3), strides=(2, 2), padding="valid" )(uconv2)
 uconv1 = concatenate([deconv1, conv1])
 uconv1 = Dropout(DropoutRatio )(uconv1)
 uconv1 = Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same" )(uconv1)
 uconv1 = residual_block(uconv1,start_neurons * 1)
 uconv1 = residual_block(uconv1,start_neurons * 1, True)
 output_layer_noActi = Conv2D(1,(1,1), padding="same", activation=None )(uconv1)
 output_layer = Activation('sigmoid' )(output_layer_noActi)
 return output_layer<compute_train_metric>
df1['TeamID1']=df1['WTeamID']
df1['TeamID2']=df1['LTeamID']
df1['Win']=np.where(df1['TeamID1']==df1['WTeamID'],1,0)
 df1=df1.rename(columns={'Seed':'TeamID1Seed'} )
def get_iou_vector(A, B):
batch_size = A.shape[0]
metric = []
for batch in range(batch_size):
t, p = A[batch]>0, B[batch]>0
intersection = np.logical_and(t, p)
 union = np.logical_or(t, p)
 iou =(np.sum(intersection > 0)+ 1e-10)/(np.sum(union > 0)+ 1e-10)
 thresholds = np.arange(0.5, 1, 0.05)
 s = []
for thresh in thresholds:
s.append(iou > thresh)
 metric.append(np.mean(s))
return np.mean(metric)
 def my_iou_metric(label, pred):
return tf.py_func(get_iou_vector, [label, pred>0.5], tf.float64)
 def my_iou_metric_2(label, pred):
return tf.py_func(get_iou_vector, [label, pred >0], tf.float64 )<compute_train_metric>
df2=df1.merge(tourney_seeds, left_on= ['LTeamID','Season'], right_on=['TeamID','Season'], how='right')
 df2=df2.rename(columns={'Seed':'TeamID2Seed','Win':'Team1Win'})
 df2.drop(['TeamID_x','TeamID_y','NumOT','WLoc','LTeamID','WTeamID'], 1, inplace = True )
def lovasz_grad(gt_sorted):

 gts = tf.reduce_sum(gt_sorted)
 intersection = gts - tf.cumsum(gt_sorted)
 union = gts + tf.cumsum(1.- gt_sorted)
 jaccard = 1.- intersection / union
jaccard = tf.concat(( jaccard[0:1], jaccard[1:] - jaccard[:-1]), 0)
 return jaccard
def lovasz_hinge(logits, labels, per_image=True, ignore=None):

 if per_image:
def treat_image(log_lab):
log, lab = log_lab
log, lab = tf.expand_dims(log, 0), tf.expand_dims(lab, 0)
 log, lab = flatten_binary_scores(log, lab, ignore)
 return lovasz_hinge_flat(log, lab)
 losses = tf.map_fn(treat_image,(logits, labels), dtype=tf.float32)
 loss = tf.reduce_mean(losses)
 else:
loss = lovasz_hinge_flat(*flatten_binary_scores(logits, labels, ignore))
return loss
def lovasz_hinge_flat(logits, labels):

 def compute_loss() :
labelsf = tf.cast(labels, logits.dtype)
 signs = 2.* labelsf - 1.
 errors = 1.- logits * tf.stop_gradient(signs)
 errors_sorted, perm = tf.nn.top_k(errors, k=tf.shape(errors)[0], name="descending_sort")
 gt_sorted = tf.gather(labelsf, perm)
 grad = lovasz_grad(gt_sorted)
 loss = tf.tensordot(tf.nn.relu(errors_sorted), tf.stop_gradient(grad), 1, name="loss_non_void")
 return loss
loss = tf.cond(tf.equal(tf.shape(logits)[0], 0),
lambda: tf.reduce_sum(logits)* 0.,
compute_loss,
strict=True,
name="loss" 
)
 return loss
def flatten_binary_scores(scores, labels, ignore=None):

 scores = tf.reshape(scores,(-1,))
labels = tf.reshape(labels,(-1,))
if ignore is None:
return scores, labels
valid = tf.not_equal(labels, ignore)
 vscores = tf.boolean_mask(scores, valid, name='valid_scores')
 vlabels = tf.boolean_mask(labels, valid, name='valid_labels')
 return vscores, vlabels
def lovasz_loss(y_true, y_pred):
y_true, y_pred = K.cast(K.squeeze(y_true, -1), 'int32'), K.cast(K.squeeze(y_pred, -1), 'float32')
 logits = y_pred
loss = lovasz_hinge(logits, y_true, per_image = True, ignore = None)
 return loss<choose_model_class>
df2['Margin']=df2['WScore']-df2['LScore']
df2.drop(['WScore','LScore'], 1, inplace = True )
input_layer = Input(( img_size_target, img_size_target, 1))
output_layer = build_model(input_layer, 16,0.5)
 model1 = Model(input_layer, output_layer)
 c = optimizers.adam(lr = 0.01)
 model1.compile(loss="binary_crossentropy", optimizer=c, metrics=[my_iou_metric])
<train_model>
lose_df=df2[['TeamID2','TeamID1','TeamID2Seed','TeamID1Seed','Season']]
lose_df=lose_df.rename(columns={'TeamID2':'TeamID','TeamID1':'OppTeamID','TeamID2Seed':'Seed','TeamID1Seed':'OppSeed'})
 lose_df['Win']=0
model_checkpoint = ModelCheckpoint(save_model_name,monitor='my_iou_metric',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='my_iou_metric', mode = 'max',factor=0.5, patience=5, min_lr=0.0001, verbose=1)
 epochs = 40
batch_size = 32
history = model1.fit(x_train, y_train,
validation_data=[x_valid, y_valid],
epochs=epochs,
batch_size=batch_size,
callbacks=[ model_checkpoint,reduce_lr],
verbose=2 )<choose_model_class>
win_df=df2[['TeamID1','TeamID2','TeamID1Seed','TeamID2Seed','Season']]
win_df['Win']=1
win_df=win_df.rename(columns={'TeamID1':'TeamID','TeamID2':'OppTeamID','TeamID1Seed':'Seed','TeamID2Seed':'OppSeed'} )
model1 = load_model(save_model_name,custom_objects={'my_iou_metric': my_iou_metric})
 input_x = model1.layers[0].input
output_layer = model1.layers[-1].input
model = Model(input_x, output_layer)
 c = optimizers.adam(lr = 0.01)
 model.compile(loss=lovasz_loss, optimizer=c, metrics=[my_iou_metric_2])
<train_model>
all_tourney_df=win_df.append(lose_df )
early_stopping = EarlyStopping(monitor='val_my_iou_metric_2', mode = 'max',patience=20, verbose=1)
 model_checkpoint = ModelCheckpoint(save_model_name,monitor='val_my_iou_metric_2',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_my_iou_metric_2', mode = 'max',factor=0.5, patience=5, min_lr=0.0001, verbose=1)
 epochs = 40
batch_size = 32
history = model.fit(x_train, y_train,
validation_data=[x_valid, y_valid],
epochs=epochs,
batch_size=batch_size,
callbacks=[ model_checkpoint,reduce_lr,early_stopping],
verbose=2 )<load_pretrained>
all_tourney_df=all_tourney_df.merge(final_elo, left_on=['OppTeamID','Season'], right_on=['TeamID','Season'],how='left')
 all_tourney_df.drop(['TeamID_y'], 1, inplace = True)
 all_tourney_df=all_tourney_df.rename(columns={'TeamID_x':'TeamID','elo':'Oppelo'} )
model = load_model(save_model_name,custom_objects={'my_iou_metric_2': my_iou_metric_2,
'lovasz_loss': lovasz_loss})
<predict_on_test>
all_tourney_df=all_tourney_df.merge(final_elo, left_on=['TeamID','Season'], right_on=['TeamID','Season'],how='left' )
def predict_result(model,x_test,img_size_target):
x_test_reflect = np.array([np.fliplr(x)for x in x_test])
 preds_test = model.predict(x_test ).reshape(-1, img_size_target, img_size_target)
 preds_test2_refect = model.predict(x_test_reflect ).reshape(-1, img_size_target, img_size_target)
 preds_test += np.array([ np.fliplr(x)for x in preds_test2_refect])
 return preds_test/2<predict_on_test>
all_tourney_df.drop(['DayNum_y','DayNum_x'], 1, inplace = True )
preds_valid = predict_result(model,x_valid,img_size_target )<define_search_space>
all_tourney_df=all_tourney_df[(~all_tourney_df['TeamID'].isnull())&(~all_tourney_df['OppSeed'].isnull())]
thresholds_ori = np.linspace(0.3, 0.7, 31)
 thresholds = np.log(thresholds_ori/(1-thresholds_ori))
ious = np.array([iou_metric_batch(y_valid, preds_valid > threshold)for threshold in tqdm_notebook(thresholds)])
 print(ious )<categorify>
all_tourney_df['elodif']=all_tourney_df['elo']-all_tourney_df['Oppelo']
all_tourney_df['seeddif']=all_tourney_df['Seed']-all_tourney_df['OppSeed']
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 )<predict_on_test>
features=all_tourney_df[['TeamID','OppSeed','Seed','Season','OppTeamID','elodif']]
target=all_tourney_df['Win']
preds_test = predict_result(model,x_test,img_size_target )<save_to_csv>
features_train, features_test, target_train, target_test = train_test_split(features,
target, test_size = 0.3)
 target_test
clf = GaussianNB()
clf.fit(features_train, target_train)
 target_pred = clf.predict(features_test)
 accuracy_score(target_test, target_pred, normalize = True)
 def conf_matrix(y_test, y_predict):
conf = sk_confusion_matrix(y_test, y_predict)
 conf = pd.DataFrame(data=conf)
 conf.columns.name = 'Predicted label'
conf.index.name = 'Actual label'
return conf
conf_matrix(target_test, target_pred )
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv(submission_file )<train_model>
from sklearn import linear_model, datasets
t_finish = time.time()
print(f"Kernel run time = {(t_finish-t_start)/3600} hours" )<set_options>
clf = linear_model.LogisticRegression()
clf.fit(features_train, target_train)
 target_pred = clf.predict(features_test)
 accuracy_score(target_test, target_pred, normalize = True )
plt.style.use('seaborn-white')
 sns.set_style("white")
<train_model>
print(metrics.classification_report(target_test, target_pred))
img_size_ori = 101
debug=False
def upsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_target, img_size_target), mode='constant', preserve_range=True)
 def downsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_ori, img_size_ori), mode='constant', preserve_range=True)
<load_from_csv>
df2018=tourney_seeds[tourney_seeds['Season']==2018]
train_df = pd.read_csv(".. /input/train.csv", index_col="id", usecols=[0])
 depths_df = pd.read_csv(".. /input/depths.csv", index_col="id")
 train_df = train_df.join(depths_df)
 test_df = depths_df[~depths_df.index.isin(train_df.index)]
if debug:
train_df=train_df.iloc[:64*4]
test_df=test_df.iloc[:64*4] 
<feature_engineering>
matchups=pd.DataFrame()
index=0
for team in teams2018:
for team2 in teams2018:
if team!=team2:
index=index+1
matchup=pd.DataFrame({'TeamID':team,'OppTeamID':team2},index=[index])
 matchups=matchups.append(matchup )
train_df["images"] = [np.array(load_img(".. /input/train/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]<feature_engineering>
final_elo2018=final_elo[final_elo['Season']==2018][['TeamID','elo']]
train_df["masks"] = [np.array(load_img(".. /input/train/masks/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]<feature_engineering>
matchups=matchups.merge(final_elo2018, left_on=['OppTeamID'],right_on=['TeamID'])
 matchups.drop(labels=['TeamID_y'], inplace=True, axis=1)
 matchups=matchups.rename(columns={'TeamID_x':'TeamID'})
 matchups=matchups.rename(columns={'elo':'Oppelo'} )
train_df["coverage"] = train_df.masks.map(np.sum)/ pow(img_size_ori, 2 )<categorify>
matchups=matchups.merge(final_elo2018, left_on=['TeamID'],right_on=['TeamID'])
 matchups=matchups.merge(df2018, left_on=['OppTeamID'], right_on=['TeamID'])
 matchups=matchups.rename(columns={'TeamID_x':'TeamID','Seed':'OppSeed'})
 matchups.drop(['TeamID_y'], 1, inplace = True )
def cov_to_class(val):
for i in range(0, 11):
if val * 10 <= i :
return i
train_df["coverage_class"] = train_df.coverage.map(cov_to_class )<compute_train_metric>
matchups=matchups.merge(df2018, left_on=['TeamID'], right_on=['TeamID'])
 matchups=matchups.rename(columns={'Season_x':'Season'})
 matchups.drop(['Season_y'], 1, inplace = True)
 matchups['elodif']=matchups['elo']-matchups['Oppelo']
matchups['seeddif']=matchups['Seed']-matchups['OppSeed']
def iouMetric(true, pred):
true = K.batch_flatten(true)
 pred = K.batch_flatten(pred)
 pred = K.cast(K.greater(pred, 0.5), K.floatx())
 intersec = true * pred
iou = K.sum(intersec)/(K.sum(true + pred - intersec)+ K.epsilon())
 return iou<split>
features=matchups[['TeamID','OppSeed','Seed','Season','OppTeamID','elodif']]
ids_train, ids_valid, x_train, x_valid, y_train, y_valid, cov_train, cov_test, depth_train, depth_test = train_test_split(
 train_df.index.values,
np.array(train_df.images.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1),
np.array(train_df.masks.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1),
train_df.coverage.values,
train_df.z.values,
test_size=0.2, stratify=train_df.coverage_class, random_state=1337 )<compute_test_metric>
probabilities=clf.predict_proba(features )
def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
 y_pred = K.cast(y_pred, 'float32')
 y_pred_f = K.cast(K.greater(K.flatten(y_pred), 0.5), 'float32')
 intersection = y_true_f * y_pred_f
score = 2.* K.sum(intersection)/(K.sum(y_true_f)+ K.sum(y_pred_f))
return score
def dice_loss(y_true, y_pred):
smooth = 1.
 y_true_f = K.flatten(y_true)
 y_pred_f = K.flatten(y_pred)
 intersection = y_true_f * y_pred_f
score =(2.* K.sum(intersection)+ smooth)/(K.sum(y_true_f)+ K.sum(y_pred_f)+ smooth)
 return 1.- score
def bce_dice_loss(y_true, y_pred):
return binary_crossentropy(y_true, y_pred)+ dice_loss(y_true, y_pred)
 def bce_logdice_loss(y_true, y_pred):
return binary_crossentropy(y_true, y_pred)- K.log(1.- dice_loss(y_true, y_pred))
def weighted_bce_loss(y_true, y_pred, weight):
epsilon = 1e-7
y_pred = K.clip(y_pred, epsilon, 1.- epsilon)
 logit_y_pred = K.log(y_pred /(1.- y_pred))
loss = weight *(logit_y_pred *(1.- y_true)+
K.log(1.+ K.exp(-K.abs(logit_y_pred)))+ K.maximum(-logit_y_pred, 0.))
 return K.sum(loss)/ K.sum(weight)
 def weighted_dice_loss(y_true, y_pred, weight):
smooth = 1.
 w, m1, m2 = weight, y_true, y_pred
intersection =(m1 * m2)
 score =(2.* K.sum(w * intersection)+ smooth)/(K.sum(w * m1)+ K.sum(w * m2)+ smooth)
 loss = 1.- K.sum(score)
 return loss
def weighted_bce_dice_loss(y_true, y_pred):
y_true = K.cast(y_true, 'float32')
 y_pred = K.cast(y_pred, 'float32')
 averaged_mask = K.pool2d(
 y_true, pool_size=(50, 50), strides=(1, 1), padding='same', pool_mode='avg')
 weight = K.ones_like(averaged_mask)
 w0 = K.sum(weight)
 weight = 5.* K.exp(-5.* K.abs(averaged_mask - 0.5))
w1 = K.sum(weight)
 weight *=(w0 / w1)
 loss = weighted_bce_loss(y_true, y_pred, weight)+ dice_loss(y_true, y_pred)
 return loss<define_search_model>
predictions=[]
for i in probabilities:
predictions.append(( i[1]))
def conv_block(m, dim, acti, bn, res, do=0):
	n = Conv2D(dim, 3, activation=acti, padding='same' )(m)
 	n = BatchNormalization()(n)if bn else n
	n = Dropout(do )(n)if do else n
	n = Conv2D(dim, 3, activation=acti, padding='same' )(n)
 	n = BatchNormalization()(n)if bn else n
	return Concatenate()([m, n])if res else n
def level_block(m, dim, depth, inc, acti, do, bn, mp, up, res):
	if depth > 0:
		n = conv_block(m, dim, acti, bn, res)
 		m = MaxPooling2D()(n)if mp else Conv2D(dim, 3, strides=2, padding='same' )(n)
 		m = level_block(m, int(inc*dim), depth-1, inc, acti, do, bn, mp, up, res)
 		if up:
			m = UpSampling2D()(m)
 			m = Conv2D(dim, 2, activation=acti, padding='same' )(m)
 		else:
			m = Conv2DTranspose(dim, 3, strides=2, activation=acti, padding='same' )(m)
 		n = Concatenate()([n, m])
 		m = conv_block(n, dim, acti, bn, res)
 	else:
		m = conv_block(m, dim, acti, bn, res, do)
 	return m
def UNet(img_shape, out_ch=1, start_ch=64, depth=4, inc_rate=2., activation='relu',
		 dropout=0.5, batchnorm=False, maxpool=True, upconv=True, residual=False):
	i = Input(shape=img_shape)
 	o = level_block(i, start_ch, depth, inc_rate, activation, dropout, batchnorm, maxpool, upconv, residual)
 	o = Conv2D(out_ch, 1, activation='sigmoid' )(o)
 	return Model(inputs=i, outputs=o )<train_model>
matchups['prediction']=predictions
img_size_target=128
model = UNet(( img_size_target,img_size_target,1),start_ch=16,depth=5,batchnorm=True )<choose_model_class>
teams_description=teams_description.iloc[:,:2]
model.compile(loss=bce_dice_loss, optimizer="adam", metrics=["accuracy",iouMetric] )<choose_model_class>
output=matchups.merge(teams_description, left_on=['OppTeamID'], right_on=['TeamID'])
 output=output.rename(columns={'TeamName':'OppTeamName'})
 output.drop(labels=['TeamID_y'], inplace=True, axis=1 )
early_stopping = EarlyStopping(patience=20, verbose=1)
 model_checkpoint = ModelCheckpoint("./keras.model", save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(factor=0.2, patience=10, min_lr=0.000005, verbose=1)
 def step_decay(epoch):
if epoch<=60:
return 0.01
elif epoch<=90:
return 0.005
elif epoch<=110:
return 0.001
else:
return 0.0005
epochs = 120
batch_size = 32
if debug:
epochs=3
gen = ImageDataGenerator()
def random_crop_keep_size(img,mask,crop_ratio):
size_x=img.shape[0]
size_y=img.shape[1]
size_x_=int(size_x*crop_ratio)
 size_y_=int(size_y*crop_ratio)
 x = random.randint(0, size_x - size_x_ + 1)
 y = random.randint(0, size_y - size_y_ + 1)
 img_crop=img[x:x+size_x_,y:y+size_y]
mask_crop=mask[x:x+size_x_,y:y+size_y]
img_crop=cv2.resize(img_crop,(size_x,size_y))
mask_crop=cv2.resize(mask_crop,(size_x,size_y))
return img_crop,mask_crop
def reflective_pad(img):
return np.pad(img,(( 13,14),(13,14)) ,'reflect')
 seq = iaa.Sequential([
iaa.Fliplr(0.5),
iaa.OneOf([
iaa.Crop(px=(0,5)) ,
iaa.Noop() ,
iaa.Noop() ,
iaa.Noop() ,
]),
])
 def gen_flow_for_two_inputs(X, y):
genX1 = gen.flow(X,y, batch_size=batch_size)
 while True:
X=genX1.next()
img_mask=np.concatenate([X[0],X[1]],axis=3)
 img_mask_aug=seq.augment_images(img_mask)
 yield np.expand_dims(img_mask_aug[:,:,:,0],axis=3),np.expand_dims(img_mask_aug[:,:,:,1],axis=3)
 print(x_valid.shape)
 gen_flow = gen_flow_for_two_inputs(x_train, y_train)
 history = model.fit_generator(gen_flow,
validation_data=[x_valid, y_valid],
epochs=epochs,
steps_per_epoch=len(x_train)/ batch_size,
callbacks=[early_stopping, model_checkpoint, reduce_lr] )<load_pretrained>
output=output.merge(teams_description, left_on=['TeamID_x'], right_on=['TeamID'] )
model = load_model("./keras.model",{"bce_dice_loss":bce_dice_loss,'iouMetric':iouMetric} )<predict_on_test>
output=output.rename(columns={'TeamID_x':'TeamID'} )
preds_valid = model.predict(x_valid ).reshape(-1, img_size_target, img_size_target)
 preds_valid = np.array([downsample(x)for x in preds_valid])
 y_valid_ori = np.array([train_df.loc[idx].masks for idx in ids_valid] )<compute_train_metric>
output=output[['TeamName','OppTeamName','prediction','Seed','OppSeed','elo','Oppelo']]
thresholds = np.linspace(0, 1, 50)
 ious = np.array([iou_metric_batch(y_valid_ori, np.int32(preds_valid > threshold)) for threshold in tqdm_notebook(thresholds)] )<find_best_params>
output.to_csv('descriptive_output.csv' )
threshold_best_index = np.argmax(ious[9:-10])+ 9
iou_best = ious[threshold_best_index]
threshold_best = thresholds[threshold_best_index]<categorify>
matchups.merge(teams_description, left_on=['TeamID'], right_on=['TeamID'] ).groupby('TeamName' ).mean().sort_values('prediction', ascending=False )
def RLenc(img, order='F', format=True):

 bytes = img.reshape(img.shape[0] * img.shape[1], order=order)
 runs = []
r = 0
pos = 1
for c in bytes:
if(c == 0):
if r != 0:
runs.append(( pos, r))
pos += r
r = 0
pos += 1
else:
r += 1
if r != 0:
runs.append(( pos, r))
pos += r
r = 0
if format:
z = ''
for rr in runs:
z += '{} {} '.format(rr[0], rr[1])
 return z[:-1]
else:
return runs<predict_on_test>
matchups_for_submission=matchups[matchups['TeamID']<matchups['OppTeamID']]
preds_test = model.predict(x_test )<compute_test_metric>
matchups_for_submission['ID'] = '2018_' + matchups_for_submission.TeamID.map(str)+ "_" + matchups_for_submission.OppTeamID.map(str)
 matchups_for_submission['pred']=matchups_for_submission['prediction']
pred_dict = {idx: RLenc(np.round(downsample(preds_test[i])> threshold_best)) for i, idx in enumerate(tqdm_notebook(test_df.index.values)) }<save_to_csv>
sample = pd.read_csv('.. /input/SampleSubmissionStage2.csv')
 del sample['Pred']
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv('submission.csv' )<set_options>
matchups_for_submission=matchups_for_submission[['ID','pred']]
<load_from_csv><EOS> 
matchups_for_submission.to_csv('submission.csv', index=None )