{"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":"I'm reading through several existing notebooks and trying to distill down the information into a new notebook to help me understand the project.  All help appreciated!\n\n# References\n\n- [Advanced EDA - Brain Tumor Data](https://www.kaggle.com/smoschou55/advanced-eda-brain-tumor-data)\n- [Team 9 Second Week](https://www.kaggle.com/evanyao27/team-9-second-week)\n  - The only model that is working. get_model02()\n- [Dataset to Model with Tensorflow](https://www.kaggle.com/ohbewise/dataset-to-model-with-tensorflow)\n- [Brain Tumer Train Class Flair](https://www.kaggle.com/lucamtb/brain-tumer-train-class-flair)\n  - Uses TPU\n  - Generates a Tensorflow model: Brain_flair_model_effect_3e-05_0.0001.h5\n- [Brain Tumor very basic inference](https://www.kaggle.com/lucamtb/brain-tumor-very-basice-inference)\n  - Uses the above mentioned model: Brain_flair_model_effect_3e-05_0.0001.h5\n  - Add this Kaggle Dataset: https://www.kaggle.com/lucamtb/effect0-brain","metadata":{}},{"cell_type":"markdown","source":"# Load Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport glob\n\nimport pandas as pd\nimport numpy as np\nfrom pathlib import Path\n\nimport random\nfrom tqdm.notebook import tqdm\nimport pydicom # Handle MRI images\n\nimport cv2  # OpenCV - https://docs.opencv.org/master/d6/d00/tutorial_py_root.html\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_auc_score\n\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import layers\n","metadata":{"execution":{"iopub.status.busy":"2021-10-03T16:56:41.518469Z","iopub.execute_input":"2021-10-03T16:56:41.519158Z","iopub.status.idle":"2021-10-03T16:56:46.84067Z","shell.execute_reply.started":"2021-10-03T16:56:41.519072Z","shell.execute_reply":"2021-10-03T16:56:46.839915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Configuration, Constants, Setup","metadata":{}},{"cell_type":"code","source":"data_dir = Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/')\n\nmri_types = [\"FLAIR\", \"T1w\", \"T2w\", \"T1wCE\"]\nexcluded_images = [109, 123, 709] # Bad images","metadata":{"execution":{"iopub.status.busy":"2021-10-03T16:56:46.842252Z","iopub.execute_input":"2021-10-03T16:56:46.842699Z","iopub.status.idle":"2021-10-03T16:56:46.854116Z","shell.execute_reply.started":"2021-10-03T16:56:46.842663Z","shell.execute_reply":"2021-10-03T16:56:46.852608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Datasets","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(data_dir / \"train_labels.csv\",\n#                        index='id',\n#                       nrows=100000\n                      )\ntest_df = pd.read_csv(data_dir / \"sample_submission.csv\")\nsample_submission = pd.read_csv(data_dir / \"sample_submission.csv\")\n\ntrain_df = train_df[~train_df.BraTS21ID.isin(excluded_images)]\n\nprint(f\"train data: Rows={train_df.shape[0]}, Columns={train_df.shape[1]}\")\n# print(f\"test data : Rows={test_df.shape[0]}, Columns={test_df.shape[1]}\")","metadata":{"execution":{"iopub.status.busy":"2021-10-03T16:56:46.856157Z","iopub.execute_input":"2021-10-03T16:56:46.856396Z","iopub.status.idle":"2021-10-03T16:56:46.902274Z","shell.execute_reply.started":"2021-10-03T16:56:46.856364Z","shell.execute_reply":"2021-10-03T16:56:46.899308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utility Functions","metadata":{}},{"cell_type":"markdown","source":"### There's a version that converts into grayscale: \n\n- https://www.kaggle.com/smoschou55/advanced-eda-brain-tumor-data\n","metadata":{}},{"cell_type":"code","source":"def load_dicom(path, size = 512):\n    ''' \n    Reads a DICOM image, standardizes so that the pixel values are between 0 and 1, then rescales to 0 and 255\n    \n    Not super sure if this kind of scaling is appropriate, but everyone seems to do it. \n    '''\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    # transform data into black and white scale / grayscale\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    return cv2.resize(data, (size, size))","metadata":{"execution":{"iopub.status.busy":"2021-10-03T16:58:05.583581Z","iopub.execute_input":"2021-10-03T16:58:05.583853Z","iopub.status.idle":"2021-10-03T16:58:05.591062Z","shell.execute_reply.started":"2021-10-03T16:58:05.583825Z","shell.execute_reply":"2021-10-03T16:58:05.589707Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_all_image_paths(brats21id, image_type, folder='train'): \n    '''\n    Returns an arry of all the images of a particular type for a particular patient ID\n    '''\n    assert(image_type in mri_types)\n    \n    patient_path = os.path.join(\n        \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/%s/\" % folder, \n        str(brats21id).zfill(5),\n    )\n\n    paths = sorted(\n        glob.glob(os.path.join(patient_path, image_type, \"*\")), \n        key=lambda x: int(x[:-4].split(\"-\")[-1]),\n    )\n    \n    num_images = len(paths)\n    \n    start = int(num_images * 0.25)\n    end = int(num_images * 0.75)\n\n    interval = 3\n    \n    if num_images < 10: \n        interval = 1\n    \n    return np.array(paths[start:end:interval])\n\ndef get_all_images(brats21id, image_type, folder='train', size=225):\n    return [load_dicom(path, size) for path in get_all_image_paths(brats21id, image_type, folder)]","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:55:00.214011Z","iopub.execute_input":"2021-10-02T17:55:00.214261Z","iopub.status.idle":"2021-10-02T17:55:00.224092Z","shell.execute_reply.started":"2021-10-02T17:55:00.21423Z","shell.execute_reply":"2021-10-02T17:55:00.223147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Images We Will Need","metadata":{}},{"cell_type":"code","source":"def get_all_data_for_train(image_type, image_size=400):\n    global train_df\n    \n    X = []\n    y = []\n    train_ids = []\n\n    for i in tqdm(train_df.index):\n        x = train_df.loc[i]\n        images = get_all_images(int(x['BraTS21ID']), image_type, 'train', image_size)\n        label = x['MGMT_value']\n\n        X += images\n        y += [label] * len(images)\n        train_ids += [int(x['BraTS21ID'])] * len(images)\n        assert(len(X) == len(y))\n    return np.array(X), np.array(y), np.array(train_ids)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:55:00.225213Z","iopub.execute_input":"2021-10-02T17:55:00.225749Z","iopub.status.idle":"2021-10-02T17:55:00.233895Z","shell.execute_reply.started":"2021-10-02T17:55:00.225697Z","shell.execute_reply":"2021-10-02T17:55:00.233175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_all_data_for_test(image_type, image_size=434):\n    global test_df\n    \n    X = []\n    test_ids = []\n\n    for i in tqdm(test_df.index):\n        x = test_df.loc[i]\n        images = get_all_images(int(x['BraTS21ID']), image_type, 'test', image_size)\n        X += images\n        test_ids += [int(x['BraTS21ID'])] * len(images)\n\n    return np.array(X), np.array(test_ids)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:55:00.235199Z","iopub.execute_input":"2021-10-02T17:55:00.235525Z","iopub.status.idle":"2021-10-02T17:55:00.245227Z","shell.execute_reply.started":"2021-10-02T17:55:00.23549Z","shell.execute_reply":"2021-10-02T17:55:00.244577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y, trainidt = get_all_data_for_train('T1wCE', image_size=32)\nX_test, testidt = get_all_data_for_test('T1wCE', image_size=32)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:55:00.246715Z","iopub.execute_input":"2021-10-02T17:55:00.246929Z","iopub.status.idle":"2021-10-02T17:56:03.975442Z","shell.execute_reply.started":"2021-10-02T17:55:00.246904Z","shell.execute_reply":"2021-10-02T17:56:03.974827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, y.shape, trainidt.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:03.97659Z","iopub.execute_input":"2021-10-02T17:56:03.977006Z","iopub.status.idle":"2021-10-02T17:56:03.984331Z","shell.execute_reply.started":"2021-10-02T17:56:03.976968Z","shell.execute_reply":"2021-10-02T17:56:03.983579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train/Validation Split","metadata":{}},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid, trainidt_train, trainidt_valid = train_test_split(X, y, trainidt, random_state=12)\n\n## Remove dimension","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:03.985612Z","iopub.execute_input":"2021-10-02T17:56:03.985969Z","iopub.status.idle":"2021-10-02T17:56:04.004015Z","shell.execute_reply.started":"2021-10-02T17:56:03.985936Z","shell.execute_reply":"2021-10-02T17:56:04.003328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = tf.expand_dims(X_train, axis=-1)\nX_valid = tf.expand_dims(X_valid, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:04.005328Z","iopub.execute_input":"2021-10-02T17:56:04.005637Z","iopub.status.idle":"2021-10-02T17:56:04.940225Z","shell.execute_reply.started":"2021-10-02T17:56:04.005604Z","shell.execute_reply":"2021-10-02T17:56:04.939287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## One-hot encode labels","metadata":{}},{"cell_type":"code","source":"y_train = to_categorical(y_train)\ny_valid = to_categorical(y_valid)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:04.944772Z","iopub.execute_input":"2021-10-02T17:56:04.945292Z","iopub.status.idle":"2021-10-02T17:56:04.952921Z","shell.execute_reply.started":"2021-10-02T17:56:04.945255Z","shell.execute_reply":"2021-10-02T17:56:04.952196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Tensorflow Models","metadata":{}},{"cell_type":"markdown","source":"## Model from:  https://www.kaggle.com/ohbewise/dataset-to-model-with-tensorflow","metadata":{}},{"cell_type":"code","source":"# Define, train, and evaluate model\n# source: https://keras.io/examples/vision/3D_image_classification/\ndef get_model01(width=128, height=128, depth=64, name='3dcnn'):\n    \"\"\"Build a 3D convolutional neural network model.\"\"\"\n\n    inputs = tf.keras.Input((width, height, depth, 1))\n\n    x = tf.keras.layers.Conv3D(filters=64, kernel_size=3, activation=\"relu\")(inputs)\n    x = tf.keras.layers.MaxPool3D(pool_size=2)(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n\n    x = tf.keras.layers.Conv3D(filters=64, kernel_size=3, activation=\"relu\")(x)\n    x = tf.keras.layers.MaxPool3D(pool_size=2)(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n\n    x = tf.keras.layers.Conv3D(filters=128, kernel_size=3, activation=\"relu\")(x)\n    x = tf.keras.layers.MaxPool3D(pool_size=2)(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n\n    x = tf.keras.layers.Conv3D(filters=256, kernel_size=3, activation=\"relu\")(x)\n    x = tf.keras.layers.MaxPool3D(pool_size=2)(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n\n    x = tf.keras.layers.GlobalAveragePooling3D()(x)\n    x = tf.keras.layers.Dense(units=512, activation=\"relu\")(x)\n    x = tf.keras.layers.Dropout(0.3)(x)\n\n    outputs = tf.keras.layers.Dense(units=1, activation=\"sigmoid\")(x)\n\n    # Define the model.\n    model = tf.keras.Model(inputs, outputs, name=name)\n    \n    # Compile model.\n    initial_learning_rate = 0.0003\n    lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n        initial_learning_rate, decay_steps=100000, decay_rate=0.96, staircase=True\n    )\n    model.compile(\n        loss=\"binary_crossentropy\",\n        optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),\n        metrics=[\"acc\"],\n    )\n    \n    return model\n\n","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:04.959181Z","iopub.execute_input":"2021-10-02T17:56:04.959622Z","iopub.status.idle":"2021-10-02T17:56:04.979274Z","shell.execute_reply.started":"2021-10-02T17:56:04.959588Z","shell.execute_reply":"2021-10-02T17:56:04.974782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model from: https://www.kaggle.com/evanyao27/team-9-second-week/notebook\n\n- Validation AUC=0.9148664856146349","metadata":{}},{"cell_type":"code","source":"def get_model02():\n    np.random.seed(0)\n    random.seed(12)\n    tf.random.set_seed(12)\n\n    inpt = keras.Input(shape=X_train.shape[1:])\n\n    h = keras.layers.experimental.preprocessing.Rescaling(1.0 / 255)(inpt)\n\n    h = keras.layers.Conv2D(64, kernel_size=(4, 4), activation=\"relu\", name=\"Conv_1\")(h)\n    h = keras.layers.MaxPool2D(pool_size=(2, 2))(h)\n\n    h = keras.layers.Conv2D(32, kernel_size=(2, 2), activation=\"relu\", name=\"Conv_2\")(h)\n    h = keras.layers.MaxPool2D(pool_size=(1, 1))(h)\n\n    h = keras.layers.Dropout(0.1)(h)\n\n    h = keras.layers.Flatten()(h)\n    h = keras.layers.Dense(32, activation=\"relu\")(h)\n\n    output = keras.layers.Dense(2, activation=\"softmax\")(h)\n\n    model = keras.Model(inpt, output)\n\n    model.compile(\n        loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[tf.keras.metrics.AUC()]\n    )\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:04.980689Z","iopub.execute_input":"2021-10-02T17:56:04.981356Z","iopub.status.idle":"2021-10-02T17:56:04.997284Z","shell.execute_reply.started":"2021-10-02T17:56:04.981302Z","shell.execute_reply":"2021-10-02T17:56:04.996543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Set up Model Checkpoint","metadata":{}},{"cell_type":"code","source":"checkpoint_filepath = \"best_model.h5\"\n\nmodel_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n    filepath=checkpoint_filepath,\n    save_weights_only=False,\n    monitor=\"val_auc\",\n    mode=\"max\",\n    save_best_only=True,\n    save_freq=\"epoch\",\n    verbose=1,\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:04.998656Z","iopub.execute_input":"2021-10-02T17:56:04.999253Z","iopub.status.idle":"2021-10-02T17:56:05.009487Z","shell.execute_reply.started":"2021-10-02T17:56:04.999219Z","shell.execute_reply":"2021-10-02T17:56:05.008689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Note that rerunning the cell below will change val_acc to val_acc_N and the model will not be saved.","metadata":{}},{"cell_type":"code","source":"model = get_model02()\n\nhistory = model.fit(x=X_train, y = y_train, epochs=200, callbacks=[model_checkpoint_callback], validation_data= (X_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:05.012387Z","iopub.execute_input":"2021-10-02T17:56:05.012705Z","iopub.status.idle":"2021-10-02T17:56:44.343118Z","shell.execute_reply.started":"2021-10-02T17:56:05.012675Z","shell.execute_reply":"2021-10-02T17:56:44.342204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Our Best Model","metadata":{}},{"cell_type":"code","source":"model_best = tf.keras.models.load_model(filepath=checkpoint_filepath)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:44.344682Z","iopub.execute_input":"2021-10-02T17:56:44.345055Z","iopub.status.idle":"2021-10-02T17:56:44.44115Z","shell.execute_reply.started":"2021-10-02T17:56:44.345017Z","shell.execute_reply":"2021-10-02T17:56:44.440477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions on Validation Set","metadata":{}},{"cell_type":"code","source":"y_pred = model_best.predict(X_valid)\n\npred = np.argmax(y_pred, axis=1)\n\nresult = pd.DataFrame(trainidt_valid)\nresult[1] = pred\n\nresult.columns = [\"BraTS21ID\", \"MGMT_value\"]\nresult2 = result.groupby(\"BraTS21ID\", as_index=False).mean()\n\nresult2 = result2.merge(train_df, on=\"BraTS21ID\")\nauc = roc_auc_score(\n    result2.MGMT_value_y,\n    result2.MGMT_value_x,\n)\nprint(f\"Validation AUC={auc}\")","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:44.442284Z","iopub.execute_input":"2021-10-02T17:56:44.442521Z","iopub.status.idle":"2021-10-02T17:56:44.654134Z","shell.execute_reply.started":"2021-10-02T17:56:44.442493Z","shell.execute_reply":"2021-10-02T17:56:44.653415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions on the Test Set","metadata":{}},{"cell_type":"code","source":"y_pred = model_best.predict(X_test)\n\npred = np.argmax(y_pred, axis=1) #\n\nresult = pd.DataFrame(testidt)\nresult[1] = pred\npred","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:44.655345Z","iopub.execute_input":"2021-10-02T17:56:44.655716Z","iopub.status.idle":"2021-10-02T17:56:44.844944Z","shell.execute_reply.started":"2021-10-02T17:56:44.655684Z","shell.execute_reply":"2021-10-02T17:56:44.84321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission File","metadata":{}},{"cell_type":"code","source":"result.columns=['BraTS21ID','MGMT_value']\n\nresult2 = result.groupby('BraTS21ID',as_index=False).mean()\nresult2['BraTS21ID'] = sample_submission['BraTS21ID']\n\n# Rounding...\nresult2['MGMT_value'] = result2['MGMT_value'].apply(lambda x:round(x*10)/10)\nresult2.to_csv('submission.csv',index=False)\nresult2","metadata":{"execution":{"iopub.status.busy":"2021-10-02T17:56:44.850825Z","iopub.execute_input":"2021-10-02T17:56:44.851526Z","iopub.status.idle":"2021-10-02T17:56:44.883698Z","shell.execute_reply.started":"2021-10-02T17:56:44.851489Z","shell.execute_reply":"2021-10-02T17:56:44.882891Z"},"trusted":true},"execution_count":null,"outputs":[]}]}