Kaggle competition: RSNA Intracranial Hemorrhage Detection
<SOS> metric: weightedmeancolumnwiselogloss Kaggle data source: rsna-intracranial-hemorrhage-detection<choose_model_class>
import os
import json
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pydicom
from keras import layers
from keras.applications import DenseNet121
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import Callback, ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
from keras.initializers import Constant
from keras.models import Sequential
from keras.optimizers import Adam
from tensorflow.python.ops import array_ops
from tqdm import tqdm
from keras import backend as K
import tensorflow as tf
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))<choose_model_class>
BASE_PATH = '/kaggle/input/rsna-intracranial-hemorrhage-detection/'
TRAIN_DIR = 'stage_1_train_images/'
TEST_DIR = 'stage_1_test_images/'
model.add(BatchNormalization() )<choose_model_class>
train_df = pd.read_csv(BASE_PATH + 'stage_1_train.csv')
 sub_df = pd.read_csv(BASE_PATH + 'stage_1_sample_submission.csv')
 train_df['filename'] = train_df['ID'].apply(lambda st: "ID_" + st.split('_')[1] + ".png")
 train_df['type'] = train_df['ID'].apply(lambda st: st.split('_')[2])
 sub_df['filename'] = sub_df['ID'].apply(lambda st: "ID_" + st.split('_')[1] + ".png")
 sub_df['type'] = sub_df['ID'].apply(lambda st: st.split('_')[2])
 print(train_df.shape)
 train_df.head()
model.add(Dense(10, activation = "softmax"))<choose_model_class>
test_df = pd.DataFrame(sub_df.filename.unique() , columns=['filename'])
 print(test_df.shape)
 test_df.head()
my_optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )<choose_model_class>
np.random.seed(1749)
 sample_files = np.random.choice(os.listdir(BASE_PATH + TRAIN_DIR), 4000)
 sample_df = train_df[train_df.filename.apply(lambda x: x.replace('.png', '.dcm')).isin(sample_files)]
model.compile(optimizer=my_optimizer,
loss='categorical_crossentropy',
metrics=['accuracy'] )<train_model>
pivot_df = sample_df[['Label', 'filename', 'type']].drop_duplicates().pivot(
 index='filename', columns='type', values='Label' ).reset_index()
print(pivot_df.shape)
 pivot_df.head()
datagen = ImageDataGenerator(rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1,
fill_mode='nearest')
 datagen.fit(X_train )<choose_model_class>
def window_image(img, window_center,window_width, intercept, slope, rescale=True):
img =(img*slope +intercept)
 img_min = window_center - window_width//2
img_max = window_center + window_width//2
img[img<img_min] = img_min
img[img>img_max] = img_max
if rescale:
img =(img - img_min)/(img_max - img_min)
 return img
def get_first_of_dicom_field_as_int(x):
if type(x)== pydicom.multival.MultiValue:
return int(x[0])
 else:
return int(x)
 def get_windowing(data):
dicom_fields = [data[('0028','1050')].value,
data[('0028','1051')].value,
data[('0028','1052')].value,
data[('0028','1053')].value]
return [get_first_of_dicom_field_as_int(x)for x in dicom_fields]
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<train_model>
save_and_resize(filenames=sample_files, load_dir=BASE_PATH + TRAIN_DIR)
 save_and_resize(filenames=os.listdir(BASE_PATH + TEST_DIR), load_dir=BASE_PATH + TEST_DIR )
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=86),
epochs=100, validation_data =(X_val, y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // 86,
callbacks=[learning_rate_reduction] )<compute_test_metric>
BATCH_SIZE = 64
def create_datagen() :
return ImageDataGenerator(validation_split=0.15)
 def create_test_gen() :
return ImageDataGenerator().flow_from_dataframe(
 test_df,
directory='/kaggle/tmp/',
x_col='filename',
class_mode=None,
target_size=(224, 224),
batch_size=BATCH_SIZE,
shuffle=False 
)
 def create_flow(datagen, subset):
return datagen.flow_from_dataframe(
 pivot_df,
directory='/kaggle/tmp/',
x_col='filename',
y_col=['any', 'epidural', 'intraparenchymal',
'intraventricular', 'subarachnoid', 'subdural'],
class_mode='multi_output',
target_size=(224, 224),
batch_size=BATCH_SIZE,
subset=subset 
)
 data_generator = create_datagen()
train_gen = create_flow(data_generator, 'training')
 val_gen = create_flow(data_generator, 'validation')
 test_gen = create_test_gen()
test_loss, test_acc = model.evaluate(X_val, y_val)
 test_acc<predict_on_test>
