Kaggle competition: RSNA Intracranial Hemorrhage Detection
<SOS> metric: weightedmeancolumnwiselogloss Kaggle data source: rsna-intracranial-hemorrhage-detection<prepare_x_and_y>
!pip install torch torchvision feather-format kornia pyarrow --upgrade > /dev/null
!pip install git+https://github.com/fastai/fastai_dev > /dev/null
x = np.array(np.arange(0, 220, 1))<train_model>
np.set_printoptions(linewidth=120)
 matplotlib.rcParams['image.cmap'] = 'bone'
def fit(model, lr, train_dl, valGen, n_epochs, crit, opt, metrics, annln=True, mult_dec=False):

 if annln: annl = lrAnnealing(lr, 40, 500, mult_dec=mult_dec)
 for epoch in range(n_epochs):
losses = []
vl = iter(valGen)
 dl = iter(train_dl)
 length = len(train_dl)
 for t in range(length):
if annln: annl(opt)
 xt, yt = next(dl)
 xt = xt.view(-1, 1, 28, 28)
 xt, yt = xt, yt
y_pred = model(xt)
 l = crit(y_pred, yt)
 losses.append(l)
 opt.zero_grad()
l.backward()
opt.step()
xv, yv = next(vl)
 yright =[]
valLosses = []
mult = len(xv)//100
for i in range(0, len(xv), 100):
if i == mult*100:
a, b = score(model, xv[i:len(xv)], yv[i:len(xv)])
 yright.append(a)
 valLosses.append(crit(b, yv[i:len(xv)] ).data.cpu().numpy())
 else:
a, b = score(model, xv[i:i+100], yv[i:i+100])
 yright.append(a)
 valLosses.append(crit(b, yv[i:i+100] ).data.cpu().numpy())
 val_scores = np.sum(yright)/len(xv)
 meanValLoss = 0
for vliter in range(len(valLosses)) :
meanValLoss += valLosses[vliter]
meanValLoss/=len(valLosses)
 mean_loss = 0
for liter in range(len(losses)) :
mean_loss += losses[liter].data.cpu().numpy()
mean_loss/=len(losses)
 if epoch == 69:
print("Epoch " + str(epoch)+ "::"
+ " loss: " + str(mean_loss)
 + ", valLoss: " + str(meanValLoss)
 +", valAcc: " + str(val_scores*100))<choose_model_class>
path = Path('.. /input/rsna-intracranial-hemorrhage-detection/')
 path_trn = path/'stage_1_train_images'
path_tst = path/'stage_1_test_images'
path_inp = Path('.. /input')
 path_xtra = path_inp/'rsna-hemorrhage-jpg'
path_meta = path_xtra/'meta'/'meta'
path_jpg = path_xtra/'train_jpg'/'train_jpg'
nets = [0]*10
for i in range(10):
nets[i] = ConvNet().cuda().double()
loss = nn.CrossEntropyLoss()
opt = optim.SGD(nets[i].parameters() , 1e-3, momentum=0.999, weight_decay=1e-3, nesterov=True)
 print(f'{i}th ConvNet:')
 %time fit(nets[i], 1e-2, dl, vgen, 70, loss, opt, metrics, mult_dec=True )<define_search_space>
df_comb = pd.read_feather(path_meta/'comb.fth' ).set_index('SOPInstanceUID')
 df_tst = pd.read_feather(path_meta/'df_tst.fth' ).set_index('SOPInstanceUID')
 df_samp = pd.read_feather(path_meta/'wgt_sample.fth' ).set_index('SOPInstanceUID')
 bins =(path_meta/'bins.pkl' ).load()
test =(test-mean)/std
test = np.resize(test,(28000, 1, 28, 28))
test = Variable(torch.DoubleTensor(test)).cuda()
yp = np.zeros(( 28000 ,2), dtype='int'); yp.shape
for i in range(0, 28000, 100):
prob_sum = np.zeros(( 100, 10), dtype='float64')
 for j in range(10):
