Kaggle competition: RSNA Intracranial Hemorrhage Detection
<SOS> metric: weightedmeancolumnwiselogloss Kaggle data source: rsna-intracranial-hemorrhage-detection<split>
pip install git+https://github.com/fastai/fastai_dev
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42 )<choose_model_class>
dir_csv = '.. /input/rsna-intracranial-hemorrhage-detection'
dir_train_img = '.. /input/rsna-train-stage-1-images-png-224x/stage_1_train_png_224x'
dir_test_img = '.. /input/rsna-test-stage-1-images-png-224x/stage_1_test_png_224x'
model = Sequential([
Conv2D(32,(5, 5), padding='Same', activation='relu', input_shape=(28, 28, 1)) ,
Conv2D(32,(5, 5), padding='Same', activation='relu'),
MaxPooling2D(( 2, 2)) ,
Dropout(0.25),
Conv2D(64,(3, 3), padding='Same', activation='relu'),
Conv2D(64,(3, 3), padding='Same', activation='relu'),
MaxPooling2D(( 2, 2)) ,
Dropout(0.25),
Flatten() ,
Dense(512, activation='relu'),
Dropout(0.5),
Dense(10, activation='softmax')
 ] )<choose_model_class>
from fastai2.torch_basics import *
from fastai2.test import *
from fastai2.layers import *
from fastai2.data.all import *
from fastai2.optimizer import *
from fastai2.learner import *
from fastai2.metrics import *
from fastai2.vision.all import *
from fastai2.vision.learner import *
from fastai2.vision.models import *
from fastai2.callback.all import *
model.compile(optimizer=Adam(lr=0.001),
loss='categorical_crossentropy',
metrics=['accuracy'] )<choose_model_class>
items = get_image_files(dir_train_img)
 items = [i for i in items if '(copy)' not in i.name]
reduce_lr = ReduceLROnPlateau(monitor='val_acc',
factor=0.5,
patience=3,
verbose=1,
min_lr=0.00001 )<choose_model_class>
!mkdir -p data
df_train = pd.read_csv(f'{dir_csv}/stage_1_train.csv')
 df_train['fn'] = df_train.ID.apply(lambda x: '_'.join(x.split('_')[:2])+ '.png')
 df_train.columns = ['ID', 'probability', 'fn']
df_train['label'] = df_train.ID.apply(lambda x: x.split('_')[-1])
 df_train.drop_duplicates('ID', inplace=True)
 pivot = df_train.pivot(index='fn', columns='label', values='probability')
 pivot.reset_index(inplace=True)
 pivot.to_csv('data/train_pivot.csv', index=False)
 d = defaultdict(list)
 for fn in df_train.fn.unique() : d[fn]
for tup in df_train.itertuples() :
if tup.probability: d[tup.fn].append(tup.label)
 ks, vs = [], []
for k, v in d.items() :
ks.append(k), vs.append(' '.join(v))
pd.DataFrame(data={'fn': ks, 'labels': vs} ).to_csv('data/train_labels_as_strings.csv', index=False )
datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
shear_range=0.3 )<train_model>
class Labeller() :

 def __init__(self):
self.df = pd.read_csv('data/train_labels_as_strings.csv')
 self.df.set_index('fn', inplace=True)
 def __call__(self, path):
fn = path.name
labels_txt = self.df.loc[fn].labels
if isinstance(labels_txt, float)or labels_txt == ' ': return []
return labels_txt.split(' ' )
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=128),
epochs=50,
validation_data=datagen.flow(X_val, y_val, batch_size=128),
verbose=1,
steps_per_epoch=X_train.shape[0] // 64,
callbacks=[reduce_lr] )<save_to_csv>
labeler = Labeller()
predictions = model.predict_classes(X_test, verbose=0)
 submissions = pd.DataFrame({"ImageId" : list(range(1, len(predictions)+1)) , "Label" : predictions})
 submissions.to_csv("digit_recognizer.csv", index=False, header=True )<import_modules>
classes = L(pd.read_csv('data/train_pivot.csv' ).columns.tolist() [1:])
 classes
