Kaggle competition: RSNA Intracranial Hemorrhage Detection
<SOS> metric: weightedmeancolumnwiselogloss Kaggle data source: rsna-intracranial-hemorrhage-detection<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.add(Conv2D(filters=32,kernel_size=(5,5),input_shape=(x_train.shape[1:]),padding="Same",activation="relu"))
model.add(Conv2D(filters=32,kernel_size=(5,5),padding="Same",activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters=64,kernel_size=(5,5),padding="Same",activation="relu"))
model.add(Conv2D(filters=64,kernel_size=(5,5),padding="Same",activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),))
model.add(Dropout(0.25))
model.add(Conv2D(filters=128,kernel_size=(5,5),padding="Same",activation="relu"))
model.add(Conv2D(filters=128,kernel_size=(5,5),padding="Same",activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
 model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(256,activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(10,activation="softmax"))<choose_model_class>
n_classes = 6
n_epochs = 1
batch_size = 64
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
 print(model.summary() )<choose_model_class>
!git clone https://github.com/NVIDIA/apex && cd apex && pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext"./
lr_reduce=ReduceLROnPlateau(monitor="val_acc",
patience=4,
verbose=1,
factor=0.5,
min_lr=0.00000001
)<train_model>
from apex import amp
import os
import cv2
import glob
import pydicom
import numpy as np
import pandas as pd
import torch
import torch.optim as optim
from albumentations import Compose, ShiftScaleRotate, Resize
from albumentations.pytorch import ToTensor
from torch.utils.data import Dataset
from tqdm import tqdm_notebook as tqdm
from matplotlib import pyplot as plt
model.fit(datagen.flow(x_train,y_train,batch_size=100),
batch_size=1,
epochs=40,
validation_data=(x_val,y_val),
verbose=1,
callbacks=[checkpoint,lr_reduce] )<load_pretrained>
class IntracranialDataset(Dataset):
def __init__(self, csv_file, path, labels, transform=None):
self.path = path
self.data = pd.read_csv(csv_file)
 self.transform = transform
self.labels = labels
def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
img_name = os.path.join(self.path, self.data.loc[idx, 'Image'] + '.png')
 img = cv2.imread(img_name)
 if self.transform:
augmented = self.transform(image=img)
 img = augmented['image']
if self.labels:
labels = torch.tensor(
 self.data.loc[idx, ['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural', 'any']])
 return {'image': img, 'labels': labels}
else:
return {'image': img}
model.load_weights("mnistCNN" )<predict_on_test>
train = pd.read_csv(os.path.join(dir_csv, 'stage_1_train.csv'))
test = pd.read_csv(os.path.join(dir_csv, 'stage_1_sample_submission.csv'))
predictions = model.predict(x_test )<prepare_output>
train[['ID', 'Image', 'Diagnosis']] = train['ID'].str.split('_', expand=True)
 train = train[['Image', 'Diagnosis', 'Label']]
train.drop_duplicates(inplace=True)
 train = train.pivot(index='Image', columns='Diagnosis', values='Label' ).reset_index()
train['Image'] = 'ID_' + train['Image']
train.head()
predictions=np.argmax(predictions, axis=1 )<create_dataframe>
png = glob.glob(os.path.join(dir_train_img, '*.png'))
png = [os.path.basename(png)[:-4] for png in png]
png = np.array(png)
 train = train[train['Image'].isin(png)]
train.to_csv('train.csv', index=False )
submission=pd.DataFrame({"ImageId":range(1,x_test.shape[0]+1),"Label": predictions})
<save_to_csv>
test[['ID','Image','Diagnosis']] = test['ID'].str.split('_', expand=True)
 test['Image'] = 'ID_' + test['Image']
test = test[['Image', 'Label']]
test.drop_duplicates(inplace=True)
 test.to_csv('test.csv', index=False )
submission = submission.to_csv("submission.csv",index=False )<import_modules>
transform_train = Compose([
ShiftScaleRotate() ,
ToTensor()
])
 transform_test= Compose([
ToTensor()
])
 train_dataset = IntracranialDataset(
 csv_file='train.csv', path=dir_train_img, transform=transform_train, labels=True)
 test_dataset = IntracranialDataset(
 csv_file='test.csv', path=dir_test_img, transform=transform_test, labels=False)
 data_loader_train = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=False, num_workers=4)
 data_loader_test = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=4 )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import albumentations as A
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Input, Dense, Flatten
from tensorflow.keras.layers import Convolution2D, MaxPool2D, Dropout, BatchNormalization, Dropout, LeakyReLU
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.applications import ResNet50
from tensorflow.python.keras.utils.data_utils import Sequence<set_options>
device = torch.device("cuda:0")
 model = torch.hub.load('facebookresearch/WSL-Images', 'resnext101_32x8d_wsl')
 model.fc = torch.nn.Linear(2048, n_classes)
 model.to(device)
 criterion = torch.nn.BCEWithLogitsLoss()
