{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":7497215,"sourceType":"datasetVersion","datasetId":4365499},{"sourceId":7578995,"sourceType":"datasetVersion","datasetId":4411931}],"dockerImageVersionId":30636,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"! ls ../input/keras-unet-collection","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:24:47.866008Z","iopub.execute_input":"2024-02-08T11:24:47.866878Z","iopub.status.idle":"2024-02-08T11:24:48.869136Z","shell.execute_reply.started":"2024-02-08T11:24:47.866842Z","shell.execute_reply":"2024-02-08T11:24:48.867892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install keras-unet-collection --no-index --find-links=file:///kaggle/input/keras-unet-collection/keras_unet_collection/","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:24:48.871233Z","iopub.execute_input":"2024-02-08T11:24:48.87158Z","iopub.status.idle":"2024-02-08T11:25:02.405785Z","shell.execute_reply.started":"2024-02-08T11:24:48.87155Z","shell.execute_reply":"2024-02-08T11:25:02.404757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport tensorflow as tf\nfrom tensorflow.keras.utils import Sequence, split_dataset\nfrom tensorflow.keras.preprocessing import sequence\nfrom sklearn.model_selection import train_test_split\nfrom typing import Callable\nfrom keras.optimizers import Adam\nfrom keras_unet_collection import models, losses\nfrom datetime import datetime","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:25:02.407736Z","iopub.execute_input":"2024-02-08T11:25:02.408076Z","iopub.status.idle":"2024-02-08T11:25:16.27342Z","shell.execute_reply.started":"2024-02-08T11:25:02.408048Z","shell.execute_reply":"2024-02-08T11:25:16.27259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LOAD IMAGES AND MASK\n### Create a dataframe using pandas which is contain image path and mask path\n\n### Preprocessing images and mask dataset in this below link\n\n[https://www.kaggle.com/code/greedyfornothing/sennet-hoa-preprocessing-images-and-mask/notebook](http://)","metadata":{}},{"cell_type":"code","source":"train_dir = \"/kaggle/input/blood-vessel-segmentation/train/\"\nimages = []\nlabels = []\n\nfor root, dirs, files in os.walk(train_dir):\n    for file in files:\n        if file.endswith(\".tif\") and \"kidney_3_dense\" not in root:\n            if \"images\" in root:\n                images.append(os.path.join(root, file))\n            elif \"labels\" in root:\n                labels.append(os.path.join(root, file))\nimages.sort()\nlabels.sort()\n\nprint(\"Images:\")\nprint(len(images))\n\nprint(\"\\nLabels:\")\nprint(len(labels))","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:25:16.274704Z","iopub.execute_input":"2024-02-08T11:25:16.275253Z","iopub.status.idle":"2024-02-08T11:25:31.566984Z","shell.execute_reply.started":"2024-02-08T11:25:16.275222Z","shell.execute_reply":"2024-02-08T11:25:31.565948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(images[6000])\nprint(labels[6000])","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:25:31.569971Z","iopub.execute_input":"2024-02-08T11:25:31.570682Z","iopub.status.idle":"2024-02-08T11:25:31.575372Z","shell.execute_reply.started":"2024-02-08T11:25:31.570642Z","shell.execute_reply":"2024-02-08T11:25:31.574423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(images)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:25:31.576497Z","iopub.execute_input":"2024-02-08T11:25:31.576788Z","iopub.status.idle":"2024-02-08T11:25:31.588901Z","shell.execute_reply.started":"2024-02-08T11:25:31.576763Z","shell.execute_reply":"2024-02-08T11:25:31.588028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split Dataset","metadata":{}},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(images, labels, \n                                                    test_size=0.2, \n                                                    random_state=42)\n\nprint(\"training images:\", len(x_train))\nprint(\"training masks:\", len(y_train))\nprint(\"validation images:\", len(x_test))\nprint(\"validation masks:\", len(y_test))","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:25:31.589907Z","iopub.execute_input":"2024-02-08T11:25:31.590155Z","iopub.status.idle":"2024-02-08T11:25:31.609262Z","shell.execute_reply.started":"2024-02-08T11:25:31.590133Z","shell.execute_reply":"2024-02-08T11:25:31.608198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train[:5]","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:25:31.610452Z","iopub.execute_input":"2024-02-08T11:25:31.610762Z","iopub.status.idle":"2024-02-08T11:25:31.617152Z","shell.execute_reply.started":"2024-02-08T11:25:31.610736Z","shell.execute_reply":"2024-02-08T11:25:31.616243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[:5]","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:25:31.618676Z","iopub.execute_input":"2024-02-08T11:25:31.619358Z","iopub.status.idle":"2024-02-08T11:25:31.627175Z","shell.execute_reply.started":"2024-02-08T11:25:31.619328Z","shell.execute_reply":"2024-02-08T11:25:31.626262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocess Image and Mask with albumentation Define with Tf Tensor and Tf Sequence","metadata":{}},{"cell_type":"code","source":"def image(path: str) -> tf.Tensor:\n    img = cv2.imread(path, cv2.IMREAD_UNCHANGED)\n    img = np.tile(img[...,None], [1,1,3])\n    img = img.astype(np.float32)\n    maks = np.max(img)\n    if maks:\n        img /= maks\n    \n    img = np.transpose(img, (2,0,1))\n    return tf.convert_to_tensor(img)\n\ndef mask(path: str) -> tf.Tensor:\n    mask = cv2.imread(path, cv2.IMREAD_UNCHANGED)\n    mask = mask.astype(np.float32)\n    mask /= 255.0\n    return tf.convert_to_tensor(mask)\n\ndef augmentation(image: tf.Tensor, mask: tf.Tensor = None) -> tuple:\n    transform = A.Compose([ A.Resize(512, 512, interpolation=cv2.INTER_NEAREST),\n                               A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5), A.ShiftScaleRotate(scale_limit=0.5, rotate_limit=0, shift_limit=0.1, p=1, border_mode=0),\n                               A.RandomCrop(height=512, width=512, always_apply=True),A.RandomBrightness(p=1),\n                               A.OneOf([A.Blur(blur_limit=3, p=1),A.MotionBlur(blur_limit=3, p=1)],p=0.9)])\n    \n    image_np = image.permute(1,2,0).numpy()\n    \n    if mask is not None:\n        mask_np = mask.numpy()\n        augmented = transform(image=image_np, mask=mask_np)\n        augmented_mask = augmented[\"mask\"]\n        augmented_mask = tf.convert_to_tensor(augmented_mask, dtype=tf.float32)\n    else:\n        augmented = transform(image = image_np)\n        \n    augmented_image = augmented[\"image\"]\n    augmented_image = tf.convert_to_tensor(augmented_image, dtype=tf.float32).permute(2,0,1)\n    \n    if mask is not None:\n        return augmented_image, augmented_mask\n    else:\n        return augmented_image, None\n","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:32:33.253971Z","iopub.execute_input":"2024-02-08T11:32:33.254348Z","iopub.status.idle":"2024-02-08T11:32:33.267756Z","shell.execute_reply.started":"2024-02-08T11:32:33.254316Z","shell.execute_reply":"2024-02-08T11:32:33.26669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset(Sequence):\n    def __init__(self, x_set, y_set, transform: Callable = None):\n        self.x, self.y = x_set, y_set\n        self.transform = transform\n    \n    def __len__(self) -> int:\n        return len(self.x)\n    \n    def __getitem__(self, idx:int) -> tuple:\n        image_item = image(self.x[idx])\n        mask_item = mask(self.y[idx])\n        image, mask = self.transform(image_item, mask_item)\n        return image, mask ,self.x[idx]\n        ","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:32:34.603203Z","iopub.execute_input":"2024-02-08T11:32:34.60406Z","iopub.status.idle":"2024-02-08T11:32:34.61037Z","shell.execute_reply.started":"2024-02-08T11:32:34.604027Z","shell.execute_reply":"2024-02-08T11:32:34.609337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = Dataset(x_train, y_train, transform=augmentation)\ntest_dataset = Dataset(x_test, y_test, transform=augmentation)\n\nprint(\"train_dataset:\", len(train_dataset))\nprint(\"test_dataset:\", len(test_dataset))","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:32:35.88393Z","iopub.execute_input":"2024-02-08T11:32:35.88434Z","iopub.status.idle":"2024-02-08T11:32:35.890694Z","shell.execute_reply.started":"2024-02-08T11:32:35.884308Z","shell.execute_reply":"2024-02-08T11:32:35.88957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.__getitem__(1)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T11:30:56.778223Z","iopub.execute_input":"2024-02-08T11:30:56.779163Z","iopub.status.idle":"2024-02-08T11:30:56.834763Z","shell.execute_reply.started":"2024-02-08T11:30:56.779128Z","shell.execute_reply":"2024-02-08T11:30:56.833513Z"},"trusted":true},"execution_count":null,"outputs":[]}]}