def focal_loss(prediction_tensor, target_tensor, weights=None, alpha=0.25, gamma=2):
r
sigmoid_p = tf.nn.sigmoid(prediction_tensor)
 zeros = array_ops.zeros_like(sigmoid_p, dtype=sigmoid_p.dtype)
 pos_p_sub = array_ops.where(target_tensor > zeros, target_tensor - sigmoid_p, zeros)
 neg_p_sub = array_ops.where(target_tensor > zeros, zeros, sigmoid_p)
 per_entry_cross_ent = - alpha *(pos_p_sub ** gamma)* tf.log(tf.clip_by_value(sigmoid_p, 1e-8, 1.0)) \
-(1 - alpha)*(neg_p_sub ** gamma)* tf.log(tf.clip_by_value(1.0 - sigmoid_p, 1e-8, 1.0))
return tf.reduce_sum(per_entry_cross_ent )
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
densenet = DenseNet121(
 weights='.. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5',
include_top=False,
input_shape=(224,224,3)
 )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )<import_modules>
def build_model() :
model = Sequential()
model.add(densenet)
 model.add(layers.GlobalAveragePooling2D())
 model.add(layers.Dropout(0.5))
model.add(layers.Dense(6, activation='sigmoid'))
model.compile(
 loss='categorical_crossentropy',
optimizer=Adam(lr=0.001),
metrics=['accuracy'] 
)
 return model
import csv
import re
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.python.framework import ops
from sklearn import preprocessing
from sklearn.model_selection import train_test_split<load_from_csv>
checkpoint = ModelCheckpoint(
 'model.h5',
monitor='val_loss',
verbose=0,
save_best_only=True,
save_weights_only=False,
mode='auto' 
)
 total_steps = sample_files.shape[0] / BATCH_SIZE
history = model.fit_generator(
 train_gen,
steps_per_epoch=2000,
validation_data=val_gen,
validation_steps=total_steps * 0.15,
callbacks=[checkpoint],
epochs=5
)
def load_data(train_path, test_path):

 train_data = pd.read_csv(train_path)
 test_data = pd.read_csv(test_path)
 print("number of training examples = " + str(train_data.shape[0]))
print("number of test examples = " + str(test_data.shape[0]))
print("train shape: " + str(train_data.shape))
print("test shape: " + str(test_data.shape))
return train_data, test_data
def pre_process_data(df):

 df = df.set_index("PassengerId")
 df['gender'] = df['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 df.loc[df.Embarked.isnull() , 'Embarked'] = df['Embarked'].mode() [0]
for gender in df['gender'].unique() :
median_age = df[(df['gender'] == gender)].Age.median()
df.loc[(df['Age'].isnull())&(df['gender'] == gender), 'Age'] = median_age
df = pd.get_dummies(df, columns=['gender'])
 df = pd.get_dummies(df, columns=['Embarked'])
 bins = [0, 4, 12, 18, 30, 50, 65, 100]
age_index =(1, 2, 3, 4, 5, 6, 7)
 df['Age-bin'] = pd.cut(df['Age'], bins, labels=age_index ).astype(int)
 df['family'] = df['SibSp'] + df['Parch'] + 1
df['family'] = df['family'].map(lambda x: 4 if x > 4 else x)
 df['FTicket'] = df['Ticket'].map(lambda x: x[0])
 df['FTicket'] = df['FTicket'].replace(['W', 'F', 'L', '5', '6', '7', '8', '9'], '4')
 df = pd.get_dummies(df, columns=['FTicket'])
 df['title'] = df.apply(lambda row: re.split('[,.]+', row['Name'])[1], axis=1)
 df['title'] = df['title'].map({' Capt': 'Other', ' Master': 'Master', ' Mr': 'Mr', ' Don': 'Other',
' Dona': 'Other', ' Lady': 'Other', ' Col': 'Other', ' Miss': 'Miss',
' the Countess': 'Other', ' Dr': 'Other', ' Jonkheer': 'Other', ' Mlle': 'Other',
' Sir': 'Other', ' Rev': 'Other', ' Ms': 'Other', ' Mme': 'Other', ' Major': 'Other',
' Mrs': 'Mrs'})
 df = pd.get_dummies(df, columns=['title'])
 df = df.drop(['Name', 'Ticket', 'Cabin', 'Sex', 'Fare', 'Age'], axis=1)
 return df
def mini_batches(train_set, train_labels, mini_batch_size):

 set_size = train_set.shape[0]
batches = []
num_complete_minibatches = set_size // mini_batch_size
for k in range(0, num_complete_minibatches):
mini_batch_x = train_set[k * mini_batch_size:(k + 1)* mini_batch_size]
mini_batch_y = train_labels[k * mini_batch_size:(k + 1)* mini_batch_size]
mini_batch =(mini_batch_x, mini_batch_y)
 batches.append(mini_batch)
 if set_size % mini_batch_size != 0:
mini_batch_x = train_set[(set_size -(set_size % mini_batch_size)) :]
mini_batch_y = train_labels[(set_size -(set_size % mini_batch_size)) :]
mini_batch =(mini_batch_x, mini_batch_y)
 batches.append(mini_batch)
 return batches<categorify>
model.load_weights('model.h5')
 y_test = model.predict_generator(
 test_gen,
steps=len(test_gen),
verbose=1
)
def create_placeholders(input_size, output_size):

 x = tf.placeholder(shape=(None, input_size), dtype=tf.float32, name="X")
 y = tf.placeholder(shape=(None, output_size), dtype=tf.float32, name="Y")
 return x, y
def forward_propagation(x, parameters, keep_prob=1.0, hidden_activation='relu'):

 a_dropout = x
n_layers = len(parameters)// 2
for l in range(1, n_layers):
a_prev = a_dropout
a_dropout = linear_activation_forward(a_prev, parameters['w%s' % l], parameters['b%s' % l], hidden_activation)
 if keep_prob < 1.0:
a_dropout = tf.nn.dropout(a_dropout, keep_prob)
 al = tf.matmul(a_dropout, parameters['w%s' % n_layers])+ parameters['b%s' % n_layers]
return al
def linear_activation_forward(a_prev, w, b, activation):

 a = None
if activation == "sigmoid":
z = tf.matmul(a_prev, w)+ b
a = tf.nn.sigmoid(z)
 elif activation == "relu":
z = tf.matmul(a_prev, w)+ b
a = tf.nn.relu(z)
 elif activation == "leaky relu":
z = tf.matmul(a_prev, w)+ b
a = tf.nn.leaky_relu(z)
 return a
def initialize_parameters(layer_dims):

 parameters = {}
n_layers = len(layer_dims)
 for l in range(1, n_layers):
parameters['w' + str(l)] = tf.get_variable('w' + str(l), [layer_dims[l - 1], layer_dims[l]],
initializer=tf.contrib.layers.xavier_initializer())
 parameters['b' + str(l)] = tf.get_variable('b' + str(l), [layer_dims[l]], initializer=tf.zeros_initializer())
 return parameters
def compute_cost(z3, y):

 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=z3, labels=y))