plist = [{'params': model.parameters() , 'lr': 2e-5}]
optimizer = optim.Adam(plist, lr=2e-5)
 model, optimizer = amp.initialize(model, optimizer, opt_level="O1")
SEED = 1
np.random.seed(SEED)
 tf.random.set_seed(SEED)
 random.seed(SEED)
 if SEED is not None:
os.environ['PYTHONHASHSEED'] = str(SEED )<load_from_disk>
for epoch in range(n_epochs):
print('Epoch {}/{}'.format(epoch, n_epochs - 1))
print('-' * 10)
 model.train()
tr_loss = 0
tk0 = tqdm(data_loader_train, desc="Iteration")
 for step, batch in enumerate(tk0):
inputs = batch["image"]
labels = batch["labels"]
inputs = inputs.to(device, dtype=torch.float)
 labels = labels.to(device, dtype=torch.float)
 outputs = model(inputs)
 loss = criterion(outputs, labels)
 with amp.scale_loss(loss, optimizer)as scaled_loss:
scaled_loss.backward()
tr_loss += loss.item()
optimizer.step()
optimizer.zero_grad()
epoch_loss = tr_loss / len(data_loader_train)
 print('Training Loss: {:.4f}'.format(epoch_loss))
!mkdir data/
!unzip -q /kaggle/input/facial-keypoints-detection/training.zip -d data/train
!unzip -q /kaggle/input/facial-keypoints-detection/test.zip -d data/test<prepare_x_and_y>
for param in model.parameters() :
param.requires_grad = False
model.eval()
test_pred = np.zeros(( len(test_dataset)* n_classes, 1))
for i, x_batch in enumerate(tqdm(data_loader_test)) :
x_batch = x_batch["image"]
x_batch = x_batch.to(device, dtype=torch.float)
 with torch.no_grad() :
pred = model(x_batch)
 test_pred[(i * batch_size * n_classes):(( i + 1)* batch_size * n_classes)] = torch.sigmoid(
 pred ).detach().cpu().reshape(( len(x_batch)* n_classes, 1))
def format_dataset(dataframe):
X = np.array([ format_image(x)for x in dataframe['Image'] ])
 if len(dataframe.columns)> 2:
y = dataframe.drop('Image', axis=1 ).values
return X, y
return X
def format_image(img_row):

 img = img_row.split(' ')
 img = np.array(img, dtype=np.float32)
 img = img.reshape(( 96,96,1))
img = img / 255.
 return img
def format_keypoints(keypoint):

 return(keypoint - 48.) / 48.
 def unformat_keypoints(keypoint):

 return keypoint*48 + 48
def show_sample(img, keypoints, axis=None, color='b'):

 if axis is None:
fig, axis = plt.subplots()
axis.scatter(keypoints[0::2], keypoints[1::2], s=10, c=color)
 axis.imshow(img.squeeze() , cmap='gray')
 def show_random_samples(X, y, n_rows=2, n_cols=5):

 fig = plt.figure(figsize=(2*n_cols, 2*n_rows), dpi=100)
 for i, idx in enumerate(np.random.randint(0, len(y), n_rows*n_cols)) :
axis = fig.add_subplot(n_rows, n_cols, i+1, xticks=[], yticks=[])
 show_sample(X[idx], y[idx], axis=axis)
 axis.set_title(f'Sample<load_from_csv>
submission = pd.read_csv(os.path.join(dir_csv, 'stage_1_sample_submission.csv'))
submission = pd.concat([submission.drop(columns=['Label']), pd.DataFrame(test_pred)], axis=1)
 submission.columns = ['ID', 'Label']
submission.to_csv('submission.csv', index=False)
 submission.head()
<train_model><EOS> 
!rm -rf /kaggle/working/apex
!rm test.csv
!rm train.csv