{"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 json\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport pydicom\nfrom keras import layers\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import Callback, ModelCheckpoint, EarlyStopping\nfrom keras.initializers import Constant\nfrom keras.models import Sequential\nfrom keras.optimizers import Adam\nfrom tensorflow.python.ops import array_ops\nfrom tqdm import tqdm\nfrom keras import backend as K\nimport tensorflow as tf\nimport keras\nfrom keras.applications import Xception\nfrom keras.models import Model, load_model\nfrom math import ceil, floor\nfrom sklearn.model_selection import ShuffleSplit\nfrom sklearn.metrics import log_loss\nfrom keras.layers import Dense, Flatten, Dropout, GlobalAveragePooling2D","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-15T07:10:23.490527Z","iopub.execute_input":"2023-02-15T07:10:23.490922Z","iopub.status.idle":"2023-02-15T07:10:30.604182Z","shell.execute_reply.started":"2023-02-15T07:10:23.490836Z","shell.execute_reply":"2023-02-15T07:10:30.603222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report\nfrom sklearn.metrics import multilabel_confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:30.611078Z","iopub.execute_input":"2023-02-15T07:10:30.611521Z","iopub.status.idle":"2023-02-15T07:10:30.617538Z","shell.execute_reply.started":"2023-02-15T07:10:30.611458Z","shell.execute_reply":"2023-02-15T07:10:30.616626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"METRICS = [tf.keras.metrics.BinaryAccuracy(), \n           tf.keras.metrics.Precision(),\n           tf.keras.metrics.Recall(),\n           tf.keras.metrics.AUC(),\n           tf.keras.metrics.SpecificityAtSensitivity(0.5),\n           tf.keras.metrics.SensitivityAtSpecificity(0.5)\n          ]","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:30.619966Z","iopub.execute_input":"2023-02-15T07:10:30.620755Z","iopub.status.idle":"2023-02-15T07:10:33.256972Z","shell.execute_reply.started":"2023-02-15T07:10:30.620712Z","shell.execute_reply":"2023-02-15T07:10:33.255999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_learning_curves(history, metrics_to_plot = ['loss','binary_accuracy', 'precision', 'recall', 'auc']):\n  ncols = 2\n  nrows = math.ceil(len(metrics_to_plot) / 2)\n  if len(metrics_to_plot) <= 2:\n        fig, axes = plt.subplots(nrows,ncols, figsize=(20,10))\n        for i in range(2):\n            axes[i].plot(history.history[metrics_to_plot[i]], label=metrics_to_plot[i] +' (training data)')\n            axes[i].plot(history.history['val_'+metrics_to_plot[i]], label=metrics_to_plot[i] + ' (val data)')\n            axes[i].set_ylabel('Value', fontsize = 20)\n            axes[i].set_xlabel('No. epoch', fontsize = 20)\n            axes[i].legend(prop={'size': 20})\n            axes[i].set_title(metrics_to_plot[i], size = 22)\n  else:        \n      fig, axes = plt.subplots(nrows,ncols, figsize=(15,20))\n\n      for i in range(ncols):\n        for j in range(nrows):\n          metric_idx = j * ncols + i\n          if metric_idx >= len(metrics_to_plot):\n                break\n          axes[j,i].plot(history.history[metrics_to_plot[metric_idx]], label=metrics_to_plot[metric_idx] +' (training data)')\n          axes[j,i].plot(history.history['val_'+metrics_to_plot[metric_idx]], label=metrics_to_plot[metric_idx] + ' (val data)')\n          axes[j,i].set_ylabel('Value', fontsize = 20)\n          axes[j,i].set_xlabel('No. epoch', fontsize = 20)\n          axes[j,i].legend(prop={'size': 20})\n          axes[j,i].set_title(metrics_to_plot[metric_idx], size = 22)\n  plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:33.258749Z","iopub.execute_input":"2023-02-15T07:10:33.259285Z","iopub.status.idle":"2023-02-15T07:10:33.271508Z","shell.execute_reply.started":"2023-02-15T07:10:33.259242Z","shell.execute_reply":"2023-02-15T07:10:33.270619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def np_multilabel_loss(class_weights=None):\n    def single_class_crossentropy(y_true, y_pred):\n        y_true = tf.cast(y_true, tf.float32)\n        y_pred = tf.cast(y_pred, tf.float32)\n        \n        y_pred = tf.where(y_pred > 1-(1e-07), 1-1e-07, y_pred)\n        y_pred = tf.where(y_pred < 1e-07, 1e-07, y_pred)\n        single_class_cross_entropies = - tf.reduce_mean(y_true * tf.math.log(y_pred) + (1-y_true) * tf.math.log(1-y_pred), axis=0)\n\n        if class_weights is None:\n            loss = tf.reduce_mean(single_class_cross_entropies)\n        else:\n            loss = tf.reduce_sum(class_weights*single_class_cross_entropies)\n        return loss\n    return single_class_crossentropy\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:33.273077Z","iopub.execute_input":"2023-02-15T07:10:33.274556Z","iopub.status.idle":"2023-02-15T07:10:33.289409Z","shell.execute_reply.started":"2023-02-15T07:10:33.274513Z","shell.execute_reply":"2023-02-15T07:10:33.2884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"METRICS = METRICS + [np_multilabel_loss()]\nMETRICS_NAMES = []\nfor metric in METRICS:\n    if hasattr(metric, 'name'):\n        METRICS_NAMES.append(metric.name)\n    else:\n        METRICS_NAMES.append(metric.__name__)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:33.290672Z","iopub.execute_input":"2023-02-15T07:10:33.290962Z","iopub.status.idle":"2023-02-15T07:10:33.304633Z","shell.execute_reply.started":"2023-02-15T07:10:33.290934Z","shell.execute_reply":"2023-02-15T07:10:33.303525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection')","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:33.306292Z","iopub.execute_input":"2023-02-15T07:10:33.306825Z","iopub.status.idle":"2023-02-15T07:10:33.328729Z","shell.execute_reply.started":"2023-02-15T07:10:33.306783Z","shell.execute_reply":"2023-02-15T07:10:33.327651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = '/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/'\nTRAIN_DIR = 'stage_2_train/'\nTEST_DIR = 'stage_2_test/'\ntrain_df = pd.read_csv(BASE_PATH + 'stage_2_train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:33.331889Z","iopub.execute_input":"2023-02-15T07:10:33.332285Z","iopub.status.idle":"2023-02-15T07:10:38.305151Z","shell.execute_reply.started":"2023-02-15T07:10:33.332239Z","shell.execute_reply":"2023-02-15T07:10:38.304203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv(BASE_PATH + 'stage_2_sample_submission.csv')\n\ntrain_df['filename'] = train_df['ID'].apply(lambda st: \"ID_\" + st.split('_')[1] + \".png\")\ntrain_df['type'] = train_df['ID'].apply(lambda st: st.split('_')[2])\nsub_df['filename'] = sub_df['ID'].apply(lambda st: \"ID_\" + st.split('_')[1] + \".png\")\nsub_df['type'] = sub_df['ID'].apply(lambda st: st.split('_')[2])\n\nprint(train_df.shape)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:38.308109Z","iopub.execute_input":"2023-02-15T07:10:38.308407Z","iopub.status.idle":"2023-02-15T07:10:45.187075Z","shell.execute_reply.started":"2023-02-15T07:10:38.308364Z","shell.execute_reply":"2023-02-15T07:10:45.186214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(sub_df.filename.unique(), columns=['filename'])\nprint(test_df.shape)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:45.188553Z","iopub.execute_input":"2023-02-15T07:10:45.188964Z","iopub.status.idle":"2023-02-15T07:10:45.277294Z","shell.execute_reply.started":"2023-02-15T07:10:45.188924Z","shell.execute_reply":"2023-02-15T07:10:45.276374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subtypes = train_df.groupby('type').sum()\nsubtypes","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:45.278637Z","iopub.execute_input":"2023-02-15T07:10:45.27902Z","iopub.status.idle":"2023-02-15T07:10:46.660489Z","shell.execute_reply.started":"2023-02-15T07:10:45.278981Z","shell.execute_reply":"2023-02-15T07:10:46.659501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(y=subtypes.index, x=subtypes.Label, palette=\"deep\")","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:46.661901Z","iopub.execute_input":"2023-02-15T07:10:46.662486Z","iopub.status.idle":"2023-02-15T07:10:46.859399Z","shell.execute_reply.started":"2023-02-15T07:10:46.662443Z","shell.execute_reply":"2023-02-15T07:10:46.858326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(2023)\nsample_files = np.random.choice(os.listdir(BASE_PATH + TRAIN_DIR), 100000) # take the rest for testing\nsample_df = train_df[train_df.filename.apply(lambda x: x.replace('.png', '.dcm')).isin(sample_files)]","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:10:46.860773Z","iopub.execute_input":"2023-02-15T07:10:46.861148Z","iopub.status.idle":"2023-02-15T07:11:49.497629Z","shell.execute_reply.started":"2023-02-15T07:10:46.861116Z","shell.execute_reply":"2023-02-15T07:11:49.496637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pivot_df = sample_df[['Label', 'filename', 'type']].drop_duplicates().pivot(\n    index='filename', columns='type', values='Label').reset_index()\nprint(pivot_df.shape)\npivot_df","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:49.498987Z","iopub.execute_input":"2023-02-15T07:11:49.499358Z","iopub.status.idle":"2023-02-15T07:11:50.38194Z","shell.execute_reply.started":"2023-02-15T07:11:49.499321Z","shell.execute_reply":"2023-02-15T07:11:50.380928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#one_df = pivot_df.drop(pivot_df.loc[pivot_df['subdural']==0].index)\n#one_df","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:50.383581Z","iopub.execute_input":"2023-02-15T07:11:50.384256Z","iopub.status.idle":"2023-02-15T07:11:50.388327Z","shell.execute_reply.started":"2023-02-15T07:11:50.384207Z","shell.execute_reply":"2023-02-15T07:11:50.38717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#zero_df = pivot_df.drop(pivot_df.loc[pivot_df['any']==1].index)\n#zero_df","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:50.389931Z","iopub.execute_input":"2023-02-15T07:11:50.390604Z","iopub.status.idle":"2023-02-15T07:11:50.403699Z","shell.execute_reply.started":"2023-02-15T07:11:50.390557Z","shell.execute_reply":"2023-02-15T07:11:50.402656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#zero_df = zero_df.sample(47166)\n#zero_df","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:50.405096Z","iopub.execute_input":"2023-02-15T07:11:50.405554Z","iopub.status.idle":"2023-02-15T07:11:50.41717Z","shell.execute_reply.started":"2023-02-15T07:11:50.405499Z","shell.execute_reply":"2023-02-15T07:11:50.416036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sample_df = pd.concat([zero_df, one_df])\n#sample_df","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:50.418685Z","iopub.execute_input":"2023-02-15T07:11:50.419137Z","iopub.status.idle":"2023-02-15T07:11:50.428962Z","shell.execute_reply.started":"2023-02-15T07:11:50.419096Z","shell.execute_reply":"2023-02-15T07:11:50.42807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = sub_df[['Label', 'filename', 'type']].drop_duplicates().pivot(\n    index='filename', columns='type', values='Label').reset_index()\nprint(test_df.shape)\ntest_df","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:50.43029Z","iopub.execute_input":"2023-02-15T07:11:50.430657Z","iopub.status.idle":"2023-02-15T07:11:51.414544Z","shell.execute_reply.started":"2023-02-15T07:11:50.430625Z","shell.execute_reply":"2023-02-15T07:11:51.41336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(2023)\nsample_test = np.random.choice(os.listdir(BASE_PATH + TEST_DIR), 30000) \ntest_sample_df = test_df[test_df.filename.apply(lambda x: x.replace('.png', '.dcm')).isin(sample_test)]\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:51.416167Z","iopub.execute_input":"2023-02-15T07:11:51.416627Z","iopub.status.idle":"2023-02-15T07:11:58.406642Z","shell.execute_reply.started":"2023-02-15T07:11:51.41658Z","shell.execute_reply":"2023-02-15T07:11:58.405651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_sample_df","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:58.408153Z","iopub.execute_input":"2023-02-15T07:11:58.408555Z","iopub.status.idle":"2023-02-15T07:11:58.431571Z","shell.execute_reply.started":"2023-02-15T07:11:58.408512Z","shell.execute_reply":"2023-02-15T07:11:58.430285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#zero_df = pivot_df.drop(pivot_df.loc[pivot_df['any']==1].index)\n#zero_df","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:58.433321Z","iopub.execute_input":"2023-02-15T07:11:58.433802Z","iopub.status.idle":"2023-02-15T07:11:58.441754Z","shell.execute_reply.started":"2023-02-15T07:11:58.433753Z","shell.execute_reply":"2023-02-15T07:11:58.440613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#zero_df = zero_df.sample(47166)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:58.448431Z","iopub.execute_input":"2023-02-15T07:11:58.448839Z","iopub.status.idle":"2023-02-15T07:11:58.454175Z","shell.execute_reply.started":"2023-02-15T07:11:58.448803Z","shell.execute_reply":"2023-02-15T07:11:58.452612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sample_df = pd.concat([zero_df, one_df])\n#sample_df","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:58.458115Z","iopub.execute_input":"2023-02-15T07:11:58.45861Z","iopub.status.idle":"2023-02-15T07:11:58.465061Z","shell.execute_reply.started":"2023-02-15T07:11:58.458558Z","shell.execute_reply":"2023-02-15T07:11:58.463679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.utils import shuffle\n#sample_df = shuffle(sample_df)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:58.466451Z","iopub.execute_input":"2023-02-15T07:11:58.466817Z","iopub.status.idle":"2023-02-15T07:11:58.476418Z","shell.execute_reply.started":"2023-02-15T07:11:58.466784Z","shell.execute_reply":"2023-02-15T07:11:58.475417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_df = pivot_df.sample(int(len(pivot_df) * 0.15))  \nvalidation_df ","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:58.477984Z","iopub.execute_input":"2023-02-15T07:11:58.478348Z","iopub.status.idle":"2023-02-15T07:11:58.508444Z","shell.execute_reply.started":"2023-02-15T07:11:58.478314Z","shell.execute_reply":"2023-02-15T07:11:58.507617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = []\nfor i in range(len(validation_df)): \n    y_true.append(validation_df.iloc[i,1])\n        \n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:58.509937Z","iopub.execute_input":"2023-02-15T07:11:58.51035Z","iopub.status.idle":"2023-02-15T07:11:58.850632Z","shell.execute_reply.started":"2023-02-15T07:11:58.510307Z","shell.execute_reply":"2023-02-15T07:11:58.849608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_true)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:58.851996Z","iopub.execute_input":"2023-02-15T07:11:58.852365Z","iopub.status.idle":"2023-02-15T07:11:58.858967Z","shell.execute_reply.started":"2023-02-15T07:11:58.852327Z","shell.execute_reply":"2023-02-15T07:11:58.857989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_true = []\nfor i in range(len(validation_df)): \n    for j in range(1,7): \n        full_true.append(validation_df.iloc[i,j])\n        \n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:11:58.860621Z","iopub.execute_input":"2023-02-15T07:11:58.86143Z","iopub.status.idle":"2023-02-15T07:12:00.816951Z","shell.execute_reply.started":"2023-02-15T07:11:58.861362Z","shell.execute_reply":"2023-02-15T07:12:00.81592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#len(full_true)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:12:00.818481Z","iopub.execute_input":"2023-02-15T07:12:00.818893Z","iopub.status.idle":"2023-02-15T07:12:00.824474Z","shell.execute_reply.started":"2023-02-15T07:12:00.818848Z","shell.execute_reply":"2023-02-15T07:12:00.822227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_df = pivot_df[~(pivot_df.filename.isin(validation_df.filename))]\ntraining_df\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:12:00.826333Z","iopub.execute_input":"2023-02-15T07:12:00.827077Z","iopub.status.idle":"2023-02-15T07:12:00.874422Z","shell.execute_reply.started":"2023-02-15T07:12:00.827032Z","shell.execute_reply":"2023-02-15T07:12:00.87335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(training_df.head())\nprint(validation_df.head())\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:12:00.875921Z","iopub.execute_input":"2023-02-15T07:12:00.876322Z","iopub.status.idle":"2023-02-15T07:12:00.889464Z","shell.execute_reply.started":"2023-02-15T07:12:00.876278Z","shell.execute_reply":"2023-02-15T07:12:00.887345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_pixels_hu(scan): \n    image = np.stack([scan.pixel_array])\n    image = image.astype(np.int16) \n    \n    image[image == -2000] = 0\n    \n    intercept = scan.RescaleIntercept\n    slope = scan.RescaleSlope\n    \n    if slope != 1: \n        image = slope * image.astype(np.float64)\n        image = image.astype(np.int16)\n    \n    image += np.int16(intercept) \n    \n    return np.array(image, dtype=np.int16)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:12:00.891355Z","iopub.execute_input":"2023-02-15T07:12:00.892127Z","iopub.status.idle":"2023-02-15T07:12:00.901433Z","shell.execute_reply.started":"2023-02-15T07:12:00.892082Z","shell.execute_reply":"2023-02-15T07:12:00.900363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def apply_window(image, center, width):\n    image = image.copy()\n    min_value = center - width // 2\n    max_value = center + width // 2\n    image[image < min_value] = min_value\n    image[image > max_value] = max_value\n    return image\n\n\ndef apply_window_policy(image):\n\n    image1 = apply_window(image, 40, 80) # brain\n    image2 = apply_window(image, 80, 200) # subdural\n    image3 = apply_window(image, 40, 380) # bone\n    image1 = (image1 - 0) / 80\n    image2 = (image2 - (-20)) / 200\n    image3 = (image3 - (-150)) / 380\n    image = np.array([\n        image1 - image1.mean(),\n        image2 - image2.mean(),\n        image3 - image3.mean(),\n    ]).transpose(1,2,0)\n\n    return image\n#maybe try a new function ","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:12:00.903039Z","iopub.execute_input":"2023-02-15T07:12:00.903864Z","iopub.status.idle":"2023-02-15T07:12:00.917525Z","shell.execute_reply.started":"2023-02-15T07:12:00.903831Z","shell.execute_reply":"2023-02-15T07:12:00.916444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_and_resize(filenames, load_dir):    \n    save_dir = '/kaggle/tmp/'\n    if not os.path.exists(save_dir):\n        os.makedirs(save_dir)\n\n    for filename in tqdm(filenames):\n        try:\n            path = load_dir + filename\n            new_path = save_dir + filename.replace('.dcm', '.png')\n            dcm = pydicom.dcmread(path)\n            image = get_pixels_hu(dcm)\n            image = apply_window_policy(image[0])\n            image -= image.min((0,1))\n            image = (255*image).astype(np.uint8)\n            image = cv2.resize(image, (299, 299)) #smaller\n            res = cv2.imwrite(new_path, image)\n            \n        except ValueError:\n            continue # it returns a black image, super weird ","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:12:00.919438Z","iopub.execute_input":"2023-02-15T07:12:00.919896Z","iopub.status.idle":"2023-02-15T07:12:00.930107Z","shell.execute_reply.started":"2023-02-15T07:12:00.919853Z","shell.execute_reply":"2023-02-15T07:12:00.929067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_and_resize(filenames=sample_files, load_dir=BASE_PATH + TRAIN_DIR)\nsave_and_resize(filenames=sample_test, load_dir=BASE_PATH + TEST_DIR)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T07:12:00.931692Z","iopub.execute_input":"2023-02-15T07:12:00.932165Z","iopub.status.idle":"2023-02-15T08:27:16.084139Z","shell.execute_reply.started":"2023-02-15T07:12:00.932123Z","shell.execute_reply":"2023-02-15T08:27:16.080966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#def create_model():    \n#   base_model = Xception(weights = 'imagenet', include_top = False, input_shape = (299,299,3))\n#    x = base_model.output\n#    x = GlobalAveragePooling2D()(x)\n #   x = Dropout(0.15)(x)\n  #  y_pred = Dense(6, activation = 'sigmoid')(x)\n#\n #   return Model(inputs = base_model.input, outputs = y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:16.088741Z","iopub.execute_input":"2023-02-15T08:27:16.089117Z","iopub.status.idle":"2023-02-15T08:27:16.094505Z","shell.execute_reply.started":"2023-02-15T08:27:16.089081Z","shell.execute_reply":"2023-02-15T08:27:16.093446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#LR = 0.00005\n#model = create_model()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:16.095931Z","iopub.execute_input":"2023-02-15T08:27:16.096541Z","iopub.status.idle":"2023-02-15T08:27:16.155338Z","shell.execute_reply.started":"2023-02-15T08:27:16.0965Z","shell.execute_reply":"2023-02-15T08:27:16.154234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from keras.utils.vis_utils import plot_model\n#plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:16.158174Z","iopub.execute_input":"2023-02-15T08:27:16.158484Z","iopub.status.idle":"2023-02-15T08:27:16.166455Z","shell.execute_reply.started":"2023-02-15T08:27:16.158453Z","shell.execute_reply":"2023-02-15T08:27:16.165449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.compile(optimizer = Adam(learning_rate = LR), \n #             loss = 'binary_crossentropy', # <- requires balance/ Binary for unbalanced\n  #            metrics = METRICS) #run both ","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:16.168149Z","iopub.execute_input":"2023-02-15T08:27:16.168692Z","iopub.status.idle":"2023-02-15T08:27:16.176714Z","shell.execute_reply.started":"2023-02-15T08:27:16.168651Z","shell.execute_reply":"2023-02-15T08:27:16.17571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras_preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:16.178335Z","iopub.execute_input":"2023-02-15T08:27:16.178837Z","iopub.status.idle":"2023-02-15T08:27:16.185649Z","shell.execute_reply.started":"2023-02-15T08:27:16.178795Z","shell.execute_reply":"2023-02-15T08:27:16.184778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 16 # had to revert back to 16 to have a comparaison point with the large model I ran locally \n\ndef create_datagen():\n    return ImageDataGenerator()\n\ndef create_test_gen():\n    return ImageDataGenerator().flow_from_dataframe(\n        test_sample_df,\n        directory=  '/kaggle/tmp/',\n        x_col='filename',\n        class_mode=None,\n        target_size=(299, 299),\n        batch_size=BATCH_SIZE,\n        shuffle=False\n    )\n\ndef create_train_gen(datagen):\n    return datagen.flow_from_dataframe(\n        training_df, \n        directory='/kaggle/tmp/',\n        \n        x_col='filename', \n        y_col=['any', 'epidural', 'intraparenchymal', \n               'intraventricular', 'subarachnoid', 'subdural'],\n        class_mode='raw',\n        target_size=(299, 299),\n        batch_size=BATCH_SIZE,\n        \n       \n    )\ndef create_val_gen(datagen): \n    return datagen.flow_from_dataframe(\n        validation_df, \n        directory='/kaggle/tmp/',\n        \n        x_col='filename', \n        y_col=['any', 'epidural', 'intraparenchymal', \n               'intraventricular', 'subarachnoid', 'subdural'],\n        class_mode='raw',\n        target_size=(299, 299),\n        batch_size=BATCH_SIZE,\n        shuffle=False,\n        \n    )\n\n# Using original generator\ndata_generator = create_datagen()\ntrain_gen = create_train_gen(data_generator)\nval_gen = create_val_gen(data_generator)\ntest_gen = create_test_gen()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:16.187069Z","iopub.execute_input":"2023-02-15T08:27:16.187483Z","iopub.status.idle":"2023-02-15T08:27:17.328195Z","shell.execute_reply.started":"2023-02-15T08:27:16.187445Z","shell.execute_reply":"2023-02-15T08:27:17.327197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.3296Z","iopub.execute_input":"2023-02-15T08:27:17.330103Z","iopub.status.idle":"2023-02-15T08:27:17.335222Z","shell.execute_reply.started":"2023-02-15T08:27:17.330062Z","shell.execute_reply":"2023-02-15T08:27:17.334135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checkpoint = ModelCheckpoint(\n#    'effnetb4.h5', \n#    monitor='val_loss', \n#    verbose=0, \n#    save_best_only=True, \n #   save_weights_only=False,\n #   mode='auto'\n#)\n#Early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=1, \n #                                             mode='auto', baseline=None, restore_best_weights=False)\n#train_length = len(train_df)\n#total_steps = sample_files.shape[0] // BATCH_SIZE\n#total_steps = total_steps // 4\n#history = model.fit_generator(\n #   train_gen,\n  #  steps_per_epoch = total_steps,\n   # validation_data=val_gen,\n   # validation_steps=total_steps * 0.15,\n   # callbacks=[checkpoint],\n    #callbacks=[checkpoint, Early_stop],\n   # epochs=10\n#)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.337142Z","iopub.execute_input":"2023-02-15T08:27:17.3379Z","iopub.status.idle":"2023-02-15T08:27:17.344694Z","shell.execute_reply.started":"2023-02-15T08:27:17.337858Z","shell.execute_reply":"2023-02-15T08:27:17.343485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#acc = history.history['binary_accuracy']\n#val_acc = history.history['val_binary_accuracy']\n#loss = history.history['loss']\n#val_loss = history.history['val_loss']\n#epochs = range(1, len(acc) + 1)\n\n#plt.plot(epochs, acc, 'b', label='Training Sens')\n#plt.plot(epochs, val_acc, 'g', label='Validation Sens')\n#plt.xlabel('Epochs')\n#plt.ylabel('Accuracy')\n\n#plt.title('Training and validation accuracy')\n#plt.legend()\n#fig = plt.figure()\n#fig.savefig('acc.png')\n\n\n#plt.plot(epochs, loss, 'b', label='Training loss')\n#plt.plot(epochs, val_loss, 'g', label='Validation loss')\n#plt.xlabel('Epochs')\n#plt.ylabel('Loss')\n#plt.title('Training and validation loss')\n\n#plt.legend()\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.346267Z","iopub.execute_input":"2023-02-15T08:27:17.346818Z","iopub.status.idle":"2023-02-15T08:27:17.355889Z","shell.execute_reply.started":"2023-02-15T08:27:17.346775Z","shell.execute_reply":"2023-02-15T08:27:17.354782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sens = history.history['sensitivity_at_specificity']\n#val_sens = history.history['val_sensitivity_at_specificity']\n#preci = history.history['precision']\n#val_preci = history.history['val_precision']\n#epochs = range(1, len(acc) + 1)\n\n#plt.plot(epochs, sens, 'b', label='Training Sens')\n#plt.plot(epochs, val_sens, 'g', label='Validation Sens')\n#plt.xlabel('Epochs')\n#plt.ylabel('sensitivity')\n\n#plt.title('Training and validation sensitivity')\n#plt.legend()\n#fig = plt.figure()\n#fig.savefig('acc.png')\n\n\n#plt.plot(epochs, preci, 'b', label='Training Precision')\n#plt.plot(epochs, val_preci, 'g', label='Validation Precision')\n#plt.xlabel('Epochs')\n#plt.ylabel('precision')\n\n#plt.legend()\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.358057Z","iopub.execute_input":"2023-02-15T08:27:17.358423Z","iopub.status.idle":"2023-02-15T08:27:17.366743Z","shell.execute_reply.started":"2023-02-15T08:27:17.358366Z","shell.execute_reply":"2023-02-15T08:27:17.365832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.evaluate(val_gen)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.368146Z","iopub.execute_input":"2023-02-15T08:27:17.368532Z","iopub.status.idle":"2023-02-15T08:27:17.379592Z","shell.execute_reply.started":"2023-02-15T08:27:17.368493Z","shell.execute_reply":"2023-02-15T08:27:17.378556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#val_preds = model.predict_generator(val_gen, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.382754Z","iopub.execute_input":"2023-02-15T08:27:17.383562Z","iopub.status.idle":"2023-02-15T08:27:17.388823Z","shell.execute_reply.started":"2023-02-15T08:27:17.383498Z","shell.execute_reply":"2023-02-15T08:27:17.387948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#val_preds","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.390313Z","iopub.execute_input":"2023-02-15T08:27:17.390729Z","iopub.status.idle":"2023-02-15T08:27:17.398069Z","shell.execute_reply.started":"2023-02-15T08:27:17.390672Z","shell.execute_reply":"2023-02-15T08:27:17.397111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y_preds = []\n#for i in range(len(val_preds)):\n#    y_preds.append(0)\n#   for value in val_preds[i]: \n #       if value > 0.5: \n  #          y_preds[i] = 1\n   #         break\n            \n        \n#len(y_preds)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.401218Z","iopub.execute_input":"2023-02-15T08:27:17.401581Z","iopub.status.idle":"2023-02-15T08:27:17.416491Z","shell.execute_reply.started":"2023-02-15T08:27:17.401541Z","shell.execute_reply":"2023-02-15T08:27:17.415204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.metrics import roc_curve\n#fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_true, y_preds)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.418189Z","iopub.execute_input":"2023-02-15T08:27:17.418581Z","iopub.status.idle":"2023-02-15T08:27:17.424587Z","shell.execute_reply.started":"2023-02-15T08:27:17.418543Z","shell.execute_reply":"2023-02-15T08:27:17.423658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.metrics import auc\n#auc_keras = auc(fpr_keras, tpr_keras)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.426241Z","iopub.execute_input":"2023-02-15T08:27:17.426703Z","iopub.status.idle":"2023-02-15T08:27:17.433052Z","shell.execute_reply.started":"2023-02-15T08:27:17.426662Z","shell.execute_reply":"2023-02-15T08:27:17.432009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.figure(1)\n#plt.plot([0, 1], [0, 1], 'k--')\n#plt.plot(fpr_keras, tpr_keras, label='Keras (area = {:.3f})'.format(auc_keras))\n\n#plt.xlabel('False positive rate')\n#plt.ylabel('True positive rate')\n#plt.title('ROC curve')\n#plt.legend(loc='best')\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.434874Z","iopub.execute_input":"2023-02-15T08:27:17.43526Z","iopub.status.idle":"2023-02-15T08:27:17.44108Z","shell.execute_reply.started":"2023-02-15T08:27:17.43522Z","shell.execute_reply":"2023-02-15T08:27:17.440132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.metrics import confusion_matrix\n#print('2*2 Confusion Matrix')\n#print(confusion_matrix(y_true, y_preds))\n#cm = confusion_matrix(y_true, y_preds)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.442807Z","iopub.execute_input":"2023-02-15T08:27:17.443313Z","iopub.status.idle":"2023-02-15T08:27:17.4496Z","shell.execute_reply.started":"2023-02-15T08:27:17.443268Z","shell.execute_reply":"2023-02-15T08:27:17.448673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import itertools   \n#def plot_confusion_matrix(cm, classes,\n #                       normalize=False,\n  #                      title='Confusion matrix',\n   #                     cmap=plt.cm.Blues):\n    \n   # This function prints and plots the confusion matrix.\n   # Normalization can be applied by setting `normalize=True`.\n    \n   # plt.imshow(cm, interpolation='nearest', cmap=cmap)\n   # plt.title(title)\n   # plt.colorbar()\n   # tick_marks = np.arange(len(classes))\n   # plt.xticks(tick_marks, classes, rotation=45)\n   # plt.yticks(tick_marks, classes)\n\n#    if normalize:\n#        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n#        print(\"Normalized confusion matrix\")\n#    else:\n #       print('Confusion matrix, without normalization')\n\n  #  print(cm) \n\n#    thresh = cm.max() / 2.\n #   for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n  #      plt.text(j, i, cm[i, j],\n   #         horizontalalignment=\"center\",\n    #        color=\"white\" if cm[i, j] > thresh else \"black\")\n\n   # plt.tight_layout()\n   # plt.ylabel('True label')\n   # plt.xlabel('Predicted label')","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.451322Z","iopub.execute_input":"2023-02-15T08:27:17.451735Z","iopub.status.idle":"2023-02-15T08:27:17.458437Z","shell.execute_reply.started":"2023-02-15T08:27:17.451694Z","shell.execute_reply":"2023-02-15T08:27:17.457477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cm_labels = ['no hemorrhage', 'has hemorrhage']","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.460304Z","iopub.execute_input":"2023-02-15T08:27:17.460776Z","iopub.status.idle":"2023-02-15T08:27:17.469512Z","shell.execute_reply.started":"2023-02-15T08:27:17.460672Z","shell.execute_reply":"2023-02-15T08:27:17.468467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot_confusion_matrix(cm=cm, classes=cm_labels, title='Confusion Matrix')","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.471308Z","iopub.execute_input":"2023-02-15T08:27:17.471727Z","iopub.status.idle":"2023-02-15T08:27:17.478424Z","shell.execute_reply.started":"2023-02-15T08:27:17.471686Z","shell.execute_reply":"2023-02-15T08:27:17.477456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####Here we are checking the training set batch and its output\n##################################################################\n#for data_batch,label_batch in train_gen:\n#    print(\"DATA SHAPE: \",data_batch.shape)\n#    print(\"LABEL SHAPE: \",label_batch.shape)\n#    break","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.480138Z","iopub.execute_input":"2023-02-15T08:27:17.480546Z","iopub.status.idle":"2023-02-15T08:27:17.486939Z","shell.execute_reply.started":"2023-02-15T08:27:17.480506Z","shell.execute_reply":"2023-02-15T08:27:17.486235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#############################################################################\n####Here we are checking the validation or testing set batch and its output\n#############################################################################\n#for data_batch,label_batch in val_gen:\n #   print(\"DATA SHAPE: \",data_batch.shape)\n  #  print(\"LABEL SHAPE: \",label_batch.shape)\n   # break","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.488475Z","iopub.execute_input":"2023-02-15T08:27:17.489044Z","iopub.status.idle":"2023-02-15T08:27:17.495448Z","shell.execute_reply.started":"2023-02-15T08:27:17.489001Z","shell.execute_reply":"2023-02-15T08:27:17.494536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import xception","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.496933Z","iopub.execute_input":"2023-02-15T08:27:17.497333Z","iopub.status.idle":"2023-02-15T08:27:17.505244Z","shell.execute_reply.started":"2023-02-15T08:27:17.497302Z","shell.execute_reply":"2023-02-15T08:27:17.504505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#############################################################################\n####Here we are setting the branch 1 of the double cnn-rf model\n#############################################################################\nfrom tensorflow.keras.models import Model\n#from tensorflow.keras.applications import mobilenet_v2\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\n#tf.keras.applications.MobileNetV2()\n#branch1 = mobilenet_v2.MobileNetV2()\n#tf.keras.applications.Xception()\nbranch1 = xception.Xception()\n#branch1 = resnet50.ResNet50()\nbranch1.layers.pop()\nfor layer in branch1.layers:\n  layer.trainable=False\nlast = branch1.layers[-2].output####################### getting last layer of pooling and it give 2046 features#######\nx = Dense(6, activation=\"softmax\")(last)#########last layer for classification for experiment no 01#########\nfinetuned_model_1= Model(branch1.input, x, name=\"Branch 1\")\nfinetuned_model_1.summary()\nfinetuned_model_1.compile(optimizer=Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy','mse', 'mae', 'mape', 'cosine'])\n\n\ncheckpoint = ModelCheckpoint(\n    'model.h5', \n    monitor='val_loss', \n    verbose=0, \n    save_best_only=True, \n    save_weights_only=False,\n    mode='auto'\n)\n'''\nResnet_Model1 = finetuned_model_1.fit_generator(\n    train_gen,\n    steps_per_epoch=200,\n    validation_data=val_gen,\n    validation_steps=100,\n    callbacks=[checkpoint],\n    epochs=30\n)\nPrediction_branch1 = finetuned_model_1.predict(test_gen)\n'''\n\n\n##############################################################################\n###########################################################################\n## if you want to perform experiment no 1, which is based on the output of bracnch 1. \n##Simply if you want to perform classisfication through branch 1, so please remove the comments. Experiment #1 described in Section 3.\n###########################################################################\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:17.506975Z","iopub.execute_input":"2023-02-15T08:27:17.507677Z","iopub.status.idle":"2023-02-15T08:27:19.894345Z","shell.execute_reply.started":"2023-02-15T08:27:17.507637Z","shell.execute_reply":"2023-02-15T08:27:19.893399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#############################################################################\n####Here we are setting the branch 2 of the double cnn-rf model\n#############################################################################\nfrom tensorflow.keras.models import Model\n#from tensorflow.keras.applications import mobilenet_v2\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\n#tf.keras.applications.MobileNetV2()\n#branch2 = mobilenet_v2.MobileNetV2()\n#branch2 = resnet50.ResNet50()\n#branch2 = resnet50.ResNet50()\ntf.keras.applications.Xception()\nbranch2 = xception.Xception()\nbranch2.layers.pop()\nfor layer in branch2.layers:\n  layer.trainable=False\nlast = branch2.layers[-2].output####################### getting last layer of pooling and it give 2046 features#######\nx = Dense(6, activation=\"softmax\")(last)#########last layer for classification for experiment no 01#########\nfinetuned_model_2= Model(branch2.input, x, name=\"Branch 2\")\nfinetuned_model_2.summary()\nfinetuned_model_2.compile(optimizer=Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy','mse', 'mae', 'mape', 'cosine'])\n\n\ncheckpoint = ModelCheckpoint(\n    'model.h5', \n    monitor='val_loss', \n    verbose=0, \n    save_best_only=True, \n    save_weights_only=False,\n    mode='auto'\n)\n'''\nResnet_Model2 = finetuned_model_2.fit_generator(\n    train_gen,\n    steps_per_epoch=300,\n    validation_data=val_gen,\n    validation_steps=200,\n    callbacks=[checkpoint],\n    epochs=60\n)\nPrediction_branch2 = finetuned_model_2.predict(test_gen)\n'''\n\n\n##############################################################################\n###########################################################################\n## if you want to perform experiment no 1, which is based on the output of bracnch 1. \n##Simply if you want to perform classisfication through branch 1, so please remove the comments. Experiment #1 described in Section 3.\n###########################################################################\n\n###################\n########parameters, including the number of epochs (60),were chosen experimentally.\n########## The training was terminated automatically if the validation loss did notdecrease for ten epochs (patience 10).#########\n############as mentioned in paper#########\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:19.895699Z","iopub.execute_input":"2023-02-15T08:27:19.896065Z","iopub.status.idle":"2023-02-15T08:27:22.429849Z","shell.execute_reply.started":"2023-02-15T08:27:19.896027Z","shell.execute_reply":"2023-02-15T08:27:22.42841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import concatenate\n###########################\n###########################we have taken the features extracted by either \n########branch after the average pooling layer as mentioned in paper###########################\n'''\nAs mentioned in the paper, after the training, features from the last block preceding the ResNet-50’s fully connected layer\nwere taken from either branch and concatenated. The joint feature vector containing 4096 elements\nwas subjected to the classification process. As we can check that in output, we are getting right same features 4096.\n'''\nprint('Getting training features and concatenation---------------start')\nmodel1= Model(branch1.input, branch1.layers[-2].output)\nmodel1_features=model1.predict(train_gen)\nmodel1_features=pd.DataFrame(model1_features)\nprint(\"Branch1_features\", model1_features.shape)\n\nmodel2= Model(branch2.input, branch2.layers[-2].output)\nmodel2_features=model2.predict(train_gen)\nmodel2_features=pd.DataFrame(model2_features)\nprint(\"Branch2_features\", model2_features.shape)\n\n####################################Concatenation of features###########################\nconcatenated_features=pd.concat([model1_features,model2_features], axis=1)\nprint(\"Combined_features\", concatenated_features.shape)\n\nprint('Getting validation features and concatenation---------------start')\nmodel1= Model(branch1.input, branch1.layers[-2].output)\nmodel1_val_features=model1.predict(val_gen)\nmodel1_val_features=pd.DataFrame(model1_val_features)\nprint(\"Branch1_val_features\", model1_val_features.shape)\n\nmodel2= Model(branch2.input, branch2.layers[-2].output)\nmodel2_val_features=model2.predict(val_gen)\nmodel2_val_features=pd.DataFrame(model2_val_features)\nprint(\"Branch2_val_features\", model2_val_features.shape)\n\n####################################Concatenation of features###########################\nconcatenated_val_features=pd.concat([model1_val_features,model2_val_features], axis=1)\nprint(\"Combined_val_features\", concatenated_val_features.shape)\n#############################################################################\n####Here, we have obtained the fatures from branch1 and branch 2 \n#############################################################################","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:27:22.431487Z","iopub.execute_input":"2023-02-15T08:27:22.432023Z","iopub.status.idle":"2023-02-15T08:44:11.714974Z","shell.execute_reply.started":"2023-02-15T08:27:22.431983Z","shell.execute_reply":"2023-02-15T08:44:11.712815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xception_model = tf.keras.applications.Xception(weights='imagenet', include_top=False, input_shape=(299, 299, 3))","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:44:11.720015Z","iopub.execute_input":"2023-02-15T08:44:11.720294Z","iopub.status.idle":"2023-02-15T08:44:13.786718Z","shell.execute_reply.started":"2023-02-15T08:44:11.720263Z","shell.execute_reply":"2023-02-15T08:44:13.785759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for layer in xception_model.layers[:-2]:\n#    layer.trainable = False\n\n#x = xception_model.output\n#x = layers.GlobalAveragePooling2D()(x)\n#x = layers.Flatten()(x)\n#x = layers.Dense(units=512, activation='relu')(x)\n#x = layers.Dropout(0.3)(x)\n#x = layers.Dense(units=512, activation='relu')(x)\n#x = layers.Dropout(0.3)(x)\n#output  = x\n#model_xception = Model(xception_model.input, output)\n\n\n#model_xception.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:44:13.788138Z","iopub.execute_input":"2023-02-15T08:44:13.788493Z","iopub.status.idle":"2023-02-15T08:44:13.793066Z","shell.execute_reply.started":"2023-02-15T08:44:13.788452Z","shell.execute_reply":"2023-02-15T08:44:13.792094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#############################################################################\n####Setting labels for combined training and testing data\n#############################################################################\n####################################Training Labels of features###########################\nfeature_train_labels=pd.DataFrame(train_gen.labels)\nprint(feature_train_labels)\n\n####################################Test Labels###########################\nfeature_test_labels=pd.DataFrame(val_gen.labels)\nlen(feature_test_labels.shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:44:13.794923Z","iopub.execute_input":"2023-02-15T08:44:13.795792Z","iopub.status.idle":"2023-02-15T08:44:13.82016Z","shell.execute_reply.started":"2023-02-15T08:44:13.795753Z","shell.execute_reply":"2023-02-15T08:44:13.81933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Flatten(input_shape=concatenated_features.shape[1:]))\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Flatten())\nmodel.add(Dense(units=256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(6, activation='softmax'))\n\nmodel.compile(optimizer='rmsprop',\n              loss='binary_crossentropy',\n              metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:44:13.821564Z","iopub.execute_input":"2023-02-15T08:44:13.82191Z","iopub.status.idle":"2023-02-15T08:44:13.889545Z","shell.execute_reply.started":"2023-02-15T08:44:13.821873Z","shell.execute_reply":"2023-02-15T08:44:13.888745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LR = 0.00005","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:44:13.896466Z","iopub.execute_input":"2023-02-15T08:44:13.896728Z","iopub.status.idle":"2023-02-15T08:44:13.900292Z","shell.execute_reply.started":"2023-02-15T08:44:13.8967Z","shell.execute_reply":"2023-02-15T08:44:13.899425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = Adam(learning_rate = LR), \n              loss = 'binary_crossentropy', # <- requires balance/ Binary for unbalanced\n              metrics = METRICS)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:44:13.902481Z","iopub.execute_input":"2023-02-15T08:44:13.903083Z","iopub.status.idle":"2023-02-15T08:44:13.919284Z","shell.execute_reply.started":"2023-02-15T08:44:13.903045Z","shell.execute_reply":"2023-02-15T08:44:13.918455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(concatenated_features, feature_train_labels,\n          epochs=30,\n          batch_size=BATCH_SIZE,\nvalidation_data=(concatenated_val_features, feature_test_labels))\n#model.save_weights('bottleneck_fc_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-02-15T08:44:13.920658Z","iopub.execute_input":"2023-02-15T08:44:13.92101Z","iopub.status.idle":"2023-02-15T09:03:40.72011Z","shell.execute_reply.started":"2023-02-15T08:44:13.920973Z","shell.execute_reply":"2023-02-15T09:03:40.719255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['binary_accuracy']\nval_acc = history.history['val_binary_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(acc) + 1)\n\nplt.plot(epochs, acc, 'b', label='Training Sens')\nplt.plot(epochs, val_acc, 'g', label='Validation Sens')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\n\nplt.title('Training and validation accuracy')\nplt.legend()\nfig = plt.figure()\nfig.savefig('acc.png')\n\n\nplt.plot(epochs, loss, 'b', label='Training loss')\nplt.plot(epochs, val_loss, 'g', label='Validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('Training and validation loss')\n\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:40.722893Z","iopub.execute_input":"2023-02-15T09:03:40.723235Z","iopub.status.idle":"2023-02-15T09:03:41.069017Z","shell.execute_reply.started":"2023-02-15T09:03:40.7232Z","shell.execute_reply":"2023-02-15T09:03:41.068207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(concatenated_val_features)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:41.070365Z","iopub.execute_input":"2023-02-15T09:03:41.070921Z","iopub.status.idle":"2023-02-15T09:03:42.757043Z","shell.execute_reply.started":"2023-02-15T09:03:41.070881Z","shell.execute_reply":"2023-02-15T09:03:42.756249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_preds = model.predict_generator(concatenated_val_features, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:42.758196Z","iopub.execute_input":"2023-02-15T09:03:42.758544Z","iopub.status.idle":"2023-02-15T09:03:43.825687Z","shell.execute_reply.started":"2023-02-15T09:03:42.758515Z","shell.execute_reply":"2023-02-15T09:03:43.824892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#val_preds","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:43.827081Z","iopub.execute_input":"2023-02-15T09:03:43.827436Z","iopub.status.idle":"2023-02-15T09:03:43.833066Z","shell.execute_reply.started":"2023-02-15T09:03:43.827372Z","shell.execute_reply":"2023-02-15T09:03:43.83224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_preds = []\nfor i in range(len(val_preds)):\n    y_preds.append(0)\n    for value in val_preds[i]: \n        if value > 0.5: \n            y_preds[i] = 1\n            break\n            \n        \nlen(y_preds)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:43.834337Z","iopub.execute_input":"2023-02-15T09:03:43.834856Z","iopub.status.idle":"2023-02-15T09:03:43.956306Z","shell.execute_reply.started":"2023-02-15T09:03:43.834818Z","shell.execute_reply":"2023-02-15T09:03:43.955267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_curve\nfpr_keras, tpr_keras, thresholds_keras = roc_curve(y_true, y_preds)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:43.957532Z","iopub.execute_input":"2023-02-15T09:03:43.957858Z","iopub.status.idle":"2023-02-15T09:03:43.969014Z","shell.execute_reply.started":"2023-02-15T09:03:43.95782Z","shell.execute_reply":"2023-02-15T09:03:43.968013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import auc\nauc_keras = auc(fpr_keras, tpr_keras)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:43.970491Z","iopub.execute_input":"2023-02-15T09:03:43.970837Z","iopub.status.idle":"2023-02-15T09:03:43.975624Z","shell.execute_reply.started":"2023-02-15T09:03:43.9708Z","shell.execute_reply":"2023-02-15T09:03:43.974658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(1)\nplt.plot([0, 1], [0, 1], 'k--')\nplt.plot(fpr_keras, tpr_keras, label='Keras (area = {:.3f})'.format(auc_keras))\n\nplt.xlabel('False positive rate')\nplt.ylabel('True positive rate')\nplt.title('ROC curve')\nplt.legend(loc='best')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:43.976887Z","iopub.execute_input":"2023-02-15T09:03:43.977284Z","iopub.status.idle":"2023-02-15T09:03:44.154863Z","shell.execute_reply.started":"2023-02-15T09:03:43.977247Z","shell.execute_reply":"2023-02-15T09:03:44.154014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nprint('2*2 Confusion Matrix')\nprint(confusion_matrix(y_true, y_preds))\ncm = confusion_matrix(y_true, y_preds)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.156125Z","iopub.execute_input":"2023-02-15T09:03:44.15664Z","iopub.status.idle":"2023-02-15T09:03:44.196703Z","shell.execute_reply.started":"2023-02-15T09:03:44.156599Z","shell.execute_reply":"2023-02-15T09:03:44.19582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import itertools   \ndef plot_confusion_matrix(cm, classes,\n                        normalize=False,\n                        title='Confusion matrix',\n                        cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    print(cm)\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n            horizontalalignment=\"center\",\n            color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.198026Z","iopub.execute_input":"2023-02-15T09:03:44.198367Z","iopub.status.idle":"2023-02-15T09:03:44.206081Z","shell.execute_reply.started":"2023-02-15T09:03:44.198329Z","shell.execute_reply":"2023-02-15T09:03:44.205226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm_labels = ['no hemorrhage', 'has hemorrhage']","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.207297Z","iopub.execute_input":"2023-02-15T09:03:44.207924Z","iopub.status.idle":"2023-02-15T09:03:44.217574Z","shell.execute_reply.started":"2023-02-15T09:03:44.207871Z","shell.execute_reply":"2023-02-15T09:03:44.216767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(cm=cm, classes=cm_labels, title='Confusion Matrix')","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.218935Z","iopub.execute_input":"2023-02-15T09:03:44.21927Z","iopub.status.idle":"2023-02-15T09:03:44.495192Z","shell.execute_reply.started":"2023-02-15T09:03:44.219233Z","shell.execute_reply":"2023-02-15T09:03:44.494442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import necessary libraries\n\n#import tensorflow as tf\n#from tensorflow.keras.layers import Input,Dense,Conv2D,Add\n#from tensorflow.keras.layers import SeparableConv2D,ReLU\n#from tensorflow.keras.layers import BatchNormalization,MaxPool2D\n#from tensorflow.keras.layers import GlobalAvgPool2D\n#from tensorflow.keras import Model\n# creating the Conv-Batch Norm block\n\n#def conv_bn(x, filters, kernel_size, strides=1):\n    \n#    x = Conv2D(filters=filters, \n #              kernel_size = kernel_size, \n  #             strides=strides, \n   #            padding = 'same', \n   #            use_bias = False)(x)\n   # x = BatchNormalization()(x)\n#return x\n# creating separableConv-Batch Norm block\n\n#def sep_bn(x, filters, kernel_size, strides=1):\n    \n #   x = SeparableConv2D(filters=filters, \n  #                      kernel_size = kernel_size, \n   #                     strides=strides, \n    #                    padding = 'same', \n     #                   use_bias = False)(x)\n    #x = BatchNormalization()(x)\n#return x\n# entry flow\n\n#def entry_flow(x):\n    \n #   x = conv_bn(x, filters =32, kernel_size =3, strides=2)\n  #  x = ReLU()(x)\n   # x = conv_bn(x, filters =64, kernel_size =3, strides=1)\n   # tensor = ReLU()(x)\n    \n   # x = sep_bn(tensor, filters = 128, kernel_size =3)\n   # x = ReLU()(x)\n   # x = sep_bn(x, filters = 128, kernel_size =3)\n   # x = MaxPool2D(pool_size=3, strides=2, padding = 'same')(x)\n    \n   # tensor = conv_bn(tensor, filters=128, kernel_size = 1,strides=2)\n   # x = Add()([tensor,x])\n    \n   # x = ReLU()(x)\n   # x = sep_bn(x, filters =256, kernel_size=3)\n   # x = ReLU()(x)\n   # x = sep_bn(x, filters =256, kernel_size=3)\n   # x = MaxPool2D(pool_size=3, strides=2, padding = 'same')(x)\n    \n   # tensor = conv_bn(tensor, filters=256, kernel_size = 1,strides=2)\n   # x = Add()([tensor,x])\n    \n   # x = ReLU()(x)\n   # x = sep_bn(x, filters =728, kernel_size=3)\n   # x = ReLU()(x)\n   # x = sep_bn(x, filters =728, kernel_size=3)\n   # x = MaxPool2D(pool_size=3, strides=2, padding = 'same')(x)\n    \n   # tensor = conv_bn(tensor, filters=728, kernel_size = 1,strides=2)\n   # x = Add()([tensor,x])\n#return x\n# middle flow\n\n#def middle_flow(tensor):\n    \n #   for _ in range(8):\n  #      x = ReLU()(tensor)\n   #     x = sep_bn(x, filters = 728, kernel_size = 3)\n    #    x = ReLU()(x)\n     #   x = sep_bn(x, filters = 728, kernel_size = 3)\n      #  x = ReLU()(x)\n       # x = sep_bn(x, filters = 728, kernel_size = 3)\n       # x = ReLU()(x)\n       # tensor = Add()([tensor,x])\n        \n    #return tensor\n# exit flow\n\n#def exit_flow(tensor):\n #   \n  #  x = ReLU()(tensor)\n   # x = sep_bn(x, filters = 728,  kernel_size=3)\n   # x = ReLU()(x)\n   # x = sep_bn(x, filters = 1024,  kernel_size=3)\n   #x = MaxPool2D(pool_size = 3, strides = 2, padding ='same')(x)\n    \n   # tensor = conv_bn(tensor, filters =1024, kernel_size=1, strides =2)\n   # x = Add()([tensor,x])\n   # \n   # x = sep_bn(x, filters = 1536,  kernel_size=3)\n   # x = ReLU()(x)\n   # x = sep_bn(x, filters = 2048,  kernel_size=3)\n   # x = GlobalAvgPool2D()(x)\n    \n   # x = Dense (units = 1000, activation = 'softmax')(x)\n    \n   # return x\n# model code\n\n#input = Input(shape = (299,299,3))\n#x = entry_flow(input)\n#x = middle_flow(x)\n#output = exit_flow(x)\n\n#model = Model (inputs=input, outputs=output)\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.496472Z","iopub.execute_input":"2023-02-15T09:03:44.496922Z","iopub.status.idle":"2023-02-15T09:03:44.502616Z","shell.execute_reply.started":"2023-02-15T09:03:44.496885Z","shell.execute_reply":"2023-02-15T09:03:44.501721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model1.layers","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.503961Z","iopub.execute_input":"2023-02-15T09:03:44.504523Z","iopub.status.idle":"2023-02-15T09:03:44.513635Z","shell.execute_reply.started":"2023-02-15T09:03:44.504487Z","shell.execute_reply":"2023-02-15T09:03:44.512844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hidden1 = model1.layers[1]","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.514932Z","iopub.execute_input":"2023-02-15T09:03:44.515563Z","iopub.status.idle":"2023-02-15T09:03:44.52172Z","shell.execute_reply.started":"2023-02-15T09:03:44.515528Z","shell.execute_reply":"2023-02-15T09:03:44.520943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hidden1.name","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.52293Z","iopub.execute_input":"2023-02-15T09:03:44.523454Z","iopub.status.idle":"2023-02-15T09:03:44.530162Z","shell.execute_reply.started":"2023-02-15T09:03:44.523419Z","shell.execute_reply":"2023-02-15T09:03:44.529438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model1.get_layer('dense_18') is hidden1","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.53135Z","iopub.execute_input":"2023-02-15T09:03:44.531886Z","iopub.status.idle":"2023-02-15T09:03:44.538525Z","shell.execute_reply.started":"2023-02-15T09:03:44.531853Z","shell.execute_reply":"2023-02-15T09:03:44.537684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#weights, biases = hidden1.get_weights()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.539873Z","iopub.execute_input":"2023-02-15T09:03:44.540534Z","iopub.status.idle":"2023-02-15T09:03:44.546625Z","shell.execute_reply.started":"2023-02-15T09:03:44.540499Z","shell.execute_reply":"2023-02-15T09:03:44.545785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#weights","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.54789Z","iopub.execute_input":"2023-02-15T09:03:44.548595Z","iopub.status.idle":"2023-02-15T09:03:44.554681Z","shell.execute_reply.started":"2023-02-15T09:03:44.54841Z","shell.execute_reply":"2023-02-15T09:03:44.553828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#weights.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.556971Z","iopub.execute_input":"2023-02-15T09:03:44.557349Z","iopub.status.idle":"2023-02-15T09:03:44.563803Z","shell.execute_reply.started":"2023-02-15T09:03:44.557313Z","shell.execute_reply":"2023-02-15T09:03:44.562735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"################################We used the RF classifier with same hyperparamters##################\n###########################as mentioned in the paper#################################\n###################################################################################################\nfrom sklearn.neighbors import KNeighborsClassifier\nknn = KNeighborsClassifier(n_neighbors=7)\n#from sklearn.ensemble import RandomForestClassifier\n#knn=LinearSVC(multi_class='crammer_singer')\nknn.fit(concatenated_features,feature_train_labels)\nknn.fit(concatenated_val_features,feature_test_labels)\nprint(\"done tarining\")","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:44.565016Z","iopub.execute_input":"2023-02-15T09:03:44.56647Z","iopub.status.idle":"2023-02-15T09:03:45.260938Z","shell.execute_reply.started":"2023-02-15T09:03:44.566431Z","shell.execute_reply":"2023-02-15T09:03:45.260062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_knn= knn.predict(concatenated_val_features)\nprint(\"pred_KNN\",pred_knn.shape)\n####################################################################################","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:03:45.262161Z","iopub.execute_input":"2023-02-15T09:03:45.262661Z","iopub.status.idle":"2023-02-15T09:04:25.093411Z","shell.execute_reply.started":"2023-02-15T09:03:45.262622Z","shell.execute_reply":"2023-02-15T09:04:25.092436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####################################################################################\n######Performance evaluation of the combined double-branch convolutional neural network (CNN) \n############################based on the ResNet-50 architecture with Random forest#######################\n########################################################################################\nfrom sklearn import metrics\naccuracy=metrics.accuracy_score(feature_test_labels,pred_knn.round())\nprint(\"Accuracy of combined model with KNN: {0:0.4f}\".format(accuracy*100))\n\nfrom sklearn.metrics import f1_score\nf1score=f1_score(pred_knn,feature_test_labels, average='weighted')\nprint(\"F1score of combined model with KNN: {0:0.4f}\".format( f1score*100))\n\nfrom sklearn.metrics import recall_score\nrecall = recall_score(feature_test_labels,pred_knn, average='weighted')\nprint('Recall score of combined model with KNN: {0:0.4f}'.format(recall*100))\n\nfrom sklearn.metrics import precision_score\nprecision = precision_score(pred_knn.round(),feature_test_labels,average='weighted')\nprint('Precision of combined model with KNN: {0:0.4f}'.format(precision*100))\n\n####################################################################################\n####################################################################################","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:04:25.094579Z","iopub.execute_input":"2023-02-15T09:04:25.094913Z","iopub.status.idle":"2023-02-15T09:04:25.137436Z","shell.execute_reply.started":"2023-02-15T09:04:25.094884Z","shell.execute_reply":"2023-02-15T09:04:25.136576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import f1_score\nf1score=f1_score(pred_knn,feature_test_labels, average='macro')\nprint(\"F1score of combined model with KNN: {0:0.4f}\".format( f1score*100))","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:04:25.138783Z","iopub.execute_input":"2023-02-15T09:04:25.13913Z","iopub.status.idle":"2023-02-15T09:04:25.153906Z","shell.execute_reply.started":"2023-02-15T09:04:25.139093Z","shell.execute_reply":"2023-02-15T09:04:25.15308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####################################################################################\n######Performance evaluation of the combined double-branch convolutional neural network (CNN) \n############################based on the ResNet-50 architecture with Random forest#######################\n########################################################################################\n\nfrom sklearn.metrics import multilabel_confusion_matrix\ncm1 = multilabel_confusion_matrix(feature_test_labels,pred_knn)\nprint(cm1)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:04:25.155108Z","iopub.execute_input":"2023-02-15T09:04:25.155464Z","iopub.status.idle":"2023-02-15T09:04:25.16668Z","shell.execute_reply.started":"2023-02-15T09:04:25.155428Z","shell.execute_reply":"2023-02-15T09:04:25.16573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####################################################################################\n######Performance evaluation of the combined double-branch convolutional neural network (CNN) \n############################based on the ResNet-50 architecture with Random forest#######################\n########################################################################################\n\nfirst_cat=cm1[0]\ntn, fp, fn, tp = first_cat.ravel()\nfpr = fp / (tn + fp)\nfnr = fn / (tp + fn)\nTPR = tp/(tp+fn)\nTNR = tn/(tn+fp)\nacc_1=(tp+tn)/(tp+tn+fp+fn)\nprint(\"fpr for any is :\",fpr*100)\nprint(\"fnr for any is :  \",fnr*100)\nprint(\"TPR for any is : \",TPR*100)\nprint(\"TNR for any is : \",TNR*100)\nprint(\"Accuracy for any is \",acc_1*100)\n\nsecond_cat=cm1[1]\ntn, fp, fn, tp = second_cat.ravel()\nfpr = fp / (tn + fp)\nfnr = fn / (tp + fn)\nTPR = tp/(tp+fn)\nTNR = tn/(tn+fp)\nacc_2=(tp+tn)/(tp+tn+fp+fn)\nprint(\"fpr for epidural is :  \",fpr*100)\nprint(\"fnr for epidural is : \",fnr*100)\nprint(\"TPR for epidural is : \",TPR*100)\nprint(\"TNR for epidural is : \",TNR*100)\nprint(\"Accuracy for epidural is : \",acc_2*100)\n\nthird_cat=cm1[2]\ntn, fp, fn, tp = third_cat.ravel()\nfpr = fp / (tn + fp)\nfnr = fn / (tp + fn)\nTPR = tp/(tp+fn)\nTNR = tn/(tn+fp)\nacc_3=(tp+tn)/(tp+tn+fp+fn)\nprint(\"fpr for intraparenchymal is : \",fpr*100)\nprint(\"fnr for intraparenchymal is : \",fnr*100)\nprint(\"TPR for intraparenchymal is : \",TPR*100)\nprint(\"TNR for intraparenchymal is : \",TNR*100)\nprint(\"Accuracy for intraparenchymal is \",acc_3*100)\n\nfourth_cat=cm1[3]\ntn, fp, fn, tp = fourth_cat.ravel()\nfpr = fp / (tn + fp)\nfnr = fn / (tp + fn)\nTPR = tp/(tp+fn)\nTNR = tn/(tn+fp)\nacc_4=(tp+tn)/(tp+tn+fp+fn)\nprint(\"fpr for intraventricular is : \",fpr*100)\nprint(\"fnr for intraventricular is :  \",fnr*100)\nprint(\"TPR for intraventricular is : \",TPR*100)\nprint(\"TNR for intraventricular is : \",TNR*100)\nprint(\"Accuracy for intraventricular : is \",acc_4*100)\n\nfifth_cat=cm1[4]\ntn, fp, fn, tp = fourth_cat.ravel()\nfpr = fp / (tn + fp)\nfnr = fn / (tp + fn)\nTPR = tp/(tp+fn)\nTNR = tn/(tn+fp)\nacc_5=(tp+tn)/(tp+tn+fp+fn)\nprint(\"fpr for subarachnoid is  : \",fpr*100)\nprint(\"fnr for subarachnoid is :  \",fnr*100)\nprint(\"TPR for subarachnoid is : \",TPR*100)\nprint(\"TNR for subarachnoid is : \",TNR*100)\nprint(\"Accuracy for subarachnoid is :  \",acc_5*100)\n\nsixth_cat=cm1[5]\ntn, fp, fn, tp = sixth_cat.ravel()\nfpr = fp / (tn + fp)\nfnr = fn / (tp + fn)\nTPR = tp/(tp+fn)\nTNR = tn/(tn+fp)\nacc_6=(tp+tn)/(tp+tn+fp+fn)\nprint(\"fpr for subdural is : \",fpr*100)\nprint(\"fnr for subdural is : \",fnr*100)\nprint(\"TPR for subdural is : \",TPR*100)\nprint(\"TNR for subdural is :\",TNR*100)\nprint(\"Accuracy for Subdural is \",acc_6*100)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T09:04:25.168077Z","iopub.execute_input":"2023-02-15T09:04:25.168421Z","iopub.status.idle":"2023-02-15T09:04:25.195865Z","shell.execute_reply.started":"2023-02-15T09:04:25.168372Z","shell.execute_reply":"2023-02-15T09:04:25.195083Z"},"trusted":true},"execution_count":null,"outputs":[]}]}