{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport tensorflow\nimport pandas as pd\nimport albumentations\nimport cv2\n\nimport keras\nimport tensorflow as tf\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import *\nfrom tensorflow.keras.callbacks import ModelCheckpoint","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2023-10-13T12:11:09.524416Z","iopub.status.busy":"2023-10-13T12:11:09.524067Z","iopub.status.idle":"2023-10-13T12:11:17.514516Z","shell.execute_reply":"2023-10-13T12:11:17.513606Z","shell.execute_reply.started":"2023-10-13T12:11:09.524390Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tf.__version__)\nprint(keras.__version__)\n!nvidia-smi","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:17.516896Z","iopub.status.busy":"2023-10-13T12:11:17.516339Z","iopub.status.idle":"2023-10-13T12:11:18.533539Z","shell.execute_reply":"2023-10-13T12:11:18.532443Z","shell.execute_reply.started":"2023-10-13T12:11:17.516864Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Metrics","metadata":{}},{"cell_type":"code","source":"#intersection over union\ndef iou(y_true, y_pred, smooth = 0.5):\n    intersection = K.sum(y_true * y_pred)\n    sum_ = K.sum(y_true + y_pred)\n    jac = (intersection + smooth) / (sum_ - intersection + smooth)\n    return jac\n\n\n# Computing Sensitivity      \ndef sensitivity(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    return true_positives / (possible_positives + K.epsilon())\n\n# Computing Specificity\ndef specificity(y_true, y_pred):\n    true_negatives = K.sum(K.round(K.clip((1-y_true) * (1-y_pred), 0, 1)))\n    possible_negatives = K.sum(K.round(K.clip(1-y_true, 0, 1)))\n    return true_negatives / (possible_negatives + K.epsilon())\n\ndef f1_score(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    recall = true_positives / (possible_positives + K.epsilon())\n    f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())\n    return f1_val\n\n# Define Dice coefficient metric\ndef dice_coefficient(y_true, y_pred, epsilon=1e-6):\n    y_true_flat = tf.reshape(y_true, [-1])\n    y_pred_flat = tf.reshape(y_pred, [-1])\n    intersection = tf.reduce_sum(y_true_flat * y_pred_flat)\n    dice_score = (2.0 * intersection + epsilon) / (tf.reduce_sum(y_true_flat) + tf.reduce_sum(y_pred_flat) + epsilon)\n    return dice_score\n\n","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:18.535516Z","iopub.status.busy":"2023-10-13T12:11:18.535149Z","iopub.status.idle":"2023-10-13T12:11:18.546689Z","shell.execute_reply":"2023-10-13T12:11:18.545731Z","shell.execute_reply.started":"2023-10-13T12:11:18.535485Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CallBacks","metadata":{}},{"cell_type":"code","source":"epochs = 10\n\nearlystop = keras.callbacks.EarlyStopping(monitor='val_loss',\n                          min_delta=0,\n                          patience=epochs,\n                          verbose=1,\n                          restore_best_weights=True\n                          )\n\nreduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss',\n                              factor=0.2,\n                              patience=5,\n                              verbose=1,\n                              min_delta=0.0001)\ncsv_logger = keras.callbacks.CSVLogger('Model.log')\n\n\ncallbacks = [earlystop, csv_logger, reduce_lr]","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:18.550193Z","iopub.status.busy":"2023-10-13T12:11:18.549031Z","iopub.status.idle":"2023-10-13T12:11:18.556328Z","shell.execute_reply":"2023-10-13T12:11:18.555466Z","shell.execute_reply.started":"2023-10-13T12:11:18.550163Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prefix = '/kaggle/input/covid-segmentation/'\n\nimages_radiopedia = np.load(os.path.join(prefix, 'images_radiopedia.npy')).astype(np.float32)\nmasks_radiopedia = np.load(os.path.join(prefix, 'masks_radiopedia.npy')).astype(np.int8)\nimages_medseg = np.load(os.path.join(prefix, 'images_medseg.npy')).astype(np.float32)\nmasks_medseg = np.load(os.path.join(prefix, 'masks_medseg.npy')).astype(np.int8)\n\ntest_images_medseg = np.load(os.path.join(prefix, 'test_images_medseg.npy')).astype(np.float32)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:18.558255Z","iopub.status.busy":"2023-10-13T12:11:18.557795Z","iopub.status.idle":"2023-10-13T12:11:33.635476Z","shell.execute_reply":"2023-10-13T12:11:33.634463Z","shell.execute_reply.started":"2023-10-13T12:11:18.558225Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize(image_batch, mask_batch=None, pred_batch=None, num_samples = 8):\n    num_classes = mask_batch.shape[-1] if mask_batch is not None else 0\n    fix, ax = plt.subplots(num_classes + 1, num_samples, figsize=(num_samples * 2, (num_classes + 1) * 2))\n\n    for i in range(num_samples):\n        ax_image = ax[0, i] if num_classes > 0 else ax[i]\n        ax_image.imshow(image_batch[i,:,:,0], cmap='Greys')\n        ax_image.set_xticks([]) \n        ax_image.set_yticks([])\n        \n        if mask_batch is not None:\n            for j in range(num_classes):\n                if pred_batch is None:\n                    mask_to_show = mask_batch[i,:,:,j]\n                else:\n                    mask_to_show = np.zeros(shape=(*mask_batch.shape[1:-1], 3)) \n                    mask_to_show[..., 0] = pred_batch[i,:,:,j] > 0.5\n                    mask_to_show[..., 1] = mask_batch[i,:,:,j]\n                ax[j + 1, i].imshow(mask_to_show, vmin=0, vmax=1)\n                ax[j + 1, i].set_xticks([]) \n                ax[j + 1, i].set_yticks([]) \n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:33.637467Z","iopub.status.busy":"2023-10-13T12:11:33.636914Z","iopub.status.idle":"2023-10-13T12:11:33.646052Z","shell.execute_reply":"2023-10-13T12:11:33.645283Z","shell.execute_reply.started":"2023-10-13T12:11:33.637434Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(images_radiopedia[30:], masks_radiopedia[30:])","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:33.647849Z","iopub.status.busy":"2023-10-13T12:11:33.647530Z","iopub.status.idle":"2023-10-13T12:11:36.142502Z","shell.execute_reply":"2023-10-13T12:11:36.141673Z","shell.execute_reply.started":"2023-10-13T12:11:33.647820Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(images_medseg, masks_medseg)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:36.143680Z","iopub.status.busy":"2023-10-13T12:11:36.143406Z","iopub.status.idle":"2023-10-13T12:11:38.508683Z","shell.execute_reply":"2023-10-13T12:11:38.507839Z","shell.execute_reply.started":"2023-10-13T12:11:36.143655Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(test_images_medseg)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:38.510765Z","iopub.status.busy":"2023-10-13T12:11:38.510189Z","iopub.status.idle":"2023-10-13T12:11:39.257051Z","shell.execute_reply":"2023-10-13T12:11:39.256169Z","shell.execute_reply.started":"2023-10-13T12:11:38.510736Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_hists(images1, images2=None):\n    plt.hist(images1.ravel(), bins=100, density=True, color='b', alpha=1 if images2 is None else 0.5)\n    if images2 is not None:\n        plt.hist(images2.ravel(), bins=100, density=True, alpha=0.5, color='orange')\n    plt.show();","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:39.264391Z","iopub.status.busy":"2023-10-13T12:11:39.261036Z","iopub.status.idle":"2023-10-13T12:11:39.273858Z","shell.execute_reply":"2023-10-13T12:11:39.272894Z","shell.execute_reply.started":"2023-10-13T12:11:39.264357Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hists(images_radiopedia, images_medseg)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:39.279248Z","iopub.status.busy":"2023-10-13T12:11:39.278443Z","iopub.status.idle":"2023-10-13T12:11:43.077030Z","shell.execute_reply":"2023-10-13T12:11:43.076177Z","shell.execute_reply.started":"2023-10-13T12:11:39.279216Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hists(test_images_medseg, images_medseg)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:43.078962Z","iopub.status.busy":"2023-10-13T12:11:43.078415Z","iopub.status.idle":"2023-10-13T12:11:43.833147Z","shell.execute_reply":"2023-10-13T12:11:43.832261Z","shell.execute_reply.started":"2023-10-13T12:11:43.078931Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_images(images_arr, mean_std=None):\n    images_arr[images_arr > 500] = 500\n    images_arr[images_arr < -1500] = -1500\n    min_perc, max_perc = np.percentile(images_arr, 5), np.percentile(images_arr, 95)\n    images_arr_valid = images_arr[(images_arr > min_perc) & (images_arr < max_perc)]\n    mean, std = (images_arr_valid.mean(), images_arr_valid.std()) if mean_std is None else mean_std\n    images_arr = (images_arr - mean) / std\n    print(f'mean {mean}, std {std}')\n    return images_arr, (mean, std)\n\nimages_radiopedia, mean_std = preprocess_images(images_radiopedia)\nimages_medseg, _ = preprocess_images(images_medseg, mean_std)\ntest_images_medseg, _ = preprocess_images(test_images_medseg, mean_std)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:43.835069Z","iopub.status.busy":"2023-10-13T12:11:43.834533Z","iopub.status.idle":"2023-10-13T12:11:50.770435Z","shell.execute_reply":"2023-10-13T12:11:50.769333Z","shell.execute_reply.started":"2023-10-13T12:11:43.835037Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hists(images_radiopedia, images_medseg)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:50.772266Z","iopub.status.busy":"2023-10-13T12:11:50.771911Z","iopub.status.idle":"2023-10-13T12:11:54.121940Z","shell.execute_reply":"2023-10-13T12:11:54.121080Z","shell.execute_reply.started":"2023-10-13T12:11:50.772236Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hists(test_images_medseg, images_medseg)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:54.123832Z","iopub.status.busy":"2023-10-13T12:11:54.123498Z","iopub.status.idle":"2023-10-13T12:11:54.891231Z","shell.execute_reply":"2023-10-13T12:11:54.889994Z","shell.execute_reply.started":"2023-10-13T12:11:54.123801Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_indexes, train_indexes = list(range(24)), list(range(24, 100))\n\ntrain_images = np.concatenate((images_medseg[train_indexes], images_radiopedia))\ntrain_masks = np.concatenate((masks_medseg[train_indexes], masks_radiopedia))\nval_images = images_medseg[val_indexes]\nval_masks = masks_medseg[val_indexes]\n\nbatch_size = len(val_masks)\n","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:54.893559Z","iopub.status.busy":"2023-10-13T12:11:54.892647Z","iopub.status.idle":"2023-10-13T12:11:55.633018Z","shell.execute_reply":"2023-10-13T12:11:55.632083Z","shell.execute_reply.started":"2023-10-13T12:11:54.893526Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SOURCE_SIZE = 512\nTARGET_SIZE = 224\n\ntrain_augs = albumentations.Compose([\n    albumentations.Rotate(limit=360, p=0.9, border_mode = cv2.BORDER_REPLICATE),\n    albumentations.RandomSizedCrop((int(SOURCE_SIZE * 0.75), SOURCE_SIZE), \n                                   TARGET_SIZE, \n                                   TARGET_SIZE, \n                                   interpolation = cv2.INTER_NEAREST),\n    albumentations.HorizontalFlip(p=0.5),\n\n])\n\nval_augs = albumentations.Compose([\n    albumentations.Resize(TARGET_SIZE, TARGET_SIZE, interpolation = cv2.INTER_NEAREST)\n])","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:55.634797Z","iopub.status.busy":"2023-10-13T12:11:55.634472Z","iopub.status.idle":"2023-10-13T12:11:55.641935Z","shell.execute_reply":"2023-10-13T12:11:55.640952Z","shell.execute_reply.started":"2023-10-13T12:11:55.634768Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset:   \n    def __init__(\n            self, \n            images, \n            masks,\n            augmentations=None\n    ):\n        self.images = images\n        self.masks = masks\n        self.augmentations = augmentations\n    \n    def __getitem__(self, i):\n        image = self.images[i]\n        mask = self.masks[i]\n        \n        if self.augmentations:\n            sample = self.augmentations(image=image, mask=mask)\n            image, mask = sample['image'], sample['mask']\n        return image, mask\n        \n    def __len__(self):\n        return len(self.images)\n    \n    \nclass Dataloder(tensorflow.keras.utils.Sequence):\n    \n    def __init__(self, dataset, batch_size=1, shuffle=False):\n        self.dataset = dataset\n        self.batch_size = batch_size\n        self.shuffle = shuffle\n        self.indexes = np.arange(len(dataset))\n\n        self.on_epoch_end()\n\n    def __getitem__(self, i):\n        \n        # collect batch data\n        start = i * self.batch_size\n        stop = (i + 1) * self.batch_size\n        images = []\n        masks = []\n        for j in range(start, stop):\n            image, mask = self.dataset[self.indexes[j]]\n            images.append(image)\n            masks.append(mask)\n        \n        images = np.stack(images, axis=0)\n        masks = np.stack(masks, axis=0).astype(np.float32)\n        \n        return (images, masks)\n    \n    def __len__(self):\n        \"\"\"Denotes the number of batches per epoch\"\"\"\n        return len(self.indexes) // self.batch_size\n    \n    def on_epoch_end(self):\n        \"\"\"Callback function to shuffle indexes each epoch\"\"\"\n        if self.shuffle:\n            self.indexes = np.random.permutation(self.indexes)\n            \ntrain_dataset = Dataset(train_images, train_masks, train_augs)\nval_dataset = Dataset(val_images, val_masks, val_augs)\n\ntrain_dataloader = Dataloder(train_dataset, batch_size = batch_size, shuffle = True)\nval_dataloader = Dataloder(val_dataset, batch_size = batch_size, shuffle = False)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:55.643815Z","iopub.status.busy":"2023-10-13T12:11:55.643059Z","iopub.status.idle":"2023-10-13T12:11:55.655685Z","shell.execute_reply":"2023-10-13T12:11:55.654759Z","shell.execute_reply.started":"2023-10-13T12:11:55.643781Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_dataset)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:55.657717Z","iopub.status.busy":"2023-10-13T12:11:55.657072Z","iopub.status.idle":"2023-10-13T12:11:55.668622Z","shell.execute_reply":"2023-10-13T12:11:55.667665Z","shell.execute_reply.started":"2023-10-13T12:11:55.657688Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert train_dataloader[0][0].shape == (batch_size, TARGET_SIZE, TARGET_SIZE, 1)\nassert train_dataloader[0][1].shape == (batch_size, TARGET_SIZE, TARGET_SIZE, 4)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:55.670682Z","iopub.status.busy":"2023-10-13T12:11:55.669778Z","iopub.status.idle":"2023-10-13T12:11:55.893644Z","shell.execute_reply":"2023-10-13T12:11:55.893000Z","shell.execute_reply.started":"2023-10-13T12:11:55.670638Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(*next(iter(train_dataloader)))","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:55.895191Z","iopub.status.busy":"2023-10-13T12:11:55.894683Z","iopub.status.idle":"2023-10-13T12:11:58.083141Z","shell.execute_reply":"2023-10-13T12:11:58.082358Z","shell.execute_reply.started":"2023-10-13T12:11:55.895161Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize(*next(iter(val_dataloader)))","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:58.085072Z","iopub.status.busy":"2023-10-13T12:11:58.084364Z","iopub.status.idle":"2023-10-13T12:11:59.914017Z","shell.execute_reply":"2023-10-13T12:11:59.913223Z","shell.execute_reply.started":"2023-10-13T12:11:58.085036Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"#from keras.layers.advanced_activations import ELU\nfrom keras.layers import ELU, PReLU, LeakyReLU","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:59.915907Z","iopub.status.busy":"2023-10-13T12:11:59.915394Z","iopub.status.idle":"2023-10-13T12:11:59.920195Z","shell.execute_reply":"2023-10-13T12:11:59.919374Z","shell.execute_reply.started":"2023-10-13T12:11:59.915876Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def elu_block(inputs, filters, kernel_size=3):\n    x = tf.keras.layers.Conv2D(filters, kernel_size, padding='same')(inputs)\n    x = ELU()(x)\n    x = tf.keras.layers.Conv2D(filters, kernel_size, padding='same')(x)\n    x = ELU()(x)\n    return x","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:59.922346Z","iopub.status.busy":"2023-10-13T12:11:59.921523Z","iopub.status.idle":"2023-10-13T12:11:59.930441Z","shell.execute_reply":"2023-10-13T12:11:59.928959Z","shell.execute_reply.started":"2023-10-13T12:11:59.922233Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def elu_unet(input_shape):\n    inputs = Input(input_shape)\n    \n    conv1 = elu_block(inputs, 32)\n    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)\n    \n    conv2 = elu_block(pool1, 64)\n    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)\n    \n    conv3 = elu_block(pool2, 128)\n    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)\n\n    conv4 = elu_block(pool3, 256)\n    \n    up5 = Concatenate()([UpSampling2D(size=(2, 2))(conv4), conv3])\n    conv5 = elu_block(up5, 128)\n    \n    up6 = Concatenate()([UpSampling2D(size=(2, 2))(conv5), conv2])\n    conv6 = elu_block(up6, 64)\n    \n    up7 = Concatenate()([UpSampling2D(size=(2, 2))(conv6), conv1])\n    conv7 = elu_block(up7, 32)\n    \n    outputs = Conv2D(4, 1, activation='softmax')(conv7)\n    \n    model = Model(inputs=inputs, outputs=outputs)\n    return model","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:59.932894Z","iopub.status.busy":"2023-10-13T12:11:59.932191Z","iopub.status.idle":"2023-10-13T12:11:59.942221Z","shell.execute_reply":"2023-10-13T12:11:59.941255Z","shell.execute_reply.started":"2023-10-13T12:11:59.932864Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import Input, Model","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:59.944350Z","iopub.status.busy":"2023-10-13T12:11:59.943763Z","iopub.status.idle":"2023-10-13T12:11:59.951475Z","shell.execute_reply":"2023-10-13T12:11:59.950448Z","shell.execute_reply.started":"2023-10-13T12:11:59.944321Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (TARGET_SIZE, TARGET_SIZE, 1)  # Adjust the number of channels according to the data\n\nmodel = elu_unet(input_shape)\nmodel.compile(loss = \"categorical_crossentropy\",\n            optimizer = tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07), \n              \n            metrics = (['accuracy', tf.keras.metrics.Precision(name='precision'),\n                        tf.keras.metrics.Recall(name='recall'),\n                        tf.keras.metrics.AUC(name='AUC'),\n                        f1_score, specificity, sensitivity, iou, dice_coefficient]))\n\nprint(model.input_shape)\nprint(model.output_shape)\nmodel.summary()","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:11:59.953457Z","iopub.status.busy":"2023-10-13T12:11:59.953091Z","iopub.status.idle":"2023-10-13T12:12:03.205607Z","shell.execute_reply":"2023-10-13T12:12:03.204810Z","shell.execute_reply.started":"2023-10-13T12:11:59.953429Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model, \n           show_shapes = True,\n           show_dtype=False,\n           show_layer_names = True, \n           rankdir = 'TB', \n           expand_nested = False, \n           dpi = 70)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:12:03.210781Z","iopub.status.busy":"2023-10-13T12:12:03.210472Z","iopub.status.idle":"2023-10-13T12:12:03.551875Z","shell.execute_reply":"2023-10-13T12:12:03.551079Z","shell.execute_reply.started":"2023-10-13T12:12:03.210751Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train","metadata":{}},{"cell_type":"code","source":"%%time\nhistory = model.fit(\n    train_dataloader,\n    steps_per_epoch=len(train_dataloader) * 6,\n    epochs = 10,\n    validation_data = val_dataloader,\n    validation_steps = len(val_dataloader),\n    callbacks=callbacks,\n    workers = 4)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:12:03.553635Z","iopub.status.busy":"2023-10-13T12:12:03.553190Z","iopub.status.idle":"2023-10-13T12:21:57.244972Z","shell.execute_reply":"2023-10-13T12:21:57.243804Z","shell.execute_reply.started":"2023-10-13T12:12:03.553607Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"Model.h5\")\nprint('Model save to Disk')","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:21:57.248044Z","iopub.status.busy":"2023-10-13T12:21:57.247457Z","iopub.status.idle":"2023-10-13T12:21:57.381092Z","shell.execute_reply":"2023-10-13T12:21:57.380190Z","shell.execute_reply.started":"2023-10-13T12:21:57.248010Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Results","metadata":{}},{"cell_type":"code","source":"import pandas as pd\ndf = pd.DataFrame(history.history) \ndf.to_excel(\"output.xlsx\")\n#df.to_csv(\"output1.csv\")\ndf.head(10)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:21:57.382958Z","iopub.status.busy":"2023-10-13T12:21:57.382580Z","iopub.status.idle":"2023-10-13T12:21:57.663645Z","shell.execute_reply":"2023-10-13T12:21:57.662724Z","shell.execute_reply.started":"2023-10-13T12:21:57.382926Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Interpreting the Metrics\nfig, ax = plt.subplots(5, 2, figsize=(15, 30))\nax = ax.ravel()\n\nfor i, met in enumerate(['accuracy', 'loss', 'precision', 'recall', 'AUC', 'f1_score','sensitivity', 'specificity','iou', 'dice_coefficient']):\n    ax[i].plot(history.history[met])\n    ax[i].plot(history.history['val_' + met])\n    ax[i].set_title('Model {}'.format(met))\n    ax[i].set_xlabel('epochs')\n    ax[i].set_ylabel(met)\n    ax[i].legend(['train', 'val'])\nplt.savefig(\"Model-Results.png\")","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:22:09.985526Z","iopub.status.busy":"2023-10-13T12:22:09.984950Z","iopub.status.idle":"2023-10-13T12:22:12.434239Z","shell.execute_reply":"2023-10-13T12:22:12.433398Z","shell.execute_reply.started":"2023-10-13T12:22:09.985493Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test model and inference Time","metadata":{}},{"cell_type":"code","source":"%%time\n# Evaluate the model on the test data using `evaluate`\nprint(\"Evaluate on test data\")\nresults = model.evaluate(val_dataloader, batch_size=16, callbacks= callbacks)\nprint(\"test loss, test acc:\", results)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:23:01.425483Z","iopub.status.busy":"2023-10-13T12:23:01.425155Z","iopub.status.idle":"2023-10-13T12:23:01.634811Z","shell.execute_reply":"2023-10-13T12:23:01.633840Z","shell.execute_reply.started":"2023-10-13T12:23:01.425457Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmodel = tf.keras.models.load_model('Model.h5',\n                                           compile=False,\n                                           custom_objects={\n                                                'Accuracy': 'accuracy', \n                                                'Precision': tf.keras.metrics.Precision(), \n                                                'Recall': tf.keras.metrics.Recall(),\n                                                'F1_Score': f1_score,\n                                                'Dice': dice_coefficient})\n\nmodel.compile(Adam(learning_rate=0.001, amsgrad=True),\n              loss = \"categorical_crossentropy\")","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:28:35.747827Z","iopub.status.busy":"2023-10-13T12:28:35.747499Z","iopub.status.idle":"2023-10-13T12:28:35.986991Z","shell.execute_reply":"2023-10-13T12:28:35.985992Z","shell.execute_reply.started":"2023-10-13T12:28:35.747801Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ninput = val_dataloader[0]\nimage_batch, mask_batch = input\n\npreds = model.predict_on_batch(image_batch)\nvisualize(image_batch, mask_batch, pred_batch=preds)","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:28:51.255223Z","iopub.status.busy":"2023-10-13T12:28:51.254862Z","iopub.status.idle":"2023-10-13T12:28:53.342923Z","shell.execute_reply":"2023-10-13T12:28:53.342168Z","shell.execute_reply.started":"2023-10-13T12:28:51.255194Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nimage_batch = np.stack([val_augs(image=img)['image'] for img in test_images_medseg], axis=0)\ntest_preds = model.predict_on_batch(image_batch)\ntest_masks_prediction = test_preds > 0.5\nvisualize(image_batch, test_masks_prediction, num_samples=len(test_images_medseg))","metadata":{"execution":{"iopub.execute_input":"2023-10-13T12:29:14.525322Z","iopub.status.busy":"2023-10-13T12:29:14.524880Z","iopub.status.idle":"2023-10-13T12:29:17.996836Z","shell.execute_reply":"2023-10-13T12:29:17.996091Z","shell.execute_reply.started":"2023-10-13T12:29:14.525285Z"}},"execution_count":null,"outputs":[]}]}