Kaggle competition: RSNA Intracranial Hemorrhage Detection
<SOS> metric: weightedmeancolumnwiselogloss Kaggle data source: rsna-intracranial-hemorrhage-detection<data_type_conversions>
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'
X_test = np.array(df_test.values.astype('float32'))<feature_engineering>
n_classes = 6
n_epochs = 2
batch_size = 64
X_train = X_train/255.0<feature_engineering>
!git clone https://github.com/NVIDIA/apex && cd apex && pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext"./
X_test = X_test/255.0<data_type_conversions>
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, CenterCrop, HorizontalFlip, RandomBrightnessContrast
from albumentations.pytorch import ToTensor
from torch.utils.data import Dataset
from tqdm import tqdm_notebook as tqdm
from matplotlib import pyplot as plt
from torchvision import transforms
y_train = np.array(df_train.iloc[:,0].values.astype('int32'))<categorify>
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}
y_train_encoded = to_categorical(y_train, num_classes=10 )<split>
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'))
X_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train_encoded, test_size = 0.1, random_state=2 )<choose_model_class>
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()
model = Sequential()
model.add(Conv2D(filters = 32,
kernel_size =(5,5),
padding = 'Same',
activation ='relu',
input_shape =(28,28,1)))
 model.add(BatchNormalization(axis=1))
model.add(Conv2D(filters = 32,
kernel_size =(5,5),
padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Conv2D(filters = 64,
kernel_size =(3,3),
padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(axis=1))
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(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
undersample_seed=0
train["any"].value_counts()
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )<choose_model_class>
num_ill_patients = train[train["any"]==1].shape[0]
num_ill_patients
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<choose_model_class>
healthy_patients = train[train["any"]==0].index.values
healthy_patients_selection = np.random.RandomState(undersample_seed ).choice(
 healthy_patients, size=num_ill_patients, replace=False 
)
 len(healthy_patients_selection )
model.compile(optimizer = optimizer ,
loss = "categorical_crossentropy",
metrics=["accuracy"] )<init_hyperparams>
sick_patients = train[train["any"]==1].index.values
selected_patients = list(set(healthy_patients_selection ).union(set(sick_patients)))
 len(selected_patients)/2
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<define_variables>
new_train = train.loc[selected_patients].copy()
new_train["any"].value_counts()
batches = datagen.flow(X_train, Y_train, batch_size=96)
 val_batches = datagen.flow(X_val, Y_val, batch_size=96 )<train_model>
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 )
history = model.fit_generator(generator=batches,
epochs=1,
validation_data=val_batches,
validation_steps=val_batches.n,
verbose=2,
steps_per_epoch=batches.n,
callbacks=[learning_rate_reduction] )<predict_on_test>
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 )
predictions = model.predict_classes(X_test, verbose=0 )<save_to_csv>
transform_train = Compose([CenterCrop(200, 200),
HorizontalFlip() ,
RandomBrightnessContrast() ,
ShiftScaleRotate() ,
ToTensor()
])
 transform_test= Compose([CenterCrop(200, 200),
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 )
submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("submission.csv", index=False, header=True )<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")
%matplotlib inline
np.random.seed(42 )<load_from_csv>
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()
if epoch == 1 and step > 6000:
epoch_loss = tr_loss / 6000
print('Training Loss: {:.4f}'.format(epoch_loss))
break
epoch_loss = tr_loss / len(data_loader_train)
 print('Training Loss: {:.4f}'.format(epoch_loss))
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
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))
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<data_type_conversions>
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()
<categorify><EOS> 
!rm -rf /kaggle/working/apex
!rm test.csv
!rm train.csv