{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nimport wandb\nimport glob\nimport re \nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport tensorflow as tf\nimport imageio\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\nimport collections\nimport json\nfrom tqdm import tqdm\n%matplotlib inline\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        os.path.join(dirname, filename)\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-13T07:46:16.389895Z","iopub.execute_input":"2021-08-13T07:46:16.390345Z","iopub.status.idle":"2021-08-13T07:46:25.596884Z","shell.execute_reply.started":"2021-08-13T07:46:16.390235Z","shell.execute_reply":"2021-08-13T07:46:25.595759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.login()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:30:58.775532Z","iopub.execute_input":"2021-08-04T10:30:58.775864Z","iopub.status.idle":"2021-08-04T10:31:16.640947Z","shell.execute_reply.started":"2021-08-04T10:30:58.775834Z","shell.execute_reply":"2021-08-04T10:31:16.64Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CONFIG = {\n    'IMG_SIZE':224,\n    'competition':'rsna-miccai-brain',\n    '_wandb_kernel':'rooneyy'\n}","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:21:13.306434Z","iopub.execute_input":"2021-08-04T11:21:13.307188Z","iopub.status.idle":"2021-08-04T11:21:13.313921Z","shell.execute_reply.started":"2021-08-04T11:21:13.307092Z","shell.execute_reply":"2021-08-04T11:21:13.312561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = '/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv'\ntrain_df = pd.read_csv(filename)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:49:19.305580Z","iopub.execute_input":"2021-08-13T07:49:19.306109Z","iopub.status.idle":"2021-08-13T07:49:19.348665Z","shell.execute_reply.started":"2021-08-13T07:49:19.306060Z","shell.execute_reply":"2021-08-13T07:49:19.347822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Number of rows: {len(train_df)}')","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:49:22.520474Z","iopub.execute_input":"2021-08-13T07:49:22.521012Z","iopub.status.idle":"2021-08-13T07:49:22.527985Z","shell.execute_reply.started":"2021-08-13T07:49:22.520976Z","shell.execute_reply":"2021-08-13T07:49:22.526795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(10,6))\nsns.countplot(y='MGMT_value', data=train_df);\nax.set_title('Distribution of labels', fontsize=15, weight='heavy')","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:49:25.401897Z","iopub.execute_input":"2021-08-13T07:49:25.402320Z","iopub.status.idle":"2021-08-13T07:49:25.591322Z","shell.execute_reply.started":"2021-08-13T07:49:25.402285Z","shell.execute_reply":"2021-08-13T07:49:25.590238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = glob.glob('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/*/*/*')\nprint(f'Total number of files: {len(filenames)}')","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:49:29.498979Z","iopub.execute_input":"2021-08-13T07:49:29.499384Z","iopub.status.idle":"2021-08-13T07:50:21.830340Z","shell.execute_reply.started":"2021-08-13T07:49:29.499350Z","shell.execute_reply":"2021-08-13T07:50:21.829319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_dict = {'FLAIR':[],\n              'T1w':[],\n              'T1wCE':[],\n              'T2w':[]\n             }\n\nfor filename in tqdm(filenames):\n    scan = filename.split('/')[-2]\n    if scan == 'FLAIR':\n        label_dict['FLAIR'].append(filename)\n    elif scan == \"T1w\":\n        label_dict['T1w'].append(filename)\n    elif scan == 'T1wCE':\n        label_dict['T1wCE'].append(filename)\n    elif scan == 'T2w':\n        label_dict['T2w'].append(filename)\n        \nprint('Size of FLAIR scan: {}\\nT1w scan: {}\\nT1wCE scan: {}\\nT2w scan: {}'.format(len(label_dict['FLAIR']),\n                                                                                 len(label_dict['T1w']),\n                                                                                 len(label_dict['T1wCE']),\n                                                                                 len(label_dict['T2w'])))","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:50:55.687465Z","iopub.execute_input":"2021-08-13T07:50:55.687875Z","iopub.status.idle":"2021-08-13T07:50:56.048217Z","shell.execute_reply.started":"2021-08-13T07:50:55.687840Z","shell.execute_reply":"2021-08-13T07:50:56.047118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run = wandb.init(project='brain-tumor-wizz', config=CONFIG)\ndata = [['FLAIR',74248],['T1w',77627],['T1wCE',96766],['T2w',100000]]\ntable = wandb.Table(data=data, columns=['Scan type','Size of Files'])\nwandb.log({'my_bar_chart_id':wandb.plot.bar(table, 'Scan type', 'Size of Files', title='Scan types vs Number of Dicom Files')})\nrun.finish()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T09:51:18.911498Z","iopub.execute_input":"2021-08-04T09:51:18.911885Z","iopub.status.idle":"2021-08-04T09:51:33.304746Z","shell.execute_reply.started":"2021-08-04T09:51:18.911847Z","shell.execute_reply":"2021-08-04T09:51:33.303885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Read Dicom Files**","metadata":{}},{"cell_type":"code","source":"def ReadMRI(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(path)\n    \n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n        \n    if fix_monochrome and dicom.PhotometricInterpretation == 'MONOCHROME1':\n        data = data - np.min(data)\n        if np.max(data) != 0:\n            data = data / np.max(data)\n        data = (data * 255).astype(np.uint8)\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:51:05.925917Z","iopub.execute_input":"2021-08-13T07:51:05.926459Z","iopub.status.idle":"2021-08-13T07:51:05.933876Z","shell.execute_reply.started":"2021-08-13T07:51:05.926425Z","shell.execute_reply":"2021-08-13T07:51:05.932075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = filenames[32346]\ndata = ReadMRI(path)\nplt.imshow(data, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:51:08.147231Z","iopub.execute_input":"2021-08-13T07:51:08.147620Z","iopub.status.idle":"2021-08-13T07:51:08.344923Z","shell.execute_reply.started":"2021-08-13T07:51:08.147584Z","shell.execute_reply":"2021-08-13T07:51:08.343209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sorted_nicely(l):\n    \"\"\" Sort the given iterable in the way that humans expect \"\"\"\n    convert = lambda text: int(text) if text.isdigit() else text\n    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]\n    return sorted(l, key=alphanum_key)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T13:14:14.158366Z","iopub.execute_input":"2021-08-04T13:14:14.158801Z","iopub.status.idle":"2021-08-04T13:14:14.165314Z","shell.execute_reply.started":"2021-08-04T13:14:14.158762Z","shell.execute_reply":"2021-08-04T13:14:14.164079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_patient_id(patient_id):\n    if patient_id < 10:\n        return '0000'+str(patient_id)\n    elif patient_id >= 10 and patient_id < 100:\n        return '000'+str(patient_id)\n    elif patient_id >= 100 and patient_id < 1000:\n        return '00'+str(patient_id)\n    else:\n        return '0'+str(patient_id)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:35:28.846097Z","iopub.execute_input":"2021-08-04T12:35:28.846524Z","iopub.status.idle":"2021-08-04T12:35:28.853529Z","shell.execute_reply.started":"2021-08-04T12:35:28.84649Z","shell.execute_reply":"2021-08-04T12:35:28.852124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_1 = train_df[train_df.MGMT_value == 1].reset_index(drop=True)\nprint(f'Number of patients with brain tumor: {len(train_df_1)}')\n\nIMG_2_log = 20\ntrain_df_1_sampled = train_df_1.sample(n=IMG_2_log).reset_index(drop=True)\nprint(f'Number of sampled patients: {len(train_df_1_sampled)}') \n\nsampled_data_at = wandb.Table(dataframe=train_df_1_sampled)\nrun = wandb.init(project='brain-tumor-viz(Sampled Patients)', config=CONFIG)\nwandb.log({f'Sampled DataFrame': sampled_data_at})\nrun.finish()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T12:26:42.538516Z","iopub.execute_input":"2021-08-04T12:26:42.538928Z","iopub.status.idle":"2021-08-04T12:27:02.512273Z","shell.execute_reply.started":"2021-08-04T12:26:42.538892Z","shell.execute_reply":"2021-08-04T12:27:02.511337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(len(train_df_1_sampled))):\n    ID = train_df_1_sampled.BraTS21ID[i]\n    patient_id = get_patient_id(ID)\n    \n    run = wandb.init(project='brain-tumor-viz(Animate MRI)', config=CONFIG, name=f'{patient_id}')\n    \n    for key in label_dict.keys():\n        if os.path.isdir(f'/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/{patient_id}/{key}'):\n            _filenames = os.listdir(f'/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/{patient_id}/{key}')\n            _filenames = sorted_nicely(_filenames)\n            for filename in _filenames:\n                mri_data = ReadMRI(f'/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/{patient_id}/{key}/{filename}')\n                wandb.log({f'{key}': [wandb.Image(mri_data)]})\n    \n    run.finish()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T13:15:05.215078Z","iopub.execute_input":"2021-08-04T13:15:05.21542Z","iopub.status.idle":"2021-08-04T13:29:25.131774Z","shell.execute_reply.started":"2021-08-04T13:15:05.215391Z","shell.execute_reply":"2021-08-04T13:29:25.130457Z"},"_kg_hide-output":false,"_kg_hide-input":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    data = data - np.min(data)\n    if np.amax(data) != 0:\n        data = data / np.amax(data)\n    data = (data * 255).astype(np.uint8)\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:51:22.157447Z","iopub.execute_input":"2021-08-13T07:51:22.157854Z","iopub.status.idle":"2021-08-13T07:51:22.163873Z","shell.execute_reply.started":"2021-08-13T07:51:22.157821Z","shell.execute_reply":"2021-08-13T07:51:22.162624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualise_sample(brats21id, slice_i, mgmt_value, types=('FLAIR','T1w','T1wCE','T2w')):\n    plt.figure(figsize=(10,6))\n    patient_path = os.path.join('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/', str(brats21id).zfill(5))\n    \n    for i, t in enumerate(types, 1):\n        t_paths = sorted(glob.glob(os.path.join(patient_path, t, '*')), key = lambda x: int(x[:-4].split('-')[-1]))\n        data = load_dicom(t_paths[int(len(t_paths) * slice_i)])\n        plt.subplot(1, 4, i)\n        plt.imshow(data, cmap='gray');\n        plt.title(f'{t}', fontsize=16)\n        plt.axis('off')\n        \n    plt.suptitle(f'MGMT value: {mgmt_value}', fontsize=14)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:51:22.749968Z","iopub.execute_input":"2021-08-13T07:51:22.750812Z","iopub.status.idle":"2021-08-13T07:51:22.758536Z","shell.execute_reply.started":"2021-08-13T07:51:22.750743Z","shell.execute_reply":"2021-08-13T07:51:22.757514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in np.random.choice(range(len(train_df)), 10):\n    _brats21id = train_df.iloc[i].BraTS21ID\n    _mgmt_value = train_df.iloc[i].MGMT_value\n    visualise_sample(brats21id=_brats21id, mgmt_value=_mgmt_value, slice_i=0.5)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:51:24.523613Z","iopub.execute_input":"2021-08-13T07:51:24.524165Z","iopub.status.idle":"2021-08-13T07:51:28.584853Z","shell.execute_reply.started":"2021-08-13T07:51:24.524126Z","shell.execute_reply":"2021-08-13T07:51:28.583664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\ndef create_animations(ims):\n    fig = plt.figure(figsize=(10,6))\n    plt.axis('off')\n    im = plt.imshow(ims[0], cmap='gray')\n    \n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n    \n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval = 1000//20)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T08:43:08.667207Z","iopub.execute_input":"2021-08-13T08:43:08.667787Z","iopub.status.idle":"2021-08-13T08:43:08.675659Z","shell.execute_reply.started":"2021-08-13T08:43:08.667732Z","shell.execute_reply":"2021-08-13T08:43:08.674043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom_line(path):\n    t_paths = sorted(glob.glob(os.path.join(path, '*')), key = lambda x: int(x[:-4].split('-')[-1]))\n    \n    images=[]\n    for filename in t_paths:\n        data = load_dicom(filename)\n        if data.max() == 0:\n            continue\n        images.append(data)\n        \n    return images","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:51:53.382096Z","iopub.execute_input":"2021-08-13T07:51:53.382659Z","iopub.status.idle":"2021-08-13T07:51:53.389641Z","shell.execute_reply.started":"2021-08-13T07:51:53.382615Z","shell.execute_reply":"2021-08-13T07:51:53.388092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00234/T2w'","metadata":{"execution":{"iopub.status.busy":"2021-08-13T09:15:45.770586Z","iopub.execute_input":"2021-08-13T09:15:45.771225Z","iopub.status.idle":"2021-08-13T09:15:45.777917Z","shell.execute_reply.started":"2021-08-13T09:15:45.771185Z","shell.execute_reply":"2021-08-13T09:15:45.776528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df.BraTS21ID == 234].MGMT_value.values","metadata":{"execution":{"iopub.status.busy":"2021-08-13T07:57:14.964169Z","iopub.execute_input":"2021-08-13T07:57:14.964587Z","iopub.status.idle":"2021-08-13T07:57:14.973096Z","shell.execute_reply.started":"2021-08-13T07:57:14.964555Z","shell.execute_reply":"2021-08-13T07:57:14.972194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('MGMT Value of patient:', train_df[train_df.BraTS21ID == 234].MGMT_value.values)\nimages = load_dicom_line(path)\ncreate_animations(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T09:15:50.262135Z","iopub.execute_input":"2021-08-13T09:15:50.262709Z","iopub.status.idle":"2021-08-13T09:15:53.173836Z","shell.execute_reply.started":"2021-08-13T09:15:50.262672Z","shell.execute_reply":"2021-08-13T09:15:53.172533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('MGMT value of patient:', train_df[train_df.BraTS21ID == 510].MGMT_value.values)\npath = '/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00510/T2w'\nimages = load_dicom_line(path)\ncreate_animations(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T08:44:34.768763Z","iopub.execute_input":"2021-08-13T08:44:34.769167Z","iopub.status.idle":"2021-08-13T08:44:52.978366Z","shell.execute_reply.started":"2021-08-13T08:44:34.769124Z","shell.execute_reply":"2021-08-13T08:44:52.977216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time \n\nimport torch \nfrom torch import nn\nfrom torch.utils import data as torch_data\nfrom sklearn import model_selection as sk_model_selection\nfrom torch.nn import functional as torch_functional \nfrom efficientnet_pytorch import EfficientNet\nimport cv2\n\nfrom sklearn.model_selection import StratifiedKFold\n","metadata":{"execution":{"iopub.status.busy":"2021-08-13T08:04:56.104687Z","iopub.execute_input":"2021-08-13T08:04:56.105158Z","iopub.status.idle":"2021-08-13T08:04:56.120515Z","shell.execute_reply.started":"2021-08-13T08:04:56.105110Z","shell.execute_reply":"2021-08-13T08:04:56.119128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed):\n    np.random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    torch.manual_seed(seed)\n    if torch.cuda.is_available():\n        torch.cuda.manual_seed_all(seed)\n        torch.backends.cudnn.deterministic = True\n        \n        \nset_seed(42)        ","metadata":{"execution":{"iopub.status.busy":"2021-08-13T08:05:24.367449Z","iopub.execute_input":"2021-08-13T08:05:24.367840Z","iopub.status.idle":"2021-08-13T08:05:24.377278Z","shell.execute_reply.started":"2021-08-13T08:05:24.367809Z","shell.execute_reply":"2021-08-13T08:05:24.375594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\ndf_train, df_valid = sk_model_selection.train_test_split(df, test_size=0.2, random_state=42, stratify = df['MGMT_value'])","metadata":{"execution":{"iopub.status.busy":"2021-08-13T08:05:28.695624Z","iopub.execute_input":"2021-08-13T08:05:28.696176Z","iopub.status.idle":"2021-08-13T08:05:28.714668Z","shell.execute_reply.started":"2021-08-13T08:05:28.696141Z","shell.execute_reply":"2021-08-13T08:05:28.712789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DataRetriever(torch_data.Dataset):\n    def __init__(self, paths, targets):\n        self.paths = paths\n        self.targets = targets\n        \n    def __len__(self):\n        return len(self.paths)\n        \n    def __getitem__(self, index):\n        _id = self.paths[index]\n        patient_path = f'/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/{str(_id).zfill(5)}/'\n        channels = []\n        for t in ('FLAIR','T1w','T1wCE','T2w'):\n            t_paths = sorted(glob.glob(os.path.join(patient_path, t, '*')), key = lambda x: int(x[:-4].split('-')[-1]))\n            x = len(t_paths)\n            if x < 10:\n                r = range(x)\n            else:\n                d = x // 10\n                r = range(d, x - d, d)\n            \n            channel = []\n            for i in r:\n                channel.append(cv2.resize(load_dicom(t_paths[i]), (256,256)) / 255)\n            channel = np.mean(channel, axis=0)\n            channels.append(channel)\n        \n        y = torch.tensor(self.targets[index], dtype = torch.float)\n        \n        \n        return {'X': torch.tensor(channels).float(), 'y':y}","metadata":{"execution":{"iopub.status.busy":"2021-08-13T08:05:35.337627Z","iopub.execute_input":"2021-08-13T08:05:35.338015Z","iopub.status.idle":"2021-08-13T08:05:35.348926Z","shell.execute_reply.started":"2021-08-13T08:05:35.337983Z","shell.execute_reply":"2021-08-13T08:05:35.347408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_retriever = DataRetriever(df_train['BraTS21ID'].values, df_train['MGMT_value'].values)\n\nvalid_data_retriever = DataRetriever(df_valid['BraTS21ID'].values, df_valid['MGMT_value'].values)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T08:05:37.825099Z","iopub.execute_input":"2021-08-13T08:05:37.825588Z","iopub.status.idle":"2021-08-13T08:05:37.831858Z","shell.execute_reply.started":"2021-08-13T08:05:37.825552Z","shell.execute_reply":"2021-08-13T08:05:37.830106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(3):\n    plt.subplot(1, 3, i+1)\n    plt.imshow(train_data_retriever[100]['X'].numpy()[i], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-08-13T08:07:24.750069Z","iopub.execute_input":"2021-08-13T08:07:24.750474Z","iopub.status.idle":"2021-08-13T08:07:26.515062Z","shell.execute_reply.started":"2021-08-13T08:07:24.750440Z","shell.execute_reply":"2021-08-13T08:07:26.513789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        os.path.join(dirname, filename)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T09:52:45.600952Z","iopub.execute_input":"2021-08-13T09:52:45.601693Z","iopub.status.idle":"2021-08-13T09:52:49.857016Z","shell.execute_reply.started":"2021-08-13T09:52:45.601650Z","shell.execute_reply":"2021-08-13T09:52:49.856117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.net = EfficientNet.from_name('efficientnet-b0')\n        checkpoint = torch.load('/kaggle/input/nfnets/pytorch-image-models-master/')# to be completed\n        self.net.load_state_dict(checkpoint)\n        n_features = self.net._fc.in_features\n        self.net._fc = nn.Linear(in_features=n_features, out_features=1, bias = True)\n        \n    def forward(self, x):\n        out = self.net(x)\n        return(out)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T09:53:02.832457Z","iopub.execute_input":"2021-08-13T09:53:02.832993Z","iopub.status.idle":"2021-08-13T09:53:02.839744Z","shell.execute_reply.started":"2021-08-13T09:53:02.832961Z","shell.execute_reply":"2021-08-13T09:53:02.838344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LossMeter:\n    def __init__(self):\n        self.avg = 0\n        self.n = 0\n        \n    def update(self, val):\n        self.n += 1\n        #incremental update\n        self.avg = val / self.n + (self.n - 1) / self.n * self.avg\n        \n\nclass AccMeter:\n    def __init__(self):\n        self.avg = 0\n        self.n = 0\n        \n    def update(self, y_true, y_pred):\n        y_true = y_true.cpu().numpy().astype(int)\n        y_pred = y_pred.cpu().numpy() >= 0\n        last_n = self.n\n        self.n += len(y_true)\n        true_count = np.sum(y_true == y_pred)\n        #incremental update\n        self.avg = true_count / self.n + last_n / self.n * self.avg","metadata":{"execution":{"iopub.status.busy":"2021-08-13T08:10:45.249978Z","iopub.execute_input":"2021-08-13T08:10:45.250382Z","iopub.status.idle":"2021-08-13T08:10:45.259590Z","shell.execute_reply.started":"2021-08-13T08:10:45.250347Z","shell.execute_reply":"2021-08-13T08:10:45.258133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Trainer:\n    def __init__(\n        self,\n        model,\n        device,\n        optimizer,\n        criterion,\n        loss_meter,\n        score_meter):\n        self.model = model\n        self.device = device\n        self.optimizer = optimizer\n        self.criterion = criterion\n        self.loss_meter = loss_meter\n        self.score_meter = score_meter\n        \n        self.best_valid_score = -np.inf\n        self.n_patience = 0\n        \n        self.messages = {\n            'epoch': '[Epoch {}: {}] loss: {:.5f}, score: {:.5f}, time: {} s',\n            'checkpoint': 'The score improved from {:.5f} to {:.5f}. Save model to {}',\n            'patience': \"\\nValid score didn't improve last {} epochs\"\n        }\n        \n    def fit(self, epochs, train_loader, valid_loader, save_path, patience):\n        for n_epoch in range(1, epochs + 1):\n            self.info_message(f'EPOCH: {n_epoch}')\n            \n            train_loss, train_score, train_time = self.train_epoch(train_loader)\n            valid_loss, valid_score, valid_time = self.valid_epoch(valid_loader)\n            \n            self.info_message(self.messages['epoch'], 'Train', n_epoch, train_loss, train_score, train_time)\n            \n            self.info_message(self.messages['epoch'], 'Valid', n_epoch, valid_loss, valid_score, valid_time)\n            \n            if True:\n#                 if self.best_valid_score < valid_score:\n                self.info_message(self.messages['checkpoint'], self.best_valid_score, valid_score, save_path)\n                self.best_valid_score = valid_score\n                self.save_model(n_epoch, save_path)\n                self.n_patience = 0\n            else:\n                self.n_patience += 1\n                \n            if self.n_patience >= patience:\n                self.info_message(self.messages['patience'], patience)\n                break\n                \n    def train_epoch (self, train_loader):\n        self.model.train()\n        t = time.time()\n        train_loss = self.loss_meter()\n        train_score = self.score_meter()\n        \n        for step, batch in enumerate(train_loader, 1):\n            X = batch['X'].to(self.device)\n            target = batch['y'].to(self.device)\n            self.optimizer.zero_grad()\n            outputs = self.model(X).squeeze(1)\n            \n            loss = self.criterion(outputs, targets)\n            loss.backward()\n            \n            train_loss.update(loss.detach().item())\n            train_score.update(targets, outputs.detach())\n            \n            self.optimizer.step()\n            \n            _loss, _score = train_loss.avg, train_score.avg\n            message = 'Train Step {}/{}, train_loss: {:.5f}, train_score: {:.5f}'\n            self.info_message(message, step, len(train_loader), _loss, _score, end='\\r')\n            \n        return train_loss.avg, train_score.avg, int(time.time() - t)\n    \n    \n    def valid_epoch(self, valid_loader):\n        self.model.eval()\n        t = time.time()\n        valid_loss = self.loss_meter()\n        valid_score = self.score_meter()\n        \n        for step, batch in enumerate(valid_loader, 1):\n            with torch.no_grad():\n                X = batch['X'].to(self.device)\n                target = batch['y'].to(self.device)\n                outputs = self.model(X).squeeze(1)\n                loss = self.criterion(outputs, targets)\n                \n                valid_loss.update(loss.detach().item())\n                valid_score.update(targets, outputs)\n                \n            _loss, _score = valid_loss.avg, valid_score.avg\n            message = 'Valid Step {}/{}, valid_loss: {:.5f}, valid_score: {:.5f}'\n            self.info_message(message, step, len(valid_loader), _loss, _score, end='\\r')\n            \n        return valid_loss.avg, valid_score.avg, int(time.time() - t)\n    \n    def save_model(self, n_epoch, save_path):\n        torch.save(\n        {\n            'model_state_dict': self.model.state_dict(),\n            'optimizer_state_dict': self.optimizer.state_dict(),\n            'best_valid_score': self.best_valid_score,\n            'n_epoch': n_epoch\n        },\n        save_path\n        )\n        \n    @staticmethod\n    def info_message(message, *args, end='\\n'):\n        print(message.format(*args), end=end)","metadata":{"execution":{"iopub.status.busy":"2021-08-13T08:10:51.628743Z","iopub.execute_input":"2021-08-13T08:10:51.629116Z","iopub.status.idle":"2021-08-13T08:10:51.651543Z","shell.execute_reply.started":"2021-08-13T08:10:51.629087Z","shell.execute_reply":"2021-08-13T08:10:51.650051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\ntrain_data_retreiver = DataRetriever(\n    df_train['BraTS21ID'].values,\n    df_train['MGMT_value'].values)\n\nvalid_data_retreiver = DataRetriever(\n    df_valid['BraTS21ID'].values,\n    df_valid['MGMT_value'].values)\n\ntrain_loader = torch_data.DataLoader(\n    train_data_retreiver,\n    batch_size=8,\n    shuffle=True,\n    num_workers=8)\n\nvalid_loader = torch_data.DataLoader(\n    valid_data_retreiver,\n    batch_size=8,\n    shuffle=False,\n    num_workers=8)\n\nmodel = Model()\nmodel.to(device)\n# \noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)\ncriterion = torch_functional.binary_cross_entropy_with_logits\n\ntrainer = Trainer(\n    model,\n    device,\n    optimizer,\n    criterion,\n    LossMeter,\n    AccMeter)\n\nhistory = trainer.fit(\n    2,\n    train_loader,\n    valid_loader,\n    f'best_model-0.path',\n    100)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Work in Progress . .","metadata":{"execution":{"iopub.status.busy":"2021-08-16T05:54:03.351792Z","iopub.execute_input":"2021-08-16T05:54:03.352408Z","iopub.status.idle":"2021-08-16T05:54:03.356627Z","shell.execute_reply.started":"2021-08-16T05:54:03.352322Z","shell.execute_reply":"2021-08-16T05:54:03.355653Z"}}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}