{"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"},{"sourceId":6921119,"sourceType":"datasetVersion","datasetId":3971791}],"dockerImageVersionId":30587,"isInternetEnabled":true,"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\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:00:23.632081Z","iopub.execute_input":"2023-11-29T12:00:23.632502Z","iopub.status.idle":"2023-11-29T12:00:31.341907Z","shell.execute_reply.started":"2023-11-29T12:00:23.632468Z","shell.execute_reply":"2023-11-29T12:00:31.340548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# First try, training a model using only the first kidney","metadata":{}},{"cell_type":"code","source":"path = Path('/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/')","metadata":{"execution":{"iopub.status.busy":"2023-11-29T11:59:37.683152Z","iopub.execute_input":"2023-11-29T11:59:37.683566Z","iopub.status.idle":"2023-11-29T11:59:37.711302Z","shell.execute_reply.started":"2023-11-29T11:59:37.683534Z","shell.execute_reply":"2023-11-29T11:59:37.709891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Trying to render some images so I can look at them\nimages = get_image_files(path/'images')\nlen(images)","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:01:50.920374Z","iopub.execute_input":"2023-11-29T12:01:50.920756Z","iopub.status.idle":"2023-11-29T12:01:51.538071Z","shell.execute_reply.started":"2023-11-29T12:01:50.920725Z","shell.execute_reply":"2023-11-29T12:01:51.536842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = get_image_files(path/'labels')\nlen(labels)","metadata":{"execution":{"iopub.status.busy":"2023-11-29T10:01:49.081558Z","iopub.execute_input":"2023-11-29T10:01:49.082195Z","iopub.status.idle":"2023-11-29T10:01:49.130335Z","shell.execute_reply.started":"2023-11-29T10:01:49.082143Z","shell.execute_reply":"2023-11-29T10:01:49.12914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmentation_block = DataBlock(\n    blocks=(ImageBlock, MaskBlock),  # ImageBlock for input, MaskBlock for label\n    get_items=get_image_files,       # Function to get image files\n    splitter=RandomSplitter(),       # Split data into training and validation sets\n    get_y=lambda x: path/'labels'/x.name,  # Function to get corresponding label for each image\n    batch_tfms=aug_transforms(size=(128, 128))  # Resize images and apply standard augmentations\n)\ndls = segmentation_block.dataloaders(path/\"images\", bs=8) ","metadata":{"execution":{"iopub.status.busy":"2023-11-29T10:09:04.827549Z","iopub.execute_input":"2023-11-29T10:09:04.82817Z","iopub.status.idle":"2023-11-29T10:09:05.797468Z","shell.execute_reply.started":"2023-11-29T10:09:04.828121Z","shell.execute_reply":"2023-11-29T10:09:05.796068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(max_n=4, vmin=1, vmax=30)","metadata":{"execution":{"iopub.status.busy":"2023-11-29T10:09:11.912367Z","iopub.execute_input":"2023-11-29T10:09:11.912761Z","iopub.status.idle":"2023-11-29T10:09:13.219383Z","shell.execute_reply.started":"2023-11-29T10:09:11.912729Z","shell.execute_reply":"2023-11-29T10:09:13.218135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Looking at the segmentation\nfrom PIL import Image\nimport numpy as np\n\n# Replace with your image file path\n# image_path = path/'labels'\n\n# Load the image\nimg = Image.open(labels[500])\n\n# Convert the image to a NumPy array\nimg_array = np.array(img)\n\n# Print the array\nprint(img_array)\n\n# Optionally, print the shape of the array\nprint(\"Image shape:\", img_array.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-29T10:21:29.313674Z","iopub.execute_input":"2023-11-29T10:21:29.314414Z","iopub.status.idle":"2023-11-29T10:21:29.344667Z","shell.execute_reply.started":"2023-11-29T10:21:29.31436Z","shell.execute_reply":"2023-11-29T10:21:29.343421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Min pixel value:\", img_array.min())\nprint(\"Max pixel value:\", img_array.max())\nunique_values = np.unique(img_array)\nprint(\"Unique pixel values:\", unique_values)\n\nplt.hist(img_array.ravel(), bins=256, range=[0,256])\nplt.title(\"Pixel Value Distribution\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-29T10:22:23.738998Z","iopub.execute_input":"2023-11-29T10:22:23.739419Z","iopub.status.idle":"2023-11-29T10:22:24.515595Z","shell.execute_reply.started":"2023-11-29T10:22:23.739386Z","shell.execute_reply":"2023-11-29T10:22:24.514567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training the model","metadata":{}},{"cell_type":"code","source":"learn = unet_learner(dls, resnet34, n_out=1, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-11-29T10:14:46.783594Z","iopub.execute_input":"2023-11-29T10:14:46.784131Z","iopub.status.idle":"2023-11-29T10:14:50.063879Z","shell.execute_reply.started":"2023-11-29T10:14:46.78409Z","shell.execute_reply":"2023-11-29T10:14:50.062625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-11-29T10:15:31.928467Z","iopub.execute_input":"2023-11-29T10:15:31.928974Z","iopub.status.idle":"2023-11-29T10:15:37.357843Z","shell.execute_reply.started":"2023-11-29T10:15:31.928933Z","shell.execute_reply":"2023-11-29T10:15:37.355413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Doing the same but trying with png images ","metadata":{}},{"cell_type":"code","source":"path_png = Path('/kaggle/input/sennet-hoa-png/train_png_normalized/kidney_1_dense')","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:00:55.068389Z","iopub.execute_input":"2023-11-29T12:00:55.068803Z","iopub.status.idle":"2023-11-29T12:00:55.074035Z","shell.execute_reply.started":"2023-11-29T12:00:55.068769Z","shell.execute_reply":"2023-11-29T12:00:55.07324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = get_image_files(path_png/'images')\nlabels = get_image_files(path/'labels')","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:02:02.593935Z","iopub.execute_input":"2023-11-29T12:02:02.594351Z","iopub.status.idle":"2023-11-29T12:02:03.216983Z","shell.execute_reply.started":"2023-11-29T12:02:02.594316Z","shell.execute_reply":"2023-11-29T12:02:03.215888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmentation_block = DataBlock(\n    blocks=(ImageBlock, MaskBlock),  # ImageBlock for input, MaskBlock for label\n    get_items=get_image_files,       # Function to get image files\n    splitter=RandomSplitter(),       # Split data into training and validation sets\n    get_y=lambda x: path_png/'labels'/x.name,  # Function to get corresponding label for each image\n    batch_tfms=aug_transforms(size=(128, 128))  # Resize images and apply standard augmentations\n)\ndls = segmentation_block.dataloaders(path_png/\"images\", bs=8)","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:13:44.079793Z","iopub.execute_input":"2023-11-29T12:13:44.080996Z","iopub.status.idle":"2023-11-29T12:13:44.768032Z","shell.execute_reply.started":"2023-11-29T12:13:44.080942Z","shell.execute_reply":"2023-11-29T12:13:44.767143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(max_n=4, vmin=1, vmax=30)","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:13:50.266015Z","iopub.execute_input":"2023-11-29T12:13:50.266437Z","iopub.status.idle":"2023-11-29T12:13:51.450849Z","shell.execute_reply.started":"2023-11-29T12:13:50.266403Z","shell.execute_reply":"2023-11-29T12:13:51.44961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = unet_learner(dls, resnet34, n_out=1, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:13:57.643332Z","iopub.execute_input":"2023-11-29T12:13:57.643776Z","iopub.status.idle":"2023-11-29T12:13:59.689479Z","shell.execute_reply.started":"2023-11-29T12:13:57.643739Z","shell.execute_reply":"2023-11-29T12:13:59.68835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-11-29T12:14:01.824447Z","iopub.execute_input":"2023-11-29T12:14:01.825057Z","iopub.status.idle":"2023-11-29T12:14:06.517606Z","shell.execute_reply.started":"2023-11-29T12:14:01.825022Z","shell.execute_reply":"2023-11-29T12:14:06.515307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}