import numpy as np
import pandas as pd
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense,MaxPooling2D,Flatten,Dropout,MaxPool2D
from keras.layers.convolutional import Conv2D
from keras import backend
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import SGD
from keras.optimizers import RMSprop
from keras.losses import categorical_crossentropy
from keras.callbacks import ReduceLROnPlateau
from seaborn import countplot<define_variables>
mcat = MultiCategorize(vocab=classes)
 mcat.o2i = classes.val2idx()
input_neurons=784
output_neurons=10<define_variables>
tfms = [PILImage.create, [Labeller() , mcat, OneHotEncode(mcat.vocab)]]
ds_img_tfms = [ToTensor() ]
dsrc = DataSource(items, tfms, splits=RandomSplitter()(items))
np.random.seed(2 )<load_from_csv>
test_paths = get_image_files(dir_test_img)
 test_tfms = [PILImage.create, [lambda x: np.array([0,0,0,0,0,0])]]
dsrc_test = DataSource([test_paths[0]] + test_paths, test_tfms, splits=[[0], L(range(len(test_paths)) ).map(lambda x: x + 1)] )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
ds_img_tfms = [ToTensor() ]
dl_tfms = [Cuda() , ByteToFloatTensor() , Normalize(*dataset_stats)]
dbch = dsrc.databunch(after_item=ds_img_tfms, after_batch=dl_tfms, bs=128, num_workers=4)
 dbch_test = dsrc_test.databunch(after_item=ds_img_tfms, after_batch=dl_tfms, bs=128, num_workers=4 )
xtrain_df=train_df.drop(['label'], axis=1)
 ytrain_df=train_df.label
xtest_df = test_df<drop_column>
dbch.show_batch(max_n=9 )
del train_df
del test_df<count_values>
model = create_cnn_model(resnet18, 6, -2 )
ytrain_df.value_counts()<concatenate>
model_segments = model[0][:6], model[0][6:], model[1]
ytrain_df = to_categorical(ytrain_df, output_neurons )<data_type_conversions>
def trainable_params_mod(model): return L(trainable_params(segment)for segment in model_segments )
def normalize(df):
df.astype(np.float32)
 df=df/255.0
return df
xtrain_df=normalize(xtrain_df)
 xtest_df=normalize(xtest_df )<choose_model_class>
