Kaggle competition: RSNA Intracranial Hemorrhage Detection
<SOS> metric: weightedmeancolumnwiselogloss Kaggle data source: rsna-intracranial-hemorrhage-detection<load_from_csv>
%matplotlib inline
def create_submission_file(model, X_test, save_name='model_preds'):
predictions = model.predict(X_test)
 print(f'Shape: {predictions.shape} - Min: {predictions.min() } - Max: {predictions.max() }')
 predictions[predictions > 96] = 96
lookid_data = pd.read_csv('/kaggle/input/facial-keypoints-detection/IdLookupTable.csv')
 image_id = list(lookid_data['ImageId']-1)
 landmark_names = list(lookid_data['FeatureName'])
 landmark_ids = [ landmark_names.index(f)for f in landmark_names ]
expected_preds = [ predictions[x,y] for x,y in zip(image_id, landmark_ids)]
rowid = pd.Series(lookid_data['RowId'], name = 'RowId')
 loc = pd.Series(expected_preds, name = 'Location')
 submission = pd.concat([rowid, loc], axis = 1)
 submission.to_csv(f'{save_name}.csv',index = False)
 print(f'Successfully created {save_name}.csv !' )<define_variables>
all_files = glob.glob(".. /input/rsna-outputs/*.csv")
 all_files
create_submission_file(model1, X_test, 'model_preds1')
 create_submission_file(model2, X_test, 'model_preds2')
 create_submission_file(model3, X_test, 'model_preds3')
 create_submission_file(model4, X_test, 'model_preds4' )<set_options>
all_files_addit = glob.glob(".. /input/rsna-public-not-checked0/*.csv")
 all_files_addit
