Kaggle competition: RSNA Intracranial Hemorrhage Detection
def build_model(learning_rate=0.00065):
optimizer = keras.optimizers.RMSprop(lr=learning_rate, decay=learning_rate/2)
 model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'] )<choose_model_class>
plateau_reduce = ReduceLROnPlateau(monitor='val_acc', factor=0.5,
patience=6, min_lr=0.0000001 )<define_variables>
Compose,
HorizontalFlip, ShiftScaleRotate,VerticalFlip,
RandomBrightness,RandomCrop,RandomContrast 
)
 test_images_dir = '.. /input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_test/'
train_images_dir = '.. /input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/'
use_generator = True<define_variables>
def _read(path, desired_size):

 dcm = pydicom.dcmread(path)
 try:
img = bsb_window(dcm,desired_size)
 except:
img = np.zeros(desired_size)
 return img
plt.imshow(
 _read(train_images_dir+'ID_5c8b5d701'+'.dcm',(256, 256)) , cmap=plt.cm.bone 
);
callbacks = [plotter]<train_model>
class DataGenerator(keras.utils.Sequence):
def __init__(self, list_IDs, labels=None, batch_size=1, img_size=(512, 512, 1),
img_dir=train_images_dir,augment=None, *args, **kwargs):
self.list_IDs = list_IDs
self.labels = labels
self.batch_size = batch_size
self.img_size = img_size
self.img_dir = img_dir
self.on_epoch_end()
self.augment = augment
def __len__(self):
return int(ceil(len(self.indices)/ self.batch_size))
def __getitem__(self, index):
indices = self.indices[index*self.batch_size:(index+1)*self.batch_size]
list_IDs_temp = [self.list_IDs[k] for k in indices]
if self.labels is not None:
X, Y = self.__data_generation(list_IDs_temp)
 return X, Y
else:
X = self.__data_generation(list_IDs_temp)
 return X
def on_epoch_end(self):
if self.labels is not None:
keep_prob = self.labels.iloc[:, 0].map({0: 0.3, 1: 1})
 keep =(keep_prob > np.random.rand(len(keep_prob)))
 self.indices = np.arange(len(self.list_IDs)) [keep]
np.random.shuffle(self.indices)
 else:
self.indices = np.arange(len(self.list_IDs))
def __data_generation(self, list_IDs_temp):
X = np.empty(( self.batch_size, *self.img_size))
if self.labels is not None:
Y = np.empty(( self.batch_size, 6), dtype=np.float32)
 for i, ID in enumerate(list_IDs_temp):
X[i,] = _read(self.img_dir+ID+".dcm", self.img_size)
 Y[i,] = self.labels.loc[ID].values
if self.augment:
X = self.__augment(X)
 return X, Y
else:
for i, ID in enumerate(list_IDs_temp):
X[i,] = _read(self.img_dir+ID+".dcm", self.img_size)
 return X
def __random_transform(self,img):
composition = Compose([
HorizontalFlip() ,
VerticalFlip() ,
ShiftScaleRotate(rotate_limit=45, shift_limit=0.15, scale_limit=0.15),
RandomBrightness() ,
RandomContrast()
])
 composed = composition(image=img)
 aug_img = composed['image']
return aug_img
def __augment(self,img_batch):
for i in range(img_batch.shape[0]):
img_batch[i, ] = self.__random_transform(img_batch[i, ])
 return img_batch