opt_func = partial(Adam, wd=0.01, eps=1e-3 )
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(output_neurons, activation = "softmax"))<choose_model_class>
learn = Learner(
 dbch,
model,
loss_func=BCEWithLogitsLossFlat() ,
metrics=[accuracy_multi],
opt_func=opt_func,
splitter=trainable_params_mod
)
learning_rate_reduction = ReduceLROnPlateau(monitor='acc',patience=3,verbose=1,factor=0.5, min_lr=0.00001 )<choose_model_class>
learn.freeze_to(-1 )
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
<choose_model_class>
learn.lr_find(start_lr=1e-6, end_lr=1 )
model.compile(
 loss=categorical_crossentropy,
optimizer=optimizer,
metrics=['accuracy']
)<init_hyperparams>
learn.fit_one_cycle(1, 2e-2 )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=13,
zoom_range = 0.13,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(xtrain_df )<train_model>
learn.save('phase-1' )
model.fit_generator(
 datagen.flow(xtrain_df, ytrain_df, batch_size=86),
steps_per_epoch=len(xtrain_df)// 86,
epochs=50,
callbacks=[learning_rate_reduction]
)<compute_test_metric>
learn.load('phase-1');
scores = model.evaluate(xtrain_df, ytrain_df)
 print("
%s: %.2f%%" %(model.metrics_names[1], scores[1]*100))<save_to_csv>
learn.freeze_to(-2 )
predictions = model.predict(xtest_df,verbose=0)
 results=np.argmax(predictions,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("digit_recognizer_datagen.csv",index=False)
 backend.clear_session()<import_modules>
learn.lr_find(start_lr=1e-8, end_lr=1e-1 )
import numpy as np
import pandas as pd
import keras
from keras import Model
from keras.layers import Dense, Conv2D, pooling, Activation, BatchNormalization, Input, Flatten, Dropout, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, f1_score
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import clear_output
import time<load_from_csv>
learn.fit_one_cycle(1, [1e-3, 1e-4, 1e-5] )
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv' )<categorify>
learn.save('phase-2' )
dataset_x = np.array(train_data.iloc[:, 1:] ).reshape(42000, 28, 28, 1)
 dataset_y = np.array(train_data.iloc[:, 0] ).ravel()
dataset_one_hot = keras.utils.to_categorical(dataset_y, num_classes=10)
 unlabelled_test_x = np.array(test_data.iloc[:, :] ).reshape(28000, 28, 28, 1)
 assert(dataset_x.shape[0] == dataset_y.size )<feature_engineering>
learn.load('phase-2');
dataset_x = dataset_x / 255.0
unlabelled_test_x = unlabelled_test_x / 255.0<split>
learn.fit_one_cycle(1, np.array([1e-3, 1e-4, 1e-5]))
train_x, test_x, train_y, test_y = train_test_split(dataset_x, dataset_one_hot, test_size=0.1 )<init_hyperparams>
learn.save('phase-3' )
transform_params = {
'featurewise_center': False,
'featurewise_std_normalization': False,
'samplewise_center': False,
'samplewise_std_normalization': False,
'rotation_range': 10,
'width_shift_range': 0.1,
'height_shift_range': 0.1,
'zoom_range': 0.1,
'validation_split': 0.15
}
img_gen = ImageDataGenerator(**transform_params )<define_variables>
learn.load('phase-3' )
visualizaion_flow = img_gen.flow(train_x, train_y, batch_size=1, shuffle=False)
 train_gen = img_gen.flow(x=train_x, y=train_y, subset='training', batch_size=96)
 valid_gen = img_gen.flow(x=train_x, y=train_y, subset='validation', batch_size=96 )<choose_model_class>
learn.dbunch = dbch_test
def conv_block(x, filters, kernel_size, strides, layer_no, use_pool=False, padding='same'):
x = Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, name=f'conv{layer_no}',
padding=padding )(x)
 x = BatchNormalization(name=f'bn{layer_no}' )(x)
 x = Activation('relu', name=f'activation{layer_no}' )(x)
 if use_pool:
x = MaxPooling2D(pool_size=[2, 2], strides=[2, 2], name=f'pool{layer_no}', padding='same' )(x)
 return x<categorify>
preds, targs = learn.get_preds()
def conv_model(X):
h, w, c = X.shape[1:]
X = Input(shape=(h, w, c))
conv1 = conv_block(X, filters=8, kernel_size=[3, 3], strides=[1, 1], layer_no=1)
 conv2 = conv_block(conv1, filters=16, kernel_size=[2, 2], strides=[1, 1], layer_no=2)
 conv3 = conv_block(conv2, filters=32, kernel_size=[2, 2], strides=[1, 1], layer_no=3, use_pool=True)
 conv4 = conv_block(conv3, filters=64, kernel_size=[3, 3], strides=[2, 2], layer_no=4)
 conv5 = conv_block(conv4, filters=128, kernel_size=[2, 2], strides=[1, 1], layer_no=5)
 conv6 = conv_block(conv5, filters=256, kernel_size=[2, 2], strides=[1, 1], layer_no=6, use_pool=True)
 flat1 = Flatten(name='flatten1' )(conv6)
 drop1 = Dropout(0.35, name='Dopout1' )(flat1)
 dens1 = Dense(128, name='dense1' )(drop1)
 bn7 = BatchNormalization(name='bn7' )(dens1)
 drop2 = Dropout(0.35, name='Dopout2' )(bn7)
 relu1 = Activation('relu', name='activation7' )(drop2)
 dens1 = Dense(256, name='dense01' )(relu1)
 bn7 = BatchNormalization(name='bn07' )(dens1)
 drop2 = Dropout(0.5, name='Dopout02' )(bn7)
 relu1 = Activation('relu', name='activation07' )(drop2)
 dens2 = Dense(10, name='dense2' )(relu1)
 bn8 = BatchNormalization(name='bn8' )(dens2)
 output_layer = Activation('softmax', name='softmax' )(bn8)
 model = Model(inputs=X, outputs=output_layer)
 return model<choose_model_class>
ids = []
labels = []
for path, pred in zip(test_paths, preds):
for i, label in enumerate(classes):
ids.append(f"{path.name.split('.')[0]}_{label}")
 predicted_probability = '{0:1.10f}'.format(pred[i].item())
 labels.append(predicted_probability )
model = conv_model(train_x )<choose_model_class>
pd.DataFrame({'ID': ids, 'Label': labels} ).to_csv(f'submission.csv', index=False )