{"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":"none","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\nfor 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":{"execution":{"iopub.status.busy":"2024-01-28T15:17:19.597284Z","iopub.execute_input":"2024-01-28T15:17:19.597721Z","iopub.status.idle":"2024-01-28T15:17:23.908619Z","shell.execute_reply.started":"2024-01-28T15:17:19.597646Z","shell.execute_reply":"2024-01-28T15:17:23.907420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom PIL import Image as im","metadata":{"execution":{"iopub.status.busy":"2024-01-28T15:17:35.188272Z","iopub.execute_input":"2024-01-28T15:17:35.188836Z","iopub.status.idle":"2024-01-28T15:17:35.194458Z","shell.execute_reply.started":"2024-01-28T15:17:35.188799Z","shell.execute_reply":"2024-01-28T15:17:35.193277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The model used is 3D F-CNN, which includes BatchNormalization and dropout for regularization of the image, as well as the Dice Loss function\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\n\ndef dice_loss(y_true, y_pred):#Dice loss function for optimization objective of the image segmentation\n    numerator = 2 * tf.reduce_sum(y_true * y_pred, axis=(1,2,3))\n    denominator = tf.reduce_sum(y_true + y_pred, axis = (1,2,3))\n    return 1 - tf.reduce_mean(numerator/(denominator + tf.keras.backend.epsilon()))\n\ndef create_3d_fcn(input_shape, num_classes, dropout_rate = 0.2):#a low dropout rate is used given that the dataset is large and diverse. This is to avoid overfitting.\n    model = models.Sequential()\n    \n    #Encoder\n    model.add(layers.Conv3D(32, (3,3,3), activation = 'relu', padding = 'same', input_shape = input_shape))\n    model.add(layers.BatchNormalization())\n    model.add(layers.MaxPooling3D((2,2,2), padding = 'same'))\n    model.add(layers.Dropout(dropout_rate))\n    \n    model.add(layers.Conv3D(64,(3,3,3), activation = 'relu', padding='same'))\n    model.add(layers.BatchNormalization())\n    model.add(layers.MaxPooling3D((2,2,2), padding='same'))\n    model.add(layers.Dropout(dropout_rate))\n    \n    #Decoder\n    model.add(layers.Conv3D(64, (3,3,3), activation= 'relu', padding='same'))\n    model.add(layers.BatchNormalization())\n    model.add(layers.UpSampling3D((2,2,2)))\n    model.add(layers.Dropout(dropout_rate))\n    \n    model.add(layers.Conv3D(32,(3,3,3), activation = 'relu', padding='same'))\n    model.add(layers.BatchNormalization())\n    model.add(layers.UpSampling3D((2,2,2)))\n    model.add(layers.Dropout(dropout_rate))\n    \n    #Output layer\n    model.add(layers.Conv3D(1,(1,1,1), activation = 'sigmoid', padding = 'same'))\n    \n    #Compile the model with Dice Loss\n    model.compile(optimizer='adam', loss=dice_loss,metrics=['accuracy'])\n        \n    return model\n\n#create the model\nheight = 128\nwidth = 128\ndepth = 32\nnum_channels = 1\n\ninput_shape = (height, width, depth, num_channels)\nnum_classes = 2\nmodel = create_3d_fcn(input_shape, num_classes)\n\nmodel.save(\"model_3dfcn\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T15:17:40.486937Z","iopub.execute_input":"2024-01-28T15:17:40.487332Z","iopub.status.idle":"2024-01-28T15:17:47.365778Z","shell.execute_reply.started":"2024-01-28T15:17:40.487299Z","shell.execute_reply":"2024-01-28T15:17:47.364866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2","metadata":{"execution":{"iopub.status.busy":"2024-01-28T15:18:07.421912Z","iopub.execute_input":"2024-01-28T15:18:07.422363Z","iopub.status.idle":"2024-01-28T15:18:07.462552Z","shell.execute_reply.started":"2024-01-28T15:18:07.422327Z","shell.execute_reply":"2024-01-28T15:18:07.461636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_and_standardize_images(path):#normalize pixel values to a standard range between 0 and 1, and standardize the pixel values to have a mean of 0 and standard deviation of 1.\n    for root, dirs, files in os.walk(path):\n        for file in files:\n            if file.endswith(('.png', '.jpg', '.jpeg', '.tif', '.png')):\n                image_path = os.path.join(root, file)\n                original_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n                \n                normalized_image = original_image / 255.0\n                \n                standardized_image = (normalized_image - np.mean(normalized_image)) / np.std(normalized_image)\n                \n                cv2.imwrite(image_path, (standardized_image * 255).astype(np.uint8))\n                \npath = '/kaggle/input/blood-vessel-segmentation/test/kidney_5/images'\nnormalize_and_standardize_images(path)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T15:18:11.838209Z","iopub.execute_input":"2024-01-28T15:18:11.838596Z","iopub.status.idle":"2024-01-28T15:18:11.926658Z","shell.execute_reply.started":"2024-01-28T15:18:11.838564Z","shell.execute_reply":"2024-01-28T15:18:11.925657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_mask(path):#creation of the mask\n    image_filenames = [f for f in os.listdir(path) in f.lower().endswith(('.png', '.jpg', '.jpeg', '.tif', '.png'))]\n    \n    normalized_images = []\n    \n    for filename in image_filenames:\n        image = cv2.imread(os.path.join(path, filename))\n        normalized_image = cv2.normalize(image,None, 0,255, cv2.NORM_MINMAX)\n        standardized_image = (normalized_image - np.mean(normalized_image))/np.std(normalized_image)\n        normalized_images.append(standardized_image)\n        \n    mask = np.stack(normalized_images, axis = 2)\n    \n    return mask","metadata":{"execution":{"iopub.status.busy":"2024-01-28T15:18:15.799546Z","iopub.execute_input":"2024-01-28T15:18:15.799956Z","iopub.status.idle":"2024-01-28T15:18:15.808760Z","shell.execute_reply.started":"2024-01-28T15:18:15.799909Z","shell.execute_reply":"2024-01-28T15:18:15.807512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import KMeans","metadata":{"execution":{"iopub.status.busy":"2024-01-28T15:18:19.405620Z","iopub.execute_input":"2024-01-28T15:18:19.406040Z","iopub.status.idle":"2024-01-28T15:18:19.834709Z","shell.execute_reply.started":"2024-01-28T15:18:19.406005Z","shell.execute_reply":"2024-01-28T15:18:19.833592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def segmentation_images(path, num_clusters = 3, target_size = (256,256), batch_size=16):#segment images\n    image_filenames = [f for f in os.listdir(path) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.tif', '.png'))]\n    \n    segmented_masks = []\n    \n    for i in range(0, len(image_filenames), batch_size):\n        batch_filenames = image_filenames[i:i + batch_size]\n        \n        batch_segmented_masks = []\n        \n        for filename in batch_filenames:\n            mri_image = cv2.imread(os.path.join(path, filename))\n            resized_mri_image = cv2.resize(mri_image, target_size)\n            flattened_mri_image= resized_mri_image.reshape((-1, mri_image.shape[-1]))\n            kmeans = KMeans(n_clusters = num_clusters, random_state=0)\n            labels = kmeans.fit.predict(flattened_mri_image)\n            segmented_mask = labels.reshape(resized_mri_image.shape[:2])\n            batch_segmented_masks.append(segmented_mask)\n            \n        segmented_masks.extend(batch_segmented_masks)\n        \n    return segmented_masks","metadata":{"execution":{"iopub.status.busy":"2024-01-28T15:18:26.685039Z","iopub.execute_input":"2024-01-28T15:18:26.686180Z","iopub.status.idle":"2024-01-28T15:18:26.696966Z","shell.execute_reply.started":"2024-01-28T15:18:26.686127Z","shell.execute_reply":"2024-01-28T15:18:26.695913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_segmentation(path, num_clusters = 3, target_size=(256,256), batch_size = 16):#visualization of segmented images\n    image_filename = [f for f in os.listdir(path) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.tif', '.png'))]\n    \n    for i in range(0, len(image_filename), batch_size):\n        batch_filename = image_filename[i:i + batch_size]\n        \n        fig, axs = plt.subplots(2, len(batch_filename), figsize=(15,6))\n        \n        for j, filename in enumerate(batch_filename):\n            mri_image = cv2.imread(os.path.join(path, filename))\n            \n            resized_mri_image = cv2.resize(mri_image, target_size)\n            \n            flattened_mri_image = resized_mri_image.reshape((-1, mri_image.shape[-1]))\n            \n            kmeans = KMeans(n_clusters=num_clusters,random_state = 0)\n            labels = kmeans.fit_predict(flattened_mri_image)\n            \n            segmented_mask = labels.reshape(resized_mri_image.shape[:2])\n            \n            axs[0,j].imshow(cv2.cvtColor(resized_mri_image, cv2.COLOR_BGR2RGB))\n            axs[0,j].axis('off')\n            axs[0,j].set_title(f'Original - {filename}')\n            \n            axs[1,j].imshow(segmented_mask,cmap = 'viridis')\n            axs[1,j].axis('off')\n            axs[1,j].set_title(f'Segmented Mask')\n            \n        plt.tight_layout()\n        plt.show()\n        \nvisualize = visualize_segmentation(path, num_clusters = 3, target_size=(256,256), batch_size = 16)\npath = ['/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0000.tif','/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0001.tif','/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0002.tif','/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0000.tif','/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0001.tif','/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0002.tif']","metadata":{"execution":{"iopub.status.busy":"2024-01-28T15:18:40.076833Z","iopub.execute_input":"2024-01-28T15:18:40.077417Z","iopub.status.idle":"2024-01-28T15:18:43.852854Z","shell.execute_reply.started":"2024-01-28T15:18:40.077376Z","shell.execute_reply":"2024-01-28T15:18:43.851825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get ids of images\ndef get_ids_from_filepaths(file_paths):\n    ids = []\n    \n    for file_path in file_paths:\n        file_name = os.path.splitext(os.path.basename(file_path))[0]\n        parts = file_path.split('/')\n        if len(parts)>2:\n            dataset_name = parts[5]\n            folder_name = parts[0]\n            \n            image_id = f\"{dataset_name}_{folder_name}{file_name}\"\n                        \n            image_id = image_id.replace('images','')\n                        \n            ids.append(image_id)\n            \n    return ids\n\nfile_paths = ['/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0000.tif','/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0001.tif','/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0002.tif','/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0000.tif','/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0001.tif','/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0002.tif']\nimage_ids = get_ids_from_filepaths(file_paths)\nprint(image_ids)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#rle\ndef empty_rle(lenght):\n    return[(0, lenght)]\n\ndef image_to_rle(image):\n    rle_encoding = []\n    \n    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    \n    current_pixel = 0\n    \n    run_lenght = 0\n    \n    for pixel in gray_image.flat:\n        if pixel != current_pixel:\n            rle_encoding.append(run_lenght)\n            current_pixel = pixel\n            run_lenght = 1\n        else:\n            run_lenght +=1\n            \n    rle_encoding.append(run_lenght)\n    \n    return rle_encoding\n\ndef generate_rle_masks(image_paths):\n    rle_masks = []\n    \n    empty_mask = empty_rle(10)\n    \n    for image_path in image_paths:\n        image = cv2.imread(image_path)\n        \n        rle_encoding = image_to_rle(image)\n        \n        combined_mask = empty_mask + rle_encoding\n        \n        rle_masks.append(combined_mask)\n        \n    return rle_masks\n\n\nimage_paths = [\"/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0000.tif\",\n             \"/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0001.tif\",\n             \"/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0002.tif\",\n             \"/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0000.tif\",\n             \"/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0001.tif\",\n             \"/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0002.tif\"]\n\nrle_masks = generate_rle_masks(image_paths)\n\nprint(rle_masks)\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_dataframe(image_ids, rle_masks):\n    data = {'id': [], 'rle': []}\n    \n    for image_id,rle_mask in zip(image_ids, rle_masks):\n        data['id'].append(image_id)\n        data['rle'].append(rle_mask)\n        \n    df = pd.DataFrame(data)\n    return df\n\n\ndf = create_dataframe(image_ids, rle_masks)\ndf.head(5)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#save dataframe to csv\ndef save_to_csv(df, filename):\n    df.to_csv(filename, index = False)\n    \n\nsave_to_csv(df, \"submission.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}