prob_sum += to_np(nets[j](test[i:i+100]))
yp[i:i+100] = np.array(np.vstack([np.arange(i+1, i+101), prob_sum.argmax(1)] ).T)
 res = pd.DataFrame(yp, index = np.arange(len(yp)) , columns=["ImageId", "Label"])
 res.to_csv("submission.csv", index=False)
 res.head()<load_from_url>
set_seed(42)
 patients = df_comb.PatientID.unique()
pat_mask = np.random.random(len(patients)) <0.8
pat_trn = patients[pat_mask]
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
def split_data(df):
idx = L.range(df)
 mask = df.PatientID.isin(pat_trn)
 return idx[mask],idx[~mask]
splits = split_data(df_samp )
import tokenization<categorify>
df_trn = df_samp.iloc[splits[0]]
p1 = L.range(df_samp)[df_samp.PatientID==df_trn.PatientID[0]]
assert len(p1)== len(set(p1)& set(splits[0]))
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<choose_model_class>
def filename(o): return os.path.splitext(os.path.basename(o)) [0]
fns = L(list(df_samp.fname)).map(filename)
 fn = fns[0]
fn
def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=2e-6), loss='binary_crossentropy', metrics=['accuracy'])
 return model<choose_model_class>
htypes = ['any','epidural','intraparenchymal','intraventricular','subarachnoid','subdural']
def fn2label(fn): return df_comb.loc[fn][htypes].values.astype(np.float32)
 fn2label(fn )
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<load_from_csv>
bs,nw = 128,4
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )<feature_engineering>
tfms = [[fn2image], [fn2label,EncodedMultiCategorize(htypes)]]
dsrc = DataSource(fns, tfms, splits=splits)
 nrm = Normalize(tensor([0.6]),tensor([0.25]))
aug = aug_transforms(p_lighting=0.)
batch_tfms = [IntToFloatTensor() , nrm, Cuda() , *aug]
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )<categorify>
def get_data(bs, sz):
return dsrc.databunch(bs=bs, num_workers=nw, after_item=[ToTensor],
after_batch=batch_tfms+[AffineCoordTfm(size=sz)] )
train_input = bert_encode(train.text.values, tokenizer, max_len=160)
 test_input = bert_encode(test.text.values, tokenizer, max_len=160)
 train_labels = train.target.values<train_model>
dbch = get_data(128, 96)
 xb,yb = to_cpu(dbch.one_batch())
 dbch.show_batch(max_n=4, figsize=(9,6))
xb.mean() ,xb.std() ,xb.shape,len(dbch.train_dl )
train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=3,
batch_size=16 
)
 model.save('model.h5' )<predict_on_test>
def accuracy_any(inp, targ, thresh=0.5, sigmoid=True):
inp,targ = flatten_check(inp[:,0],targ[:,0])
 if sigmoid: inp = inp.sigmoid()
return(( inp>thresh)==targ.bool() ).float().mean()
test_pred = model.predict(test_input )<save_to_csv>
def get_loss(scale=1.0):
loss_weights = tensor(2.0, 1, 1, 1, 1, 1 ).cuda() *scale
return BaseLoss(nn.BCEWithLogitsLoss, pos_weight=loss_weights, floatify=True, flatten=False,
is_2d=False, activation=torch.sigmoid )
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<import_modules>
loss_func = get_loss(0.14*2)
 opt_func = partial(Adam, wd=0.01, eps=1e-3)
 metrics=[accuracy_multi,accuracy_any]
import keras
import plotly.express as px
from keras import backend as K
from keras.optimizers import Adam
import plotly.graph_objects as go
import plotly.figure_factory as ff
from keras.models import Sequential
from keras.layers import Activation
from keras.metrics import categorical_crossentropy
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense,Flatten,Dropout,Conv2D,MaxPool2D<load_from_csv>
def get_learner() :
dbch = get_data(128,128)
 learn = cnn_learner(dbch, xresnet50, loss_func=loss_func, opt_func=opt_func, metrics=metrics)
 return learn.to_fp16()
df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 df_train.describe()<prepare_x_and_y>
learn = get_learner()
df_train_x = df_train.drop('label',axis =1)
 df_train_y = df_train[['label']]<split>
def do_fit(bs,sz,epochs,lr, freeze=True):
learn.dbunch = get_data(bs, sz)
 if freeze:
