{"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 os\nimport glob\n\nimport pandas as pd\nimport numpy as np\nfrom pathlib import Path\nfrom ipywidgets import FileUpload\n\nimport matplotlib.pyplot as plt\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\n\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout,Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras import backend as K\nfrom keras.optimizers import Adam \nfrom tensorflow.keras.layers.experimental.preprocessing import Rescaling\n\nimport tensorflow as tf\nfrom tensorflow.keras import layers\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-03T03:35:22.927425Z","iopub.execute_input":"2023-03-03T03:35:22.928127Z","iopub.status.idle":"2023-03-03T03:35:22.93595Z","shell.execute_reply.started":"2023-03-03T03:35:22.928092Z","shell.execute_reply":"2023-03-03T03:35:22.934804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\ncolors_dark = [\"#1F1F1F\", \"#313131\", '#636363', '#AEAEAE', '#DADADA']\ncolors_red = [\"#331313\", \"#582626\", '#9E1717', '#D35151', '#E9B4B4']\ncolors_green = ['#01411C','#4B6F44','#4F7942','#74C365','#D0F0C0']\n\nsns.palplot(colors_dark)\nsns.palplot(colors_green)\nsns.palplot(colors_red)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:22.940901Z","iopub.execute_input":"2023-03-03T03:35:22.942078Z","iopub.status.idle":"2023-03-03T03:35:23.1779Z","shell.execute_reply.started":"2023-03-03T03:35:22.942043Z","shell.execute_reply":"2023-03-03T03:35:23.176517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-03-03T03:35:23.180441Z","iopub.execute_input":"2023-03-03T03:35:23.181224Z","iopub.status.idle":"2023-03-03T03:35:23.187564Z","shell.execute_reply.started":"2023-03-03T03:35:23.181181Z","shell.execute_reply":"2023-03-03T03:35:23.185977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-03-03T03:35:23.189687Z","iopub.execute_input":"2023-03-03T03:35:23.190556Z","iopub.status.idle":"2023-03-03T03:35:23.211761Z","shell.execute_reply.started":"2023-03-03T03:35:23.190441Z","shell.execute_reply":"2023-03-03T03:35:23.210528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path, size = 256):\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":"2023-03-03T03:35:23.214532Z","iopub.execute_input":"2023-03-03T03:35:23.215163Z","iopub.status.idle":"2023-03-03T03:35:23.222704Z","shell.execute_reply.started":"2023-03-03T03:35:23.215125Z","shell.execute_reply":"2023-03-03T03:35:23.220973Z"},"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 = 10\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=256):\n    return [load_dicom(path, size) for path in get_all_image_paths(brats21id, image_type, folder)]","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:23.224639Z","iopub.execute_input":"2023-03-03T03:35:23.225402Z","iopub.status.idle":"2023-03-03T03:35:23.237476Z","shell.execute_reply.started":"2023-03-03T03:35:23.225362Z","shell.execute_reply":"2023-03-03T03:35:23.235661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_all_data_for_train(image_type, image_size):\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":"2023-03-03T03:35:23.239228Z","iopub.execute_input":"2023-03-03T03:35:23.240046Z","iopub.status.idle":"2023-03-03T03:35:23.248965Z","shell.execute_reply.started":"2023-03-03T03:35:23.239982Z","shell.execute_reply":"2023-03-03T03:35:23.24753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_all_data_for_test(image_type, image_size):\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":"2023-03-03T03:35:23.250726Z","iopub.execute_input":"2023-03-03T03:35:23.251461Z","iopub.status.idle":"2023-03-03T03:35:23.259807Z","shell.execute_reply.started":"2023-03-03T03:35:23.251411Z","shell.execute_reply":"2023-03-03T03:35:23.258494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Model**","metadata":{}},{"cell_type":"code","source":"def get_model(size):\n\n    model=Sequential()\n\n    model.add(Rescaling(1.0 / 255, input_shape=(size,size,1)))\n\n    model.add(Conv2D(128,kernel_size=(4,4),activation='relu',input_shape=(size,size,1))) \n    model.add(MaxPooling2D(pool_size=(2,2)))\n\n    model.add(Conv2D(64,(4,4),activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2,2)))\n\n    model.add(Conv2D(32,(2,2),activation='relu'))\n    model.add(MaxPooling2D(pool_size=(1,1)))\n\n\n\n    model.add(Dropout(0.1)) #1st dropout ratio has to be less than 2nd dropout \n    model.add(Flatten())\n\n    model.add(Dense(128,activation='relu'))\n\n    model.add(Dense(2, activation='softmax'))\n        #compilation part\n\n\n    initial_learning_rate =  0.0002\n    lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n                initial_learning_rate,\n                decay_steps=100000,\n                decay_rate=0.96, \n                staircase=True\n        )\n    roc_auc = tf.keras.metrics.AUC(name='roc_auc', curve='ROC')\n    model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(learning_rate=lr_schedule),metrics=[roc_auc])\n    print(model.summary())\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:23.261743Z","iopub.execute_input":"2023-03-03T03:35:23.262588Z","iopub.status.idle":"2023-03-03T03:35:23.277803Z","shell.execute_reply.started":"2023-03-03T03:35:23.262542Z","shell.execute_reply":"2023-03-03T03:35:23.276242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-03-03T04:08:11.385098Z","iopub.execute_input":"2023-03-03T04:08:11.386239Z","iopub.status.idle":"2023-03-03T04:08:11.394183Z","shell.execute_reply.started":"2023-03-03T04:08:11.386193Z","shell.execute_reply":"2023-03-03T04:08:11.393013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1= get_model(size = 32)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:23.27955Z","iopub.execute_input":"2023-03-03T03:35:23.280261Z","iopub.status.idle":"2023-03-03T03:35:23.396526Z","shell.execute_reply.started":"2023-03-03T03:35:23.280212Z","shell.execute_reply":"2023-03-03T03:35:23.395709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Early Stop**","metadata":{}},{"cell_type":"code","source":"checkpoint_filepath = \"best_model.h5\"\n\nmodel_checkpoint_cb = tf.keras.callbacks.ModelCheckpoint(\n    filepath=checkpoint_filepath,\n    save_weights_only=False,\n    monitor=\"val_roc_auc\",\n    mode=\"max\",\n    save_best_only=True,\n    save_freq=\"epoch\",\n    verbose=1,\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:23.401199Z","iopub.execute_input":"2023-03-03T03:35:23.401596Z","iopub.status.idle":"2023-03-03T03:35:23.41301Z","shell.execute_reply.started":"2023-03-03T03:35:23.401558Z","shell.execute_reply":"2023-03-03T03:35:23.411806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# early_stopping_cb = tf.keras.callbacks.EarlyStopping(monitor=tf.keras.metrics.AUC(), mode='auto', verbose=1, patience=5)\n# early_stopping_cb = tf.keras.callbacks.EarlyStopping(monitor=\"val_acc\", patience=15)\nearly_stopping_cb = tf.keras.callbacks.EarlyStopping(monitor=\"val_roc_auc\", mode='max', patience=5)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:23.414185Z","iopub.execute_input":"2023-03-03T03:35:23.414521Z","iopub.status.idle":"2023-03-03T03:35:23.422002Z","shell.execute_reply.started":"2023-03-03T03:35:23.414487Z","shell.execute_reply":"2023-03-03T03:35:23.421019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **T1wCE**","metadata":{}},{"cell_type":"code","source":"X, y, trainidt = get_all_data_for_train('T1wCE', image_size=64)\nX_test, testidt = get_all_data_for_test('T1wCE', image_size=64)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:23.423438Z","iopub.execute_input":"2023-03-03T03:35:23.423779Z","iopub.status.idle":"2023-03-03T03:35:39.216207Z","shell.execute_reply.started":"2023-03-03T03:35:23.423745Z","shell.execute_reply":"2023-03-03T03:35:39.215103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, y.shape, trainidt.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:39.218039Z","iopub.execute_input":"2023-03-03T03:35:39.218757Z","iopub.status.idle":"2023-03-03T03:35:39.226481Z","shell.execute_reply.started":"2023-03-03T03:35:39.218719Z","shell.execute_reply":"2023-03-03T03:35:39.225333Z"},"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, trainidt, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:39.228506Z","iopub.execute_input":"2023-03-03T03:35:39.228938Z","iopub.status.idle":"2023-03-03T03:35:39.257747Z","shell.execute_reply.started":"2023-03-03T03:35:39.228903Z","shell.execute_reply":"2023-03-03T03:35:39.256689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:39.258898Z","iopub.execute_input":"2023-03-03T03:35:39.259173Z","iopub.status.idle":"2023-03-03T03:35:39.266378Z","shell.execute_reply.started":"2023-03-03T03:35:39.259148Z","shell.execute_reply":"2023-03-03T03:35:39.265208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nX_train = tf.expand_dims(X_train, axis=-1)\nX_valid = tf.expand_dims(X_valid, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:39.268018Z","iopub.execute_input":"2023-03-03T03:35:39.270332Z","iopub.status.idle":"2023-03-03T03:35:39.296645Z","shell.execute_reply.started":"2023-03-03T03:35:39.270293Z","shell.execute_reply":"2023-03-03T03:35:39.295599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:39.298344Z","iopub.execute_input":"2023-03-03T03:35:39.299129Z","iopub.status.idle":"2023-03-03T03:35:39.306687Z","shell.execute_reply.started":"2023-03-03T03:35:39.299089Z","shell.execute_reply":"2023-03-03T03:35:39.305487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train)\ny_valid = to_categorical(y_valid)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:39.309969Z","iopub.execute_input":"2023-03-03T03:35:39.310247Z","iopub.status.idle":"2023-03-03T03:35:39.316644Z","shell.execute_reply.started":"2023-03-03T03:35:39.310209Z","shell.execute_reply":"2023-03-03T03:35:39.315826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping\n\n\n# Define the early stopping callback\nearly_stop = EarlyStopping(monitor='val_loss', patience=3)\n\n# Train the model with early stopping\nhistory = model.fit(\n  X_train,\n  y_train,\n  epochs=10,\n  batch_size=32,\n  validation_data=(X_valid, y_valid),\n \n  #steps_per_epoch=len(X_train),\n  #validation_steps=len(X_valid),\n  #callbacks=[early_stop]\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:35:39.318027Z","iopub.execute_input":"2023-03-03T03:35:39.319038Z","iopub.status.idle":"2023-03-03T03:36:19.18517Z","shell.execute_reply.started":"2023-03-03T03:35:39.319003Z","shell.execute_reply":"2023-03-03T03:36:19.184214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Flair**","metadata":{}},{"cell_type":"code","source":"\"\"\"model2=Sequential()\n\nmodel2.add(Rescaling(1.0 / 255, input_shape=(32,32,1)))\n\nmodel2.add(Conv2D(128,kernel_size=(4,4),activation='relu',input_shape=(32,32,1))) \nmodel2.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel2.add(Conv2D(64,(4,4),activation='relu'))\nmodel2.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel2.add(Conv2D(32,(2,2),activation='relu'))\nmodel2.add(MaxPooling2D(pool_size=(1,1)))\n\n\n\nmodel2.add(Dropout(0.1)) #1st dropout ratio has to be less than 2nd dropout \nmodel2.add(Flatten())\n\nmodel2.add(Dense(128,activation='relu'))\n\nmodel2.add(Dense(2, activation='softmax'))\n#compilation part\n\n\ninitial_learning_rate =  0.0002\nlr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n        initial_learning_rate,\n        decay_steps=100000,\n        decay_rate=0.96, \n        staircase=True\n    )\nroc_auc = tf.keras.metrics.AUC(name='roc_auc', curve='ROC')\nmodel2.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(learning_rate=lr_schedule),metrics=[roc_auc])\nprint(model2.summary())\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:36:19.186855Z","iopub.execute_input":"2023-03-03T03:36:19.187152Z","iopub.status.idle":"2023-03-03T03:36:19.194361Z","shell.execute_reply.started":"2023-03-03T03:36:19.187126Z","shell.execute_reply":"2023-03-03T03:36:19.193164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y, trainidt = get_all_data_for_train('FLAIR', image_size=32)\nX_test, testidt = get_all_data_for_test('FLAIR', image_size=32)\n\n\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)\n\nX_train = tf.expand_dims(X_train, axis=-1)\nX_valid = tf.expand_dims(X_valid, axis=-1)\n\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:36:19.196091Z","iopub.execute_input":"2023-03-03T03:36:19.196785Z","iopub.status.idle":"2023-03-03T03:36:32.611911Z","shell.execute_reply.started":"2023-03-03T03:36:19.19675Z","shell.execute_reply":"2023-03-03T03:36:32.610958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train)\ny_valid = to_categorical(y_valid)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:36:32.613416Z","iopub.execute_input":"2023-03-03T03:36:32.613791Z","iopub.status.idle":"2023-03-03T03:36:32.619534Z","shell.execute_reply.started":"2023-03-03T03:36:32.613754Z","shell.execute_reply":"2023-03-03T03:36:32.618345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = get_model(size = 32)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:36:32.621098Z","iopub.execute_input":"2023-03-03T03:36:32.621897Z","iopub.status.idle":"2023-03-03T03:36:32.734056Z","shell.execute_reply.started":"2023-03-03T03:36:32.62186Z","shell.execute_reply":"2023-03-03T03:36:32.733259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping\n\n\n# Define the early stopping callback\nearly_stop = EarlyStopping(monitor='val_loss', patience=3)\n\n# Train the model with early stopping\nhistory2 = model2.fit(\n  X_train,\n  y_train,\n  epochs=10,\n  batch_size=32,\n  validation_data=(X_valid, y_valid),\n \n  #steps_per_epoch=len(X_train),\n  #validation_steps=len(X_valid),\n  #callbacks=[early_stop]\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:36:32.735091Z","iopub.execute_input":"2023-03-03T03:36:32.735622Z","iopub.status.idle":"2023-03-03T03:36:39.860265Z","shell.execute_reply.started":"2023-03-03T03:36:32.735594Z","shell.execute_reply":"2023-03-03T03:36:39.859195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"f2 = get_model() # LB score 0.5\"\"\"\n","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:36:39.862574Z","iopub.execute_input":"2023-03-03T03:36:39.862961Z","iopub.status.idle":"2023-03-03T03:36:39.871602Z","shell.execute_reply.started":"2023-03-03T03:36:39.862922Z","shell.execute_reply":"2023-03-03T03:36:39.870336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Fusion**","metadata":{}},{"cell_type":"code","source":"def get_model_flatten(size):\n\n    model=Sequential()\n\n    model.add(Rescaling(1.0 / 255, input_shape=(size,size,1)))\n\n    model.add(Conv2D(128,kernel_size=(4,4),activation='relu',input_shape=(size,size,1))) \n    model.add(MaxPooling2D(pool_size=(2,2)))\n\n    model.add(Conv2D(64,(4,4),activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2,2)))\n\n    model.add(Conv2D(32,(2,2),activation='relu'))\n    model.add(MaxPooling2D(pool_size=(1,1)))\n\n\n\n    model.add(Dropout(0.1)) #1st dropout ratio has to be less than 2nd dropout \n    model.add(Flatten())\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-03-03T05:12:19.532047Z","iopub.execute_input":"2023-03-03T05:12:19.532436Z","iopub.status.idle":"2023-03-03T05:12:19.540847Z","shell.execute_reply.started":"2023-03-03T05:12:19.532402Z","shell.execute_reply":"2023-03-03T05:12:19.539085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Input, Dense, concatenate\nfrom keras.models import Model\n\n# Define input shapes for the two models\ninput_shape1 = (32, 32, 1)\ninput_shape2 = (64, 64, 1)\n\n# Define first model\ninput_layer1 = Input(shape=input_shape1)\nconv_layer1 = Conv2D(32, kernel_size=(3, 3), activation='relu')(input_layer1)\nflatten_layer1 = Flatten()(conv_layer1)\ndense_layer1 = Dense(64, activation='relu')(flatten_layer1)\noutput_layer1 = Dense(num_classes, activation='softmax')(dense_layer1)\nmodel11 = Model(inputs=input_layer1, outputs=output_layer1)\n\n# Define second model\n\ninput_layer2 = Input(shape=input_shape2)\nconv_layer2 = Conv2D(32, kernel_size=(3, 3), activation='relu')(input_layer2)\nflatten_layer2 = Flatten()(conv_layer2)\ndense_layer2 = Dense(64, activation='relu')(flatten_layer2)\noutput_layer2 = Dense(num_classes, activation='softmax')(dense_layer2)\nmodel12 = Model(inputs=input_layer2, outputs=output_layer2)\n\n# Concatenate the output of the two models\nmerged_layer = concatenate([model11.output, model12.output])\n\n# Define the final output layer\noutput_layer = Dense(num_classes, activation='softmax')(merged_layer)\n\n# Define the concatenated model\nmodel = Model(inputs=[model11.input, model12.input], outputs=output_layer)\n\n# Print model summary\nmodel.summary()\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nmodel.fit([, train_data], train_labels, epochs=10, batch_size=32, validation_data=([val_images, val_data], val_labels))\n","metadata":{"execution":{"iopub.status.busy":"2023-03-03T05:23:44.672249Z","iopub.execute_input":"2023-03-03T05:23:44.672942Z","iopub.status.idle":"2023-03-03T05:23:44.729666Z","shell.execute_reply.started":"2023-03-03T05:23:44.672906Z","shell.execute_reply":"2023-03-03T05:23:44.728318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Input, Dense, concatenate\nfrom keras.models import Model\nmodel11 = get_model_flatten(32)\n\n\nmodel12 = get_model_flatten(32)\n\n\n\nmodel33 = concatenate([model11.output,model12.output])\nmodel = Model(inputs=[model11.input, model12.input],outputs = model33)\n\nmodel33.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T05:15:53.345713Z","iopub.execute_input":"2023-03-03T05:15:53.346713Z","iopub.status.idle":"2023-03-03T05:15:53.475988Z","shell.execute_reply.started":"2023-03-03T05:15:53.346676Z","shell.execute_reply":"2023-03-03T05:15:53.474122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# concating\n\n","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:36:39.874354Z","iopub.execute_input":"2023-03-03T03:36:39.874964Z","iopub.status.idle":"2023-03-03T03:36:39.881041Z","shell.execute_reply.started":"2023-03-03T03:36:39.874824Z","shell.execute_reply":"2023-03-03T03:36:39.87997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfig, axs = plt.subplots(2, 2, figsize=(10, 4))\n\nplt.subplot(1, 2, 1)\n\nplt.plot(history.history['roc_auc'])\nplt.plot(history.history['val_roc_auc'])\nplt.title('Model Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\n\n\nplt.subplot(1, 2, 2)\n\nimport matplotlib.pyplot as plt\n\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model Loss')\nplt.ylabel('loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:36:39.909378Z","iopub.execute_input":"2023-03-03T03:36:39.909808Z","iopub.status.idle":"2023-03-03T03:36:40.282876Z","shell.execute_reply.started":"2023-03-03T03:36:39.909774Z","shell.execute_reply":"2023-03-03T03:36:40.281879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report,confusion_matrix\n\npred = model.predict(X_valid)\n\npred = np.argmax(pred,axis=1)\n\ny_test_new = np.argmax(y_valid,axis=1)\n\nprint(classification_report(y_test_new,pred))","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:36:40.28448Z","iopub.execute_input":"2023-03-03T03:36:40.285102Z","iopub.status.idle":"2023-03-03T03:36:40.360738Z","shell.execute_reply.started":"2023-03-03T03:36:40.285063Z","shell.execute_reply":"2023-03-03T03:36:40.359201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score, plot_roc_curve\n\nlabels = ['yes','no']\n\nfig,ax=plt.subplots(1,1,figsize=(14,7))\nsns.heatmap(confusion_matrix(y_test_new,pred),ax=ax,xticklabels=labels,yticklabels=labels,annot=True,\n           cmap=colors_green[::-1],alpha=0.7,linewidths=2,linecolor=colors_dark[3])\nfig.text(s='Heatmap of the Confusion Matrix',size=18,fontweight='bold',\n             fontname='monospace',color=colors_dark[1],y=0.92,x=0.28,alpha=0.8)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T03:36:40.362111Z","iopub.status.idle":"2023-03-03T03:36:40.362634Z","shell.execute_reply.started":"2023-03-03T03:36:40.362375Z","shell.execute_reply":"2023-03-03T03:36:40.362403Z"},"trusted":true},"execution_count":null,"outputs":[]}]}