Kaggle competition: RSNA Intracranial Hemorrhage Detection
<SOS> metric: weightedmeancolumnwiselogloss Kaggle data source: rsna-intracranial-hemorrhage-detection<load_from_csv>
import os
import numpy as np
import pandas as pd
from tqdm import tqdm
import gc
import seaborn as sns; sns.set()
import pydicom
import matplotlib.pyplot as plt
import cv2
import torch.optim as optim
import torch
import torchvision.models as models
from torch.utils.data import Dataset
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
<drop_column>
BASE_PATH = '/kaggle/input/rsna-intracranial-hemorrhage-detection/'
TRAIN_DIR = 'stage_1_train_images/'
TEST_DIR = 'stage_1_test_images/'
TRAIN_CSV = 'stage_1_train.csv'
TEST_CSV = 'stage_1_sample_submission.csv'
MODEL_PATH = '/kaggle/input/baseline-resnext50/resnext50_10.pth'
TRAIN_CSV_PATH = os.path.join(BASE_PATH, TRAIN_CSV)
 TEST_CSV_PATH = os.path.join(BASE_PATH, TEST_CSV )
x_train = train.drop(["label"], axis=1 )<prepare_x_and_y>
df_train = pd.read_csv(TRAIN_CSV_PATH)
 df_train[['id', 'img', 'subtype']] = df_train['ID'].str.split('_', n=3, expand=True)
 df_train['img'] = 'ID_' + df_train['img']
df_train.drop_duplicates(inplace=True)
 df_train = df_train.pivot(index='img', columns='subtype', values='Label' ).reset_index()
df_train['path'] = os.path.join(BASE_PATH, TRAIN_DIR)+ df_train['img'] + '.dcm'
legit_images = pd.read_csv('/kaggle/input/true-imagescsv/legit-images.csv')
 df_train = df_train.merge(legit_images, left_on='img', right_on='0' ).drop(['0'], axis=1)
 df_train.head()
y_train = train["label"]<prepare_x_and_y>
df_test = pd.read_csv(TEST_CSV_PATH)
 df_test[['id','img','subtype']] = df_test['ID'].str.split('_', expand=True)
 df_test['img'] = 'ID_' + df_test['img']
df_test = df_test[['img', 'Label']]
df_test['path'] = os.path.join(BASE_PATH, TEST_DIR)+ df_test['img'] + '.dcm'
df_test.drop_duplicates(inplace=True)
 df_test = df_test.reset_index(drop=True )
x_test=test<prepare_x_and_y>
params = {'batch_size': 64,
'shuffle': False,
'num_workers': 4}
train_dataset = RSNADataset(df= df_train, labels=True)
 test_dataset = RSNADataset(df= df_test, labels=False)
 data_train_generator = torch.utils.data.DataLoader(train_dataset, **params)
 data_test_generator = torch.utils.data.DataLoader(test_dataset,**params )
x_train = np.array(x_train)
 x_test = np.array(x_test)
 x_train=x_train.reshape(x_train.shape[0],28,28,1)
 x_test=x_test.reshape(x_test.shape[0],28,28,1 )<categorify>
device = torch.device("cuda:0")
 model0 = models.resnext50_32x4d(pretrained=True)
 model = torch.nn.Sequential(model0, torch.nn.Linear(1000, 6))
model = model.to(device)
 criterion = torch.nn.BCEWithLogitsLoss()
y_train = utils.to_categorical(y_train,10 )<feature_engineering>
n_epochs = 2
optimizer = optim.Adam(model.parameters() , lr=4e-5)
 try:
model.load_state_dict(torch.load(MODEL_PATH))
torch.save(model.state_dict() , 'resnext50_0.pth')
 except Exception as e:
print('The pre-trained model is used' )
x_train = x_train / 255
x_test = x_test / 255<split>
for epoch in range(1, n_epochs+1):
print('Epoch {}/{}'.format(epoch, n_epochs))
print('-' * 10)
 model.train()
tr_loss = 0
tk0 = tqdm(data_train_generator, 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)
 loss.backward()
tr_loss += loss.item()
optimizer.step()
optimizer.zero_grad()
torch.save(model.state_dict() , f'resnext50_{epoch}.pth')
 epoch_loss = tr_loss / len(data_train_generator)
 print('Training Loss: {:.4f}'.format(epoch_loss))
x_train, x_val, y_train , y_val=train_test_split(x_train,y_train,test_size=0.1 )<choose_model_class>
for param in model.parameters() :
param.requires_grad = False
model.eval()
test_pred = np.zeros(( len(test_dataset)* 6, 1))
for i, batch_ in enumerate(tqdm(data_test_generator)) :
batch_ = batch_["image"]
batch_ = batch_.to(device, dtype=torch.float)
 with torch.no_grad() :
pred = model(batch_)
 test_pred[(i * 64 * 6):(( i + 1)* 64 * 6)] = torch.sigmoid(
 pred ).detach().cpu().reshape(( len(batch_)* 6, 1))
<choose_model_class><EOS> 
submission = pd.read_csv(TEST_CSV_PATH)
 submission = pd.concat([submission.drop(columns=['Label']), pd.DataFrame(test_pred)], axis=1)
 submission.columns = ['ID', 'Label']
submission.to_csv('submission.csv', index=False )