{"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":"# import display to clear output\nfrom IPython import display","metadata":{"id":"e4gJOSYXeo2O","execution":{"iopub.status.busy":"2023-10-08T17:45:29.133947Z","iopub.execute_input":"2023-10-08T17:45:29.134308Z","iopub.status.idle":"2023-10-08T17:45:29.139091Z","shell.execute_reply.started":"2023-10-08T17:45:29.134279Z","shell.execute_reply":"2023-10-08T17:45:29.138232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# install pydicom\n!pip install \"/kaggle/input/pydicom/pydicom-2.4.3-py3-none-any.whl\"\ndisplay.clear_output()","metadata":{"id":"0xqE5T6DdDXj","execution":{"iopub.status.busy":"2023-10-08T17:45:30.361392Z","iopub.execute_input":"2023-10-08T17:45:30.362346Z","iopub.status.idle":"2023-10-08T17:46:04.781522Z","shell.execute_reply.started":"2023-10-08T17:45:30.362304Z","shell.execute_reply":"2023-10-08T17:46:04.780404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pydicom\nimport cv2\nimport os\nimport glob\nfrom tqdm.notebook import tqdm\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\nfrom keras.applications import InceptionV3\nfrom keras.layers import *","metadata":{"id":"G4y0oZ7JdzmL","execution":{"iopub.status.busy":"2023-10-08T17:46:04.783551Z","iopub.execute_input":"2023-10-08T17:46:04.784214Z","iopub.status.idle":"2023-10-08T17:46:13.663439Z","shell.execute_reply.started":"2023-10-08T17:46:04.784178Z","shell.execute_reply":"2023-10-08T17:46:13.662491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# some info\nTYPES = [\"FLAIR\", \"T1w\", \"T2w\", \"T1wCE\"]\nEXCLUDE = [109, 123, 709]","metadata":{"id":"HeGThQSRmaPr","execution":{"iopub.status.busy":"2023-10-08T17:46:13.664687Z","iopub.execute_input":"2023-10-08T17:46:13.665553Z","iopub.status.idle":"2023-10-08T17:46:13.671981Z","shell.execute_reply.started":"2023-10-08T17:46:13.665521Z","shell.execute_reply":"2023-10-08T17:46:13.671166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\ntest_df = pd.read_csv('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')\ntrain_df = train_df[~train_df.BraTS21ID.isin(EXCLUDE)]","metadata":{"id":"CfUSgGc0omGS","execution":{"iopub.status.busy":"2023-10-08T17:46:13.674759Z","iopub.execute_input":"2023-10-08T17:46:13.675043Z","iopub.status.idle":"2023-10-08T17:46:13.733930Z","shell.execute_reply.started":"2023-10-08T17:46:13.675021Z","shell.execute_reply":"2023-10-08T17:46:13.733000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading file\ndef load_dicom(path, size = 224):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\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":{"id":"FogvOLa9lk-r","execution":{"iopub.status.busy":"2023-10-08T17:46:13.735137Z","iopub.execute_input":"2023-10-08T17:46:13.735676Z","iopub.status.idle":"2023-10-08T17:46:13.742059Z","shell.execute_reply.started":"2023-10-08T17:46:13.735646Z","shell.execute_reply":"2023-10-08T17:46:13.741068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read image paths\ndef get_all_image_paths(brats21id, image_type, folder='train'):\n    assert(image_type in TYPES) # check that image_type is correct\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    interval = 3\n    if num_images < 10:\n        interval = 1\n    return np.array(paths[start:end:interval])","metadata":{"id":"rjtqnrcXlk8L","execution":{"iopub.status.busy":"2023-10-08T17:46:13.743595Z","iopub.execute_input":"2023-10-08T17:46:13.744285Z","iopub.status.idle":"2023-10-08T17:46:13.756484Z","shell.execute_reply.started":"2023-10-08T17:46:13.744254Z","shell.execute_reply":"2023-10-08T17:46:13.755288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# return images ,labels  and these ids\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)]\nIMAGE_SIZE = 128\n\ndef get_all_data_for_train(image_type):\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":{"id":"dF7lDr1Tlk5Q","execution":{"iopub.status.busy":"2023-10-08T17:46:13.758008Z","iopub.execute_input":"2023-10-08T17:46:13.758721Z","iopub.status.idle":"2023-10-08T17:46:13.769946Z","shell.execute_reply.started":"2023-10-08T17:46:13.758689Z","shell.execute_reply":"2023-10-08T17:46:13.768963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# getting train_data\nX , y ,trainidt = get_all_data_for_train('T1wCE')","metadata":{"id":"ajL8UD4ar81y","outputId":"72f367bd-9bcc-44e1-84a9-086e8909e4db","execution":{"iopub.status.busy":"2023-10-08T17:46:13.771199Z","iopub.execute_input":"2023-10-08T17:46:13.771983Z","iopub.status.idle":"2023-10-08T17:49:17.598427Z","shell.execute_reply.started":"2023-10-08T17:46:13.771953Z","shell.execute_reply":"2023-10-08T17:49:17.597487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# splitting data\nX_train, X_valid, y_train, y_valid, trainidt_train, trainidt_valid = train_test_split(X, y, trainidt, test_size=0.2, random_state=42)","metadata":{"id":"ALiEh0ffsw2v","execution":{"iopub.status.busy":"2023-10-08T17:49:17.599810Z","iopub.execute_input":"2023-10-08T17:49:17.600754Z","iopub.status.idle":"2023-10-08T17:49:17.683382Z","shell.execute_reply.started":"2023-10-08T17:49:17.600720Z","shell.execute_reply":"2023-10-08T17:49:17.682439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# expand dims\nX_train = tf.expand_dims(X_train, axis=-1)\nX_valid = tf.expand_dims(X_valid, axis=-1)\n\ny_train = to_categorical(y_train)\ny_valid = to_categorical(y_valid)","metadata":{"id":"Xi3wMVxat1Z0","execution":{"iopub.status.busy":"2023-10-08T17:49:17.686293Z","iopub.execute_input":"2023-10-08T17:49:17.686643Z","iopub.status.idle":"2023-10-08T17:49:21.160530Z","shell.execute_reply.started":"2023-10-08T17:49:17.686611Z","shell.execute_reply":"2023-10-08T17:49:21.159546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# see shape\nX_train.shape, y_train.shape, X_valid.shape, y_valid.shape, trainidt_train.shape, trainidt_valid.shape","metadata":{"id":"8kPxEIbgse1o","outputId":"bade542e-aa1d-4190-a37d-6c442eee9be9","execution":{"iopub.status.busy":"2023-10-08T03:41:16.849726Z","iopub.execute_input":"2023-10-08T03:41:16.850065Z","iopub.status.idle":"2023-10-08T03:41:16.857915Z","shell.execute_reply.started":"2023-10-08T03:41:16.850032Z","shell.execute_reply":"2023-10-08T03:41:16.857018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_all_data_for_test(image_type):\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)\n","metadata":{"id":"H4rWWNYSlk2J","execution":{"iopub.status.busy":"2023-10-08T03:41:16.859741Z","iopub.execute_input":"2023-10-08T03:41:16.860389Z","iopub.status.idle":"2023-10-08T03:41:16.868489Z","shell.execute_reply.started":"2023-10-08T03:41:16.860349Z","shell.execute_reply":"2023-10-08T03:41:16.867620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test , testids = get_all_data_for_test('T1wCE')","metadata":{"id":"Oqmv9m9RlkzL","outputId":"ec12807d-cb76-475d-8086-c190aae865c4","execution":{"iopub.status.busy":"2023-10-08T03:41:16.869796Z","iopub.execute_input":"2023-10-08T03:41:16.870597Z","iopub.status.idle":"2023-10-08T03:41:41.583288Z","shell.execute_reply.started":"2023-10-08T03:41:16.870565Z","shell.execute_reply":"2023-10-08T03:41:41.582394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model ","metadata":{"id":"xSoOxItddmuh"}},{"cell_type":"code","source":"# Modeling\ninput_shape = (128,128,1)\nmodel  = tf.keras.Sequential(\n    (\n      Input(shape = input_shape),\n      Conv2D(32, kernel_size=(3, 3),strides=(1,1), activation=\"relu\", name=\"Conv_1\", padding=\"valid\",),\n      BatchNormalization(axis=-1),\n      Conv2D(64, kernel_size=(3, 3),strides=(1,1), activation=\"relu\", name=\"Conv_2\", padding=\"same\"),\n      BatchNormalization(axis=-1),\n      MaxPool2D(pool_size=(2,2)),\n      BatchNormalization(axis=-1),\n      Conv2D(64, kernel_size=(3, 3), activation=\"relu\", name=\"Conv_3\",padding =\"same\"),\n      BatchNormalization(axis=-1),\n      MaxPool2D(pool_size=(1,1)),\n      BatchNormalization(axis=-1),\n      Conv2D(128, kernel_size=(3, 3), activation=\"relu\", name=\"Conv_4\",padding =\"valid\"),\n      BatchNormalization(axis=-1),\n      Conv2D(128, kernel_size=(3, 3), activation=\"relu\", name=\"Conv_5\",padding =\"same\"),\n      BatchNormalization(axis=-1),\n      MaxPool2D(pool_size=(2,2)),\n      BatchNormalization(axis=-1),\n      Dropout(0.3),\n\n      Flatten(),\n      Dense(128, activation='relu'),\n\n      Dense(2, activation=\"softmax\"),\n\n    )\n)\nmodel.summary()","metadata":{"id":"P9bInoXZs1X7","outputId":"27da1a39-cb96-49c4-b04f-cc743a99e4b2","execution":{"iopub.status.busy":"2023-10-08T03:41:41.584943Z","iopub.execute_input":"2023-10-08T03:41:41.585592Z","iopub.status.idle":"2023-10-08T03:41:41.852434Z","shell.execute_reply.started":"2023-10-08T03:41:41.585559Z","shell.execute_reply":"2023-10-08T03:41:41.851684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compile\nmodel.compile(\n    loss='categorical_crossentropy',\n    optimizer=tf.keras.optimizers.SGD(learning_rate=0.0001),\n    metrics=[tf.keras.metrics.AUC()]\n)","metadata":{"id":"VTkhCo3ZxiEH","execution":{"iopub.status.busy":"2023-10-08T03:41:41.853410Z","iopub.execute_input":"2023-10-08T03:41:41.853769Z","iopub.status.idle":"2023-10-08T03:41:41.898991Z","shell.execute_reply.started":"2023-10-08T03:41:41.853737Z","shell.execute_reply":"2023-10-08T03:41:41.898096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fitting\nhistory1 = model.fit(X_train , y_train , validation_data = (X_valid , y_valid) ,epochs = 49 ,verbose = 1)","metadata":{"id":"3P8MMDbrx-X9","outputId":"89db8d8d-d918-4810-81c1-d97bb1c6c2ae","_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-10-08T03:41:41.900116Z","iopub.execute_input":"2023-10-08T03:41:41.900895Z","iopub.status.idle":"2023-10-08T04:03:58.858973Z","shell.execute_reply.started":"2023-10-08T03:41:41.900862Z","shell.execute_reply":"2023-10-08T04:03:58.858030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axs = plt.subplots(figsize = (10,5))\nfig.suptitle('Model Evaluation' , ha = 'center' , fontsize = 15 , fontweight = 'bold')\nplt.subplot(1,2,1)\nplt.title('AUC')\nsns.lineplot(history1.history['auc'],label = 'train')\nsns.lineplot(history1.history['val_auc'],label = 'val')\nplt.xlabel('Epochs')\n\nplt.subplot(1,2,2)\nplt.title('Loss')\nsns.lineplot(history1.history['loss'],label = 'train')\nsns.lineplot(history1.history['val_loss'],label = 'val')\nplt.xlabel('Epochs')","metadata":{"id":"dBES5d5jT1Tx","outputId":"51fadd84-df0e-472e-9037-4a218f1eda78","execution":{"iopub.status.busy":"2023-10-08T04:03:58.860591Z","iopub.execute_input":"2023-10-08T04:03:58.860929Z","iopub.status.idle":"2023-10-08T04:03:59.421196Z","shell.execute_reply.started":"2023-10-08T04:03:58.860898Z","shell.execute_reply":"2023-10-08T04:03:59.420352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# make prediction\ny_pred = model.predict(X_test)\npred = np.argmax(y_pred ,axis = 1)","metadata":{"id":"pMnLDJERXf9t","outputId":"e9de48d7-e348-4e7f-a662-99327f3d0332","execution":{"iopub.status.busy":"2023-10-08T04:03:59.422355Z","iopub.execute_input":"2023-10-08T04:03:59.423146Z","iopub.status.idle":"2023-10-08T04:04:00.780703Z","shell.execute_reply.started":"2023-10-08T04:03:59.423113Z","shell.execute_reply":"2023-10-08T04:04:00.779715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame(testids)\nresult[1] = pred\nresult.columns=['BraTS21ID','MGMT_value']\nprint(result.shape)\n\nresult2 = result.groupby('BraTS21ID',as_index=False).mean()\nresult2['BraTS21ID'] = test_df['BraTS21ID']\n\nresult2.to_csv('submission.csv',index=False)\nresult2","metadata":{"id":"KWS_yqKea5uV","outputId":"22cb4526-66e4-4508-f3f7-fb4525508740","execution":{"iopub.status.busy":"2023-10-08T04:04:00.782246Z","iopub.execute_input":"2023-10-08T04:04:00.782610Z","iopub.status.idle":"2023-10-08T04:04:00.807597Z","shell.execute_reply.started":"2023-10-08T04:04:00.782579Z","shell.execute_reply":"2023-10-08T04:04:00.806464Z"},"trusted":true},"execution_count":null,"outputs":[]}]}