text_plain_output_1.png
from IPython.core.magic import register_cell_magic

from efficientnet_pytorch_3d import EfficientNet3D

from pathlib import Path

from pytorch_lightning.core.memory import ModelSummary

from sklearn.metrics import roc_auc_score, roc_curve, auc

from time import time

from torch.utils.data import DataLoader, Dataset

import cv2

import glob

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import pydicom

import pytorch_lightning as pl

import re

import sys

import time

import torch

import torch

from IPython.core.magic import register_cell_magic
import os
from pathlib import Path
@register_cell_magic
def write_and_run(line, cell):
    argz = line.split()
    file = argz[-1]
    mode = 'w'
    if len(argz) == 2 and argz[0] == '-a':
        mode = 'a'
    with open(file, mode) as f:
        f.write(cell)
    get_ipython().run_cell(cell)
Path('/kaggle/working/scripts').mkdir(exist_ok=True)
models_dir = Path('/kaggle/working/models')
models_dir.mkdir(exist_ok=True)

import os
import json
import glob
import random
import collections
import numpy as np
import pandas as pd
import pydicom
from pydicom.pixel_data_handlers.util import apply_voi_lut
import cv2
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn-talk')
import torch
from sklearn.metrics import roc_auc_score, roc_curve, auc
from torchvision import transforms
import time
import torch
from torch import nn
from torch.utils.data import DataLoader, Dataset
from sklearn import model_selection as sk_model_selection
from torch.nn import functional as F
from sklearn.model_selection import StratifiedKFold
import pytorch_lightning as pl
from transformers import DeiTFeatureExtractor, DeiTForImageClassification, AutoConfig
from pytorch_lightning.core.memory import ModelSummary
import sys
sys.path.append('../input/efficientnetpyttorch3d/EfficientNet-PyTorch-3D')
from efficientnet_pytorch_3d import EfficientNet3D

class Config:
    seed = 42
    img_size = 256
    num_imgs = 64
    lr = 2e-08
    data_dir = Path('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification')
pl.utilities.seed.seed_everything(Config.seed, workers=True)

kfold_df = pd.read_csv('../input/braintumor-sampling/brain_tumor_kfold.csv')

import re
def load_dicom_image(path, rotate=False):
    dicom = pydicom.read_file(path)
    data = dicom.pixel_array
    data = dicom.pixel_array
    if rotate:
        data = cv2.rotate(data, cv2.ROTATE_180)
    data = cv2.resize(data, (Config.img_size, Config.img_size))
    return data
def load_images_3d(dp_id, mri_type='FLAIR', split='train', augment=False):
    dp_dir = str(Config.data_dir / split / dp_id / mri_type / '*.dcm')
    files = sorted(glob.glob(dp_dir), key=lambda var: [int(x) if x.isdigit() else x for x in re.findall('[^0-9]|[0-9]+', var)])
    middle = len(files) // 2
    num_imgs2 = Config.num_imgs // 2
    p1 = max(0, middle - num_imgs2)
    p2 = min(len(files), middle + num_imgs2)
    img3d = np.stack([load_dicom_image(f, rotate=augment) for f in files[p1:p2]]).T
    if img3d.shape[-1] < Config.num_imgs:
        n_zero = np.zeros((Config.img_size, Config.img_size, Config.num_imgs - img3d.shape[-1]))
        img3d = np.concatenate((img3d, n_zero), axis=-1)
    if np.min(img3d) < np.max(img3d):
        img3d = img3d - np.min(img3d)
        img3d = img3d / np.max(img3d)
    return np.expand_dims(img3d, 0)

train_df = kfold_df[kfold_df.fold != 0]
train_ds = DataRetriever(train_df['BraTS21ID'].values, '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train', 'FLAIR', targets=train_df['MGMT_value'].values)
train_dl = DataLoader(train_ds, batch_size=1, shuffle=True, num_workers=1)
next(iter(train_dl))['X'].shape

def calc_roc_auc(y_true, y_pred):
    return roc_auc_score(y_true, y_pred)
    return 0.5

from time import time
class Metrics:
    def __init__(self):
        self.losses = []
        self.reduced_losses = []
        self.y_batches = []
        self.y_hat_batches = []
        self.roc_auc_list = []
        self.train_epoch_start_time = None
        self.validation_epoch_start_time = None
