{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"}],"dockerImageVersionId":30579,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#           print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-15T16:54:15.952262Z","iopub.execute_input":"2023-11-15T16:54:15.952993Z","iopub.status.idle":"2023-11-15T16:54:15.957945Z","shell.execute_reply.started":"2023-11-15T16:54:15.952957Z","shell.execute_reply":"2023-11-15T16:54:15.957036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport os\nimport random\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nplt.style.use(\"ggplot\")\n%matplotlib inline\n\nimport cv2\nfrom tqdm import tqdm_notebook, tnrange\nfrom glob import glob\nfrom itertools import chain\nfrom skimage.io import imread, imshow, concatenate_images\nfrom skimage.transform import resize\n#from skimage.morphology import label\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom skimage.color import rgb2gray\nfrom tensorflow.keras import Input\nfrom tensorflow.keras.models import Model, load_model, save_model\nfrom tensorflow.keras.layers import Input, Activation, BatchNormalization, Dropout, Lambda, Conv2D, Conv2DTranspose, MaxPooling2D, concatenate\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:54:15.961676Z","iopub.execute_input":"2023-11-15T16:54:15.961955Z","iopub.status.idle":"2023-11-15T16:54:15.975351Z","shell.execute_reply.started":"2023-11-15T16:54:15.961931Z","shell.execute_reply":"2023-11-15T16:54:15.974453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\ndef visualize_images_and_labels(data_folder, num_images=3):\n    images_folder = os.path.join(data_folder, \"images\")\n    labels_folder = os.path.join(data_folder, \"labels\")\n\n    count = 0\n\n    for file_name in os.listdir(images_folder):\n        if file_name.endswith(\".tif\"):\n            image_path = os.path.join(images_folder, file_name)\n            label_path = os.path.join(labels_folder, file_name)\n\n            if os.path.exists(label_path):\n                image = Image.open(image_path)\n                label = Image.open(label_path)\n\n                plt.figure(figsize=(8, 4))\n                plt.subplot(1, 2, 1)\n               # plt.imshow(image, cmap='gray')\n                plt.imshow(image)\n                plt.title('Image')\n                plt.grid(False)  # Turn off the grid\n\n                plt.subplot(1, 2, 2)\n                plt.imshow(label)\n                plt.title('Label')\n                plt.grid(False)  # Turn off the grid\n\n                plt.show()\n\n                count += 1\n\n                if count >= num_images:\n                    return\n            \ndata_folder = \"/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense\"\nvisualize_images_and_labels(data_folder, num_images=2)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:54:15.977106Z","iopub.execute_input":"2023-11-15T16:54:15.977442Z","iopub.status.idle":"2023-11-15T16:54:17.016016Z","shell.execute_reply.started":"2023-11-15T16:54:15.977411Z","shell.execute_reply":"2023-11-15T16:54:17.015006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_folder = \"/kaggle/input/blood-vessel-segmentation/train/kidney_2\"\nvisualize_images_and_labels(data_folder, num_images=2)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:54:17.017244Z","iopub.execute_input":"2023-11-15T16:54:17.017550Z","iopub.status.idle":"2023-11-15T16:54:18.134975Z","shell.execute_reply.started":"2023-11-15T16:54:17.017524Z","shell.execute_reply":"2023-11-15T16:54:18.134096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_folder ='/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse'\nvisualize_images_and_labels(data_folder, num_images=2)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:54:18.136907Z","iopub.execute_input":"2023-11-15T16:54:18.137224Z","iopub.status.idle":"2023-11-15T16:54:19.530479Z","shell.execute_reply.started":"2023-11-15T16:54:18.137197Z","shell.execute_reply":"2023-11-15T16:54:19.529518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for data load\nimport os\n\n# for reading and processing images\nimport imageio\nfrom PIL import Image\nimport tifffile\n# !pip install imagecodecs\nimport imagecodecs\nimport cv2\n\n# for visualizations\nimport matplotlib.pyplot as plt\n\nimport numpy as np # for using np arrays\nfrom numpy import asarray\n\n# for bulding and running deep learning model\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Input\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Dropout \nfrom tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras.layers import Conv2DTranspose\nfrom tensorflow.keras.layers import concatenate\nfrom tensorflow.keras.losses import binary_crossentropy\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:54:19.531667Z","iopub.execute_input":"2023-11-15T16:54:19.531958Z","iopub.status.idle":"2023-11-15T16:54:19.539057Z","shell.execute_reply.started":"2023-11-15T16:54:19.531928Z","shell.execute_reply":"2023-11-15T16:54:19.538132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, BatchNormalization, Activation, Concatenate\nfrom sklearn.model_selection import train_test_split\nimport tifffile\n\n   \ndef preprocess_image(data_folder):\n    # Load the image using tifffile\n    image = tifffile.imread(data_folder)\n    \n    # If the image has more than one channel, extract just one channel\n    if image.ndim > 2 and image.shape[2] > 1:\n        image = image[..., 0]\n    \n    # Normalize the image to [0, 1] range\n    image = image / 255.0\n    \n    # Convert image to a TensorFlow tensor\n    image_tensor = tf.convert_to_tensor(image, dtype=tf.float32)\n    \n    # Add a channel dimension if it does not exist\n    if image_tensor.ndim == 2:\n        image_tensor = image_tensor[..., tf.newaxis]\n    \n    # Ensure image tensor is 3D at this point\n    if image_tensor.ndim != 3:\n        raise ValueError('Image tensor must be 3 dimensions [height, width, channels]')\n    \n    # Resize the image to the desired size\n    image_tensor = tf.image.resize(image_tensor, [256, 256])\n    \n    return image_tensor\n\ndef preprocess_mask(path):\n    # Load the mask using tifffile\n    mask = tifffile.imread(path)\n    \n    # If the mask has more than one channel, extract just one channel\n    if mask.ndim > 2 and mask.shape[2] > 1:\n        mask = mask[..., 0]\n    \n    # Normalize the mask to be in [0, 1]\n    mask = mask / 255.0 if mask.max() > 1 else mask\n    \n    # Convert mask to a TensorFlow tensor\n    mask_tensor = tf.convert_to_tensor(mask, dtype=tf.float32)\n    \n    # Add a channel dimension if it does not exist\n    if mask_tensor.ndim == 2:\n        mask_tensor = mask_tensor[..., tf.newaxis]\n    \n    # Ensure mask tensor is 3D at this point\n    if mask_tensor.ndim != 3:\n        raise ValueError('Mask tensor must be 3 dimensions [height, width, channels]')\n    \n    # Resize the mask to the desired size\n    mask_tensor = tf.image.resize(mask_tensor, [256, 256], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)\n    \n    # The resize operation could push the values away from 0 and 1, we threshold to ensure it's a proper mask\n    mask_tensor = tf.where(mask_tensor > 0.5, 1, 0)\n    \n    return mask_tensor\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:54:19.540340Z","iopub.execute_input":"2023-11-15T16:54:19.540681Z","iopub.status.idle":"2023-11-15T16:54:19.555919Z","shell.execute_reply.started":"2023-11-15T16:54:19.540657Z","shell.execute_reply":"2023-11-15T16:54:19.555051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# Define the paths to your image and label folders\nimages_folder = '/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/images'\nlabels_folder = '/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/labels'\n\n# Get all files in the image and label folders\nimage_files = [os.path.join(images_folder, f) for f in os.listdir(images_folder) if f.endswith('.tif')]\nlabel_files = [os.path.join(labels_folder, f) for f in os.listdir(labels_folder) if f.endswith('.tif')]\n\n# Subset 10% of the dataset for quick experiments\nsubset_size = int(0.9 * len(image_files))\nimage_files_subset = image_files[:subset_size]\nlabel_files_subset = label_files[:subset_size]\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:54:19.556964Z","iopub.execute_input":"2023-11-15T16:54:19.557278Z","iopub.status.idle":"2023-11-15T16:54:19.581988Z","shell.execute_reply.started":"2023-11-15T16:54:19.557253Z","shell.execute_reply":"2023-11-15T16:54:19.581335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array([preprocess_image(f) for f in image_files_subset])\nmasks = np.array([preprocess_mask(f) for f in label_files_subset])\n\n# Split into train and validation sets\nX_train, X_val, y_train, y_val = train_test_split(images, masks, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:54:19.582944Z","iopub.execute_input":"2023-11-15T16:54:19.583210Z","iopub.status.idle":"2023-11-15T16:55:19.786927Z","shell.execute_reply.started":"2023-11-15T16:54:19.583187Z","shell.execute_reply":"2023-11-15T16:55:19.786118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape\n","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:55:19.790022Z","iopub.execute_input":"2023-11-15T16:55:19.790330Z","iopub.status.idle":"2023-11-15T16:55:19.796520Z","shell.execute_reply.started":"2023-11-15T16:55:19.790305Z","shell.execute_reply":"2023-11-15T16:55:19.795605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:55:19.797614Z","iopub.execute_input":"2023-11-15T16:55:19.797858Z","iopub.status.idle":"2023-11-15T16:55:19.812588Z","shell.execute_reply.started":"2023-11-15T16:55:19.797836Z","shell.execute_reply":"2023-11-15T16:55:19.811710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Building the  Model\ndef encoder_block(input_tensor, n_filters, kernel_size=3, batchnorm=True):\n    # first layer\n    x = Conv2D(filters=n_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer=\"he_normal\",\n               padding=\"same\")(input_tensor)\n    if batchnorm:\n        x = BatchNormalization()(x)\n    x = Activation(\"relu\")(x)\n    # second layer\n    x = Conv2D(filters=n_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer=\"he_normal\",\n               padding=\"same\")(x)\n    if batchnorm:\n        x = BatchNormalization()(x)\n    x = Activation(\"relu\")(x)\n    return x\n\ndef decoder_block(input_tensor, skip_tensor, n_filters, kernel_size=3, batchnorm=True):\n    x = UpSampling2D(size=(2, 2))(input_tensor)\n    x = Concatenate()([x, skip_tensor])\n    x = Conv2D(filters=n_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer=\"he_normal\",\n               padding=\"same\")(x)\n    if batchnorm:\n        x = BatchNormalization()(x)\n    x = Activation(\"relu\")(x)\n    x = Conv2D(filters=n_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer=\"he_normal\",\n               padding=\"same\")(x)\n    if batchnorm:\n        x = BatchNormalization()(x)\n    x = Activation(\"relu\")(x)\n    return x\n\ndef get_segnet_model(input_img, n_filters=16, n_classes=1, dropout=0.1, batchnorm=True):\n    # Contracting Path (encoder)\n    c1 = encoder_block(input_img, n_filters * 1, kernel_size=3, batchnorm=batchnorm)\n    p1 = MaxPooling2D((2, 2))(c1)\n    \n    c2 = encoder_block(p1, n_filters * 2, kernel_size=3, batchnorm=batchnorm)\n    p2 = MaxPooling2D((2, 2))(c2)\n    \n    c3 = encoder_block(p2, n_filters * 4, kernel_size=3, batchnorm=batchnorm)\n    p3 = MaxPooling2D((2, 2))(c3)\n    \n    c4 = encoder_block(p3, n_filters * 8, kernel_size=3, batchnorm=batchnorm)\n    p4 = MaxPooling2D((2, 2))(c4)\n    \n    # Expanding Path (decoder)\n    u6 = decoder_block(c4, c3, n_filters * 4, kernel_size=3, batchnorm=batchnorm)\n    u7 = decoder_block(u6, c2, n_filters * 2, kernel_size=3, batchnorm=batchnorm)\n    u8 = decoder_block(u7, c1, n_filters * 1, kernel_size=3, batchnorm=batchnorm)\n    \n    # Output layer\n    output_img = Conv2D(n_classes, (1, 1), activation='sigmoid')(u8)\n    \n    return Model(inputs=input_img, outputs=output_img)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:55:19.813700Z","iopub.execute_input":"2023-11-15T16:55:19.813970Z","iopub.status.idle":"2023-11-15T16:55:19.830426Z","shell.execute_reply.started":"2023-11-15T16:55:19.813947Z","shell.execute_reply":"2023-11-15T16:55:19.829588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K\nfrom keras.losses import binary_crossentropy\nimport tensorflow as tf\n\ndef dice_coef(y_true, y_pred, smooth=1):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\ndef iou_coef(y_true, y_pred, smooth=1):\n  intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])\n  union = K.sum(y_true,[1,2,3])+K.sum(y_pred,[1,2,3])-intersection\n  iou = K.mean((intersection + smooth) / (union + smooth), axis=0)\n  return iou\n\ndef dice_loss(y_true, y_pred):\n    smooth = 1.\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = y_true_f * y_pred_f\n    score = (2. * K.sum(intersection) + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n    return 1. - score\n\ndef bce_dice_loss(y_true, y_pred):\n    return binary_crossentropy(tf.cast(y_true, tf.float32), y_pred) + 0.5 * dice_loss(tf.cast(y_true, tf.float32), y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:55:19.831705Z","iopub.execute_input":"2023-11-15T16:55:19.832056Z","iopub.status.idle":"2023-11-15T16:55:19.846523Z","shell.execute_reply.started":"2023-11-15T16:55:19.832025Z","shell.execute_reply":"2023-11-15T16:55:19.845791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adjust the input shape to match your dataset (height, width, channels)\ninput_img = Input((256, 256, 1), name='img')\n\nmodel = get_segnet_model(input_img)\n\nmodel.compile(optimizer='adam', loss=bce_dice_loss,metrics=[dice_coef,iou_coef])\nmodel.summary()\n\n# Train the model\n\nresults = model.fit(X_train, y_train, batch_size=32, epochs=30,validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2023-11-15T16:55:19.847616Z","iopub.execute_input":"2023-11-15T16:55:19.847896Z","iopub.status.idle":"2023-11-15T17:00:22.096351Z","shell.execute_reply.started":"2023-11-15T16:55:19.847873Z","shell.execute_reply":"2023-11-15T17:00:22.095460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Extract the history from the results\nhistory = results.history\n\n# Plotting Training and Validation Loss\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history['loss'], label='Train Loss')\nplt.plot(history['val_loss'], label='Validation Loss')\nplt.title('Loss Over Epochs')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\n# Plotting Training and Validation Dice Coefficient\nplt.subplot(1, 2, 2)\nplt.plot(history['dice_coef'], label='Train Dice Coefficient')\nplt.plot(history['val_dice_coef'], label='Validation Dice Coefficient')\nplt.title('Dice Coefficient Over Epochs')\nplt.xlabel('Epoch')\nplt.ylabel('Dice Coefficient')\nplt.legend()\n\nplt.show()\n\n# Plotting Training and Validation IoU Coefficient\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history['iou_coef'], label='Train IoU Coefficient')\nplt.plot(history['val_iou_coef'], label='Validation IoU Coefficient')\nplt.title('IoU Coefficient Over Epochs')\nplt.xlabel('Epoch')\nplt.ylabel('IoU Coefficient')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:00:22.097880Z","iopub.execute_input":"2023-11-15T17:00:22.098216Z","iopub.status.idle":"2023-11-15T17:00:22.769782Z","shell.execute_reply.started":"2023-11-15T17:00:22.098188Z","shell.execute_reply":"2023-11-15T17:00:22.768837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\n# Assuming X_val and y_val are your validation images and masks\nnum_samples = 3  # Choose the number of samples you want to display\nsample_indices = random.sample(range(len(X_val)), num_samples)\n\nsample_images = X_val[sample_indices]\nsample_true_masks = y_val[sample_indices]\n\n\n#................\n\nsample_pred_masks = model.predict(sample_images)\n\n# Thresholding example (adjust threshold as needed)\nsample_pred_masks = (sample_pred_masks > 0.5).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:01:42.977056Z","iopub.execute_input":"2023-11-15T17:01:42.977439Z","iopub.status.idle":"2023-11-15T17:01:43.043217Z","shell.execute_reply.started":"2023-11-15T17:01:42.977407Z","shell.execute_reply":"2023-11-15T17:01:43.042427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(num_samples):\n    plt.figure(figsize=(12, 5))\n\n    # Display original image\n    plt.subplot(1, 3, 1)\n    plt.imshow(sample_images[i])\n    plt.title('Original Image')\n    plt.axis('off')\n\n    # Display true mask\n    plt.subplot(1, 3, 2)\n    plt.imshow(sample_true_masks[i])\n    plt.title('True Mask')\n    plt.axis('off')\n\n    # Display predicted mask\n    plt.subplot(1, 3, 3)\n    plt.imshow(sample_pred_masks[i])\n    plt.title('Predicted Mask')\n    plt.axis('off')\n\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:01:47.364262Z","iopub.execute_input":"2023-11-15T17:01:47.364629Z","iopub.status.idle":"2023-11-15T17:01:48.396889Z","shell.execute_reply.started":"2023-11-15T17:01:47.364596Z","shell.execute_reply":"2023-11-15T17:01:48.395981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_base_path = '/kaggle/input/blood-vessel-segmentation/test'\ntest_image_files = []\n\nfor dataset in os.listdir(test_base_path):\n    images_path = os.path.join(test_base_path, dataset, 'images')\n    if os.path.isdir(images_path):\n        test_image_files += sorted([os.path.join(images_path, f) for f in os.listdir(images_path) if f.endswith('.tif')])\n\n# Preprocess test images (similar to training images preprocessing)\ntest_images = np.array([preprocess_image(f) for f in test_image_files])","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:00:23.681401Z","iopub.execute_input":"2023-11-15T17:00:23.681745Z","iopub.status.idle":"2023-11-15T17:00:23.753160Z","shell.execute_reply.started":"2023-11-15T17:00:23.681708Z","shell.execute_reply":"2023-11-15T17:00:23.752394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict masks\npredicted_masks = model.predict(test_images)\n\n# Post-process masks: threshold and resize to original size\nthresholded_masks = (predicted_masks > 0.5).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:00:23.754238Z","iopub.execute_input":"2023-11-15T17:00:23.754531Z","iopub.status.idle":"2023-11-15T17:00:23.823909Z","shell.execute_reply.started":"2023-11-15T17:00:23.754505Z","shell.execute_reply":"2023-11-15T17:00:23.822962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(img):\n  \n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    rle = ' '.join(str(x) for x in runs)\n    if rle=='':\n        rle = '1 1'\n    return rle","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:00:23.825080Z","iopub.execute_input":"2023-11-15T17:00:23.825427Z","iopub.status.idle":"2023-11-15T17:00:23.832476Z","shell.execute_reply.started":"2023-11-15T17:00:23.825400Z","shell.execute_reply":"2023-11-15T17:00:23.831459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\nrles = [rle_encode(mask.reshape(256, 256)) for mask in thresholded_masks]\nids = [f'{p.split(\"/\")[-3]}_{os.path.basename(p).split(\".\")[0]}' for p in test_image_files]\n\nsubmission = pd.DataFrame({\n    \"id\": ids,\n    \"rle\": rles\n})","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:00:23.833684Z","iopub.execute_input":"2023-11-15T17:00:23.833950Z","iopub.status.idle":"2023-11-15T17:00:23.845617Z","shell.execute_reply.started":"2023-11-15T17:00:23.833926Z","shell.execute_reply":"2023-11-15T17:00:23.844722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:00:23.846716Z","iopub.execute_input":"2023-11-15T17:00:23.847048Z","iopub.status.idle":"2023-11-15T17:00:23.858804Z","shell.execute_reply.started":"2023-11-15T17:00:23.847016Z","shell.execute_reply":"2023-11-15T17:00:23.857877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:00:23.859805Z","iopub.execute_input":"2023-11-15T17:00:23.860073Z","iopub.status.idle":"2023-11-15T17:00:23.868429Z","shell.execute_reply.started":"2023-11-15T17:00:23.860050Z","shell.execute_reply":"2023-11-15T17:00:23.867592Z"},"trusted":true},"execution_count":null,"outputs":[]}]}