{"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":"\n\nimport 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 tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import Callback, ModelCheckpoint, EarlyStopping\nfrom tensorflow.keras.initializers import Constant\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.python.ops import array_ops\nfrom tqdm import tqdm\nfrom tensorflow.keras import backend as K\nimport tensorflow as tf\n\n#import keras\nfrom tensorflow.keras.applications import Xception\nfrom tensorflow.keras.models import Model, load_model\nfrom math import ceil, floor\nfrom sklearn.model_selection import ShuffleSplit\nfrom sklearn.metrics import log_loss\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, GlobalAveragePooling2D\n","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:40:17.95487Z","iopub.execute_input":"2023-01-13T14:40:17.955289Z","iopub.status.idle":"2023-01-13T14:40:23.991572Z","shell.execute_reply.started":"2023-01-13T14:40:17.955201Z","shell.execute_reply":"2023-01-13T14:40:23.990582Z"},"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-01-13T14:40:23.993805Z","iopub.execute_input":"2023-01-13T14:40:23.994458Z","iopub.status.idle":"2023-01-13T14:40:24.007493Z","shell.execute_reply.started":"2023-01-13T14:40:23.99442Z","shell.execute_reply":"2023-01-13T14:40:24.006406Z"},"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-01-13T14:40:24.009215Z","iopub.execute_input":"2023-01-13T14:40:24.009921Z","iopub.status.idle":"2023-01-13T14:40:28.195282Z","shell.execute_reply.started":"2023-01-13T14:40:24.009882Z","shell.execute_reply":"2023-01-13T14:40:28.194168Z"},"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-01-13T14:40:28.19819Z","iopub.execute_input":"2023-01-13T14:40:28.198563Z","iopub.status.idle":"2023-01-13T14:40:34.500376Z","shell.execute_reply.started":"2023-01-13T14:40:28.198527Z","shell.execute_reply":"2023-01-13T14:40:34.499418Z"},"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-01-13T14:40:34.501871Z","iopub.execute_input":"2023-01-13T14:40:34.502498Z","iopub.status.idle":"2023-01-13T14:40:34.585602Z","shell.execute_reply.started":"2023-01-13T14:40:34.502453Z","shell.execute_reply":"2023-01-13T14:40:34.584595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subtypes = train_df.groupby('type').sum()\nsubtypes","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:40:34.587256Z","iopub.execute_input":"2023-01-13T14:40:34.587979Z","iopub.status.idle":"2023-01-13T14:40:35.829574Z","shell.execute_reply.started":"2023-01-13T14:40:34.587939Z","shell.execute_reply":"2023-01-13T14:40:35.828623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(2022)\nsample_files = np.random.choice(os.listdir(BASE_PATH + TRAIN_DIR), 200008) # 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-01-13T14:40:35.831149Z","iopub.execute_input":"2023-01-13T14:40:35.831535Z","iopub.status.idle":"2023-01-13T14:41:06.751027Z","shell.execute_reply.started":"2023-01-13T14:40:35.8315Z","shell.execute_reply":"2023-01-13T14:41:06.750047Z"},"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-01-13T14:41:06.752515Z","iopub.execute_input":"2023-01-13T14:41:06.753012Z","iopub.status.idle":"2023-01-13T14:41:08.247236Z","shell.execute_reply.started":"2023-01-13T14:41:06.752969Z","shell.execute_reply":"2023-01-13T14:41:08.246187Z"},"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-01-13T14:41:08.249158Z","iopub.execute_input":"2023-01-13T14:41:08.249548Z","iopub.status.idle":"2023-01-13T14:41:09.141755Z","shell.execute_reply.started":"2023-01-13T14:41:08.249514Z","shell.execute_reply":"2023-01-13T14:41:09.140629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(2022)\nsample_test = np.random.choice(os.listdir(BASE_PATH + TEST_DIR), 11000) \ntest_sample_df = test_df[test_df.filename.apply(lambda x: x.replace('.png', '.dcm')).isin(sample_test)]","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:41:09.146381Z","iopub.execute_input":"2023-01-13T14:41:09.146873Z","iopub.status.idle":"2023-01-13T14:41:10.427086Z","shell.execute_reply.started":"2023-01-13T14:41:09.146836Z","shell.execute_reply":"2023-01-13T14:41:10.426062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_sample_df","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:41:10.428635Z","iopub.execute_input":"2023-01-13T14:41:10.429272Z","iopub.status.idle":"2023-01-13T14:41:10.452007Z","shell.execute_reply.started":"2023-01-13T14:41:10.429225Z","shell.execute_reply":"2023-01-13T14:41:10.45095Z"},"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-01-13T14:41:10.453472Z","iopub.execute_input":"2023-01-13T14:41:10.454123Z","iopub.status.idle":"2023-01-13T14:41:10.478674Z","shell.execute_reply.started":"2023-01-13T14:41:10.454084Z","shell.execute_reply":"2023-01-13T14:41:10.477686Z"},"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])","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:41:10.479979Z","iopub.execute_input":"2023-01-13T14:41:10.480416Z","iopub.status.idle":"2023-01-13T14:41:11.151909Z","shell.execute_reply.started":"2023-01-13T14:41:10.48038Z","shell.execute_reply":"2023-01-13T14:41:11.150961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_true)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:41:11.153481Z","iopub.execute_input":"2023-01-13T14:41:11.153893Z","iopub.status.idle":"2023-01-13T14:41:11.161604Z","shell.execute_reply.started":"2023-01-13T14:41:11.153855Z","shell.execute_reply":"2023-01-13T14:41:11.160708Z"},"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])","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:41:11.163043Z","iopub.execute_input":"2023-01-13T14:41:11.164111Z","iopub.status.idle":"2023-01-13T14:41:15.435827Z","shell.execute_reply.started":"2023-01-13T14:41:11.164076Z","shell.execute_reply":"2023-01-13T14:41:15.434855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_df = pivot_df[~(pivot_df.filename.isin(validation_df.filename))]\ntraining_df","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:41:15.437399Z","iopub.execute_input":"2023-01-13T14:41:15.43781Z","iopub.status.idle":"2023-01-13T14:41:15.502172Z","shell.execute_reply.started":"2023-01-13T14:41:15.437772Z","shell.execute_reply":"2023-01-13T14:41:15.501065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(training_df.head())\nprint(validation_df.head())","metadata":{"execution":{"iopub.status.busy":"2023-01-13T14:41:15.50356Z","iopub.execute_input":"2023-01-13T14:41:15.504015Z","iopub.status.idle":"2023-01-13T14:41:15.513999Z","shell.execute_reply.started":"2023-01-13T14:41:15.503981Z","shell.execute_reply":"2023-01-13T14:41:15.513029Z"},"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-01-13T14:41:15.515237Z","iopub.execute_input":"2023-01-13T14:41:15.516048Z","iopub.status.idle":"2023-01-13T14:41:15.525528Z","shell.execute_reply.started":"2023-01-13T14:41:15.516012Z","shell.execute_reply":"2023-01-13T14:41:15.524538Z"},"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-01-13T14:41:15.526931Z","iopub.execute_input":"2023-01-13T14:41:15.527483Z","iopub.status.idle":"2023-01-13T14:41:15.537559Z","shell.execute_reply.started":"2023-01-13T14:41:15.527447Z","shell.execute_reply":"2023-01-13T14:41:15.536624Z"},"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-01-13T14:41:15.539145Z","iopub.execute_input":"2023-01-13T14:41:15.539556Z","iopub.status.idle":"2023-01-13T14:41:15.551176Z","shell.execute_reply.started":"2023-01-13T14:41:15.539521Z","shell.execute_reply":"2023-01-13T14:41:15.550253Z"},"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-01-13T14:41:15.554311Z","iopub.execute_input":"2023-01-13T14:41:15.554575Z","iopub.status.idle":"2023-01-13T16:18:00.249307Z","shell.execute_reply.started":"2023-01-13T14:41:15.554551Z","shell.execute_reply":"2023-01-13T16:18:00.247662Z"},"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-01-13T16:18:00.253823Z","iopub.execute_input":"2023-01-13T16:18:00.254106Z","iopub.status.idle":"2023-01-13T16:18:01.884428Z","shell.execute_reply.started":"2023-01-13T16:18:00.254079Z","shell.execute_reply":"2023-01-13T16:18:01.883388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##################################################################\n####Here we are checking the training set batch and its output\n##################################################################\nfor 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-01-13T16:18:01.885969Z","iopub.execute_input":"2023-01-13T16:18:01.886574Z","iopub.status.idle":"2023-01-13T16:18:02.023297Z","shell.execute_reply.started":"2023-01-13T16:18:01.886534Z","shell.execute_reply":"2023-01-13T16:18:02.02228Z"},"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#############################################################################\nfor 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-01-13T16:18:02.024863Z","iopub.execute_input":"2023-01-13T16:18:02.025469Z","iopub.status.idle":"2023-01-13T16:18:02.126661Z","shell.execute_reply.started":"2023-01-13T16:18:02.025431Z","shell.execute_reply":"2023-01-13T16:18:02.125674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import xception","metadata":{"execution":{"iopub.status.busy":"2023-01-13T16:18:02.128312Z","iopub.execute_input":"2023-01-13T16:18:02.128802Z","iopub.status.idle":"2023-01-13T16:18:02.134158Z","shell.execute_reply.started":"2023-01-13T16:18:02.12876Z","shell.execute_reply":"2023-01-13T16:18:02.133097Z"},"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()\ntf.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'''\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-01-13T16:18:02.13598Z","iopub.execute_input":"2023-01-13T16:18:02.136786Z","iopub.status.idle":"2023-01-13T16:18:08.67632Z","shell.execute_reply.started":"2023-01-13T16:18:02.136669Z","shell.execute_reply":"2023-01-13T16:18:08.675346Z"},"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-01-13T16:18:08.678098Z","iopub.execute_input":"2023-01-13T16:18:08.678766Z","iopub.status.idle":"2023-01-13T16:18:11.160904Z","shell.execute_reply.started":"2023-01-13T16:18:08.678715Z","shell.execute_reply":"2023-01-13T16:18:11.159809Z"},"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-01-13T16:18:11.166133Z","iopub.execute_input":"2023-01-13T16:18:11.166431Z","iopub.status.idle":"2023-01-13T16:49:04.876897Z","shell.execute_reply.started":"2023-01-13T16:18:11.166404Z","shell.execute_reply":"2023-01-13T16:49:04.874182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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)\n#len(feature_test_labels.shape)\nprint(feature_test_labels)","metadata":{"execution":{"iopub.status.busy":"2023-01-13T16:51:26.022455Z","iopub.execute_input":"2023-01-13T16:51:26.023045Z","iopub.status.idle":"2023-01-13T16:51:26.110633Z","shell.execute_reply.started":"2023-01-13T16:51:26.02293Z","shell.execute_reply":"2023-01-13T16:51:26.108961Z"},"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-01-13T16:49:04.905297Z","iopub.execute_input":"2023-01-13T16:49:04.905706Z"},"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":{"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":{"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":{"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)\npre_1=tp/(tp+fp)\n\nprint(\"Precision for any is \",pre_1*100)\n\nrec_1=tp/(tp+fn)\n\nprint(\"Recall for any is \",rec_1*100)\n\nprint(\"------------------------------------------------------------------------\")\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)\npre_2=tp/(tp+fp)\n\nprint(\"Precision for epidural is : \",pre_2*100)\nrec_2=tp/(tp+fn)\n\nprint(\"Recall for epidural is \",rec_2*100)\n\nprint(\"------------------------------------------------------------------------\")\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)\npre_3=tp/(tp+fp)\n\nprint(\"Precision for intraparenchymal is : \",pre_3*100)\nrec_3=tp/(tp+fn)\n\nprint(\"Recall for intraparenchymal is \",rec_3*100)\n\nprint(\"------------------------------------------------------------------------\")\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)\npre_4=tp/(tp+fp)\n\nprint(\"Precision for intraventricular is : \",pre_4*100)\nrec_4=tp/(tp+fn)\n\nprint(\"Recall for intraventricular is \",rec_4*100)\n\nprint(\"------------------------------------------------------------------------\")\n\nfifth_cat=cm1[4]\ntn, fp, fn, tp = fifth_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)\npre_5=tp/(tp+fp)\n\nprint(\"Precision for subarachnoid is : \",pre_5*100)\nrec_5=tp/(tp+fn)\n\nprint(\"Recall for subarachnoid is \",rec_5*100)\n\nprint(\"------------------------------------------------------------------------\")\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)\npre_6=tp/(tp+fp)\n\nprint(\"Precision for Subdural is : \",pre_6*100)\nrec_6=tp/(tp+fn)\n\nprint(\"Recall for Subdural is \",rec_6*100)\n\nprint(\"------------------------------------------------------------------------\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}