{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":13451,"datasetId":654585,"databundleVersionId":1188070},{"sourceType":"datasetVersion","sourceId":1149901,"datasetId":649431,"databundleVersionId":1180608},{"sourceType":"datasetVersion","sourceId":7618151,"datasetId":4437043,"databundleVersionId":7713843},{"sourceType":"datasetVersion","sourceId":7622452,"datasetId":4440090,"databundleVersionId":7718310},{"sourceType":"datasetVersion","sourceId":7526984,"datasetId":4384129,"databundleVersionId":7620724},{"sourceType":"datasetVersion","sourceId":7729234,"datasetId":4516219,"databundleVersionId":7829063},{"sourceType":"datasetVersion","sourceId":2170623,"datasetId":1303051,"databundleVersionId":2211890},{"sourceType":"datasetVersion","sourceId":7736888,"datasetId":4521703,"databundleVersionId":7837032}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install imutils\n!pip install efficientnet\n!pip install iterative-stratification\n!pip install albumentations==1.3.1 > /dev/null\n!pip install image-classifiers==1.0.0b1\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:32:16.307792Z","iopub.execute_input":"2024-03-01T13:32:16.308629Z","iopub.status.idle":"2024-03-01T13:33:27.575389Z","shell.execute_reply.started":"2024-03-01T13:32:16.308589Z","shell.execute_reply":"2024-03-01T13:33:27.57404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install albumentations ","metadata":{"execution":{"iopub.status.busy":"2024-02-29T16:16:59.338617Z","iopub.execute_input":"2024-02-29T16:16:59.339472Z","iopub.status.idle":"2024-02-29T16:16:59.343757Z","shell.execute_reply.started":"2024-02-29T16:16:59.339434Z","shell.execute_reply":"2024-02-29T16:16:59.342786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install streamlit\n!pip install streamlit-option-menu\n!pip install pillow\n!pip install numpy\n!pip install opencv-python","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:33:27.577943Z","iopub.execute_input":"2024-03-01T13:33:27.578817Z","iopub.status.idle":"2024-03-01T13:34:39.407937Z","shell.execute_reply.started":"2024-03-01T13:33:27.578776Z","shell.execute_reply":"2024-03-01T13:34:39.40648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport os\nimport gc\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport pydicom\nimport os\nimport random\nimport imutils\nimport matplotlib.pyplot as plt\nimport collections\nfrom tqdm import tqdm_notebook as tqdm\nfrom datetime import datetime\n\nfrom math import ceil, floor, log\nimport cv2\nfrom scipy import ndimage\nimport sys\nfrom keras.utils import Sequence\n\nimport tensorflow as tf\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Activation, Input, BatchNormalization, Add, GlobalAveragePooling2D,AveragePooling2D,GlobalMaxPooling2D,concatenate\nfrom tensorflow.keras.layers import Lambda, Reshape, DepthwiseConv2D, ZeroPadding2D, Add, MaxPooling2D,Activation, Flatten, Conv2D, Dense, Input, Dropout, Concatenate, GlobalMaxPooling2D, GlobalAveragePooling2D, BatchNormalization\n\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint,TensorBoard,TerminateOnNaN, LearningRateScheduler\nfrom tensorflow.keras.optimizers import Adam,RMSprop\nfrom tensorflow.keras.models import Model,load_model\n\nfrom sklearn.model_selection import ShuffleSplit\nfrom albumentations import (\n    Compose, HorizontalFlip, CLAHE, HueSaturationValue,\n    RandomBrightness, RandomContrast, RandomGamma,OneOf,\n    ToFloat, ShiftScaleRotate,GridDistortion, ElasticTransform, JpegCompression, HueSaturationValue, VerticalFlip,\n    RGBShift, RandomBrightness, RandomContrast, Blur, MotionBlur, MedianBlur, GaussNoise,CenterCrop,Normalize,\n    IAAAdditiveGaussianNoise,GaussNoise,OpticalDistortion,RandomSizedCrop,RandomCrop,RandomResizedCrop,RandomRotate90,Transpose\n)\n\nimport efficientnet.tfkeras as efn\nfrom iterstrat.ml_stratifiers import MultilabelStratifiedShuffleSplit, MultilabelStratifiedKFold\nfrom classification_models.tfkeras import Classifiers\n\nif 'checkpoint' not in os.listdir('./'):\n    os.mkdir('./checkpoint')\n\n\ninput_path = \"../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/\"\ntest_images_dir = input_path + 'stage_2_test/'\ntrain_images_dir = input_path + 'stage_2_train/'\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input d","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2024-03-01T13:34:39.411036Z","iopub.execute_input":"2024-03-01T13:34:39.411461Z","iopub.status.idle":"2024-03-01T13:34:39.427488Z","shell.execute_reply.started":"2024-03-01T13:34:39.411392Z","shell.execute_reply":"2024-03-01T13:34:39.426205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_path = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/'\ntest_images_path = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_test/'\ntrain_files = os.listdir(train_images_path)\ntest_files= os.listdir(test_images_path)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:34:39.428763Z","iopub.execute_input":"2024-03-01T13:34:39.429044Z","iopub.status.idle":"2024-03-01T13:34:40.691601Z","shell.execute_reply.started":"2024-03-01T13:34:39.429019Z","shell.execute_reply":"2024-03-01T13:34:40.690459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_files)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:34:40.694479Z","iopub.execute_input":"2024-03-01T13:34:40.694863Z","iopub.status.idle":"2024-03-01T13:34:40.702002Z","shell.execute_reply.started":"2024-03-01T13:34:40.694826Z","shell.execute_reply":"2024-03-01T13:34:40.700963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_files)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:34:40.703322Z","iopub.execute_input":"2024-03-01T13:34:40.703694Z","iopub.status.idle":"2024-03-01T13:34:40.715884Z","shell.execute_reply.started":"2024-03-01T13:34:40.703667Z","shell.execute_reply":"2024-03-01T13:34:40.71501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hem_df = pd.read_csv('../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train.csv')\nhem_df['sub_type'] = hem_df['ID'].str.split('_',expand = True)[2]\nhem_df['image'] = 'ID_' + hem_df['ID'].str.split('_',expand = True)[1] + '.dcm'\nhem_df = hem_df.pivot_table(index='image',columns=['sub_type'],  values='Label', aggfunc='first')\nhem_df.head(12)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:34:40.717282Z","iopub.execute_input":"2024-03-01T13:34:40.717954Z","iopub.status.idle":"2024-03-01T13:35:26.859793Z","shell.execute_reply.started":"2024-03-01T13:34:40.717921Z","shell.execute_reply":"2024-03-01T13:35:26.858712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hem_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:26.861437Z","iopub.execute_input":"2024-03-01T13:35:26.86181Z","iopub.status.idle":"2024-03-01T13:35:26.868715Z","shell.execute_reply.started":"2024-03-01T13:35:26.86178Z","shell.execute_reply":"2024-03-01T13:35:26.867483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hem=pd.read_csv('/kaggle/input/rsna-csv-files/RSNA_DATA/DATA.csv')","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:26.870162Z","iopub.execute_input":"2024-03-01T13:35:26.870512Z","iopub.status.idle":"2024-03-01T13:35:29.133076Z","shell.execute_reply.started":"2024-03-01T13:35:26.870485Z","shell.execute_reply":"2024-03-01T13:35:29.132197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hem.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:29.134704Z","iopub.execute_input":"2024-03-01T13:35:29.135116Z","iopub.status.idle":"2024-03-01T13:35:29.150324Z","shell.execute_reply.started":"2024-03-01T13:35:29.135081Z","shell.execute_reply":"2024-03-01T13:35:29.149324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df= pd.read_csv('../input/rsna-csv-files/RSNA_DATA/good_slices.csv',index_col = 'Unnamed: 0')\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:29.151593Z","iopub.execute_input":"2024-03-01T13:35:29.151954Z","iopub.status.idle":"2024-03-01T13:35:29.928469Z","shell.execute_reply.started":"2024-03-01T13:35:29.151928Z","shell.execute_reply":"2024-03-01T13:35:29.927293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:29.931817Z","iopub.execute_input":"2024-03-01T13:35:29.932152Z","iopub.status.idle":"2024-03-01T13:35:29.948368Z","shell.execute_reply.started":"2024-03-01T13:35:29.932124Z","shell.execute_reply":"2024-03-01T13:35:29.947254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def correct_dcm(dcm):\n    x = dcm.pixel_array + 1000\n    px_mode = 4096\n    x[x>=px_mode] = x[x>=px_mode] - px_mode\n    dcm.PixelData = x.tobytes()\n    dcm.RescaleIntercept = -1000\n    \ndef sigmoid_window(img, window_center, window_width, U=1.0, eps=(1.0 / 255.0)):\n    img = window_image(img, window_center, window_width)\n    ue = np.log((U / eps) - 1.0)\n    W = (2 / window_width) * ue\n    b = ((-2 * window_center) / window_width) * ue\n    z = W * img + b\n    img = U / (1 + np.power(np.e, -1.0 * z))\n    img = (img - np.min(img)) / (np.max(img) - np.min(img))\n    return img\n\n\ndef window_image(dcm, window_center, window_width, desired_size):\n    \n    if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):\n        correct_dcm(dcm)\n    \n    img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept\n    img = cv2.resize(img, desired_size[:2], interpolation = cv2.INTER_AREA)  # resize image\n    img_min = window_center - window_width // 2\n    img_max = window_center + window_width // 2\n    img = np.clip(img, img_min, img_max)\n\n    return img\n\ndef bsb_window(dcm, desired_size=(256,256)):\n    brain_img = window_image(dcm, 40, 80, desired_size)\n    subdural_img = window_image(dcm, 80, 200, desired_size)\n    soft_img = window_image(dcm, 40, 380, desired_size)\n    \n    brain_img = (brain_img - 0) / 80\n    subdural_img = (subdural_img - (-20)) / 200\n    soft_img = (soft_img - (-150)) / 380\n    bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1,2,0)\n    return bsb_img\n\ndicom = pydicom.dcmread(train_images_path + 'ID_5c8b5d701' + '.dcm')\nplt.imshow(bsb_window(dicom,(256,256)), cmap=plt.cm.bone);","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:29.950043Z","iopub.execute_input":"2024-03-01T13:35:29.950593Z","iopub.status.idle":"2024-03-01T13:35:30.323064Z","shell.execute_reply.started":"2024-03-01T13:35:29.950562Z","shell.execute_reply":"2024-03-01T13:35:30.322058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_img_val(path, desired_size=(256,256)):\n    dcm = pydicom.dcmread(path)\n    try:\n        img = bsb_window(dcm, desired_size)\n    except:\n        img = np.zeros(desired_size)\n        \n    return img","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:30.324455Z","iopub.execute_input":"2024-03-01T13:35:30.324843Z","iopub.status.idle":"2024-03-01T13:35:30.331327Z","shell.execute_reply.started":"2024-03-01T13:35:30.324787Z","shell.execute_reply":"2024-03-01T13:35:30.3303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_path = \"../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/\"\ntest_images_dir = input_path + 'stage_2_test/'\ntrain_images_dir = input_path + 'stage_2_train/'","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:30.332648Z","iopub.execute_input":"2024-03-01T13:35:30.333276Z","iopub.status.idle":"2024-03-01T13:35:30.345382Z","shell.execute_reply.started":"2024-03-01T13:35:30.333243Z","shell.execute_reply":"2024-03-01T13:35:30.344592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:30.346628Z","iopub.execute_input":"2024-03-01T13:35:30.346928Z","iopub.status.idle":"2024-03-01T13:35:30.371643Z","shell.execute_reply.started":"2024-03-01T13:35:30.346896Z","shell.execute_reply":"2024-03-01T13:35:30.370572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hem_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:30.373112Z","iopub.execute_input":"2024-03-01T13:35:30.373627Z","iopub.status.idle":"2024-03-01T13:35:30.390081Z","shell.execute_reply.started":"2024-03-01T13:35:30.37359Z","shell.execute_reply":"2024-03-01T13:35:30.388844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def window_with_correction(dcm, window_center, window_width):\n    if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):\n        correct_dcm(dcm)\n    img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept\n    img_min = window_center - window_width // 2\n    img_max = window_center + window_width // 2\n    img = np.clip(img, img_min, img_max)\n    return img\n\ndef window_without_correction(dcm, window_center, window_width):\n    img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept\n    img_min = window_center - window_width // 2\n    img_max = window_center + window_width // 2\n    img = np.clip(img, img_min, img_max)\n    return img\n\ndef window_testing(img, window):\n    brain_img = window(img, 40, 80)\n    subdural_img = window(img, 80, 200)\n    soft_img = window(img, 40, 380)\n    \n    brain_img = (brain_img - 0) / 80\n    subdural_img = (subdural_img - (-20)) / 200\n    soft_img = (soft_img - (-150)) / 380\n    bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1,2,0)\n\n    return bsb_img\n\n# example of a \"bad data point\" (i.e. (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100) == True)\ndicom = pydicom.dcmread(train_images_dir + \"ID_036db39b7\" + \".dcm\")\n\nfig, ax = plt.subplots(1, 2)\n\nax[0].imshow(window_testing(dicom, window_without_correction), cmap=plt.cm.bone);\nax[0].set_title(\"original\")\nax[1].imshow(window_testing(dicom, window_with_correction), cmap=plt.cm.bone);\nax[1].set_title(\"corrected\");","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:30.39162Z","iopub.execute_input":"2024-03-01T13:35:30.392019Z","iopub.status.idle":"2024-03-01T13:35:30.958656Z","shell.execute_reply.started":"2024-03-01T13:35:30.391973Z","shell.execute_reply":"2024-03-01T13:35:30.957602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import trange\ndef get_partition_labels_good_slices(df):\n    partition = dict()\n    labels = dict()\n    for i in trange(len(df)):\n        id_ = df.Image[i]\n        label = df.iloc[i,1:7].to_numpy(dtype = 'int32')\n        labels[id_] = label\n        \n    df = df.sample(frac = 0.1)\n    training = df.sample(frac = 0.8)\n    \n    validation = df.drop(training.index, axis = 0)\n    test = validation.sample(frac = 0.5)\n    validation = validation.drop(test.index, axis = 0) \n    \n    partition['train'] = list(training.Image)\n    partition['validation'] = list(validation.Image)\n    partition['test'] = list(test.Image)\n    return partition,labels","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:30.960181Z","iopub.execute_input":"2024-03-01T13:35:30.960611Z","iopub.status.idle":"2024-03-01T13:35:30.970277Z","shell.execute_reply.started":"2024-03-01T13:35:30.960573Z","shell.execute_reply":"2024-03-01T13:35:30.969281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"partition,labels= get_partition_labels_good_slices(df)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:35:30.97159Z","iopub.execute_input":"2024-03-01T13:35:30.971981Z","iopub.status.idle":"2024-03-01T13:36:26.199748Z","shell.execute_reply.started":"2024-03-01T13:35:30.971941Z","shell.execute_reply":"2024-03-01T13:36:26.198731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(partition['train']))\nvalues_view = labels.values()\nvalue_iterator = iter(values_view)\nfirst_value = next(value_iterator)\nprint(next(iter(labels)))\nprint(first_value)\n\n\nvalues_view2 = partition.values()\nvalue_iterator2 = iter(values_view)\nfirst_value2 = next(value_iterator)\nprint(next(iter(partition['train'])))\nprint(first_value2)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:38:45.265617Z","iopub.execute_input":"2024-03-01T13:38:45.266521Z","iopub.status.idle":"2024-03-01T13:38:45.280473Z","shell.execute_reply.started":"2024-03-01T13:38:45.266482Z","shell.execute_reply":"2024-03-01T13:38:45.279211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _read(path, desired_size):\n    dcm = pydicom.dcmread(path)\n    \n    try:\n        img = bsb_window(dcm)\n    except:\n        img = np.zeros(desired_size)\n    \n    \n    #img = cv2.resize(img, desired_size[:2], interpolation=cv2.INTER_LINEAR)\n    return img\n \nplt.imshow(\n    _read(train_images_dir+'ID_5c8b5d701'+'.dcm', (256, 256,3)), cmap=plt.cm.bone\n);","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:38:46.783595Z","iopub.execute_input":"2024-03-01T13:38:46.784294Z","iopub.status.idle":"2024-03-01T13:38:47.081708Z","shell.execute_reply.started":"2024-03-01T13:38:46.784247Z","shell.execute_reply":"2024-03-01T13:38:47.080343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(_read(train_images_dir+'ID_5c8b5d701'+'.dcm', (256, 256,3)))","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:38:47.554564Z","iopub.execute_input":"2024-03-01T13:38:47.554969Z","iopub.status.idle":"2024-03-01T13:38:47.569305Z","shell.execute_reply.started":"2024-03-01T13:38:47.554936Z","shell.execute_reply":"2024-03-01T13:38:47.568278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport keras\n\nclass DataGenerator(keras.utils.Sequence):\n    'Generates data for Keras'\n    def __init__(self, list_IDs, labels, batch_size=64, dim=(32,32,32), n_channels=1,\n                 n_classes=10, shuffle=True):\n        'Initialization'\n        self.dim = dim\n        self.batch_size = batch_size\n        self.labels = labels\n        self.list_IDs = list_IDs\n        self.n_channels = n_channels\n        self.n_classes = n_classes\n        self.shuffle = shuffle\n        self.true_labels = []\n        self.on_epoch_end()\n        \n    def __len__(self):\n        'Denotes the number of batches per epoch'\n        return int(np.floor(len(self.list_IDs) / self.batch_size))\n\n    def __getitem__(self, index):\n        'Generate one batch of data'\n        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n\n        list_IDs_temp = [self.list_IDs[k] for k in indexes]\n\n        X, y = self.__data_generation(list_IDs_temp)\n        \n        self.true_labels.append(y)\n\n        return X, y\n    def on_epoch_end(self):\n        'Updates indexes after each epoch'\n        self.indexes = np.arange(len(self.list_IDs))\n        if self.shuffle == True:\n            np.random.shuffle(self.indexes)\n\n    def __data_generation(self, list_IDs_temp):\n        'Generates data containing batch_size samples' \n        X = np.empty((self.batch_size, *self.dim)) \n        y = np.empty((self.batch_size,6), dtype=int)\n\n        # Generate data\n        for i, ID in enumerate(list_IDs_temp):\n            # Store sample\n            image_dir = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/'\n            X[i,] = _read(image_dir+ID+'.dcm',self.dim)\n            #X[i]= read_img_val(image_dir+ ID + '.dcm')\n            #print(X)\n            # Store class\n            \n            y[i] = self.labels[ID]\n\n        return X,y\n            \n    ","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:38:48.140912Z","iopub.execute_input":"2024-03-01T13:38:48.142036Z","iopub.status.idle":"2024-03-01T13:38:48.155649Z","shell.execute_reply.started":"2024-03-01T13:38:48.141993Z","shell.execute_reply":"2024-03-01T13:38:48.154574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications import EfficientNetB4\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Flatten\nfrom keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\nfrom keras.losses import BinaryCrossentropy\nfrom keras.optimizers import Adam\n\nparams = {\n    'dim': (256, 256, 3),\n    'batch_size': 32,\n    'n_classes': 6,\n    'n_channels': 0,\n    'shuffle': True\n}\n\ncallbacks = [\n    ModelCheckpoint(filepath='./checkpoint/model.keras', monitor='val_weighted_loss', save_best_only=True, verbose=3),\n    ReduceLROnPlateau(monitor='val_weighted_loss', factor=0.1, patience=3, verbose=1, mode='auto', min_delta=0.0001)\n]\n\n# Generators\ntraining_generator = DataGenerator(partition['train'], labels, **params)\nvalidation_generator = DataGenerator(partition['validation'], labels, **params)\n\n# Design model\nmodel = Sequential([\n    EfficientNetB4(\n        include_top=False,\n        weights=\"imagenet\",\n        input_shape=params['dim'],\n        pooling='max'),\n    Flatten(),\n    Dense(256, activation='relu'),\n    Dense(6, activation='sigmoid')  # Sigmoid activation for multi-label classification\n])\n\nmodel.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])\n#model.compile(optimizer=Adam(), loss = SparseCategoricalCrossentropy(from_logits=False), metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:38:59.193303Z","iopub.execute_input":"2024-03-01T13:38:59.194167Z","iopub.status.idle":"2024-03-01T13:39:01.68829Z","shell.execute_reply.started":"2024-03-01T13:38:59.194128Z","shell.execute_reply":"2024-03-01T13:39:01.687456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train model on dataset\nhistory = model.fit(training_generator, validation_data=validation_generator, epochs=10, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-02-14T06:40:01.463865Z","iopub.execute_input":"2024-02-14T06:40:01.464784Z","iopub.status.idle":"2024-02-14T06:40:01.468817Z","shell.execute_reply.started":"2024-02-14T06:40:01.464749Z","shell.execute_reply":"2024-02-14T06:40:01.467697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\n# Save history to a pickel file\nwith open('history.pkl', 'wb') as file:\n    pickle.dump(history.history, file)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\n\n# Convert history to JSON\nhistory_json = history.history\n\n# Save history to a JSON file\nwith open('history.json', 'w') as file:\n    json.dump(history_json, file)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model\nmodel.save('my_model.h5')\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\n# Open the pickle file in read mode\nwith open('/kaggle/input/history-and-model/history.pkl', 'rb') as file:\n    # Load the contents of the pickle file\n    history = pickle.load(file)\n\n# Now you have the loaded data in the history_data variable\nprint(history)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:39:37.561632Z","iopub.execute_input":"2024-03-01T13:39:37.562045Z","iopub.status.idle":"2024-03-01T13:39:37.571734Z","shell.execute_reply.started":"2024-03-01T13:39:37.562014Z","shell.execute_reply":"2024-03-01T13:39:37.570564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting of training and valid. accuracy and training and validation loss from pickle file\n# Extract metrics\nimport matplotlib.pyplot as plt\nloss = history['loss']\naccuracy = history['accuracy']\nval_loss = history['val_loss']\nval_accuracy = history['val_accuracy']\n\n# Plot training and validation accuracy\nepochs = range(1, len(loss) + 1)\nplt.plot(epochs, accuracy, 'r', label='Training Accuracy')\nplt.plot(epochs, val_accuracy, 'b', label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()\n\n# Plot training and validation loss\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:39:38.449817Z","iopub.execute_input":"2024-03-01T13:39:38.450601Z","iopub.status.idle":"2024-03-01T13:39:39.031615Z","shell.execute_reply.started":"2024-03-01T13:39:38.450563Z","shell.execute_reply":"2024-03-01T13:39:39.030478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To plot the graph at the same time of training the model\nimport matplotlib.pyplot as plt\n\nacc = history.history['accuracy']\nval_acc =  history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1, len(acc) + 1)\n\nplt.plot(epochs, acc, 'r', label='Training Accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation Accuracy')\nplt.title('Training and validation acc')\nplt.legend()\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.figure()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-01T12:39:17.371019Z","iopub.execute_input":"2024-03-01T12:39:17.371619Z","iopub.status.idle":"2024-03-01T12:39:17.420879Z","shell.execute_reply.started":"2024-03-01T12:39:17.371588Z","shell.execute_reply":"2024-03-01T12:39:17.419742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import backend as K\n\ndef weighted_log_loss(y_true, y_pred):\n    class_weights =  tf.Variable([2., 1., 1., 1., 1., 1.])\n    eps = K.epsilon()\n    \n    y_pred = K.clip(y_pred, eps, 1.0-eps)\n\n    out = -(         y_true  * K.log(      y_pred) * class_weights\n            + (1.0 - y_true) * K.log(1.0 - y_pred) * class_weights)\n    \n    return K.mean(out, axis=-1)\n####\ndef weighted_log_loss_V2(y_true, y_pred):\n    class_weights =  tf.constant([2., 1., 1., 1., 1., 1.])\n    \n    eps = tf.keras.backend.epsilon()\n    y_pred = tf.clip_by_value(y_pred, eps, 1.0-eps)\n\n    out = -(         y_true  * tf.math.log(      y_pred) * class_weights\n            + (1.0 - y_true) * tf.math.log(1.0 - y_pred) * class_weights)\n    \n    return tf.reduce_mean(out, axis=-1)\n\n\ndef _normalized_weighted_average(arr, weights=None):\n    if weights is not None:\n        scl = K.sum(weights)\n        weights = K.expand_dims(weights, axis=1)\n        return K.sum(K.dot(arr, weights), axis=1) / scl\n    return K.mean(arr, axis=1)\n\ndef weighted_loss(y_true, y_pred):\n\n    class_weights = tf.constant([2., 1., 1., 1., 1., 1.])\n    eps = tf.keras.backend.epsilon()\n    y_pred = tf.clip_by_value(y_pred, eps, 1.0-eps)\n\n    loss = -(        y_true  * tf.math.log(      y_pred)\n            + (1.0 - y_true) * tf.math.log(1.0 - y_pred))\n    \n    loss_samples = _normalized_weighted_average(loss, class_weights)\n    return tf.reduce_mean(loss_samples)\n\ndef weighted_log_loss_metric(trues, preds):\n    class_weights = [2., 1., 1., 1., 1., 1.]\n    \n    epsilon = 1e-7\n    \n    preds = np.clip(preds, epsilon, 1-epsilon)\n    loss = trues * np.log(preds) + (1 - trues) * np.log(1 - preds)\n    loss_samples = np.average(loss, axis=1, weights=class_weights)\n\n    return - loss_samples.mean()","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:42:40.182604Z","iopub.execute_input":"2024-03-01T13:42:40.183024Z","iopub.status.idle":"2024-03-01T13:42:40.199173Z","shell.execute_reply.started":"2024-03-01T13:42:40.182992Z","shell.execute_reply":"2024-03-01T13:42:40.197953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:42:37.639345Z","iopub.execute_input":"2024-03-01T13:42:37.640138Z","iopub.status.idle":"2024-03-01T13:42:37.644994Z","shell.execute_reply.started":"2024-03-01T13:42:37.640104Z","shell.execute_reply":"2024-03-01T13:42:37.643736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GradCAM:\n\tdef __init__(self, model, classIdx, layerName=None):\n\n\t\tself.model = model\n\t\tself.classIdx = classIdx\n\t\tself.layerName = layerName\n\n\t\t\n\t\tif self.layerName is None:\n\t\t\tself.layerName = self.find_target_layer()\n\n\tdef find_target_layer(self):\n\n\t\tfor layer in reversed(self.model.layers):\n\t\t\tif len(layer.output.shape) == 4:\n\t\t\t\treturn layer.name\n\n\t\n\t\traise ValueError(\"Could not find 4D layer. Cannot apply GradCAM.\")\n\n\tdef compute_heatmap(self, image, eps=1e-8):\n\n\t\tgradModel = Model(\n\t\t\tinputs=self.model.inputs,\n\t\t\toutputs=[self.model.get_layer(self.layerName).output, \n                     \n\t\t\t\tself.model.output])\n\n\t\twith tf.GradientTape() as tape:\n\t\t\n\t\t\tinputs = tf.cast(image, tf.float32)\n\t\t\t(convOutputs, predictions) = gradModel(inputs)\n\t\t\tloss = predictions[:, self.classIdx]\n\n\t\tgrads = tape.gradient(loss, convOutputs)\n\n\t\tcastConvOutputs = tf.cast(convOutputs > 0, \"float32\")\n\t\tcastGrads = tf.cast(grads > 0, \"float32\")\n\t\tguidedGrads = castConvOutputs * castGrads * grads\n\n\n\t\tconvOutputs = convOutputs[0]\n\t\tguidedGrads = guidedGrads[0]\n\n\t\n\t\tweights = tf.reduce_mean(guidedGrads, axis=(0, 1))\n\t\tcam = tf.reduce_sum(tf.multiply(weights, convOutputs), axis=-1)\n\n\n\t\t(w, h) = (image.shape[2], image.shape[1])\n\t\theatmap = cv2.resize(cam.numpy(), (w, h))\n\n\t\n\t\tnumer = heatmap - np.min(heatmap)\n\t\tdenom = (heatmap.max() - heatmap.min()) + eps\n\t\theatmap = numer / denom\n\t\theatmap = (heatmap * 255).astype(\"uint8\")\n\n\t\treturn heatmap\n\n\tdef overlay_heatmap(self, heatmap, image, alpha=0.5,\n\t\tcolormap=cv2.COLORMAP_JET):\n\n\t\theatmap = cv2.applyColorMap(heatmap, colormap)\n\t\toutput = cv2.addWeighted(image, alpha, heatmap, 1 - alpha, 0)\n\n\t\n\t\treturn (heatmap, output)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:00.066974Z","iopub.execute_input":"2024-03-01T13:44:00.067663Z","iopub.status.idle":"2024-03-01T13:44:00.081394Z","shell.execute_reply.started":"2024-03-01T13:44:00.06763Z","shell.execute_reply":"2024-03-01T13:44:00.080511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GradCAM:\n    def __init__(self, model, classIdx, layerName=None):\n\n        self.model = model\n        self.classIdx = classIdx\n        self.layerName = layerName\n\n\n        if self.layerName is None:\n            self.layerName = self.find_target_layer()\n\n    \n    def find_target_layer(self):\n\n        for layer in self.model.layers:\n            if len(layer.output.shape) == 4:\n                return layer.name\n        \n        raise ValueError(\"Could not find 4D layer. Cannot apply GradCAM.\") \n\n    def compute_heatmap(self, image, eps=1e-8):\n\n        gradModel = Model(\n            inputs= self.model.inputs,\n            outputs=[self.model.get_layer(self.layerName).output, \n                     \n                self.model.output])\n\n        with tf.GradientTape() as tape:\n\n            inputs = tf.cast(image, tf.float32)\n            (convOutputs, predictions) = gradModel(inputs)\n            loss = predictions[:, self.classIdx]\n\n        grads = tape.gradient(loss, convOutputs)\n\n        castConvOutputs = tf.cast(convOutputs > 0, \"float32\")\n        castGrads = tf.cast(grads > 0, \"float32\")\n        guidedGrads = castConvOutputs * castGrads * grads\n\n\n        convOutputs = convOutputs[0]\n        guidedGrads = guidedGrads[0]\n\n\n        weights = tf.reduce_mean(guidedGrads, axis=(0, 1))\n        cam = tf.reduce_sum(tf.multiply(weights, convOutputs), axis=-1)\n\n\n        (w, h) = (image.shape[2], image.shape[1])\n        heatmap = cv2.resize(cam.numpy(), (w, h))\n\n\n        numer = heatmap - np.min(heatmap)\n        denom = (heatmap.max() - heatmap.min()) + eps\n        heatmap = numer / denom\n        heatmap = (heatmap * 255).astype(\"uint8\")\n\n        return heatmap\n\n    def overlay_heatmap(self, heatmap, image, alpha=0.5,\n        colormap=cv2.COLORMAP_JET):\n\n        heatmap = cv2.applyColorMap(heatmap, colormap)\n        output = cv2.addWeighted(image, alpha, heatmap, 1 - alpha, 0)\n\n        return (heatmap, output)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:30:32.320985Z","iopub.execute_input":"2024-03-01T13:30:32.321999Z","iopub.status.idle":"2024-03-01T13:30:32.337416Z","shell.execute_reply.started":"2024-03-01T13:30:32.321967Z","shell.execute_reply":"2024-03-01T13:30:32.336149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create a model by loading the weight generated while training\ndef create_model():\n    \n    base_model =  efn.EfficientNetB4(weights = 'imagenet', include_top = False, pooling = 'avg', input_shape = (256,256,3))\n    x = base_model.output\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)\nmodel = create_model()\n#model.load_weights(\"/kaggle/input/weight/efficientnetb4_model.h5\")\nmodel.load_weights('../input/efficientnetb4-hemorrhage/efficientnetb4_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:12.615163Z","iopub.execute_input":"2024-03-01T13:44:12.615558Z","iopub.status.idle":"2024-03-01T13:44:17.523464Z","shell.execute_reply.started":"2024-03-01T13:44:12.615527Z","shell.execute_reply":"2024-03-01T13:44:17.522247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Save the model\nmodel.save(\"efficientnet.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-03-01T12:41:08.247518Z","iopub.execute_input":"2024-03-01T12:41:08.248359Z","iopub.status.idle":"2024-03-01T12:41:09.048462Z","shell.execute_reply.started":"2024-03-01T12:41:08.248321Z","shell.execute_reply":"2024-03-01T12:41:09.047587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  Loaded model with weight and architecture\nfrom keras.models import load_model\nloaded_model = load_model('/kaggle/working/efficientnet.h5')\n","metadata":{"execution":{"iopub.status.busy":"2024-03-01T12:41:46.527024Z","iopub.execute_input":"2024-03-01T12:41:46.527901Z","iopub.status.idle":"2024-03-01T12:41:49.348924Z","shell.execute_reply.started":"2024-03-01T12:41:46.527867Z","shell.execute_reply":"2024-03-01T12:41:49.347928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hem_df[hem_df.subarachnoid.values == 1].sample(20)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:26.735751Z","iopub.execute_input":"2024-03-01T13:44:26.736147Z","iopub.status.idle":"2024-03-01T13:44:26.766739Z","shell.execute_reply.started":"2024-03-01T13:44:26.736116Z","shell.execute_reply":"2024-03-01T13:44:26.765334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subdural_img = 'ID_29c9c2ee8.dcm'\t\nintraventricular_img = 'ID_004780f8e.dcm'\nintraparenchymal_img = 'ID_000d69988.dcm'\nsubarachnoid_img = 'ID_00058bb06.dcm'\nepidural_img = 'ID_00f1e66e1.dcm'  \nepidural_img2 = 'ID_ff0afaa64.dcm'\nsubdural_img2 = 'ID_47f130bfa.dcm'\t\nsubarachnoid_img2 = 'ID_526b45786.dcm'\n\ntype_list = [subdural_img, intraventricular_img, intraparenchymal_img, subarachnoid_img, epidural_img2]","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:27.20376Z","iopub.execute_input":"2024-03-01T13:44:27.204183Z","iopub.status.idle":"2024-03-01T13:44:27.210721Z","shell.execute_reply.started":"2024-03-01T13:44:27.20415Z","shell.execute_reply":"2024-03-01T13:44:27.209564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_id = subarachnoid_img2\nimg = hem_df.loc[hem_df.index == img_id].index[0]\nimage = read_img_val(train_images_path + img,(256, 256))\nlabel = hem_df.loc[hem_df.index == img_id].values  # we replace .Label.values by .values\nlabel","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:27.888832Z","iopub.execute_input":"2024-03-01T13:44:27.890219Z","iopub.status.idle":"2024-03-01T13:44:28.413938Z","shell.execute_reply.started":"2024-03-01T13:44:27.89017Z","shell.execute_reply":"2024-03-01T13:44:28.412866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:28.594129Z","iopub.execute_input":"2024-03-01T13:44:28.595232Z","iopub.status.idle":"2024-03-01T13:44:28.946063Z","shell.execute_reply.started":"2024-03-01T13:44:28.595189Z","shell.execute_reply":"2024-03-01T13:44:28.944818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_label = ['any', 'EPH', 'IPH','IVH', 'SAH', 'SDH']","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:29.51497Z","iopub.execute_input":"2024-03-01T13:44:29.515438Z","iopub.status.idle":"2024-03-01T13:44:29.520382Z","shell.execute_reply.started":"2024-03-01T13:44:29.515387Z","shell.execute_reply":"2024-03-01T13:44:29.519473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(image[np.newaxis,...])\ni = np.argmax(preds[0])\n\n# decode the ImageNet predictions to obtain the human-readable label\n\n# # initialize our gradient class activation map and build the heatmap\ncam = GradCAM(model, i)\nheatmap = cam.compute_heatmap(image[np.newaxis,...])\n\nimg_copy = np.copy(image)\nimg_copy -= img_copy.min((0,1))\nimg_copy = (255*img_copy).astype(np.uint8)\n# resize the resulting heatmap to the original input image dimensions\n# and then overlay heatmap on top of the image\nheatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]))\n(heatmap, output) = cam.overlay_heatmap(heatmap, img_copy, alpha=0.5)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:30.651717Z","iopub.execute_input":"2024-03-01T13:44:30.652109Z","iopub.status.idle":"2024-03-01T13:44:41.769782Z","shell.execute_reply.started":"2024-03-01T13:44:30.652079Z","shell.execute_reply":"2024-03-01T13:44:41.768803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Hemorrhage type: {}\".format(class_label))\nprint(\"actual label: {}\".format(label))\nprint(\"predicted label: {}\".format(preds))","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:58.692185Z","iopub.execute_input":"2024-03-01T13:44:58.693147Z","iopub.status.idle":"2024-03-01T13:44:58.699367Z","shell.execute_reply.started":"2024-03-01T13:44:58.69311Z","shell.execute_reply":"2024-03-01T13:44:58.698135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(output)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:59.16556Z","iopub.execute_input":"2024-03-01T13:44:59.166386Z","iopub.status.idle":"2024-03-01T13:44:59.507961Z","shell.execute_reply.started":"2024-03-01T13:44:59.166353Z","shell.execute_reply":"2024-03-01T13:44:59.506926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_label_list = []\nfor img_type in type_list:\n    img_id = img_type\n    img = hem_df.loc[hem_df.index == img_id].index[0]\n    image = read_img_val(train_images_path + img,(256, 256))\n    label = hem_df.loc[hem_df.index == img_id].values  #we replace .Label.values by .values\n    img_label_list.append((image, label))\n","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:44:59.946808Z","iopub.execute_input":"2024-03-01T13:44:59.947173Z","iopub.status.idle":"2024-03-01T13:45:02.315712Z","shell.execute_reply.started":"2024-03-01T13:44:59.947146Z","shell.execute_reply":"2024-03-01T13:45:02.314468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef plot_map(img_label_list):\n    fig, axes = plt.subplots(5, 2, figsize=(15, 15))\n    fig.suptitle('Grad-CAM\\nPredicted / Actual / Probability',fontsize=20)\n    \n    for i, img_label in enumerate(img_label_list):\n        img, label = img_label\n        preds = model.predict(img[np.newaxis,...])\n        axes[i,0].imshow(img, cmap = 'bone')\n        axes[i,0].set_xticks([])\n        axes[i,0].set_yticks([])\n        axes[i,0].set_title(f'{class_label[np.argmax(preds[:, 1:]) + 1]} / {class_label[np.argmax(label[:, 1:]) + 1]} / {np.max(preds[:, 1:]):.4f}')\n        heatmap = cam.compute_heatmap(img[np.newaxis,...])\n        img_copy = np.copy(img)\n        img_copy -= img_copy.min((0,1))\n        img_copy = (255*img_copy).astype(np.uint8)\n        # resize the resulting heatmap to the original input image dimensions\n        # and then overlay heatmap on top of the image\n        heatmap = cv2.resize(heatmap, (img_copy.shape[1], img_copy.shape[0]))\n        (heatmap, output) = cam.overlay_heatmap(heatmap, img_copy, alpha=0.5)\n        axes[i,1].imshow(output)\n        axes[i,1].set_xticks([])\n        axes[i,1].set_yticks([])\n        axes[i,1].set_title(\"heatmap showing hemorrhage location\")\n    plt.subplots_adjust(wspace=1, hspace=0.2)\n    plt.savefig('hemorrhageGradCAM.png')\nplot_map(img_label_list)","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:45:02.318148Z","iopub.execute_input":"2024-03-01T13:45:02.319228Z","iopub.status.idle":"2024-03-01T13:45:09.447743Z","shell.execute_reply.started":"2024-03-01T13:45:02.31918Z","shell.execute_reply":"2024-03-01T13:45:09.446811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile my_app.py\n\nimport os\nimport gc\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport pydicom\nimport os\nimport random\nimport imutils\nimport matplotlib.pyplot as plt\nimport collections\nfrom tqdm import tqdm_notebook as tqdm\nfrom datetime import datetime\n\nfrom math import ceil, floor, log\nimport cv2\nfrom scipy import ndimage\nimport sys\n\nimport tensorflow as tf\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Activation, Input, BatchNormalization, Add, GlobalAveragePooling2D,AveragePooling2D,GlobalMaxPooling2D,concatenate\nfrom tensorflow.keras.layers import Lambda, Reshape, DepthwiseConv2D, ZeroPadding2D, Add, MaxPooling2D,Activation, Flatten, Conv2D, Dense, Input, Dropout, Concatenate, GlobalMaxPooling2D, GlobalAveragePooling2D, BatchNormalization\n\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint,TensorBoard,TerminateOnNaN, LearningRateScheduler\nfrom tensorflow.keras.optimizers import Adam,RMSprop\nfrom tensorflow.keras.models import Model,load_model\n\nfrom sklearn.model_selection import ShuffleSplit\nfrom albumentations import (\n    Compose, HorizontalFlip, CLAHE, HueSaturationValue,\n    RandomBrightness, RandomContrast, RandomGamma,OneOf,\n    ToFloat, ShiftScaleRotate,GridDistortion, ElasticTransform, JpegCompression, HueSaturationValue, VerticalFlip,\n    RGBShift, RandomBrightness, RandomContrast, Blur, MotionBlur, MedianBlur, GaussNoise,CenterCrop,Normalize,\n    IAAAdditiveGaussianNoise,GaussNoise,OpticalDistortion,RandomSizedCrop,RandomCrop,RandomResizedCrop,RandomRotate90,Transpose\n)\n\nimport efficientnet.tfkeras as efn\nfrom iterstrat.ml_stratifiers import MultilabelStratifiedShuffleSplit, MultilabelStratifiedKFold\nfrom classification_models.tfkeras import Classifiers\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input d\nimport streamlit as st\nimport tensorflow as tf\nfrom streamlit_option_menu import option_menu\n#from keras.models import load_model\nfrom PIL import Image\nimport numpy as np\nimport cv2\nimport base64\n#from util import classify, set_background\n#\ndef set_background(image_file):\n    \"\"\"\n    This function sets the background of a Streamlit app to an image specified by the given image file.\n\n    Parameters:\n        image_file (str): The path to the image file to be used as the background.\n\n    Returns:\n        None\n    \"\"\"\n    with open(image_file, \"rb\") as f:\n        img_data = f.read()\n    b64_encoded = base64.b64encode(img_data).decode()\n    style = f\"\"\"\n        <style>\n        .stApp {{\n            background-image: url(data:image/png;base64,{b64_encoded});\n            background-size: cover;\n        }}\n        </style>\n    \"\"\"\n    st.markdown(style, unsafe_allow_html=True)\n\n\n\nset_background('/kaggle/input/photos/home1.jpeg')\n#st.set_page_config(layout='wide')\n\n\nselected = option_menu(\n    menu_title=None,\n    options=[\"Home\",\"Model\",\"Predict\",\"Types\"],\n    icons=[\"house\",\"bookshelf\",\"book\",\"envelope\"],\n    default_index=0,\n    orientation=\"horizontal\",\n)\n    \n\n\n######################  PREDICT  ###########################################################3\n\nif selected== \"Predict\":\n    #st.set_page_config(layout='wide')\n\n\n\n    # set header\n    #st.header('Please upload a CT scan image')\n\n\n    #st.write(' ')\n\n# upload file\n    file = st.file_uploader('/kaggle/input/rsna-intracranial-hemorrhage-detection', type=['jpeg', 'jpg', 'png', 'dcm'])\n    \n    from tensorflow.keras.models import load_model,Model\n    def window_image(dcm, window_center, window_width, desired_size):\n\n        if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):\n            correct_dcm(dcm)\n\n        img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept\n        img = cv2.resize(img, desired_size[:2], interpolation = cv2.INTER_AREA)  # resize image\n        img_min = window_center - window_width // 2\n        img_max = window_center + window_width // 2\n        img = np.clip(img, img_min, img_max)\n\n        return img\n\n    def bsb_window(dcm, desired_size):\n        brain_img = window_image(dcm, 40, 80, desired_size)\n        subdural_img = window_image(dcm, 80, 200, desired_size)\n        soft_img = window_image(dcm, 40, 380, desired_size)\n\n        brain_img = (brain_img - 0) / 80\n        subdural_img = (subdural_img - (-20)) / 200\n        soft_img = (soft_img - (-150)) / 380\n        bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1,2,0)\n        return bsb_img\n\n    def read_img_val(path, desired_size):\n        dcm = pydicom.dcmread(path)\n        \n        try:\n            img = bsb_window(dcm, desired_size)\n        except:\n            img = np.zeros(desired_size)\n\n        return img\n    \n    class GradCAM:\n        def __init__(self, model, classIdx, layerName=None):\n\n            self.model = model\n            self.classIdx = classIdx\n            self.layerName = layerName\n\n\n            if self.layerName is None:\n                self.layerName = self.find_target_layer()\n\n        def find_target_layer(self):\n\n            for layer in reversed(self.model.layers):\n                if len(layer.output.shape) == 4:\n                    return layer.name\n\n\n            raise ValueError(\"Could not find 4D layer. Cannot apply GradCAM.\")\n\n        def compute_heatmap(self, image, eps=1e-8):\n\n            gradModel = Model(\n                inputs=self.model.inputs,\n                outputs=[self.model.get_layer(self.layerName).output, \n\n                    self.model.output])\n\n            with tf.GradientTape() as tape:\n\n                inputs = tf.cast(image, tf.float32)\n                (convOutputs, predictions) = gradModel(inputs)\n                loss = predictions[:, self.classIdx]\n\n            grads = tape.gradient(loss, convOutputs)\n\n            castConvOutputs = tf.cast(convOutputs > 0, \"float32\")\n            castGrads = tf.cast(grads > 0, \"float32\")\n            guidedGrads = castConvOutputs * castGrads * grads\n\n\n            convOutputs = convOutputs[0]\n            guidedGrads = guidedGrads[0]\n\n\n            weights = tf.reduce_mean(guidedGrads, axis=(0, 1))\n            cam = tf.reduce_sum(tf.multiply(weights, convOutputs), axis=-1)\n\n\n            (w, h) = (image.shape[2], image.shape[1])\n            heatmap = cv2.resize(cam.numpy(), (w, h))\n\n\n            numer = heatmap - np.min(heatmap)\n            denom = (heatmap.max() - heatmap.min()) + eps\n            heatmap = numer / denom\n            heatmap = (heatmap * 255).astype(\"uint8\")\n\n            return heatmap\n\n        def overlay_heatmap(self, heatmap, image, alpha=0.5,\n            colormap=cv2.COLORMAP_JET):\n\n            heatmap = cv2.applyColorMap(heatmap, colormap)\n            output = cv2.addWeighted(image, alpha, heatmap, 1 - alpha, 0)\n\n\n            return (heatmap, output)\n\n    #path=\"/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/ID_0002081b6.dcm\"\n    def classify_hemorrhage(model,path):\n        fig, axes = plt.subplots(1, 2, figsize=(10, 10))\n        image = read_img_val(path,(256, 256))\n        preds = model.predict(image[np.newaxis,...])\n        #axes[0].imshow(image, cmap = 'bone')\n        # Mask the values less than 0.5 to 0\n        masked_preds = np.where(preds < 0.5, 0, preds)\n        class_label = ['any', 'EPH', 'IPH','IVH', 'SAH', 'SDH']\n\n        st.write(\"Hemorrhage type: {}\".format(class_label))\n        st.write(\"predicted label: {}\".format(masked_preds))\n        col1,col2= st.columns(2)\n        # Check if any value in masked_preds is greater than 0.5\n        if np.any(masked_preds > 0.5):\n            st.write(\"<h2>Hemorrhage detected</h2>\", unsafe_allow_html=True)\n            i = np.argmax(preds[0])\n            # # initialize our gradient class activation map and build the heatmap\n            cam = GradCAM(model, i)\n            heatmap = cam.compute_heatmap(image[np.newaxis,...])\n            img_copy = np.copy(image)\n            img_copy -= img_copy.min((0,1))\n            img_copy = (255*img_copy).astype(np.uint8)\n            # resize the resulting heatmap to the original input image dimensions\n            # and then overlay heatmap on top of the image\n            heatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]))\n            (heatmap, output) = cam.overlay_heatmap(heatmap, img_copy, alpha=0.5)\n            #axes[1].imshow(output)\n            with col1:\n                st.image(image, caption=\"Uploaded Image \", use_column_width=True)\n\n            with col2:\n                st.image(output, caption=\"Output with heatmap\", use_column_width=True)\n\n\n        else:\n            st.write(\"<h2>Hemorrhage not detected</h2>\", unsafe_allow_html=True)\n            # Then display the image\n\n            with col1:\n                st.image(image, caption=\"Uploaded Image\", use_column_width=True)\n\n           \n\n   \n   \n    def create_model():\n\n        base_model =  efn.EfficientNetB4(weights = 'imagenet', include_top = False, pooling = 'avg', input_shape = (256,256,3))\n        x = base_model.output\n        x = Dropout(0.15)(x)\n        y_pred = Dense(6, activation = 'sigmoid')(x)\n        return Model(inputs = base_model.input, outputs = y_pred)\n\n    model = create_model()\n    model.load_weights('../input/efficientnetb4-hemorrhage/efficientnetb4_model.h5')\n    #path=\"/kaggle/input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/ID_0002081b6.dcm\"\n    \n    if file is not None:\n        classify_hemorrhage(model,file)\n\n\n\n\n\n\n################### TYPES ############################################################\n\nif selected == \"Types\":\n    #st.set_page_config(layout='wide')\n\n\n    tab1, tab2, tab3,tab4,tab5 = st.tabs([\"Epidural hematoma\", \"Subdural Hematoma\", \"Subarachnoid Hemorrhage\",\"Intracerebral hemorrhage\",\"Intraventricular hemorrhage\"])\n\n\n    with tab1:\n        st.header(\"Epidural hematoma\")\n        #img1=cv2.imread(r\"/kaggle/input/imagere/Images/image4.jpg\")\n        #st.image(img1,width=200)\n\n        #st.markdown(\n          #  f\"<div style='text-align: center;'><img src='{img1}' alt='Your Image' width='400px'></div>\",\n           # unsafe_allow_html=True\n        #)\n\n\n\n\n        st.write(\"The temporal area of the head is usually the site of blunt trauma that results in the traditional arterial epidural hematoma. They might also happen following a piercing head injury. Usually, there is a fracture to the skull and bleeding into the possible epidural space due to injury to the middle meningeal artery.\")\n\n    with tab2:\n        st.header(\"Subdural Hematoma\")\n       # img2=cv2.imread(r\"/kaggle/input/imagere/Images/image5.jpg\")\n        #st.image(img2,width=200)\n        st.write(\"When blood enters the subdural space, which is physically the arachnoid space, subdural bleeding takes place. Subdural hemorrhage often happens when a blood artery that connects the brain to the skull is strained, fractured, or ruptured, causing blood to leak into the subdural region.\")\n\n    with tab3:\n        st.header(\"Subarachnoid Hemorrhage\")\n        #img3=cv2.imread(r\"/kaggle/input/imagere/Images/image2.jpg\")\n        #st.image(img3,width=200)\n        st.write(\"The subarachnoid is leaking blood due to a subarachnoid hemorrhage.   Subarachnoid hemorrhages are classified as either aneurysmal or non-aneurysmal under a second classification method. Aneurysmal subarachnoid hemorrhage happens when a brain aneurysm bursts, causing blood to leak into the subarachnoid space. A subarachnoid hemorrhage that is not associated with an identifiable aneurysm is defined as bleeding into the subarachnoid space.\")\n\n    with tab4:\n        st.header(\"Intracerebral hemorrhage\")\n        #img4=cv2.imread(r\"/kaggle/input/imagere/Images/image3.jpg\")\n        #st.image(img4,width=200)\n        st.write(\"Internal bleeding within the brain's parenchyma is known as intracerebral hemorrhage (ICH). Numerous factors, including uncontrolled hypertension, burst saccular aneurysms, vascular anomalies, or significant damage, may be to blame for this.The brain's small veins are harmed by high blood pressure, which weakens the arterial wall and increases the risk of rupture.\")\n\n    with tab5:\n        st.header(\"Intraventricular hemorrhage\")\n        #img5=cv2.imread(r\"/kaggle/input/imagere/Images/image1.jpg\")\n        #st.image(img5,width=200)\n        st.write(\"Bleeding into the brain's ventricles, which are fluid-filled areas, is known as intraventricular hemorrhage (IVH). Premature newborns are the most prevalent victims of the condition, and the likelihood of IVH increases with the size and prematureness of the child. This is due to the exceedingly weak and immature blood vessels in the brains of preterm newborns. Rarely is IVH present at birth, and when it is, it generally manifests itself in the first few days of life.\")\n\n\n\n     \n    \n     \n########################### HOME #########################################################\n   \nif selected == \"Home\":\n    st.title('DETECTION OF INTRACRANIAL HEMORRHAGE')\n    st.write(\"The term intracranial hemorrhage (ICH) describes bleeding that starts inside the skull and ends up in the brain. Intracerebral hemorrhages come in a variety of forms, each with unique traits and possible causes.\")\n\n\n    col1, col2, col3 = st.columns(3)\n\n    with col1:\n        st.header(\"Types\")\n        st.write(\"1. Epidural hematoma\")\n        st.write(\"2. Subdural Hematoma\")\n        st.write(\"3. Subarachnoid Hemorrhage\")\n        st.write(\"4. Intracerebral hemorrhage\")\n        st.write(\"5. Intraventricular hemorrhage\")\n\n    with col2:\n        st.header(\"Causes\")\n        st.write(\"1. Hypertension\")\n        st.write(\"2. Vascular abnormalities\")\n        st.write(\"3. Trauma\")\n        st.write(\"4. Blood thinners\")\n        st.write(\"5. Spontaneous\")\n        \n    with col3:\n        st.header(\"Symptoms\")\n        st.write(\"1. Sudden and severe headache\")\n        st.write(\"2. Nausea and vomiting\")\n        st.write(\"3. Seizures\")\n        st.write(\"4. Temporary loss of vision\")\n        \n        ","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:48:55.984563Z","iopub.execute_input":"2024-03-01T13:48:55.984992Z","iopub.status.idle":"2024-03-01T13:48:56.00261Z","shell.execute_reply.started":"2024-03-01T13:48:55.984961Z","shell.execute_reply":"2024-03-01T13:48:56.001354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Use the generated code below as a tunnel password \n!wget -q -O - ipv4.icanhazip.com","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:48:57.409289Z","iopub.execute_input":"2024-03-01T13:48:57.410118Z","iopub.status.idle":"2024-03-01T13:48:58.738432Z","shell.execute_reply.started":"2024-03-01T13:48:57.410079Z","shell.execute_reply":"2024-03-01T13:48:58.736822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!npm install -g localtunnel","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:48:58.741754Z","iopub.execute_input":"2024-03-01T13:48:58.74225Z","iopub.status.idle":"2024-03-01T13:49:04.078673Z","shell.execute_reply.started":"2024-03-01T13:48:58.742208Z","shell.execute_reply":"2024-03-01T13:49:04.077013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Click on the Your url is: .... link below\n! streamlit run my_app.py & npx localtunnel --port 8501","metadata":{"execution":{"iopub.status.busy":"2024-03-01T13:49:04.080643Z","iopub.execute_input":"2024-03-01T13:49:04.081056Z"},"trusted":true},"execution_count":null,"outputs":[]}]}