{"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":"markdown","source":"Thanks to :\n\n    https://www.kaggle.com/arnabs007/part-1-rsna-miccai-btrc-understanding-the-data\n    https://www.kaggle.com/rluethy/efficientnet3d-with-one-mri-type\n    https://www.kaggle.com/ammarnassanalhajali/brain-tumor-3d-training\n    https://www.kaggle.com/ammarnassanalhajali/brain-tumor-3d-inference\n    https://keras.io/examples/vision/3D_image_classification/\n    ","metadata":{"papermill":{"duration":0.0143,"end_time":"2021-08-19T20:35:07.545878","exception":false,"start_time":"2021-08-19T20:35:07.531578","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import os \nimport glob\nimport re\nimport math\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport sys\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\nfrom random import shuffle\nfrom sklearn import model_selection as sk_model_selection\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom tensorflow.keras.metrics import AUC\n\n","metadata":{"papermill":{"duration":5.035942,"end_time":"2021-08-19T20:35:12.596347","exception":false,"start_time":"2021-08-19T20:35:07.560405","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-02T12:24:21.388194Z","iopub.execute_input":"2021-09-02T12:24:21.388667Z","iopub.status.idle":"2021-09-02T12:24:26.531883Z","shell.execute_reply.started":"2021-09-02T12:24:21.388582Z","shell.execute_reply":"2021-09-02T12:24:26.530985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install keras\n# !pip install keras_applications","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:26.533398Z","iopub.execute_input":"2021-09-02T12:24:26.533723Z","iopub.status.idle":"2021-09-02T12:24:26.538128Z","shell.execute_reply.started":"2021-09-02T12:24:26.533689Z","shell.execute_reply":"2021-09-02T12:24:26.536252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from keras_applications.imagenet_utils import _obtain_input_shape ","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:26.540190Z","iopub.execute_input":"2021-09-02T12:24:26.540621Z","iopub.status.idle":"2021-09-02T12:24:26.550015Z","shell.execute_reply.started":"2021-09-02T12:24:26.540583Z","shell.execute_reply":"2021-09-02T12:24:26.549199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# keras_3d_path = \"../input/keras-efficientnet3d-orig\"\n\n# sys.path.append(keras_3d_path)\n# import efficientnet_3D.keras as efn ","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:26.553517Z","iopub.execute_input":"2021-09-02T12:24:26.553826Z","iopub.status.idle":"2021-09-02T12:24:26.558829Z","shell.execute_reply.started":"2021-09-02T12:24:26.553802Z","shell.execute_reply":"2021-09-02T12:24:26.558045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ## Enable this to run on PC\n\n# print(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))\n\n# gpus = tf.config.list_physical_devices('GPU')\n# tf.config.experimental.set_memory_growth(gpus[0], enable=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:26.560037Z","iopub.execute_input":"2021-09-02T12:24:26.560607Z","iopub.status.idle":"2021-09-02T12:24:26.566893Z","shell.execute_reply.started":"2021-09-02T12:24:26.560570Z","shell.execute_reply":"2021-09-02T12:24:26.566092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Data","metadata":{"papermill":{"duration":0.015919,"end_time":"2021-08-19T20:35:12.627716","exception":false,"start_time":"2021-08-19T20:35:12.611797","status":"completed"},"tags":[]}},{"cell_type":"code","source":"data_directory = '../input/rsna-miccai-brain-tumor-radiogenomic-classification'\n# pytorch3dpath = \"../input/efficientnetpyttorch3d/EfficientNet-PyTorch-3D\"\n \nmri_types_orig = ['FLAIR','T1w','T1wCE','T2w']\nmri_types = ['FLAIR','T1w','T1wCE','T2w']\n\nIMAGE_SIZE = 128\nNUM_IMAGES_PER_TYPE = 32\nNUM_IMAGES = NUM_IMAGES_PER_TYPE * len(mri_types)\nBATCH_SIZE= 4\n\ntrain_df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\n\nto_exclude = [109, 123, 709]\ntrain_df = train_df[~train_df['BraTS21ID'].isin(to_exclude)]\n\ntrain_df['BraTS21ID5'] = [format(x, '05d') for x in train_df.BraTS21ID]\nprint(len(train_df))\ntrain_df.head(3)","metadata":{"papermill":{"duration":0.053371,"end_time":"2021-08-19T20:35:12.696135","exception":false,"start_time":"2021-08-19T20:35:12.642764","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-02T12:24:26.569343Z","iopub.execute_input":"2021-09-02T12:24:26.569594Z","iopub.status.idle":"2021-09-02T12:24:26.609086Z","shell.execute_reply.started":"2021-09-02T12:24:26.569570Z","shell.execute_reply":"2021-09-02T12:24:26.608368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')\ntest=sample_submission\ntest['BraTS21ID5'] = [format(x, '05d') for x in test.BraTS21ID]\ntest.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:26.611514Z","iopub.execute_input":"2021-09-02T12:24:26.611751Z","iopub.status.idle":"2021-09-02T12:24:26.629465Z","shell.execute_reply.started":"2021-09-02T12:24:26.611728Z","shell.execute_reply":"2021-09-02T12:24:26.628782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Functions to load images\n","metadata":{"papermill":{"duration":0.015034,"end_time":"2021-08-19T20:35:12.726544","exception":false,"start_time":"2021-08-19T20:35:12.71151","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def load_dicom_image(path, img_size=IMAGE_SIZE, voi_lut=True, rotate=0):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n        \n    if rotate > 0:\n        rot_choices = [0, cv2.ROTATE_90_CLOCKWISE, cv2.ROTATE_90_COUNTERCLOCKWISE, cv2.ROTATE_180]\n        data = cv2.rotate(data, rot_choices[rotate])\n        \n    data = cv2.resize(data, (img_size, img_size))\n    return data\n\n\ndef load_dicom_images_3d(scan_id, split = 'train', mri_type=\"FLAIR\", num_imgs=NUM_IMAGES_PER_TYPE, img_size=IMAGE_SIZE, rotate=0):\n\n    files = sorted(glob.glob(f\"{data_directory}/{split}/{scan_id}/{mri_type}/*.dcm\"), \n               key=lambda var:[int(x) if x.isdigit() else x for x in re.findall(r'[^0-9]|[0-9]+', var)])\n\n    middle = len(files)//2\n    num_imgs2 = num_imgs\n    p1 = max(0, middle - num_imgs2)\n    p2 = min(len(files), middle + num_imgs2)\n    img3d = np.stack([load_dicom_image(f, rotate=rotate) for f in files[p1:p2:2]]).T \n\n    if img3d.shape[-1] <= num_imgs//2:\n        middle = len(files)//2\n        num_imgs2 = num_imgs//2\n        p1 = max(0, middle - num_imgs2)\n        p2 = min(len(files), middle + num_imgs2)\n        img3d = np.stack([load_dicom_image(f, rotate=rotate) for f in files[p1:p2]]).T \n    \n    if img3d.shape[-1] < num_imgs:\n        # n_zero = np.zeros((img_size, img_size, num_imgs - img3d.shape[-1]))\n        n_zero_front = np.zeros((img_size, img_size, (num_imgs - img3d.shape[-1])//2))\n        n_zero_back = np.zeros((img_size, img_size, num_imgs - img3d.shape[-1] - n_zero_front.shape[-1]))\n        # img3d = np.concatenate((img3d, n_zero), axis = -1)\n        img3d = np.concatenate((n_zero_front, img3d, n_zero_back), axis = -1)\n\n    if np.min(img3d) < np.max(img3d):\n        img3d = img3d - np.min(img3d)\n        img3d = img3d / np.max(img3d)\n            \n    return np.expand_dims(img3d,0)\n\ndef load_dicom_images_3d_all(scan_id, split = 'train'):\n    img3d_all = np.concatenate([load_dicom_images_3d(scan_id, split, mri_type) for mri_type in mri_types], axis = -1)\n    \n    return img3d_all\n\na = load_dicom_images_3d_all(\"00001\", 'test')\nprint(a.shape)\nprint(np.min(a), np.max(a), np.mean(a), np.median(a))","metadata":{"papermill":{"duration":1.266786,"end_time":"2021-08-19T20:35:14.00852","exception":false,"start_time":"2021-08-19T20:35:12.741734","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-02T12:24:26.632114Z","iopub.execute_input":"2021-09-02T12:24:26.632439Z","iopub.status.idle":"2021-09-02T12:24:28.272470Z","shell.execute_reply.started":"2021-09-02T12:24:26.632405Z","shell.execute_reply":"2021-09-02T12:24:28.271536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import Sequence\n\nclass Dataset(Sequence):\n    def __init__(self,df,split = 'train', is_train=True,batch_size=BATCH_SIZE,shuffle=True):\n        self.idx = df[\"BraTS21ID\"].values\n        self.paths = df[\"BraTS21ID5\"].values\n        self.y =  df[\"MGMT_value\"].values\n        self.is_train = is_train\n        self.batch_size = batch_size\n        self.shuffle = shuffle\n        self.split = split\n        \n    def __len__(self):\n        return math.ceil(len(self.idx)/self.batch_size)\n   \n    def __getitem__(self,ids):\n        id_path= self.paths[ids]\n        batch_paths = self.paths[ids * self.batch_size:(ids + 1) * self.batch_size]\n        split=self.split\n        \n        if self.y is not None:\n            batch_y = self.y[ids * self.batch_size: (ids + 1) * self.batch_size]\n            \n        if split == 'train':\n            list_x =  [load_dicom_images_3d_all(x, split) for x in batch_paths]\n            batch_X = np.stack(list_x, axis=4)\n        else:\n            list_x =  load_dicom_images_3d_all(id_path, split)\n            batch_X = np.stack(list_x)\n            \n        if self.is_train:\n            return batch_X, batch_y\n        else:\n            return batch_X\n","metadata":{"papermill":{"duration":0.034492,"end_time":"2021-08-19T20:35:16.541772","exception":false,"start_time":"2021-08-19T20:35:16.50728","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-02T12:24:28.274347Z","iopub.execute_input":"2021-09-02T12:24:28.274852Z","iopub.status.idle":"2021-09-02T12:24:28.284381Z","shell.execute_reply.started":"2021-09-02T12:24:28.274811Z","shell.execute_reply":"2021-09-02T12:24:28.283508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting Data","metadata":{"papermill":{"duration":0.021535,"end_time":"2021-08-19T20:35:16.384696","exception":false,"start_time":"2021-08-19T20:35:16.363161","status":"completed"},"tags":[]}},{"cell_type":"code","source":"df_train, df_valid = sk_model_selection.train_test_split(\n    train_df, \n    test_size=0.2, \n    random_state=12, \n    stratify=train_df[\"MGMT_value\"],\n)","metadata":{"papermill":{"duration":0.03518,"end_time":"2021-08-19T20:35:16.441369","exception":false,"start_time":"2021-08-19T20:35:16.406189","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-02T12:24:28.287138Z","iopub.execute_input":"2021-09-02T12:24:28.287406Z","iopub.status.idle":"2021-09-02T12:24:28.298043Z","shell.execute_reply.started":"2021-09-02T12:24:28.287382Z","shell.execute_reply":"2021-09-02T12:24:28.297029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:28.301103Z","iopub.execute_input":"2021-09-02T12:24:28.301374Z","iopub.status.idle":"2021-09-02T12:24:28.316757Z","shell.execute_reply.started":"2021-09-02T12:24:28.301350Z","shell.execute_reply":"2021-09-02T12:24:28.315820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = Dataset(df_train, 'train')\nvalid_dataset = Dataset(df_valid, 'train')\n# test_dataset = Dataset(test, 'test', is_train=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:28.319215Z","iopub.execute_input":"2021-09-02T12:24:28.319537Z","iopub.status.idle":"2021-09-02T12:24:28.323213Z","shell.execute_reply.started":"2021-09-02T12:24:28.319511Z","shell.execute_reply":"2021-09-02T12:24:28.322442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_df","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:28.324348Z","iopub.execute_input":"2021-09-02T12:24:28.324779Z","iopub.status.idle":"2021-09-02T12:24:28.333967Z","shell.execute_reply.started":"2021-09-02T12:24:28.324745Z","shell.execute_reply":"2021-09-02T12:24:28.333171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Plotting train data","metadata":{}},{"cell_type":"code","source":"def plot_sample_all(images, label, j): \n    plt.figure(figsize=(35, 35))\n    for i in range(NUM_IMAGES):\n        # plt.subplot(14,14,(i+1))\n        plt.subplot(16,16,(i+1))\n        plt.xticks([])\n        plt.yticks([])\n        plt.grid(False)\n        plt.imshow(images[0,:,:,i,j], cmap=\"gray\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:28.335332Z","iopub.execute_input":"2021-09-02T12:24:28.335699Z","iopub.status.idle":"2021-09-02T12:24:28.342741Z","shell.execute_reply.started":"2021-09-02T12:24:28.335661Z","shell.execute_reply":"2021-09-02T12:24:28.341769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0\nj = 0\nimages, label = train_dataset[i]\nplot_sample_all(images, label, j)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:28.344230Z","iopub.execute_input":"2021-09-02T12:24:28.344682Z","iopub.status.idle":"2021-09-02T12:24:38.473961Z","shell.execute_reply.started":"2021-09-02T12:24:28.344629Z","shell.execute_reply":"2021-09-02T12:24:38.473143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0\nj = 1\nimages, label = train_dataset[i]\nplot_sample_all(images, label, j)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-09-02T12:24:38.474988Z","iopub.execute_input":"2021-09-02T12:24:38.475285Z","iopub.status.idle":"2021-09-02T12:24:46.358847Z","shell.execute_reply.started":"2021-09-02T12:24:38.475255Z","shell.execute_reply":"2021-09-02T12:24:46.352473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0\nj = 2\nimages, label = train_dataset[i]\nplot_sample_all(images, label, j)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:46.360330Z","iopub.execute_input":"2021-09-02T12:24:46.360673Z","iopub.status.idle":"2021-09-02T12:24:53.811876Z","shell.execute_reply.started":"2021-09-02T12:24:46.360640Z","shell.execute_reply":"2021-09-02T12:24:53.810940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0\nj = 3\nimages, label = train_dataset[i]\nplot_sample_all(images, label, j)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:24:53.813222Z","iopub.execute_input":"2021-09-02T12:24:53.813569Z","iopub.status.idle":"2021-09-02T12:25:01.475223Z","shell.execute_reply.started":"2021-09-02T12:24:53.813535Z","shell.execute_reply":"2021-09-02T12:25:01.474311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting the middle images of each MRI types","metadata":{}},{"cell_type":"markdown","source":"## Train images","metadata":{}},{"cell_type":"code","source":"def plot_sample_train(images, label): \n    plt.figure(figsize=(16, 16))\n    idx_base = int(NUM_IMAGES_PER_TYPE/2)\n    idx = [idx_base, idx_base*3, idx_base*5, idx_base*7]\n    # idx = [idx_base, idx_base*3, idx_base*5]\n    for i in range(len(idx)*BATCH_SIZE):\n        plt.subplot(BATCH_SIZE,len(idx),i+1)\n        plt.xticks([])\n        plt.yticks([])\n        plt.grid(False)\n        j = int(i/len(idx))\n        plt.imshow(images[0,:,:,idx[i%len(idx)],j], cmap=\"gray\")\n        plt.xlabel(f'{idx[i%len(idx)]} {label[j]}')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:25:01.476614Z","iopub.execute_input":"2021-09-02T12:25:01.477001Z","iopub.status.idle":"2021-09-02T12:25:01.485491Z","shell.execute_reply.started":"2021-09-02T12:25:01.476958Z","shell.execute_reply":"2021-09-02T12:25:01.484482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0\nimages, label = train_dataset[i]\nprint(\"Dimension of the CT scan is:\", images.shape)\nprint(\"label=\",label ,label.shape)\nplot_sample_train(images, label)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:25:01.486842Z","iopub.execute_input":"2021-09-02T12:25:01.487185Z","iopub.status.idle":"2021-09-02T12:25:04.380010Z","shell.execute_reply.started":"2021-09-02T12:25:01.487153Z","shell.execute_reply":"2021-09-02T12:25:04.379088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{"papermill":{"duration":0.024861,"end_time":"2021-08-19T20:35:19.407712","exception":false,"start_time":"2021-08-19T20:35:19.382851","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def get_model(width=IMAGE_SIZE, height=IMAGE_SIZE, depth=NUM_IMAGES):\n    \"\"\"Build a 3D convolutional neural network model.\"\"\"\n\n    inputs = keras.Input((width, height, depth, 1))\n\n    x = layers.Conv3D(filters=64, kernel_size=3, activation=\"relu\")(inputs)\n    x = layers.BatchNormalization()(x)\n    \n    x = layers.Conv3D(filters=64, kernel_size=3, activation=\"relu\")(x)\n    x = layers.BatchNormalization()(x)\n    \n    x = layers.MaxPool3D(pool_size=2)(x)\n    x = layers.Dropout(0.2)(x)\n\n    x = layers.Conv3D(filters=128, kernel_size=3, activation=\"relu\")(x)\n    x = layers.BatchNormalization()(x)\n    \n    x = layers.Conv3D(filters=128, kernel_size=3, activation=\"relu\")(x)\n    x = layers.BatchNormalization()(x)\n    \n    x = layers.MaxPool3D(pool_size=2)(x)\n    x = layers.Dropout(0.2)(x)\n    \n    x = layers.Conv3D(filters=256, kernel_size=3, activation=\"relu\")(x)\n    x = layers.BatchNormalization()(x)\n\n    x = layers.Conv3D(filters=256, kernel_size=3, activation=\"relu\")(x)\n    x = layers.BatchNormalization()(x)\n    \n    x = layers.MaxPool3D(pool_size=2)(x)\n    x = layers.Dropout(0.2)(x)\n\n    x = layers.GlobalAveragePooling3D()(x)\n\n    x = layers.Dense(units=256, activation=\"relu\")(x)\n    x = layers.Dropout(0.4)(x)\n\n    outputs = layers.Dense(units=1, activation=\"sigmoid\")(x)\n\n    # Define the model.\n    model = keras.Model(inputs, outputs, name=\"3D_CNN\")\n\n    return model","metadata":{"papermill":{"duration":2.318395,"end_time":"2021-08-19T20:35:21.750413","exception":false,"start_time":"2021-08-19T20:35:19.432018","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-02T12:25:04.381313Z","iopub.execute_input":"2021-09-02T12:25:04.381784Z","iopub.status.idle":"2021-09-02T12:25:04.394844Z","shell.execute_reply.started":"2021-09-02T12:25:04.381737Z","shell.execute_reply":"2021-09-02T12:25:04.393940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def get_model(width=IMAGE_SIZE, height=IMAGE_SIZE, depth=NUM_IMAGES):\n#     \"\"\"Build a 3D convolutional neural network model.\"\"\"\n\n#     inputs = keras.Input((width, height, depth, 1))\n\n#     model = efn.EfficientNetB0(input_shape=inputs, weights='imagenet')\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:25:04.397120Z","iopub.execute_input":"2021-09-02T12:25:04.397675Z","iopub.status.idle":"2021-09-02T12:25:04.410869Z","shell.execute_reply.started":"2021-09-02T12:25:04.397635Z","shell.execute_reply":"2021-09-02T12:25:04.410106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build model.\nmodel = get_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:25:04.415191Z","iopub.execute_input":"2021-09-02T12:25:04.415497Z","iopub.status.idle":"2021-09-02T12:25:06.516874Z","shell.execute_reply.started":"2021-09-02T12:25:04.415472Z","shell.execute_reply":"2021-09-02T12:25:06.516119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{"papermill":{"duration":0.023922,"end_time":"2021-08-19T20:35:21.799287","exception":false,"start_time":"2021-08-19T20:35:21.775365","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Compile model.\ninitial_learning_rate = 0.0001\nlr_schedule = keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate, decay_steps=100000, decay_rate=0.96, staircase=True\n)\nmodel.compile(\n    loss=\"binary_crossentropy\",\n    optimizer=keras.optimizers.Adam(learning_rate=lr_schedule),\n#     metrics=['acc'],\n    metrics=[AUC(name='auc'), 'acc'],\n)\n# Define callbacks.\nmodel_save = ModelCheckpoint('Brain_Tumor_All_MRI_3D_CNN.h5', \n                             save_best_only = True, \n                             monitor = 'val_acc', \n                             mode = 'max', verbose = 1)\nearly_stop = EarlyStopping(monitor = 'val_acc', \n                           patience = 30, mode = 'max', verbose = 1,\n                           restore_best_weights = True)\n\n# Train the model, doing validation at the end of each epoch\nepochs = 100\nmodel.fit(\n    train_dataset,\n    validation_data=valid_dataset,\n    epochs=epochs,\n    batch_size=1,\n    shuffle=True,\n    verbose=1,\n    callbacks = [model_save, early_stop],\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T12:25:06.519966Z","iopub.execute_input":"2021-09-02T12:25:06.520214Z","iopub.status.idle":"2021-09-02T19:08:28.655991Z","shell.execute_reply.started":"2021-09-02T12:25:06.520189Z","shell.execute_reply":"2021-09-02T19:08:28.654137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing model performance","metadata":{"papermill":{"duration":1.077633,"end_time":"2021-08-19T22:36:58.345072","exception":false,"start_time":"2021-08-19T22:36:57.267439","status":"completed"},"tags":[]}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(20, 7))\nax = ax.ravel()\n\nfor i, metric in enumerate([\"acc\", \"loss\"]):\n    ax[i].plot(model.history.history[metric])\n    ax[i].plot(model.history.history[\"val_\" + metric])\n    ax[i].set_title(\"Model {}\".format(metric))\n    ax[i].set_xlabel(\"epochs\")\n    ax[i].set_ylabel(metric)\n    ax[i].legend([\"train\", \"val\"])","metadata":{"papermill":{"duration":1.339771,"end_time":"2021-08-19T22:37:00.618072","exception":false,"start_time":"2021-08-19T22:36:59.278301","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-02T19:08:28.661261Z","iopub.execute_input":"2021-09-02T19:08:28.661584Z","iopub.status.idle":"2021-09-02T19:08:29.090398Z","shell.execute_reply.started":"2021-09-02T19:08:28.661552Z","shell.execute_reply":"2021-09-02T19:08:29.089424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Model Weights from file","metadata":{}},{"cell_type":"code","source":"# model.load_weights('../input/brain-tumor-model-v3-6/Brain_Tumor_All_MRI_3D_CNN_v3_6.h5')","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:08:29.091917Z","iopub.execute_input":"2021-09-02T19:08:29.092267Z","iopub.status.idle":"2021-09-02T19:08:29.096197Z","shell.execute_reply.started":"2021-09-02T19:08:29.092232Z","shell.execute_reply":"2021-09-02T19:08:29.095215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Images","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.utils import Sequence\n\nclass Dataset(Sequence):\n    def __init__(self,df,is_train=True,batch_size=1,shuffle=True):\n        self.idx = df[\"BraTS21ID\"].values\n        self.paths = df[\"BraTS21ID5\"].values\n        self.y =  df[\"MGMT_value\"].values\n        self.is_train = is_train\n        self.batch_size = batch_size\n        self.shuffle = shuffle\n    def __len__(self):\n        return math.ceil(len(self.idx)/self.batch_size)\n   \n    def __getitem__(self,ids):\n        id_path= self.paths[ids]\n        batch_paths = self.paths[ids * self.batch_size:(ids + 1) * self.batch_size]\n        \n        if self.y is not None:\n            batch_y = self.y[ids * self.batch_size: (ids + 1) * self.batch_size]\n            \n        list_x =  load_dicom_images_3d_all(id_path, 'test') #str(scan_id).zfill(5)\n        #list_x =  [load_dicom_images_3d(x) for x in batch_paths]\n        batch_X = np.stack(list_x)\n        if self.is_train:\n            return batch_X,batch_y\n        else:\n            return batch_X\n    \n    def on_epoch_end(self):\n        if self.shuffle and self.is_train:\n            ids_y = list(zip(self.idx, self.y))\n            shuffle(ids_y)\n            self.idx, self.y = list(zip(*ids_y))","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:08:29.097927Z","iopub.execute_input":"2021-09-02T19:08:29.098462Z","iopub.status.idle":"2021-09-02T19:08:29.111531Z","shell.execute_reply.started":"2021-09-02T19:08:29.098418Z","shell.execute_reply":"2021-09-02T19:08:29.110609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = Dataset(test,is_train=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:08:29.112889Z","iopub.execute_input":"2021-09-02T19:08:29.113360Z","iopub.status.idle":"2021-09-02T19:08:29.123756Z","shell.execute_reply.started":"2021-09-02T19:08:29.113224Z","shell.execute_reply":"2021-09-02T19:08:29.122996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sample_test(images): \n    plt.figure(figsize=(16, 16))\n    idx_base = int(NUM_IMAGES_PER_TYPE/2)\n    idx = [idx_base, idx_base*3, idx_base*5, idx_base*7]\n    # idx = [idx_base, idx_base*3, idx_base*5]\n    # idx = [idx_base]\n    for i in range(len(idx)):\n        plt.subplot(1,len(idx),i+1)\n        plt.xticks([])\n        plt.yticks([])\n        plt.grid(False)\n        plt.imshow(images[0,:,:,idx[i]], cmap=\"gray\")\n        plt.xlabel(idx[i])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:08:29.125001Z","iopub.execute_input":"2021-09-02T19:08:29.125377Z","iopub.status.idle":"2021-09-02T19:08:29.132986Z","shell.execute_reply.started":"2021-09-02T19:08:29.125343Z","shell.execute_reply":"2021-09-02T19:08:29.131816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0\nimages = test_dataset[i]\n# print(test_dataset[0])\nprint(\"Dimension of the CT scan is:\", images.shape)\nplot_sample_test(images)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:08:29.134349Z","iopub.execute_input":"2021-09-02T19:08:29.134698Z","iopub.status.idle":"2021-09-02T19:08:30.887248Z","shell.execute_reply.started":"2021-09-02T19:08:29.134663Z","shell.execute_reply":"2021-09-02T19:08:30.886472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_dataset)\npredictions = predictions.reshape(-1)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:08:30.888621Z","iopub.execute_input":"2021-09-02T19:08:30.889017Z","iopub.status.idle":"2021-09-02T19:10:18.668602Z","shell.execute_reply.started":"2021-09-02T19:08:30.888976Z","shell.execute_reply":"2021-09-02T19:10:18.667715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame({'BraTS21ID':sample_submission['BraTS21ID'],'MGMT_value':predictions})","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:10:18.669854Z","iopub.execute_input":"2021-09-02T19:10:18.670177Z","iopub.status.idle":"2021-09-02T19:10:18.674798Z","shell.execute_reply.started":"2021-09-02T19:10:18.670145Z","shell.execute_reply":"2021-09-02T19:10:18.674020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:10:18.675959Z","iopub.execute_input":"2021-09-02T19:10:18.676581Z","iopub.status.idle":"2021-09-02T19:10:18.699097Z","shell.execute_reply.started":"2021-09-02T19:10:18.676542Z","shell.execute_reply":"2021-09-02T19:10:18.698254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['BraTS21ID'] = [format(x, '05d') for x in submission.BraTS21ID]","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:10:18.700670Z","iopub.execute_input":"2021-09-02T19:10:18.701049Z","iopub.status.idle":"2021-09-02T19:10:18.707575Z","shell.execute_reply.started":"2021-09-02T19:10:18.701012Z","shell.execute_reply":"2021-09-02T19:10:18.706547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:10:18.708785Z","iopub.execute_input":"2021-09-02T19:10:18.709256Z","iopub.status.idle":"2021-09-02T19:10:18.722935Z","shell.execute_reply.started":"2021-09-02T19:10:18.709220Z","shell.execute_reply":"2021-09-02T19:10:18.721994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T19:10:18.723917Z","iopub.execute_input":"2021-09-02T19:10:18.725616Z","iopub.status.idle":"2021-09-02T19:10:18.736358Z","shell.execute_reply.started":"2021-09-02T19:10:18.725589Z","shell.execute_reply":"2021-09-02T19:10:18.735469Z"},"trusted":true},"execution_count":null,"outputs":[]}]}