if use_generator:
model.fit_generator(train_gen, validation_data=valid_gen, epochs=120,
steps_per_epoch=train_x.shape[0]*0.85//96,
validation_steps=train_x.shape[0]*0.15//96, callbacks=callbacks)
 else:
model.fit(x=train_x, y=train_y, epochs=80, batch_size=32, callbacks=callbacks,
validation_split=0.15 )<save_model>
def weighted_log_loss(y_true, y_pred):

 class_weights = np.array([2., 1., 1., 1., 1., 1.])
 eps = K.epsilon()
y_pred = K.clip(y_pred, eps, 1.0-eps)
 out = -(y_true * K.log(y_pred)* class_weights
+(1.0 - y_true)* K.log(1.0 - y_pred)* class_weights)
 return K.mean(out, axis=-1)
 def _normalized_weighted_average(arr, weights=None):

 if weights is not None:
scl = K.sum(weights)
 weights = K.expand_dims(weights, axis=1)
 return K.sum(K.dot(arr, weights), axis=1)/ scl
return K.mean(arr, axis=1)
 def weighted_loss(y_true, y_pred):

 class_weights = K.variable([2., 1., 1., 1., 1., 1.])
 eps = K.epsilon()
y_pred = K.clip(y_pred, eps, 1.0-eps)
 loss = -(y_true * K.log(y_pred)
 +(1.0 - y_true)* K.log(1.0 - y_pred))
loss_samples = _normalized_weighted_average(loss, class_weights)
 return K.mean(loss_samples)
 def weighted_log_loss_metric(trues, preds):

 class_weights = [2., 1., 1., 1., 1., 1.]
epsilon = 1e-7
preds = np.clip(preds, epsilon, 1-epsilon)
 loss = trues * np.log(preds)+(1 - trues)* np.log(1 - preds)
 loss_samples = np.average(loss, axis=1, weights=class_weights)
 return - loss_samples.mean()
def f1_score(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 recall = true_positives /(possible_positives + K.epsilon())
 return(2*precision*recall)/(precision+recall+ K.epsilon())
keras.models.save_model(model, f'model_{time.time() }.h5' )<compute_test_metric>
class PredictionCheckpoint(keras.callbacks.Callback):
def __init__(self, test_df, valid_df,
test_images_dir=test_images_dir,
valid_images_dir=train_images_dir,
batch_size=32, input_size=(224, 224, 3)) :
self.test_df = test_df
self.valid_df = valid_df
self.test_images_dir = test_images_dir
self.valid_images_dir = valid_images_dir
self.batch_size = batch_size
self.input_size = input_size
def on_train_begin(self, logs={}):
self.test_predictions = []
self.valid_predictions = []
def on_epoch_end(self,batch, logs={}):
self.test_predictions.append(
 self.model.predict_generator(
 DataGenerator(self.test_df.index, None, self.batch_size, self.input_size, self.test_images_dir), verbose=2)[:len(self.test_df)])
 class MyDeepModel:
def __init__(self, engine, input_dims, batch_size=5, num_epochs=4, learning_rate=1e-3,
decay_rate=1.0, decay_steps=1, weights="imagenet", verbose=1):
self.engine = engine
self.input_dims = input_dims
self.batch_size = batch_size
self.num_epochs = num_epochs
self.learning_rate = learning_rate
self.decay_rate = decay_rate
self.decay_steps = decay_steps
self.weights = weights
self.verbose = verbose
self._build()
def _build(self):
engine = self.engine(include_top=False, weights=self.weights, input_shape=self.input_dims,
backend = keras.backend, layers = keras.layers,
models = keras.models, utils = keras.utils)
 x = keras.layers.GlobalAveragePooling2D(name='avg_pool' )(engine.output)
 x = keras.layers.Dropout(0.3 )(x)
 out = keras.layers.Dense(6, activation="sigmoid", name='dense_output' )(x)
 self.model = keras.models.Model(inputs=engine.input, outputs=out)
 self.model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Nadam() , metrics=[weighted_loss,f1_score])
 def fit_and_predict(self, train_df, valid_df, test_df):
pred_history = PredictionCheckpoint(test_df, valid_df, input_size=self.input_dims)
 scheduler = keras.callbacks.LearningRateScheduler(lambda epoch: self.learning_rate * pow(self.decay_rate, floor(epoch / self.decay_steps)))
 history = self.model.fit_generator(
 DataGenerator(
 train_df.index,
train_df,
self.batch_size,
self.input_dims,
train_images_dir,
augment=None 
),
epochs=self.num_epochs,
verbose=self.verbose,
use_multiprocessing=True,
workers=4,
callbacks=[scheduler] 
)
 return history
def save(self, path):
self.model.save_weights(path)
 def load(self, path):
self.model.load_weights(path )
def calculate_performance(labels, pred, dataset):
pred_cat = np.argmax(pred, axis=1)
 labels_cat = np.argmax(labels, axis=1)
 corrects =(pred_cat == labels_cat)
 falses = dataset[~corrects]
falses_labels = labels_cat[~corrects]
falses_preds = pred[~corrects]
examples_num = labels.shape[0]
accuracy = np.count_nonzero(corrects)/ examples_num
return accuracy, [falses, falses_labels, falses_preds], [labels_cat, pred_cat]<predict_on_test>
def read_testset(filename=".. /input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_sample_submission.csv"):
df = pd.read_csv(filename)
 df["Image"] = df["ID"].str.slice(stop=12)
 df["Diagnosis"] = df["ID"].str.slice(start=13)
 df = df.loc[:, ["Label", "Diagnosis", "Image"]]
df = df.set_index(['Image', 'Diagnosis'] ).unstack(level=-1)
 return df
def read_trainset(filename=".. /input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train.csv"):
df = pd.read_csv(filename)
 df["Image"] = df["ID"].str.slice(stop=12)
 df["Diagnosis"] = df["ID"].str.slice(start=13)

df = df[df.duplicated() ==False]
df = df.reset_index(drop=True)
 df = df.loc[:, ["Label", "Diagnosis", "Image"]]
df = df.set_index(['Image', 'Diagnosis'] ).unstack(level=-1)
 return df
test_df = read_testset()
df = read_trainset()
random_idx = np.random.choice(range(test_y.shape[0]), size=10)
 examples = test_x[random_idx]
preds = model.predict(examples)
 labels = np.argmax(test_y[random_idx], axis=1)
 preds_cat = np.argmax(preds, axis=1 )<predict_on_test>
1/K.epsilon()
random_idx = np.random.choice(range(test_y.shape[0]), size=10)
 examples = unlabelled_test_x[random_idx]
preds = model.predict(examples)
 preds_cat = np.argmax(preds, axis=1 )<prepare_output>
ss = ShuffleSplit(n_splits=10, test_size=0.1, random_state=42 ).split(df.index)
 train_idx, valid_idx = next(ss)
 model = MyDeepModel(engine=DenseNet121, input_dims=(224, 224, 3), batch_size=32, learning_rate=1e-3,
num_epochs=5, decay_rate=0.8, decay_steps=1, weights="imagenet", verbose=1)
 history = model.fit_and_predict(df.iloc[train_idx], df.iloc[valid_idx], test_df)
falses_data = test_falses_data
falses_examples, falses_labels, falses_preds = falses_data
falses_idx = np.argmax(falses_preds, axis=1 )<define_variables>
keep_prob = df.iloc[train_idx].iloc[:, 0].map({0: 0.3, 1: 1})
 keep =(keep_prob > np.random.rand(len(keep_prob)) )
random_falses = np.random.choice(range(falses_labels.size), size=np.min(( 10, falses_labels.size)) , replace=False)
 examples = falses_examples[random_falses]
preds_probs = falses_preds[random_falses]
labels = falses_labels[random_falses]
preds_binary = np.argmax(preds_probs, axis=1 )<predict_on_test>
df.iloc[train_idx][keep]['Label']['any'].value_counts()
<save_to_csv><EOS> 
prediction = model.model.predict_generator(DataGenerator(test_df.index, None, 32,(224, 224, 3), test_images_dir), verbose=1)
 test_df_2 = test_df.copy()
test_df.iloc[:, :] = prediction[:len(test_df)]
test_df = test_df.stack().reset_index()
test_df.insert(loc=0, column='ID', value=test_df['Image'].astype(str)+ "_" + test_df['Diagnosis'])
 test_df = test_df.drop(["Image", "Diagnosis"], axis=1)
 test_df.to_csv('submission.csv', index=False)