{"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":30636,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:44:15.095435Z","iopub.execute_input":"2024-01-28T08:44:15.095879Z","iopub.status.idle":"2024-01-28T08:44:16.592091Z","shell.execute_reply.started":"2024-01-28T08:44:15.095843Z","shell.execute_reply":"2024-01-28T08:44:16.591063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data\nLoad data using pandas dataframe","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))\n","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:44:21.936481Z","iopub.execute_input":"2024-01-28T08:44:21.937014Z","iopub.status.idle":"2024-01-28T08:44:30.953475Z","shell.execute_reply.started":"2024-01-28T08:44:21.936980Z","shell.execute_reply":"2024-01-28T08:44:30.952344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(images[6000])\nprint(labels[6000])","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:44:34.374743Z","iopub.execute_input":"2024-01-28T08:44:34.375177Z","iopub.status.idle":"2024-01-28T08:44:34.381296Z","shell.execute_reply.started":"2024-01-28T08:44:34.375138Z","shell.execute_reply":"2024-01-28T08:44:34.379794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(images)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:44:35.664894Z","iopub.execute_input":"2024-01-28T08:44:35.665568Z","iopub.status.idle":"2024-01-28T08:44:35.674077Z","shell.execute_reply.started":"2024-01-28T08:44:35.665527Z","shell.execute_reply":"2024-01-28T08:44:35.672921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(list(zip(images, labels)), columns = [\"image path\", \"mask path\"])\ndf.to_csv(\"path.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:45:07.333015Z","iopub.execute_input":"2024-01-28T08:45:07.333471Z","iopub.status.idle":"2024-01-28T08:45:07.414509Z","shell.execute_reply.started":"2024-01-28T08:45:07.333435Z","shell.execute_reply":"2024-01-28T08:45:07.413586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:45:08.384512Z","iopub.execute_input":"2024-01-28T08:45:08.385225Z","iopub.status.idle":"2024-01-28T08:45:08.398020Z","shell.execute_reply.started":"2024-01-28T08:45:08.385178Z","shell.execute_reply":"2024-01-28T08:45:08.397186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train, df_val = train_test_split(df, test_size=0.2, random_state = 0)\ndf_train.to_csv(\"df_train.csv\")\ndf_val.to_csv(\"df_val.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:45:28.839261Z","iopub.execute_input":"2024-01-28T08:45:28.839662Z","iopub.status.idle":"2024-01-28T08:45:28.923074Z","shell.execute_reply.started":"2024-01-28T08:45:28.839629Z","shell.execute_reply":"2024-01-28T08:45:28.922094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\nTotal images and masks in data train: \",len(df_train),\n      \"\\nTotal images and masks in data valid:\",len(df_val))","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:45:34.053496Z","iopub.execute_input":"2024-01-28T08:45:34.054028Z","iopub.status.idle":"2024-01-28T08:45:34.060308Z","shell.execute_reply.started":"2024-01-28T08:45:34.053978Z","shell.execute_reply":"2024-01-28T08:45:34.059393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:45:50.436040Z","iopub.execute_input":"2024-01-28T08:45:50.437295Z","iopub.status.idle":"2024-01-28T08:45:50.448306Z","shell.execute_reply.started":"2024-01-28T08:45:50.437243Z","shell.execute_reply":"2024-01-28T08:45:50.447175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_val.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:46:04.080601Z","iopub.execute_input":"2024-01-28T08:46:04.081019Z","iopub.status.idle":"2024-01-28T08:46:04.092421Z","shell.execute_reply.started":"2024-01-28T08:46:04.080973Z","shell.execute_reply":"2024-01-28T08:46:04.091179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PREPROCESSING IMAGES\nmake a new images by image processing using cv2","metadata":{}},{"cell_type":"code","source":"def save_images_resize_CLAHE_512x512(df, output_path, target_size=(512,512)):\n    os.makedirs(output_path, exist_ok=True)\n    for i, row in df.iterrows():\n        image_path = row[\"image path\"]\n        if image_path.lower().endswith((\".tif\")):\n            img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n            clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))\n            clahe_img = clahe.apply(img)\n            resize_img = cv2.resize(clahe_img, target_size)\n            norm_img = cv2.normalize(resize_img, None, 0, 255, cv2.NORM_MINMAX)\n            \n            filename = f\"images_{i}.tif\"\n            output_file_path = os.path.join(output_path, filename)\n            cv2.imwrite(output_file_path, norm_img)\n            \n            print(f\"Processed: {filename}\")\n\n\ndef save_masks_resize_512x512(df, output_path, target_size=(512,512)):\n    os.makedirs(output_path, exist_ok=True)\n    for i, row in df.iterrows():\n        mask_path = row[\"mask path\"]\n        if mask_path.lower().endswith((\".tif\")):\n            mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)\n            resize_mask = cv2.resize(mask, target_size)\n            norm_mask = cv2.normalize(resize_mask, None, 0, 1, cv2.NORM_MINMAX)\n            \n            filename = f\"masks_{i}.tif\"\n            output_file_path = os.path.join(output_path, filename)\n            cv2.imwrite(output_file_path, norm_mask)\n            \n            print(f\"Processed: {filename}\")\n            ","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:46:06.687559Z","iopub.execute_input":"2024-01-28T08:46:06.688332Z","iopub.status.idle":"2024-01-28T08:46:06.700667Z","shell.execute_reply.started":"2024-01-28T08:46:06.688292Z","shell.execute_reply":"2024-01-28T08:46:06.699411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_path_train_images = \"/kaggle/working/img_512x512/images\"\noutput_path_train_masks = \"/kaggle/working/img_512x512/masks\"\noutput_path_val_images = \"/kaggle/working/val_img_512x512/images\"\noutput_path_val_masks = \"/kaggle/working/val_img_512x512/masks\"","metadata":{"execution":{"iopub.status.busy":"2024-01-28T08:46:08.166878Z","iopub.execute_input":"2024-01-28T08:46:08.167427Z","iopub.status.idle":"2024-01-28T08:46:08.173528Z","shell.execute_reply.started":"2024-01-28T08:46:08.167372Z","shell.execute_reply":"2024-01-28T08:46:08.172592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_img = save_images_resize_CLAHE_512x512(df_train, output_path = output_path_train_images,\n                                                target_size=(512,512))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-01-28T08:46:18.377720Z","iopub.execute_input":"2024-01-28T08:46:18.378182Z","iopub.status.idle":"2024-01-28T08:54:08.170872Z","shell.execute_reply.started":"2024-01-28T08:46:18.378141Z","shell.execute_reply":"2024-01-28T08:54:08.168527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_mask = save_masks_resize_512x512(df_train, output_path = output_path_train_masks,\n                                                target_size=(512,512))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-01-28T08:54:08.175475Z","iopub.execute_input":"2024-01-28T08:54:08.176464Z","iopub.status.idle":"2024-01-28T08:57:29.782507Z","shell.execute_reply.started":"2024-01-28T08:54:08.176392Z","shell.execute_reply":"2024-01-28T08:57:29.781148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_val_img = save_images_resize_CLAHE_512x512(df_val, output_path = output_path_val_images,\n                                                target_size=(512,512))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-01-28T08:57:29.784149Z","iopub.execute_input":"2024-01-28T08:57:29.784527Z","iopub.status.idle":"2024-01-28T08:59:27.354694Z","shell.execute_reply.started":"2024-01-28T08:57:29.784483Z","shell.execute_reply":"2024-01-28T08:59:27.352915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_val_mask = save_masks_resize_512x512(df_val, output_path = output_path_val_masks,\n                                                target_size=(512,512))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-01-28T08:59:27.357305Z","iopub.execute_input":"2024-01-28T08:59:27.357656Z","iopub.status.idle":"2024-01-28T09:00:19.990921Z","shell.execute_reply.started":"2024-01-28T08:59:27.357624Z","shell.execute_reply":"2024-01-28T09:00:19.989798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#see total images equal to total images in df\nprint(\"\\nTotal images in df train: \",len(df_train),\n      \"\\nTotal new images:\",len(os.listdir(output_path_train_images)))\nprint(\"\\nTotal masks in df train: \",len(df_train),\n      \"\\nTotal new masks:\",len(os.listdir(output_path_train_masks)))\nprint(\"\\nTotal images in df val: \",len(df_val),\n      \"\\nTotal new images:\",len(os.listdir(output_path_val_images)))\nprint(\"\\nTotal masks in df val: \",len(df_val),\n      \"\\nTotal new masks:\",len(os.listdir(output_path_val_masks)))","metadata":{"execution":{"iopub.status.busy":"2024-01-28T09:00:19.992434Z","iopub.execute_input":"2024-01-28T09:00:19.993983Z","iopub.status.idle":"2024-01-28T09:00:20.017512Z","shell.execute_reply.started":"2024-01-28T09:00:19.993948Z","shell.execute_reply":"2024-01-28T09:00:20.016266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let's see the image and mask","metadata":{}},{"cell_type":"code","source":"x = \"/kaggle/working/img_512x512/images/images_2812.tif\"\nimg = cv2.imread(x, cv2.IMREAD_GRAYSCALE)\nplt.imshow(img, cmap=\"gray\")\nprint(\"images shape:\", img.shape)\nprint(\"max_value:\",img.min(),\n      \"min_value:\",img.max(),\n      \"\\ndtype:\",img.dtype)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T09:00:20.018949Z","iopub.execute_input":"2024-01-28T09:00:20.019288Z","iopub.status.idle":"2024-01-28T09:00:20.520360Z","shell.execute_reply.started":"2024-01-28T09:00:20.019257Z","shell.execute_reply":"2024-01-28T09:00:20.519194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = \"/kaggle/working/img_512x512/masks/masks_2812.tif\"\nmsk = cv2.imread(y, cv2.IMREAD_GRAYSCALE)\nplt.imshow(msk, cmap=\"gray\")\nprint(\"masks shape:\", msk.shape)\nprint(\"max_value:\",msk.min(),\n      \"min_value:\",msk.max(),\n      \"\\ndtype:\",msk.dtype)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T09:00:20.522220Z","iopub.execute_input":"2024-01-28T09:00:20.522862Z","iopub.status.idle":"2024-01-28T09:00:21.198340Z","shell.execute_reply.started":"2024-01-28T09:00:20.522819Z","shell.execute_reply":"2024-01-28T09:00:21.197333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #downlaod new train img_mask and val img_mask\n# !zip -r blood_vessel_segmentation_512x512.zip /kaggle/working","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-01-28T09:00:21.199981Z","iopub.execute_input":"2024-01-28T09:00:21.200624Z","iopub.status.idle":"2024-01-28T09:00:21.205155Z","shell.execute_reply.started":"2024-01-28T09:00:21.200587Z","shell.execute_reply":"2024-01-28T09:00:21.203796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}