%matplotlib inline 
<load_from_csv>
all_files_addit_manoj = glob.glob(".. /input/inceptionresnetv2manoj/*.csv")
 all_files_addit_manoj
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<train_model>
!cp.. /input/rsna-public-not-checked0/*.csv.. /input/alltogether/
!cp.. /input/inceptionresnetv2manoj/*.csv.. /input/alltogether/
!cp.. /input/rsna-outputs/*.csv.. /input/alltogether/
X_train=train.drop(labels = ["label"],axis = 1)
 Y_train=train['label']
print(X_train.shape)
 print(Y_train.shape )<count_values>
all_together = glob.glob(".. /input/alltogether/*.csv")
 all_together
Y_train.value_counts()<data_type_conversions>
outs = [pd.read_csv(f, index_col=0)for f in all_together]
concat_sub = reduce(lambda left,right: pd.merge(left,right,on='ID'), outs)
 cols = list(map(lambda x: f"{all_together[x].split('/')[3]}", range(len(concat_sub.columns))))
concat_sub.columns = cols
ncol = concat_sub.shape[1]
X_train=X_train.astype('float32')/255
test=test.astype('float32')/255<train_model>
concat_sub['m_median'] = concat_sub.iloc[:, 1:].median(axis=1)
 concat_sub['m_max'] = concat_sub.iloc[:, 1:].max(axis=1)
 concat_sub['m_min'] = concat_sub.iloc[:, 1:].min(axis=1 )
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )<categorify>
cutoff_lo = 0.8
cutoff_hi = 0.2
Y_train = to_categorical(Y_train, num_classes = 10 )<split>
rank = np.tril(corr.values,-1)
 rank[rank<0.92] = 1
m =(rank>0 ).sum() -(rank>0.97 ).sum()
m_gmean, s = 0, 0
for n in range(m):
mx = np.unravel_index(rank.argmin() , rank.shape)
 w =(m-n)/m
m_gmean += w*(np.log(concat_sub.iloc[:,mx[0]])+np.log(concat_sub.iloc[:,mx[1]])) /2
s += w
rank[mx] = 1
m_gmean = np.exp(m_gmean/s )
X_train, X_valid, Y_train, Y_valid = train_test_split(X_train, Y_train, test_size = 0.1, random_state=42 )<import_modules>
concat_sub['Label'] = m_gmean
concat_sub['m_mean'] = m_gmean
concat_sub['ID'] = concat_sub.index
concat_sub[['ID', 'Label']].to_csv('rsna_simple_gmean.csv', index=False )
from keras.layers import Input,InputLayer, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout
from keras.models import Sequential,Model
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint,LearningRateScheduler
import keras
from keras import backend as K<define_search_model>
concat_sub['Label'] = concat_sub['m_median']
concat_sub[['ID', 'Label']].to_csv('stack_median.csv',
index=False, float_format='%.6f' )
inputShape=(28,28,1)
 input = Input(inputShape)
 x = Conv2D(64,(3,3),strides =(1,1),name='layer_conv1',padding='same' )(input)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = MaxPooling2D(( 2,2),name='maxPool1' )(x)
 x = Conv2D(64,(3,3),strides =(1,1),name='layer_conv2',padding='same' )(x)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = MaxPooling2D(( 2,2),name='maxPool2' )(x)
 x = Conv2D(32,(3,3),strides =(1,1),name='conv3',padding='same' )(x)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = MaxPooling2D(( 2,2),name='maxPool3' )(x)
 x = Flatten()(x)
 x = Dense(64,activation = 'relu',name='fc0' )(x)
 x = Dropout(0.25 )(x)
 x = Dense(32,activation = 'relu',name='fc1' )(x)
 x = Dropout(0.25 )(x)
 x = Dense(10,activation = 'softmax',name='fc2' )(x)
 model = Model(inputs = input,outputs = x,name='Predict')
<choose_model_class>
concat_sub['Label'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
0, concat_sub['m_median']))
concat_sub[['ID', 'Label']].to_csv('stack_pushout_median.csv',
index=False, float_format='%.6f' )
momentum = 0.5
sgd = SGD(lr=0.01, momentum=momentum, decay=0.0, nesterov=False)
 model.compile(loss='categorical_crossentropy',optimizer=sgd, metrics=['accuracy'] )<normalization>
concat_sub['Label'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1),
concat_sub['m_max'],
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
concat_sub['m_min'],
concat_sub['m_mean']))
concat_sub[['ID', 'Label']].to_csv('stack_minmax_mean.csv',
index=False, float_format='%.6f' )
def step_decay(epoch):
initial_lrate=0.1
drop=0.6
epochs_drop = 3.0
lrate= initial_lrate * math.pow(drop,
math.floor(( 1+epoch)/epochs_drop))
return lrate
lrate = LearningRateScheduler(step_decay)
 callbacks_list = [ lrate] 
<train_model>
concat_sub['Label'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1),
concat_sub['m_max'],
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
concat_sub['m_min'],
concat_sub['m_median']))
concat_sub[['ID', 'Label']].to_csv('stack_minmax_median.csv',
index=False, float_format='%.6f' )
history=model.fit(X_train, Y_train, validation_data=(X_valid, Y_valid),
epochs=35,callbacks=callbacks_list,verbose=1 )<compute_test_metric>
ll = 6
df_suball = []
for i in range(0,ll):
df_suball.append(0)
 for i in range(0,ll):
df_suball[i] = pd.read_csv(glob.glob(".. /input/alltogether/*.csv")[i] )
print("on valid data")
 pred1=model.evaluate(X_valid,Y_valid)
 print("accuaracy", str(pred1[1]*100))
print("Total loss",str(pred1[0]*100))<concatenate>
df_suball[0].loc[df_suball[0]['ID'] == 'ID_000012eaf_any']
display_activation(activations, 8, 8, 1 )<concatenate>
df_suball[1].loc[df_suball[1]['ID'] == 'ID_000012eaf_any']
display_activation(activations, 8, 8, 7 )<compute_train_metric>
df_suball[2].loc[df_suball[2]['ID'] == 'ID_000012eaf_any']
Y_prediction = model.predict(X_valid)
 Y_pred_classes = np.argmax(Y_prediction,axis = 1)
 Y_true = np.argmax(Y_valid,axis = 1)
 confusion_mtx = confusion_matrix(Y_true, Y_pred_classes )<predict_on_test>
df_suball[3].loc[df_suball[3]['ID'] == 'ID_000012eaf_any']
results = model.predict(test)
 results = np.argmax(results,axis = 1)
<save_to_csv>
df_suball[4].loc[df_suball[4]['ID'] == 'ID_000012eaf_any']
submissions=pd.DataFrame({"ImageId": list(range(1,len(results)+1)) ,
"Label": results})
 submissions.to_csv("re2-submission.csv", index=False, header=True )<import_modules>
df_suball[5].loc[df_suball[5]['ID'] == 'ID_000012eaf_any']
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
import torch.nn as nn
import torch.optim as optim
import torchvision
from torch.autograd import Variable
import torch
from torchvision import transforms<categorify>
def to_np(v):

 if isinstance(v,(np.ndarray, np.generic)) : return v
if isinstance(v,(list,tuple)) : return [to_np(o)for o in v]
if isinstance(v, Variable): v=v.data
if torch.cuda.is_available() :
if isinstance(v, torch.cuda.HalfTensor): v=v.float()
if isinstance(v, torch.FloatTensor): v=v.float()
return v.cpu().numpy()
def score(model, x, y=None, ret=0):

 y_pred = model(x)
 if ret == 1:
return to_np(y_pred)
 elif ret == 2:
return to_np(y_pred ).argmax(1)
 else:
ypred_argmax = to_np(y_pred ).argmax(axis=1)
 return np.sum(ypred_argmax == to_np(y)) , y_pred
def accuracy(preds, targs):

 preds = torch.max(preds, dim=1)[1]
return(preds==targs ).float().mean()
def show(img, title=None):

 plt.imshow(img, cmap="gray")
 if title is not None: plt.title(title)
 def plots(ims, figsize=(12, 6), rows=2, titles=None):

 f = plt.figure(figsize=figsize)
 cols = len(ims)//rows
for i in range(len(ims)) :
sp = f.add_subplot(rows, cols, i+1)
 sp.axis('Off')
 if titles is not None: sp.set_title(titles[i], fontsize=16)
 plt.imshow(ims[i], cmap='gray' )<define_variables>
for i in range(1,ll):
df_suball[0]['Label'] += 0.15*df_suball[0]['Label'] + 0.15*df_suball[1]['Label'] + 0.20*df_suball[2]['Label'] + 0.15*df_suball[3]['Label']+ 0.20*df_suball[4]['Label'] + 0.15*df_suball[5]['Label']
df_suball[0]
PATH = ".. /input/"<load_from_csv>
df_suball[0].to_csv('submission_average.csv', index=False )