class MetricsCallback(pl.callbacks.Callback):
    def __init__(self):
        self.train_metrics = Metrics()
        self.validation_metrics = Metrics()
        self.best_validation_roc_auc = float('-inf')
    def on_train_epoch_start(self, trainer, pl_module):
        self.train_epoch_start_time = time()
    def on_validation_epoch_start(self, trainer, pl_module):
        self.validation_epoch_start_time = time()
    def on_train_batch_end(self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx):
        self.after_batch(self.train_metrics, outputs)
    def on_validation_batch_end(self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx):
        self.after_batch(self.validation_metrics, outputs)
    def on_train_epoch_end(self, trainer, pl_module):
        self.train_epoch_start_time = None
    def on_validation_epoch_end(self, trainer, pl_module):
        train_loss = self.get_avg_loss(self.train_metrics)
        validation_loss = self.get_avg_loss(self.validation_metrics)
        train_roc_auc = self.get_roc_auc(self.train_metrics)
        validation_roc_auc = self.get_roc_auc(self.validation_metrics)
        self.log('roc_auc', validation_roc_auc)
        if validation_roc_auc > self.best_validation_roc_auc:
            self.best_validation_roc_auc = validation_roc_auc
        self.validation_epoch_start_time = None
    def get_avg_loss(self, metrics):
        avg_loss = np.array(metrics.losses).mean()
        metrics.reduced_losses.append(avg_loss)
        metrics.losses = []
        return avg_loss
    def after_batch(self, metrics, outputs):
        metrics.losses.append(outputs['loss'].item())
        metrics.y_batches.append(outputs['y'])
        metrics.y_hat_batches.append(outputs['y_hat'])
    def get_roc_auc(self, metrics):
        if not metrics.y_batches:
            return None
        y_np = torch.hstack(metrics.y_batches).detach().cpu().numpy()
        y_hat_np = torch.hstack(metrics.y_hat_batches).detach().cpu().numpy()
        roc_auc = calc_roc_auc(y_np, y_hat_np)
        metrics.roc_auc_list.append(roc_auc)
        metrics.y_batches = []
        metrics.y_hat_batches = []
        return roc_auc

def plot_metrics(metrics_callback):
    train_losses = metrics_callback.train_metrics.reduced_losses
    validation_losses = metrics_callback.validation_metrics.reduced_losses
    train_roc_aucs = metrics_callback.train_metrics.roc_auc_list
    validation_roc_aucs = metrics_callback.validation_metrics.roc_auc_list

mri_types = ['FLAIR', 'T1w', 'T1wCE', 'T2w']
mri_type_roc_aucs = []
for mri_type in mri_types:
    print('MRI TYPE:', mri_type)
    roc_aucs = []
    for fold_n in range(5):
        print('Fold:', fold_n + 1)
        train_df = kfold_df[kfold_df.fold != fold_n]
        train_ds = DataRetriever(train_df['BraTS21ID'].values, '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train', mri_type=mri_type, targets=train_df['MGMT_value'].values)
        val_df = kfold_df[kfold_df.fold == fold_n]
        val_ds = DataRetriever(val_df['BraTS21ID'].values, '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train', mri_type=mri_type, targets=val_df['MGMT_value'].values)
        train_dl = DataLoader(train_ds, batch_size=4, shuffle=True, num_workers=2, pin_memory=True)
        val_dl = DataLoader(val_ds, batch_size=4, shuffle=False, num_workers=2, pin_memory=True)
        net = EfficientNet3D.from_name('efficientnet-b5', override_params={'num_classes': 1}, in_channels=1)
        model = Model(net)
        checkpoint_callback = pl.callbacks.ModelCheckpoint(dirpath='models', filename=f'model_{mri_type}_{fold_n}_' + '{epoch}_{roc_auc:.3}', monitor='roc_auc', mode='max', save_weights_only=True)
        metrics_callback = MetricsCallback()
        print(ModelSummary(model))
        trainer = pl.Trainer(fast_dev_run=False, max_epochs=10, gpus=1, auto_lr_find=True, precision=16, limit_train_batches=5, limit_val_batches=5, num_sanity_val_steps=0, val_check_interval=1.0, callbacks=[metrics_callback, checkpoint_callback])
        trainer.fit(model, train_dl, val_dl)
        plot_metrics(metrics_callback)
        roc_aucs.append(metrics_callback.best_validation_roc_auc)
    print('roc_auc_s:', roc_aucs)
    print('roc_auc mean:', np.array(roc_aucs).mean())
    print('roc_auc std:', np.array(roc_aucs).std())
    mri_type_roc_aucs.append(np.array(roc_aucs).mean())
print('mri_type roc_auc_s:', mri_type_roc_aucs)
print('mri_type roc_auc mean:', np.array(mri_type_roc_aucs).mean())
print('mri_type roc_auc std:', np.array(mri_type_roc_aucs).std())