return cost
def predict(data, parameters):

 init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
 dataset = tf.cast(tf.constant(data), tf.float32)
 fw_prop_result = forward_propagation(dataset, parameters)
 fw_prop_activation = tf.nn.softmax(fw_prop_result)
 prediction = fw_prop_activation.eval()
return prediction
def accuracy(predictions, labels):

 prediction_size = predictions.shape[0]
prediction_accuracy = np.sum(np.argmax(predictions, 1)== np.argmax(labels, 1)) / prediction_size
return 100 * prediction_accuracy
def minibatch_accuracy(predictions, labels):

 prediction_size = predictions.shape[0]
prediction_accuracy = np.sum(np.argmax(predictions, 1)== np.argmax(labels, 1)) / prediction_size
return 100 * prediction_accuracy
def l2_regularizer(cost, l2_beta, parameters, n_layers):

 regularizer = 0
for i in range(1, n_layers):
regularizer += tf.nn.l2_loss(parameters['w%s' % i])
 cost = tf.reduce_mean(cost + l2_beta * regularizer)
 return cost
def build_submission_name(layers_dims, num_epochs, lr_decay,
learning_rate, l2_beta, keep_prob, minibatch_size, num_examples):

 submission_name = 'ly{}-epoch{}.csv' \ 
.format(layers_dims, num_epochs)
 if lr_decay != 0:
submission_name = 'lrdc{}-'.format(lr_decay)+ submission_name
else:
submission_name = 'lr{}-'.format(learning_rate)+ submission_name
if l2_beta > 0:
submission_name = 'l2{}-'.format(l2_beta)+ submission_name
if keep_prob < 1:
submission_name = 'dk{}-'.format(keep_prob)+ submission_name
if minibatch_size != num_examples:
submission_name = 'mb{}-'.format(minibatch_size)+ submission_name
return submission_name
def plot_model_cost(train_costs, validation_costs, submission_name):

 plt.plot(np.squeeze(train_costs), label='Train cost')
 plt.plot(np.squeeze(validation_costs), label='Validation cost')
 plt.ylabel('cost')
 plt.xlabel('iterations(per tens)')
 plt.title("Model: " + submission_name)
 plt.legend()
plt.show()
def plot_model_accuracy(train_accuracies, validation_accuracies, submission_name):

 plt.plot(np.squeeze(train_accuracies), label='Train accuracy')
 plt.plot(np.squeeze(validation_accuracies), label='Validation accuracy')
 plt.ylabel('accuracy')
 plt.xlabel('iterations(per tens)')
 plt.title("Model: " + submission_name)
 plt.legend()
plt.show()<train_model>
test_df = test_df.join(pd.DataFrame(y_test, columns=[
'any', 'epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural'
]))
test_df = test_df.melt(id_vars=['filename'])
 test_df['ID'] = test_df.filename.apply(lambda x: x.replace('.png', '')) + '_' + test_df.variable
test_df['Label'] = test_df['value']
test_df[['ID', 'Label']].to_csv('submission.csv', index=False )