{"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":"!pip install --no-index --no-deps /kaggle/input/pycocotools-206/wheels/*.whl\nimport pycocotools\n!pip show pycocotools","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-09T04:05:23.991875Z","iopub.execute_input":"2023-07-09T04:05:23.992610Z","iopub.status.idle":"2023-07-09T04:06:10.268153Z","shell.execute_reply.started":"2023-07-09T04:05:23.992578Z","shell.execute_reply":"2023-07-09T04:06:10.266388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nfrom collections import Counter\n\nfrom PIL import Image\nimport imageio\nimport json \nimport cv2\nimport ipywidgets as widgets\nimport IPython.display as ipd\nimport glob\nfrom pycocotools import _mask as coco_mask\nimport logging\n\nimport base64\nimport numpy as np\nimport typing as t\nimport zlib\n\nimport tensorflow as tf\nfrom keras import backend as K\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Conv2D, Conv2DTranspose, MaxPooling2D, Dropout, concatenate\n\nprint(f\"tensorflow version: {tf.__version__}\")","metadata":{"execution":{"iopub.status.busy":"2023-07-09T04:06:10.271837Z","iopub.execute_input":"2023-07-09T04:06:10.272378Z","iopub.status.idle":"2023-07-09T04:06:19.664962Z","shell.execute_reply.started":"2023-07-09T04:06:10.272331Z","shell.execute_reply":"2023-07-09T04:06:19.663764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def jaccard_coef(y_true, y_pred):\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 (intersection + 1.0) / (K.sum(y_true_f) + K.sum(y_pred_f) - intersection + 1.0)\n\n\ndef jaccard_loss(y_true, y_pred):\n    return -jaccard_coef(y_true, y_pred)\n\nwith tf.keras.utils.custom_object_scope({'jaccard_loss': jaccard_loss, 'jaccard_coef':jaccard_coef}):\n    model = tf.keras.models.load_model('/kaggle/input/deeplabv4/deeplab_model_checkpoint (3).h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_watershed(prediction, plot=False):\n    normalized_prediction = cv2.normalize(prediction, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC3)\n    ret, thresh = cv2.threshold(normalized_prediction, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n    \n    kernel = np.ones((3,3),np.uint8)\n    opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 4)\n    \n    sure_bg = cv2.dilate(opening,kernel,iterations=3)\n    \n    dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)\n    ret, sure_fg = cv2.threshold(dist_transform,0.15*dist_transform.max(),255,0)\n    \n    sure_fg = np.uint8(sure_fg)\n    unknown = cv2.subtract(sure_bg,sure_fg)\n    \n    ret3, markers = cv2.connectedComponents(sure_fg)\n    \n    markers_norm = np.int32(markers + 10)\n    markers_norm[unknown == 255] = 0\n    \n    prediction_rgb = cv2.cvtColor(prediction.astype(np.float32), cv2.COLOR_GRAY2BGR)\n    image_cv = np.uint8(prediction_rgb)\n\n    markers = cv2.watershed(image_cv, markers_norm)\n    \n    if plot:\n        fig, axs = plt.subplots(6, 1, figsize=(6, 6 * 6))\n        \n        axs[0].imshow(thresh, cmap='gray')\n        axs[0].set_title(\"Thresholded Prediction\")\n        \n        axs[1].imshow(opening, cmap='gray')\n        axs[1].set_title('Prediction after removing noise')\n            \n        axs[2].imshow(sure_bg, cmap='gray')\n        axs[2].set_title('Sure Background')\n        \n        axs[3].imshow(sure_fg, cmap='gray')\n        axs[3].set_title('Sure Foreground')\n        \n        axs[4].imshow(unknown, cmap='gray')\n        axs[4].set_title('Unknown Region')\n        \n        axs[5].imshow(markers, cmap='gray')\n        axs[5].set_title('Watershed image (instantiated image)')\n        \n        plt.show()\n    return markers\n\ndef get_binary_masks(markers, background_label=10, marker_label=-1):\n    \"\"\"\n    takes watershed markers and \n    \"\"\"\n    unique_labels = np.unique(markers)\n    \n    if background_label in unique_labels:\n        unique_labels = unique_labels[unique_labels != background_label]\n    if marker_label in unique_labels:\n        unique_labels = unique_labels[unique_labels != marker_label]\n\n    binary_masks = []\n\n    for label in unique_labels:\n        mask = np.where(markers == label, 1, 0).astype(np.uint8)\n        kernel = np.ones((3, 3), np.uint8)\n        mask = cv2.dilate(mask, kernel, iterations=4)\n        binary_masks.append(mask)\n        \n    return binary_masks\n\ndef plot_binary_masks(prediction):\n    markers = get_watershed(prediction)\n    binary_masks = get_binary_masks(markers)\n\n    fig, axs = plt.subplots(len(binary_masks), 1, figsize=(6,len(binary_masks) * 6))\n    for i, mask in enumerate(binary_masks):\n        axs[i].imshow(mask, cmap='gray')\n        axs[i].set_title(f\"Instance Segmentation {i + 1}\")\n        \ndef encode_binary_mask(mask: np.ndarray) -> str:\n    # Check input mask\n    if mask.dtype != np.bool_:\n        raise ValueError(\n            \"encode_binary_mask expects a binary mask, received dtype == %s\" %\n            mask.dtype)\n\n    mask = np.squeeze(mask)\n    if len(mask.shape) != 2:\n        raise ValueError(\n            \"encode_binary_mask expects a 2d mask, received shape == %s\" %\n            mask.shape)\n\n    # convert input mask to expected COCO API input --\n    mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)\n    mask_to_encode = mask_to_encode.astype(np.uint8)\n    mask_to_encode = np.asfortranarray(mask_to_encode)\n\n    # RLE encode mask --\n    encoded_mask = coco_mask.encode(mask_to_encode)[0][\"counts\"]\n\n    # compress and base64 encoding --\n    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n    base64_str = base64.b64encode(binary_str)\n    return base64_str\n\ndef encode_all_masks(binary_masks):\n    encoded_masks = []\n    for mask in binary_masks:\n        encoded_mask = encode_binary_mask(mask.astype(np.bool_))\n        encoded_masks.append(encoded_mask.decode('utf-8'))\n    prefixed_masks = ['0 1.0 ' + mask for mask in encoded_masks]\n    encoded_preds = ' '.join(prefixed_masks)\n    return encoded_preds\n\ndef create_submission(directory, model, plot=True):\n    file_paths = [os.path.join(directory, path) for path in os.listdir(directory)]\n    predictions = []\n    image_ids = []\n    for path in file_paths:\n        image_id = os.path.splitext(os.path.basename(path))[0]\n        image=cv2.imread(path)/255\n        resized_image = cv2.resize(image, (512, 512), interpolation=cv2.INTER_AREA)\n        if resized_image.shape[2] == 1:\n            resized_image = cv2.cvtColor(resized_image, cv2.COLOR_GRAY2BGR) \n        prediction = model.predict(np.expand_dims(resized_image, axis=0))[0]\n        markers = get_watershed(prediction, plot=False)\n        binary_masks = get_binary_masks(markers)\n        encoded_preds = encode_all_masks(binary_masks)\n        \n        image_ids.append(image_id)\n        predictions.append(encoded_preds)\n    \n    if plot:\n        fig,axs = plt.subplots(1, 3, figsize=(24, 8))\n        axs[0].imshow(resized_image)\n        axs[0].set_title(f\"Image {image_id}\")\n        \n        axs[1].imshow(prediction, cmap='gray')\n        axs[1].set_title(f\"U-net Prediction\")\n        \n        axs[2].imshow(markers, cmap='gray')\n        axs[2].set_title(\"watershed + postprocessing\")\n        \n    df = pd.DataFrame({'id': image_ids, 'height': 512, 'width':512 ,'prediction_string': predictions})\n\n    return df\n        ","metadata":{"execution":{"iopub.status.busy":"2023-07-09T04:06:25.194207Z","iopub.execute_input":"2023-07-09T04:06:25.194591Z","iopub.status.idle":"2023-07-09T04:06:25.238211Z","shell.execute_reply.started":"2023-07-09T04:06:25.194561Z","shell.execute_reply":"2023-07-09T04:06:25.236830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = create_submission(\"/kaggle/input/hubmap-hacking-the-human-vasculature/test\", model)\ndf.to_csv('submission.csv', index=False)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-09T04:06:25.244253Z","iopub.execute_input":"2023-07-09T04:06:25.244781Z","iopub.status.idle":"2023-07-09T04:06:29.821538Z","shell.execute_reply.started":"2023-07-09T04:06:25.244737Z","shell.execute_reply":"2023-07-09T04:06:29.820465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}