{"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":"markdown","source":"**(Version 1)**\n- Trying `ResNet50` \n- Epochs 60\n- Datasize 5000\n- Test data\n- Keep last 10 layers trainable\n- *Note:* Removed all layers after base_model except the necessary ones\n\n**(Version 2)**\n- Trying `InceptionV3`\n\n\n### ResNet152V2\n\n### DenseNet201 Training all\n\n### V6: DenseNet201 Training all - Soft Tissues\n\n### V7: DenseNet201 - L2 Regularization - alpha 0.01\n\n### V8: DenseNet201 - L1 L2 Regularization - alpha 0.0001\n\n### V9: DenseNet201 - L2 Regularization - alpha 0.001\n\n### V11: On 30,000 images without Regularization\n\n### V12: On 107,933 images without Regularization\n\n### V13: All data on DenseNet121\n\n### V14: All data on DenseNet201 but with batch size of 128 and 8 epochs - Failed\n\n### V15: All data on DenseNet201 but with batch size of 64 and 8 epochs\n","metadata":{}},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2021-12-01T16:43:19.389746Z","iopub.execute_input":"2021-12-01T16:43:19.390034Z","iopub.status.idle":"2021-12-01T16:43:20.092992Z","shell.execute_reply.started":"2021-12-01T16:43:19.389937Z","shell.execute_reply":"2021-12-01T16:43:20.092218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' #disable warnings\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nimport seaborn as sns\nimport seaborn as sns\nimport pydicom\nimport math\nimport tensorflow_hub as hub\nimport matplotlib\nmatplotlib.rc('xtick', labelsize=15) \nmatplotlib.rc('ytick', labelsize=15) \nsns.set_style(\"darkgrid\")\nsns.set_context(\"notebook\", font_scale=1.5, rc={\"lines.linewidth\": 4})","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-01T16:47:00.54988Z","iopub.execute_input":"2021-12-01T16:47:00.55062Z","iopub.status.idle":"2021-12-01T16:47:06.916509Z","shell.execute_reply.started":"2021-12-01T16:47:00.550579Z","shell.execute_reply":"2021-12-01T16:47:06.915784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pd.__version__)\nprint(tf.__version__)\nprint(pydicom.__version__)\nprint(np.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-12-01T16:47:47.524409Z","iopub.execute_input":"2021-12-01T16:47:47.524947Z","iopub.status.idle":"2021-12-01T16:47:47.529811Z","shell.execute_reply.started":"2021-12-01T16:47:47.524907Z","shell.execute_reply":"2021-12-01T16:47:47.528953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat /proc/cpuinfo","metadata":{"execution":{"iopub.status.busy":"2021-12-01T16:49:09.28201Z","iopub.execute_input":"2021-12-01T16:49:09.282563Z","iopub.status.idle":"2021-12-01T16:49:09.947684Z","shell.execute_reply.started":"2021-12-01T16:49:09.282522Z","shell.execute_reply":"2021-12-01T16:49:09.946894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_learning_curves(history, metrics_to_plot = ['loss','binary_accuracy']):\n  import math\n  ncols = 2\n  nrows = math.ceil(len(metrics_to_plot) / 2)\n  if len(metrics_to_plot) <= 2:\n        fig, axes = plt.subplots(nrows,ncols, figsize=(20,10))\n        for i in range(2):\n            axes[i].plot(history.history[metrics_to_plot[i]], label=metrics_to_plot[i] +' (training data)')\n            axes[i].plot(history.history['val_'+metrics_to_plot[i]], label=metrics_to_plot[i] + ' (test data)')\n            axes[i].set_ylabel('Value', fontsize = 20)\n            axes[i].set_xlabel('No. epoch', fontsize = 20)\n            axes[i].legend(prop={'size': 20})\n            axes[i].set_title(metrics_to_plot[i], size = 22)\n  else:        \n      fig, axes = plt.subplots(nrows,ncols, figsize=(15,20))\n\n      for i in range(ncols):\n        for j in range(nrows):\n          metric_idx = j * ncols + i\n          if metric_idx >= len(metrics_to_plot):\n                break\n          axes[j,i].plot(history.history[metrics_to_plot[metric_idx]], label=metrics_to_plot[metric_idx] +' (training data)')\n          axes[j,i].plot(history.history['val_'+metrics_to_plot[metric_idx]], label=metrics_to_plot[metric_idx] + ' (test data)')\n          axes[j,i].set_ylabel('Value', fontsize = 20)\n          axes[j,i].set_xlabel('No. epoch', fontsize = 20)\n          axes[j,i].legend(prop={'size': 20})\n          axes[j,i].set_title(metrics_to_plot[metric_idx], size = 22)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:36:41.336677Z","iopub.execute_input":"2021-11-28T14:36:41.336977Z","iopub.status.idle":"2021-11-28T14:36:41.354932Z","shell.execute_reply.started":"2021-11-28T14:36:41.336927Z","shell.execute_reply":"2021-11-28T14:36:41.353032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SEED = 42\nSAMPLE_NORMAL = 107933\nSAMPLE_ABNORMAL = 107933\nSUBCLASSES = ['any', 'epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural']\nIMAGE_SIZE = (224,224)\nBATCH_SIZE = 64\nNUM_CLASSES = 1\nEPOCHS = 8\nALPHA = 0.0001\nMETRICS = [tf.keras.metrics.BinaryAccuracy()]","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:36:41.358918Z","iopub.execute_input":"2021-11-28T14:36:41.359847Z","iopub.status.idle":"2021-11-28T14:36:44.142463Z","shell.execute_reply.started":"2021-11-28T14:36:41.359805Z","shell.execute_reply":"2021-11-28T14:36:44.141504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/'\ntrain_df = pd.read_csv('../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:36:44.145302Z","iopub.execute_input":"2021-11-28T14:36:44.145716Z","iopub.status.idle":"2021-11-28T14:36:48.415183Z","shell.execute_reply.started":"2021-11-28T14:36:44.145674Z","shell.execute_reply":"2021-11-28T14:36:48.414266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = train_df.Label\ntrain_df = train_df.ID.str.rsplit('_', n=1, expand=True)\ntrain_df['label'] = label\ntrain_df.rename({0: 'id', 1: 'subtype'}, axis=1, inplace = True)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:36:48.416963Z","iopub.execute_input":"2021-11-28T14:36:48.417284Z","iopub.status.idle":"2021-11-28T14:37:00.615461Z","shell.execute_reply.started":"2021-11-28T14:36:48.417244Z","shell.execute_reply":"2021-11-28T14:37:00.614292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.pivot_table(train_df, index='id', columns = 'subtype', values= 'label')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:00.617Z","iopub.execute_input":"2021-11-28T14:37:00.620441Z","iopub.status.idle":"2021-11-28T14:37:10.324258Z","shell.execute_reply.started":"2021-11-28T14:37:00.620409Z","shell.execute_reply":"2021-11-28T14:37:10.323284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.index = train_df.index.astype(str) + '.dcm'\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:10.325952Z","iopub.execute_input":"2021-11-28T14:37:10.326542Z","iopub.status.idle":"2021-11-28T14:37:11.206181Z","shell.execute_reply.started":"2021-11-28T14:37:10.326482Z","shell.execute_reply":"2021-11-28T14:37:11.205254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['any'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.208032Z","iopub.execute_input":"2021-11-28T14:37:11.20959Z","iopub.status.idle":"2021-11-28T14:37:11.224054Z","shell.execute_reply.started":"2021-11-28T14:37:11.209547Z","shell.execute_reply":"2021-11-28T14:37:11.222482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.226558Z","iopub.execute_input":"2021-11-28T14:37:11.227113Z","iopub.status.idle":"2021-11-28T14:37:11.242114Z","shell.execute_reply.started":"2021-11-28T14:37:11.227012Z","shell.execute_reply":"2021-11-28T14:37:11.240947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df[['any']]\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.247826Z","iopub.execute_input":"2021-11-28T14:37:11.249247Z","iopub.status.idle":"2021-11-28T14:37:11.260798Z","shell.execute_reply.started":"2021-11-28T14:37:11.249212Z","shell.execute_reply":"2021-11-28T14:37:11.259598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal_df = train_df[train_df['any'] == 0]\nabnormal_df = train_df[train_df['any'] == 1]","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.262534Z","iopub.execute_input":"2021-11-28T14:37:11.263702Z","iopub.status.idle":"2021-11-28T14:37:11.310479Z","shell.execute_reply.started":"2021-11-28T14:37:11.263648Z","shell.execute_reply":"2021-11-28T14:37:11.309442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal_sample = normal_df.sample(SAMPLE_NORMAL, replace = False, random_state = SEED, axis = 0)\nabnormal_sample = abnormal_df.sample(SAMPLE_ABNORMAL, replace = False, random_state = SEED, axis = 0)\nsample_df = normal_sample.append(abnormal_sample)\nsample_df = sample_df.sample(frac = 1, random_state = SEED, axis = 0)\nsample_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.312367Z","iopub.execute_input":"2021-11-28T14:37:11.312833Z","iopub.status.idle":"2021-11-28T14:37:11.350618Z","shell.execute_reply.started":"2021-11-28T14:37:11.312768Z","shell.execute_reply":"2021-11-28T14:37:11.349681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df['any'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.353967Z","iopub.execute_input":"2021-11-28T14:37:11.354187Z","iopub.status.idle":"2021-11-28T14:37:11.36279Z","shell.execute_reply.started":"2021-11-28T14:37:11.354161Z","shell.execute_reply":"2021-11-28T14:37:11.361458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df['any']","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.364812Z","iopub.execute_input":"2021-11-28T14:37:11.365528Z","iopub.status.idle":"2021-11-28T14:37:11.379083Z","shell.execute_reply.started":"2021-11-28T14:37:11.365462Z","shell.execute_reply":"2021-11-28T14:37:11.377017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(sample_df.index, sample_df, test_size = 0.3, random_state = SEED)\nX_train, X_val, y_train, y_val = train_test_split(y_train.index, y_train, test_size = 0.3, random_state = SEED)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.380821Z","iopub.execute_input":"2021-11-28T14:37:11.381623Z","iopub.status.idle":"2021-11-28T14:37:11.393935Z","shell.execute_reply.started":"2021-11-28T14:37:11.381372Z","shell.execute_reply":"2021-11-28T14:37:11.392533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_test), len(y_val), len(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.395978Z","iopub.execute_input":"2021-11-28T14:37:11.396461Z","iopub.status.idle":"2021-11-28T14:37:11.4047Z","shell.execute_reply.started":"2021-11-28T14:37:11.396415Z","shell.execute_reply":"2021-11-28T14:37:11.403467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = y_train\nval_df = y_val\ntest_df = y_test","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.406353Z","iopub.execute_input":"2021-11-28T14:37:11.407423Z","iopub.status.idle":"2021-11-28T14:37:11.412777Z","shell.execute_reply.started":"2021-11-28T14:37:11.407351Z","shell.execute_reply":"2021-11-28T14:37:11.411631Z"},"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 get_first_of_dicom_field_as_int(x):\n    if type(x) == pydicom.multival.MultiValue:\n        return int(x[0])\n    return int(x)\n    \ndef get_windowing(data):\n    dicom_fields = [data[('0028','1050')].value, # window center\n                    data[('0028','1051')].value, # window width\n                    data[('0028','1052')].value, # intercept\n                    data[('0028','1053')].value, # slope\n                   ]\n    return [get_first_of_dicom_field_as_int(x) for x in dicom_fields]\n    \n\ndef get_min_max_of_window_value(window_center, window_width):\n    mini = window_center - (window_width // 2)\n    maxi = window_center + (window_width // 2) \n    return mini, maxi\n\ndef window_image(img, window_center, window_width):\n    try:\n        _,_, intercept, slope = get_windowing(img)\n        img = img.pixel_array * slope + intercept\n        img_min, img_max = get_min_max_of_window_value(window_center, window_width)\n        img[img < img_min] = img_min\n        img[img > img_max] = img_max\n#         img = (img - np.min(img)) / (np.max(img) - np.min(img)) #normalize\n    except:\n        img = img_min * np.ones(IMAGE_SIZE)\n        \n    return img\n\ndef normalize(channel, wc_ww: tuple, norm_type = 'none'):\n    if norm_type.lower() == 'none':\n        return channel\n    if norm_type.lower() == 'min_max':\n        mini, maxi = get_min_max_of_window_value(wc_ww[0], wc_ww[1])\n        resulted_channel = (channel - mini) / (maxi - mini)\n        return resulted_channel\n    \n\ndef bsb_window(img):\n    bsb_config = {'brain': (40,80),\n             'subdural': (80,200),\n             'soft': (50, 350)}\n    brain_img = window_image(img, *bsb_config['brain'])\n    subdural_img = window_image(img,*bsb_config['subdural'])\n    soft_img = window_image(img, *bsb_config['soft'])\n    \n    brain_img = normalize(brain_img, bsb_config['brain'], 'min_max')\n    subdural_img = normalize(subdural_img, bsb_config['subdural'], 'min_max')\n    soft_img = normalize(soft_img, bsb_config['soft'], 'min_max')\n    \n#         print(np.min(soft_img))\n#     brain_img = (brain_img - 0) / 80\n#     subdural_img = (subdural_img - (-20)) / 200\n#     soft_img = (soft_img - (-150)) / 380 # (-150 = 40 - 380 / 2)\n#         print(np.min(soft_img))\n    bsb_img = np.zeros((brain_img.shape[0], brain_img.shape[1],3))\n    bsb_img[:, :, 0] = brain_img\n    bsb_img[:, :, 1] = subdural_img\n    bsb_img[:, :, 2] = soft_img\n    \n    if (np.any(np.isnan(bsb_img))):\n        bsb_img = np.ones((*IMAGE_SIZE,3))\n        \n    return bsb_img\n\n\n    \nclass ImageGenerator(tf.keras.utils.Sequence):\n    def __init__(self, dataframe,batch_size,shuffle, num_classes = NUM_CLASSES):\n        self.dataframe = dataframe\n        self.num_classes = num_classes\n        self.batch_size = batch_size\n        self.shuffle = shuffle\n        \n    def __len__(self):\n        return math.ceil(len(self.dataframe) / self.batch_size)\n    \n    def __getitem__(self, index):\n        batch_df = self.dataframe.iloc[index * self.batch_size: (index+1) * self.batch_size]\n        paths = TRAIN_PATH + batch_df.index.astype(str)\n        X = np.empty((len(batch_df), *IMAGE_SIZE, 3))\n        y = np.empty((len(batch_df), self.num_classes))\n        for i, path in enumerate(paths):\n            dcm = pydicom.dcmread(path)\n            # correct dcm\n            if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):\n                correct_dcm(dcm)\n#             rescaled_img = rescale_pixelarray(dcm)\n#             windowed_img = set_manual_window(rescaled_img, HU_MIN, HU_MAX)\n#             img = tf.convert_to_tensor(windowed_img, dtype=tf.float32)\n            img = bsb_window(dcm)\n            img = tf.convert_to_tensor(img, dtype=tf.float64)\n#             assert tf.reduce_min(img) >= -1 and tf.reduce_max(img) <= 1, 'Check these values img in (-1,1)'\n            X[i] = tf.image.resize(img, IMAGE_SIZE)\n            y[i] = batch_df.iloc[i].values\n#             assert tf.reduce_min(y[i]) >= 0 and tf.reduce_max(y[i]) <= 1, 'Check target values in (0,1)'\n            \n        return X, y\n    \n    def on_epoch_end(self):\n        if self.shuffle:\n            self.dataframe = self.dataframe.sample(len(self.dataframe), replace = False, random_state = SEED)\n        self.current_epoch += 1","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.415017Z","iopub.execute_input":"2021-11-28T14:37:11.415679Z","iopub.status.idle":"2021-11-28T14:37:11.445181Z","shell.execute_reply.started":"2021-11-28T14:37:11.415635Z","shell.execute_reply":"2021-11-28T14:37:11.444155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport tempfile\n\ndef add_regularization(model, regularizer=tf.keras.regularizers.l2(0.0001)):\n\n    if not isinstance(regularizer, tf.keras.regularizers.Regularizer):\n      print(\"Regularizer must be a subclass of tf.keras.regularizers.Regularizer\")\n      return model\n\n    for layer in model.layers:\n        for attr in ['kernel_regularizer']:\n            if hasattr(layer, attr):\n              setattr(layer, attr, regularizer)\n\n    # When we change the layers attributes, the change only happens in the model config file\n    model_json = model.to_json()\n\n    # Save the weights before reloading the model.\n    tmp_weights_path = os.path.join(tempfile.gettempdir(), 'tmp_weights.h5')\n    model.save_weights(tmp_weights_path)\n\n    # load the model from the config\n    model = tf.keras.models.model_from_json(model_json)\n    \n    # Reload the model weights\n    model.load_weights(tmp_weights_path, by_name=True)\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.448253Z","iopub.execute_input":"2021-11-28T14:37:11.448782Z","iopub.status.idle":"2021-11-28T14:37:11.462963Z","shell.execute_reply.started":"2021-11-28T14:37:11.4487Z","shell.execute_reply":"2021-11-28T14:37:11.461569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_generator_train = ImageGenerator(train_df, BATCH_SIZE, shuffle=True)\nimg_generator_val = ImageGenerator(val_df, BATCH_SIZE, shuffle = True)\nimg_generator_test = ImageGenerator(test_df, BATCH_SIZE, shuffle = False)\ntrain_data = tf.data.Dataset.from_generator(lambda: map(tuple, img_generator_train), \n                                            output_types=(tf.float64, tf.uint8),\n                                            output_shapes = (\n                                                    tf.TensorShape((None, *IMAGE_SIZE,3)),\n                                                    tf.TensorShape((None, NUM_CLASSES))\n                                            ))\nval_data = tf.data.Dataset.from_generator(lambda: map(tuple, img_generator_val), \n                                          output_types=(tf.float64, tf.uint8),\n                                          output_shapes = (\n                                                    tf.TensorShape((None, *IMAGE_SIZE,3)),\n                                                    tf.TensorShape((None, NUM_CLASSES))\n                                            ))\ntest_data = tf.data.Dataset.from_generator(lambda: map(tuple, img_generator_test), \n                                          output_types=(tf.float64, tf.uint8),\n                                          output_shapes = (\n                                                    tf.TensorShape((None, *IMAGE_SIZE,3)),\n                                                    tf.TensorShape((None, NUM_CLASSES))\n                                            ))\n\n\n\ndata_augmentation = tf.keras.Sequential([\n   tf.keras.layers.experimental.preprocessing.RandomFlip(\"horizontal\"),\n   tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),\n   tf.keras.layers.experimental.preprocessing.RandomZoom(0.2),\n   tf.keras.layers.experimental.preprocessing.RandomHeight(0.2),\n   tf.keras.layers.experimental.preprocessing.RandomWidth(0.2)\n])\n\n# base_model = hub.KerasLayer(\"https://tfhub.dev/google/imagenet/inception_v1/feature_vector/5\", trainable=False)\nbase_model = tf.keras.applications.DenseNet121(include_top = False)\n\nfor layer in base_model.layers:\n    layer.trainable = True\n# for layer in base_model.layers[-10:]:\n#     layer.trainable = True\n# base_model = add_regularization(base_model,regularizer=tf.keras.regularizers.l2(ALPHA))\n    \n# for layer in base_model.layers[:-10]:\n#     layer.trainable = False\n\ninputs = tf.keras.layers.Input(shape = IMAGE_SIZE + (3,), name = \"input_layer\")\nx = data_augmentation(inputs)\n\nx = base_model(x)\n\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\nx = tf.keras.layers.Dropout(0.8)(x)\n\noutputs = tf.keras.layers.Dense(NUM_CLASSES, activation='sigmoid')(x)\nmodel = tf.keras.Model(inputs, outputs)\n\nmodel.compile(loss=tf.keras.losses.BinaryCrossentropy(),\n             optimizer = tf.keras.optimizers.Adam(learning_rate=0.000125),\n             metrics = METRICS)\nprint(model.summary())\nhistory = model.fit(train_data, \n                   epochs = EPOCHS,\n                   validation_data = val_data,\n                   callbacks = [tf.keras.callbacks.ModelCheckpoint('best_model.h5', monitor='val_loss', save_best_only=True, mode='min', save_freq='epoch'),\n                               tf.keras.callbacks.EarlyStopping(restore_best_weights=True, patience=5),\n                               tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',\n                                           factor=0.5,\n                                           patience=2,\n                                           min_lr=1e-8,\n                                           mode=\"min\")\n                               ]\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T14:37:11.465264Z","iopub.execute_input":"2021-11-28T14:37:11.465759Z","iopub.status.idle":"2021-11-28T16:01:10.883842Z","shell.execute_reply.started":"2021-11-28T14:37:11.465719Z","shell.execute_reply":"2021-11-28T16:01:10.880746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_data)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:01:10.885492Z","iopub.execute_input":"2021-11-28T16:01:10.885771Z","iopub.status.idle":"2021-11-28T16:01:52.888604Z","shell.execute_reply.started":"2021-11-28T16:01:10.885734Z","shell.execute_reply":"2021-11-28T16:01:52.887436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:01:52.894792Z","iopub.execute_input":"2021-11-28T16:01:52.895122Z","iopub.status.idle":"2021-11-28T16:01:52.908912Z","shell.execute_reply.started":"2021-11-28T16:01:52.895082Z","shell.execute_reply":"2021-11-28T16:01:52.905717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_prob = model.predict(test_data)\npred_labels = (pred_prob > 0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:01:52.910803Z","iopub.execute_input":"2021-11-28T16:01:52.911198Z","iopub.status.idle":"2021-11-28T16:02:17.046475Z","shell.execute_reply.started":"2021-11-28T16:01:52.911108Z","shell.execute_reply":"2021-11-28T16:02:17.045548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = pd.DataFrame({'prob': pred_prob.flatten(), 'label': pred_labels.flatten()}, index = test_df.index)\ncomp = pd.DataFrame({'res_prob': res.prob, 'res_label': res.label, 'test_label': test_df['any']}, index = test_df.index)\ncomp.head(20)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:02:17.047764Z","iopub.execute_input":"2021-11-28T16:02:17.048067Z","iopub.status.idle":"2021-11-28T16:02:17.073541Z","shell.execute_reply.started":"2021-11-28T16:02:17.048028Z","shell.execute_reply":"2021-11-28T16:02:17.072571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the accuracy\n(comp['res_label'] == comp['test_label']).sum() / comp.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:02:17.075173Z","iopub.execute_input":"2021-11-28T16:02:17.075819Z","iopub.status.idle":"2021-11-28T16:02:17.089038Z","shell.execute_reply.started":"2021-11-28T16:02:17.075772Z","shell.execute_reply":"2021-11-28T16:02:17.087907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:02:17.091348Z","iopub.execute_input":"2021-11-28T16:02:17.09258Z","iopub.status.idle":"2021-11-28T16:02:17.106799Z","shell.execute_reply.started":"2021-11-28T16:02:17.09254Z","shell.execute_reply":"2021-11-28T16:02:17.105886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.to_csv('res.csv')\ntest_df.to_csv('test_df.csv')\ncomp.to_csv('comp.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:02:17.1082Z","iopub.execute_input":"2021-11-28T16:02:17.108961Z","iopub.status.idle":"2021-11-28T16:02:17.145269Z","shell.execute_reply.started":"2021-11-28T16:02:17.108921Z","shell.execute_reply":"2021-11-28T16:02:17.144224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"lr\"], 'o-')\nplt.title('Learning Rate')\nplt.xlabel('Epochs')\nplt.ylabel('LR')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:02:17.163915Z","iopub.execute_input":"2021-11-28T16:02:17.164849Z","iopub.status.idle":"2021-11-28T16:02:17.599495Z","shell.execute_reply.started":"2021-11-28T16:02:17.164804Z","shell.execute_reply":"2021-11-28T16:02:17.598575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_learning_curves(history)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:02:19.451572Z","iopub.execute_input":"2021-11-28T16:02:19.452284Z","iopub.status.idle":"2021-11-28T16:02:20.537215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:02:20.538563Z","iopub.execute_input":"2021-11-28T16:02:20.538893Z","iopub.status.idle":"2021-11-28T16:02:20.54591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(test_df['any'], pred_labels))","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:02:20.548332Z","iopub.execute_input":"2021-11-28T16:02:20.549716Z","iopub.status.idle":"2021-11-28T16:02:20.586369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef plot_confusion_matrix(cm, class_name=None,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n\n    # Plot confusion matrix in a beautiful manner\n    fig = plt.figure(figsize=(12, 10))\n    ax= plt.subplot()\n    sns.heatmap(cm, annot=True, ax = ax, fmt = 'g', cmap = cmap); #annot=True to annotate cells\n    # labels, title and ticks\n    ax.set_xlabel('Predicted', fontsize=20)\n    ax.xaxis.set_label_position('bottom')\n    plt.xticks(rotation=0)\n    ax.xaxis.tick_bottom()\n\n    ax.set_ylabel('True', fontsize=20)\n    plt.yticks(rotation=0)\n\n    plt.title(title, fontsize=20)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:02:20.58793Z","iopub.execute_input":"2021-11-28T16:02:20.588791Z","iopub.status.idle":"2021-11-28T16:02:20.607762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(confusion_matrix(test_df['any'], pred_labels))","metadata":{"execution":{"iopub.status.busy":"2021-11-28T16:02:20.609731Z","iopub.execute_input":"2021-11-28T16:02:20.610074Z","iopub.status.idle":"2021-11-28T16:02:21.052397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}