{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":29653,"databundleVersionId":2420395,"sourceType":"competition"}],"dockerImageVersionId":30476,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport json\nimport glob\nimport random\nimport collections\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import layers\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-05-30T22:14:21.407086Z","iopub.execute_input":"2024-05-30T22:14:21.408142Z","iopub.status.idle":"2024-05-30T22:14:21.414439Z","shell.execute_reply.started":"2024-05-30T22:14:21.408110Z","shell.execute_reply":"2024-05-30T22:14:21.413485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CSV dosyasından eğitim verilerini yükleme\ntrain_df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\n\n# Eğitim verilerini görüntüleme\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2024-05-30T22:14:26.951595Z","iopub.execute_input":"2024-05-30T22:14:26.952043Z","iopub.status.idle":"2024-05-30T22:14:26.980733Z","shell.execute_reply.started":"2024-05-30T22:14:26.951978Z","shell.execute_reply":"2024-05-30T22:14:26.979865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  işlenen MGMT_value değişkeninin bir sayım grafiğini oluşturma\nplt.figure(figsize=(5, 5))\nsns.countplot(data=train_df, x=\"MGMT_value\")","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:18:10.845644Z","iopub.execute_input":"2024-05-30T19:18:10.845962Z","iopub.status.idle":"2024-05-30T19:18:11.065164Z","shell.execute_reply.started":"2024-05-30T19:18:10.845931Z","shell.execute_reply":"2024-05-30T19:18:11.064340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DICOM dosyalarını yüklemek için oluşturulan fonksiyon\ndef load_dicom_file(path):\n    dicom_file = pydicom.read_file(path)\n    pixel_array = dicom_file.pixel_array\n    pixel_array = pixel_array - np.min(pixel_array)\n    if np.max(pixel_array) != 0:\n        pixel_array = pixel_array / np.max(pixel_array)\n    pixel_array = (pixel_array * 255).astype(np.uint8)\n    return pixel_array\n\n# Beyin tümörü radyogenomik veri örneğini görselleştirmek için oluşturulan fonksiyon\ndef visualize_sample(brats21id, slice_i, mgmt_value, types=(\"FLAIR\", \"T1w\", \"T1wCE\", \"T2w\")):\n    plt.figure(figsize=(16, 5))\n    patient_path = os.path.join(\n        \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\", \n        str(brats21id).zfill(5),\n    )\n    for i, t in enumerate(types, 1):\n        t_paths = sorted(\n            glob.glob(os.path.join(patient_path, t, \"*\")), \n            key=lambda x: int(x[:-4].split(\"-\")[-1]),\n        )\n        pixel_array = load_dicom_file(t_paths[int(len(t_paths) * slice_i)])\n        plt.subplot(1, 4, i)\n        plt.imshow(pixel_array, cmap=\"gray\")\n        plt.title(f\"{t}\", fontsize=16)\n        plt.axis(\"off\")\n\n    plt.suptitle(f\"MGMT_value: {mgmt_value}\", fontsize=16)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:18:11.067188Z","iopub.execute_input":"2024-05-30T19:18:11.067855Z","iopub.status.idle":"2024-05-30T19:18:11.077132Z","shell.execute_reply.started":"2024-05-30T19:18:11.067820Z","shell.execute_reply":"2024-05-30T19:18:11.076271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Eğitim veri kümesinden 10 beyin tümörü radyogenomik görüntüsünden rastgele bir örnek seçme\nrandom_indices = random.sample(range(train_df.shape[0]), 10)\n\n# Her bir örneği `visualize_sample()` fonksiyonunu kullanarak görselleştirme\nfor i in random_indices:\n    brats21id = train_df.iloc[i][\"BraTS21ID\"]\n    mgmt_value = train_df.iloc[i][\"MGMT_value\"]\n    visualize_sample(brats21id=brats21id, mgmt_value=mgmt_value, slice_i=0.5)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:18:12.286117Z","iopub.execute_input":"2024-05-30T19:18:12.286691Z","iopub.status.idle":"2024-05-30T19:18:22.751111Z","shell.execute_reply.started":"2024-05-30T19:18:12.286658Z","shell.execute_reply":"2024-05-30T19:18:22.750204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\n\ndef create_animation(ims):\n    fig = plt.figure(figsize=(6, 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//24)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:18:22.752829Z","iopub.execute_input":"2024-05-30T19:18:22.753138Z","iopub.status.idle":"2024-05-30T19:18:22.764087Z","shell.execute_reply.started":"2024-05-30T19:18:22.753112Z","shell.execute_reply":"2024-05-30T19:18:22.763240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom_images(path):\n   \n    # Get the paths to the DICOM files and sort them by slice number\n    dicom_paths = sorted(\n        glob.glob(os.path.join(path, \"*\")), \n        key=lambda x: int(x[:-4].split(\"-\")[-1]),\n    )\n\n    # Load the DICOM images into a list\n    images = []\n    for path in dicom_paths:\n        image = load_dicom_file(path)\n        if image.max() != 0:\n            images.append(image)\n\n    return images","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:18:22.765216Z","iopub.execute_input":"2024-05-30T19:18:22.765543Z","iopub.status.idle":"2024-05-30T19:18:22.774203Z","shell.execute_reply.started":"2024-05-30T19:18:22.765516Z","shell.execute_reply":"2024-05-30T19:18:22.773509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_images(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:18:22.776211Z","iopub.execute_input":"2024-05-30T19:18:22.776494Z","iopub.status.idle":"2024-05-30T19:18:48.111567Z","shell.execute_reply.started":"2024-05-30T19:18:22.776447Z","shell.execute_reply":"2024-05-30T19:18:48.110078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_images(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:18:48.113749Z","iopub.execute_input":"2024-05-30T19:18:48.114545Z","iopub.status.idle":"2024-05-30T19:18:50.741693Z","shell.execute_reply.started":"2024-05-30T19:18:48.114488Z","shell.execute_reply":"2024-05-30T19:18:50.740493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_images(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1wCE\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:18:50.743542Z","iopub.execute_input":"2024-05-30T19:18:50.744085Z","iopub.status.idle":"2024-05-30T19:18:58.992121Z","shell.execute_reply.started":"2024-05-30T19:18:50.744038Z","shell.execute_reply":"2024-05-30T19:18:58.991064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\n\ndef plot_brain_3d(directory):\n    # Dizindeki tüm DICOM dosyalarını yükleme\n    files = sorted([os.path.join(directory, f) for f in os.listdir(directory) if f.endswith('.dcm')], key=lambda path: int(os.path.splitext(os.path.basename(path))[0].split('-')[-1]))\n\n    print(\"File count: {}\".format(len(files)))\n\n    # DICOM dosyalarını belleğe yükleme ve SliceLocation özniteliği olmayanları atla\n    slices = []\n    skipcount = 0\n    for fname in files:\n        ds = pydicom.dcmread(fname)\n        if hasattr(ds, 'SliceLocation'):\n            slices.append(ds)\n        else:\n            skipcount += 1\n    print(\"Skipped, no SliceLocation: {}\".format(skipcount))\n\n    # Dilimleri SliceLocation özniteliklerine göre sıralama\n    slices = sorted(slices, key=lambda s: s.SliceLocation)\n\n    # Her görünüm için piksel en boy oranlarını hesaplama\n    ps = slices[0].PixelSpacing\n    ss = slices[0].SliceThickness\n    ax_aspect = ps[1]/ps[0]\n    sag_aspect = ps[1]/ss\n    cor_aspect = ss/ps[0]\n\n    # Görüntü verilerini depolamak için bir 3D dizi oluşturma\n    img_shape = list(slices[0].pixel_array.shape)\n    img_shape.append(len(slices))\n    img3d = np.zeros(img_shape)\n\n    # 3D dizisini her dilimden gelen piksel verileriyle doldurma\n    for i, s in enumerate(slices):\n        img2d = s.pixel_array\n        img3d[:, :, i] = img2d\n\n    # 3 ortogonal görünümü çizme\n    fig, axes = plt.subplots(1, 3, figsize=(25, 25))\n    fig.suptitle(\"Brain CT Scan in three different views (Horizontal, Sagittal, Coronal)\", fontsize=24)\n\n    axes[0].imshow(img3d[:, :, img_shape[2]//2], cmap='hot')\n    axes[0].set_aspect(ax_aspect)\n\n    axes[1].imshow(img3d[:, img_shape[1]//2, :], cmap='hot')\n    axes[1].set_aspect(sag_aspect)\n\n    axes[2].imshow(img3d[img_shape[0]//2, :, :].T, cmap='hot')\n    axes[2].set_aspect(cor_aspect)\n\n    plt.show()\n\n# Örnek kullanım: '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00006/FLAIR/' dizinindeki 3B beyin taramalarını çizme\ndirectory = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00006/FLAIR/'\nplot_brain_3d(directory)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:18:58.995253Z","iopub.execute_input":"2024-05-30T19:18:58.995572Z","iopub.status.idle":"2024-05-30T19:19:02.034389Z","shell.execute_reply.started":"2024-05-30T19:18:58.995540Z","shell.execute_reply":"2024-05-30T19:19:02.033505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/input/')\nmri_types = [\"FLAIR\", \"T1w\", \"T2w\", \"T1wCE\"]\nthreshold = 10\nexc = [109, 123, 709]","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:19:02.035697Z","iopub.execute_input":"2024-05-30T19:19:02.036025Z","iopub.status.idle":"2024-05-30T19:19:02.041426Z","shell.execute_reply.started":"2024-05-30T19:19:02.035988Z","shell.execute_reply":"2024-05-30T19:19:02.040574Z"},"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\")\ntrain_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:19:02.042584Z","iopub.execute_input":"2024-05-30T19:19:02.042858Z","iopub.status.idle":"2024-05-30T19:19:02.059931Z","shell.execute_reply.started":"2024-05-30T19:19:02.042834Z","shell.execute_reply":"2024-05-30T19:19:02.059118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv\")\ntest_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:19:02.064035Z","iopub.execute_input":"2024-05-30T19:19:02.064275Z","iopub.status.idle":"2024-05-30T19:19:02.083089Z","shell.execute_reply.started":"2024-05-30T19:19:02.064254Z","shell.execute_reply":"2024-05-30T19:19:02.082320Z"},"jupyter":{"source_hidden":true,"outputs_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df[~train_df.BraTS21ID.isin(exc)]\ntrain_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:19:02.084008Z","iopub.execute_input":"2024-05-30T19:19:02.084267Z","iopub.status.idle":"2024-05-30T19:19:02.094185Z","shell.execute_reply.started":"2024-05-30T19:19:02.084245Z","shell.execute_reply":"2024-05-30T19:19:02.093358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dicom görüntüsü, piksel değerleri 0 ile 1 arasında olacak şekilde standartlaştırılır\n# 0 ve 255 olarak yeniden ölçeklendirin\ndef dicomImage(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))\n\n# Belirli bir hasta kimliği için belirli bir türdeki tüm görüntülerin bir aryasını döndürür\ndef allImagePath(brats21id, image_type, folder='train'): \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 [dicomImage(path, size) for path in allImagePath(brats21id, image_type, folder)]","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:19:02.095238Z","iopub.execute_input":"2024-05-30T19:19:02.095510Z","iopub.status.idle":"2024-05-30T19:19:02.105172Z","shell.execute_reply.started":"2024-05-30T19:19:02.095478Z","shell.execute_reply":"2024-05-30T19:19:02.104283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgaeSize = 128\n\ndef get_all_data_for_train(image_type):\n    global train_df\n    var1 = []\n    var2 = []\n    train_ids = []\n\n    for i in tqdm(train_df.index):\n        x = train_df.loc[i]\n        allImages = get_all_images(int(x['BraTS21ID']), image_type, 'train', imgaeSize)\n        label = x['MGMT_value']\n        var1 += allImages\n        var2 += [label] * len(allImages)\n        train_ids += [int(x['BraTS21ID'])] * len(allImages)\n        assert(len(var1) == len(var2))\n    return np.array(var1), np.array(var2), np.array(train_ids)\n\ndef get_all_data_for_test(image_type):\n    global test_df\n    var1 = []\n    test_ids = []\n\n    for i in tqdm(test_df.index):\n        x = test_df.loc[i]\n        allImages = get_all_images(int(x['BraTS21ID']), image_type, 'test', imgaeSize)\n        var1 += allImages\n        test_ids += [int(x['BraTS21ID'])] * len(allImages)\n\n    return np.array(var1), np.array(test_ids)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:19:02.106209Z","iopub.execute_input":"2024-05-30T19:19:02.106509Z","iopub.status.idle":"2024-05-30T19:19:02.118030Z","shell.execute_reply.started":"2024-05-30T19:19:02.106477Z","shell.execute_reply":"2024-05-30T19:19:02.117295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, Y, train_idt = get_all_data_for_train('T1wCE')\nX_test, test_idt = get_all_data_for_test('T1wCE')\nX.shape, Y.shape, train_idt.shape, test_idt.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:19:02.119142Z","iopub.execute_input":"2024-05-30T19:19:02.119488Z","iopub.status.idle":"2024-05-30T19:24:31.673272Z","shell.execute_reply.started":"2024-05-30T19:19:02.119431Z","shell.execute_reply":"2024-05-30T19:24:31.672391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, Y.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:24:31.674540Z","iopub.execute_input":"2024-05-30T19:24:31.675234Z","iopub.status.idle":"2024-05-30T19:24:31.680909Z","shell.execute_reply.started":"2024-05-30T19:24:31.675197Z","shell.execute_reply":"2024-05-30T19:24:31.680021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid, trainidt_train, trainidt_valid = train_test_split(X, Y, train_idt, test_size=0.1, random_state=140)\nsplit = int(X.shape[0] * 0.9)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:24:31.682059Z","iopub.execute_input":"2024-05-30T19:24:31.682381Z","iopub.status.idle":"2024-05-30T19:24:31.768214Z","shell.execute_reply.started":"2024-05-30T19:24:31.682351Z","shell.execute_reply":"2024-05-30T19:24:31.767435Z"},"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)\n\ny_train = to_categorical(y_train)\ny_valid = to_categorical(y_valid)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:24:31.769263Z","iopub.execute_input":"2024-05-30T19:24:31.769588Z","iopub.status.idle":"2024-05-30T19:24:34.184775Z","shell.execute_reply.started":"2024-05-30T19:24:31.769555Z","shell.execute_reply":"2024-05-30T19:24:34.183897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape, X_valid.shape, y_valid.shape, trainidt_train.shape, trainidt_valid.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:24:34.186026Z","iopub.execute_input":"2024-05-30T19:24:34.186675Z","iopub.status.idle":"2024-05-30T19:24:34.192941Z","shell.execute_reply.started":"2024-05-30T19:24:34.186643Z","shell.execute_reply":"2024-05-30T19:24:34.192005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(0)\nrandom.seed(12)\ntf.random.set_seed(12)\n\ninputShape = keras.Input(shape = X_train.shape[1:])\n\npreProcessing = keras.layers.experimental.preprocessing.Rescaling(1./255)(inputShape)\n# h = augmentation(h)\n\n# konvolüsyonel katman\nl = keras.layers.Conv2D(32, kernel_size=(3, 3),strides=(1,1), activation=\"relu\", name=\"Conv_1\", padding=\"valid\")(preProcessing) \nl = tf.keras.layers.BatchNormalization(axis=-1)(l)\nl = keras.layers.Conv2D(32, kernel_size=(3, 3),strides=(1,1), activation=\"relu\", name=\"Conv_1b\", padding=\"same\")(l) \nl = tf.keras.layers.BatchNormalization(axis=-1)(l)\nl = keras.layers.MaxPool2D(pool_size=(2,2))(l)\nl = keras.layers.Conv2D(64, kernel_size=(3, 3),strides=(1,1), activation=\"relu\", name=\"Conv_2\", padding=\"valid\")(l) \nl = tf.keras.layers.BatchNormalization(axis=-1)(l)\n\n# havuzlama katmanı\nl = keras.layers.MaxPool2D(pool_size=(2,2))(l)\nl = tf.keras.layers.BatchNormalization(axis=-1)(l)\n\n# konvolüsyonel katman!\nl = keras.layers.Conv2D(64, kernel_size=(3, 3), activation=\"relu\", name=\"Conv_3\",padding =\"same\")(l)\n\n# havuzlama katmanı\nl = tf.keras.layers.BatchNormalization(axis=-1)(l)\nl = keras.layers.Dropout(0.4)(l)\nl = keras.layers.Flatten()(l)\nl1 = keras.layers.experimental.preprocessing.Rescaling(1./255)(preProcessing)\nl1 =  keras.layers.MaxPool2D(strides=(5,5))(l1)\nl1 = keras.layers.Conv2D(32, kernel_size=(3, 3), activation=\"swish\", name=\"Conv_4b\")(l1)\nl1 = tf.keras.layers.BatchNormalization(axis=-1)(l1)\nl1 =  keras.layers.MaxPool2D(strides=(5,5))(l1)\nl1 = keras.layers.Dropout(0.3)(l1)\nl1 = keras.layers.Conv2D(32, kernel_size=(3, 3), activation=\"swish\", name=\"Conv_5b\")(l1)\nl1 = tf.keras.layers.BatchNormalization(axis=-1)(l1)\nl1 =  keras.layers.MaxPool2D(strides=(5,5))(l1)\nl1 = keras.layers.Dropout(0.3)(l1)\nl1 = keras.layers.Flatten()(l1)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:24:34.194034Z","iopub.execute_input":"2024-05-30T19:24:34.194301Z","iopub.status.idle":"2024-05-30T19:24:34.438209Z","shell.execute_reply.started":"2024-05-30T19:24:34.194278Z","shell.execute_reply":"2024-05-30T19:24:34.437337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge = keras.layers.Concatenate()([l,l1])\ndropOut = keras.layers.Dropout(0.4)(merge)\ndropOut = keras.layers.Dense(150, activation='relu')(dropOut)  \noutPut = keras.layers.Dense(2, activation=\"softmax\")(dropOut)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:24:34.439447Z","iopub.execute_input":"2024-05-30T19:24:34.439824Z","iopub.status.idle":"2024-05-30T19:24:34.477948Z","shell.execute_reply.started":"2024-05-30T19:24:34.439788Z","shell.execute_reply":"2024-05-30T19:24:34.477084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Model(inputShape, outPut)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:24:34.479084Z","iopub.execute_input":"2024-05-30T19:24:34.480113Z","iopub.status.idle":"2024-05-30T19:24:34.491640Z","shell.execute_reply.started":"2024-05-30T19:24:34.480078Z","shell.execute_reply":"2024-05-30T19:24:34.490882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import SGD\n\nfilepathCheckpoint = 'best_model.h5'\n\nmodelCallbackCheckpoint = tf.keras.callbacks.ModelCheckpoint(\n    filepath = filepathCheckpoint,\n    save_weights_only = False,\n    monitor = 'val_auc',\n    mode = 'max',\n    save_best_only = True,\n    save_freq = 'epoch'\n)\n\nmodel.compile(\n    loss = 'categorical_crossentropy',\n    optimizer = tf.keras.optimizers.SGD(learning_rate =0.0001),\n    metrics = [tf.keras.metrics.AUC()]\n)\n\nhistory = model.fit(x = X_train, y = y_train, epochs = 100, callbacks = [modelCallbackCheckpoint], validation_data = (X_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:24:34.492641Z","iopub.execute_input":"2024-05-30T19:24:34.492906Z","iopub.status.idle":"2024-05-30T19:47:34.051309Z","shell.execute_reply.started":"2024-05-30T19:24:34.492883Z","shell.execute_reply":"2024-05-30T19:47:34.050501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = tf.keras.models.load_model(filepath = filepathCheckpoint)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:47:34.052602Z","iopub.execute_input":"2024-05-30T19:47:34.052898Z","iopub.status.idle":"2024-05-30T19:47:34.667643Z","shell.execute_reply.started":"2024-05-30T19:47:34.052871Z","shell.execute_reply":"2024-05-30T19:47:34.666666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model1.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')\nroc_auc_score(result2.MGMT_value_y, result2.MGMT_value_x,)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:47:34.668852Z","iopub.execute_input":"2024-05-30T19:47:34.669148Z","iopub.status.idle":"2024-05-30T19:47:35.276317Z","shell.execute_reply.started":"2024-05-30T19:47:34.669123Z","shell.execute_reply":"2024-05-30T19:47:35.275394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(result2)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:47:35.277371Z","iopub.execute_input":"2024-05-30T19:47:35.277942Z","iopub.status.idle":"2024-05-30T19:47:35.285228Z","shell.execute_reply.started":"2024-05-30T19:47:35.277915Z","shell.execute_reply":"2024-05-30T19:47:35.284375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import SGD\n\nfilepathCheckpoint = 'best_model.h5'\n\nmodelCallbackCheckpoint = tf.keras.callbacks.ModelCheckpoint(\n    filepath = filepathCheckpoint,\n    save_weights_only = False,\n    monitor = 'val_auc',\n    mode = 'max',\n    save_best_only = True,\n    save_freq = 'epoch'\n)\n\nmodel.compile(\n    loss = 'categorical_crossentropy',\n    optimizer = tf.keras.optimizers.SGD(learning_rate =0.0001),\n    metrics = [tf.keras.metrics.AUC()]\n)\nmodel.summary()\n#history = model.fit(x = X_train, y = y_train, epochs = 100, callbacks = [modelCallbackCheckpoint], validation_data = (X_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:47:35.286317Z","iopub.execute_input":"2024-05-30T19:47:35.286629Z","iopub.status.idle":"2024-05-30T19:47:35.373788Z","shell.execute_reply.started":"2024-05-30T19:47:35.286605Z","shell.execute_reply":"2024-05-30T19:47:35.372949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nplot_model(model, to_file='model.png', show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:47:35.375039Z","iopub.execute_input":"2024-05-30T19:47:35.375847Z","iopub.status.idle":"2024-05-30T19:47:35.673291Z","shell.execute_reply.started":"2024-05-30T19:47:35.375812Z","shell.execute_reply":"2024-05-30T19:47:35.672366Z"},"trusted":true},"execution_count":null,"outputs":[]}]}