if learn.opt is not None: learn.opt.clear_state()
learn.freeze()
learn.fit_one_cycle(1, slice(lr))
learn.unfreeze()
learn.fit_one_cycle(epochs, slice(lr))
np_train_x = df_train_x.to_numpy().reshape(-1,28,28,1)
 np_train_y = df_train_y.to_numpy()
x_train,x_test,y_train,y_test = train_test_split(np_train_x, np_train_y,test_size=0.10,random_state=42 )<train_model>
fns = L(list(df_comb.fname)).map(filename)
 splits = split_data(df_comb )
datagen = ImageDataGenerator(
 featurewise_center=False,
featurewise_std_normalization=False,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
horizontal_flip=False,
vertical_flip=False,
brightness_range = [0.8,1.])
 datagen.fit(x_train )<choose_model_class>
def fix_pxrepr(dcm):
if dcm.PixelRepresentation != 0 or dcm.RescaleIntercept<-100: return
x = dcm.pixel_array + 1000
px_mode = 4096
x[x>=px_mode] = x[x>=px_mode] - px_mode
dcm.PixelData = x.tobytes()
dcm.RescaleIntercept = -1000
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(10, activation = "softmax"))
model.compile(Adam(lr = 0.0001),loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
 keras.utils.plot_model(model, "Neural_Network.png", show_shapes=True )<train_model>
def dcm_tfm(fn):
fn =(path_trn/fn ).with_suffix('.dcm')
 try:
x = fn.dcmread()
fix_pxrepr(x)
 except Exception as e:
print(fn,e)
 raise SkipItemException
if x.Rows != 512 or x.Columns != 512: x.zoom_to(( 512,512))
px = x.scaled_px
return TensorImage(px.to_3chan(dicom_windows.brain,dicom_windows.subdural, bins=bins))
model.fit_generator(datagen.flow(x_train,y_train,batch_size = 30),
steps_per_epoch = 1000,validation_data =(x_test,y_test),
epochs = 50, verbose = 2 )<load_from_csv>
tfms = [[dcm_tfm], [fn2label,EncodedMultiCategorize(htypes)]]
dsrc = DataSource(fns, tfms, splits=splits)
 batch_tfms = [nrm, Cuda() , *aug]
df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 df_test.describe()<data_type_conversions>
def get_data(bs, sz):
return dsrc.databunch(bs=bs, num_workers=nw, after_batch=batch_tfms+[AffineCoordTfm(size=sz)] )
np_test = df_test.to_numpy().reshape(-1,28,28,1 )<predict_on_test>
dbch = get_data(64,256)
 x,y = to_cpu(dbch.one_batch())
 dbch.show_batch(max_n=4)
 x.shape
df_test['label'] = model.predict_classes(np_test )<save_to_csv>
learn.loss_func = get_loss(1.0 )
df_test[['ImageId','label']].to_csv('submission.csv',index=False )<set_options>
def fit_tune(bs, sz, epochs, lr):
dbch = get_data(bs, sz)
 learn.dbunch = dbch
learn.opt.clear_state()
learn.unfreeze()
learn.fit_one_cycle(epochs, slice(lr))
%matplotlib inline<load_from_csv>
test_fns = [(path_tst/f'{filename(o)}.dcm' ).absolute() for o in df_tst.fname.values]
df_train = pd.read_csv(".. /input/train.csv")
 df_test = pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
tst = test_dl(dbch, test_fns)
 x = tst.one_batch() [0]
x.min() ,x.max()
y_train = df_train["label"]<drop_column>
preds,targs = learn.get_preds(dl=tst)
 preds_clipped = preds.clamp (.0001,.999 )
X_train = df_train.drop(columns="label" )<prepare_x_and_y>
ids = []
labels = []
for idx,pred in zip(df_tst.index, preds_clipped):
for i,label in enumerate(htypes):
ids.append(f"{idx}_{label}")
 predicted_probability = '{0:1.10f}'.format(pred[i].item())
 labels.append(predicted_probability )
<data_type_conversions><EOS> 
from IPython.display import FileLink, FileLinks