{"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"}],"dockerImageVersionId":30587,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os ,cv2 , random\nfrom skimage.io import imread , imshow\nfrom skimage.transform import resize\nfrom skimage import exposure\nimport matplotlib.pyplot as plt\nfrom skimage import img_as_ubyte , exposure","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-17T06:06:53.386324Z","iopub.execute_input":"2024-02-17T06:06:53.387383Z","iopub.status.idle":"2024-02-17T06:06:54.319419Z","shell.execute_reply.started":"2024-02-17T06:06:53.387345Z","shell.execute_reply":"2024-02-17T06:06:54.318250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.python.client import device_lib\nprint(device_lib.list_local_devices())\n\nimport tensorflow as tf\n\nif tf.test.gpu_device_name():\n    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))\nelse:\n    print(\"Please install GPU version of TF\")","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:06:54.321546Z","iopub.execute_input":"2024-02-17T06:06:54.321945Z","iopub.status.idle":"2024-02-17T06:06:54.336025Z","shell.execute_reply.started":"2024-02-17T06:06:54.321910Z","shell.execute_reply":"2024-02-17T06:06:54.334889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow \ntensorflow.__version__","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:06:54.337307Z","iopub.execute_input":"2024-02-17T06:06:54.337632Z","iopub.status.idle":"2024-02-17T06:06:54.348255Z","shell.execute_reply.started":"2024-02-17T06:06:54.337607Z","shell.execute_reply":"2024-02-17T06:06:54.347440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"height , width , channel =  640  ,608 , 1","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:06:54.350049Z","iopub.execute_input":"2024-02-17T06:06:54.350324Z","iopub.status.idle":"2024-02-17T06:06:54.360181Z","shell.execute_reply.started":"2024-02-17T06:06:54.350300Z","shell.execute_reply":"2024-02-17T06:06:54.359298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Checking the pixel Intensity for train ##\ntry:\n    image = imread(\"/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/images/0000.tif\")\n    print(\"MIN pixel value :{} Max Pixel value :{} \".format(np.min(image) , np.max(image)) )\n    print(\"Dtype \" ,  image.dtype)\n    hist, bins = exposure.histogram(image.flatten(), nbins=256)\n    plt.plot(bins, hist, color='gray')\n    plt.title('Pixel Intensity Histogram')\n    plt.xlabel('Pixel Intensity')\n    plt.ylabel('Frequency')\n    plt.show()\n    \nexcept  :\n    print(\"No file found\")","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:06:54.361281Z","iopub.execute_input":"2024-02-17T06:06:54.361639Z","iopub.status.idle":"2024-02-17T06:06:54.673303Z","shell.execute_reply.started":"2024-02-17T06:06:54.361605Z","shell.execute_reply":"2024-02-17T06:06:54.672415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Checking the pixel Intensity for test##\ntry :\n    image = imread(\"/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0000.tif\")\n    print(\"MIN pixel value :{} Max Pixel value :{} \".format(np.min(image) , np.max(image)) )\n    print(image.shape)\n    print(image.dtype)\n    hist, bins = exposure.histogram(image.flatten(), nbins=256)\n    plt.plot(bins, hist, color='gray')\n    plt.title('Pixel Intensity Histogram')\n    plt.xlabel('Pixel Intensity')\n    plt.ylabel('Frequency')\n    plt.show()\n\nexcept :\n    print('No img found')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:06:54.674940Z","iopub.execute_input":"2024-02-17T06:06:54.675219Z","iopub.status.idle":"2024-02-17T06:06:54.981776Z","shell.execute_reply.started":"2024-02-17T06:06:54.675194Z","shell.execute_reply":"2024-02-17T06:06:54.980876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Checking the pixel Intensity for test##\ntry :\n    image = imread(\"/kaggle/input/blood-vessel-segmentation/test/kidney_6/images/0000.tif\")\n    print(image.shape)\n    print(\"MIN pixel value :{} Max Pixel value :{} \".format(np.min(image) , np.max(image)) )\n    hist, bins = exposure.histogram(image.flatten(), nbins=256)\n    plt.plot(bins, hist, color='gray')\n    plt.title('Pixel Intensity Histogram')\n    plt.xlabel('Pixel Intensity')\n    plt.ylabel('Frequency')\n    plt.show()\nexcept :\n    print('No img found')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:06:54.982761Z","iopub.execute_input":"2024-02-17T06:06:54.983006Z","iopub.status.idle":"2024-02-17T06:06:55.270361Z","shell.execute_reply.started":"2024-02-17T06:06:54.982985Z","shell.execute_reply":"2024-02-17T06:06:55.269360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import img_as_ubyte , exposure\ntry :\n    image = imread(\"/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/images/0000.tif\")\n    print(image.shape)\n    print(\"MIN pixel value :{} Max Pixel value :{} \".format(np.min(image) , np.max(image)) )\n    hist, bins = exposure.histogram(image.flatten(), nbins=256)\n    plt.plot(bins, hist, color='gray')\n    plt.title('Pixel Intensity Histogram')\n    plt.xlabel('Pixel Intensity')\n    plt.ylabel('Frequency')\n    plt.show()\nexcept :\n    print('No img found')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:06:55.272497Z","iopub.execute_input":"2024-02-17T06:06:55.272831Z","iopub.status.idle":"2024-02-17T06:06:55.568911Z","shell.execute_reply.started":"2024-02-17T06:06:55.272803Z","shell.execute_reply":"2024-02-17T06:06:55.567880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntry :\n    image = imread(\"/kaggle/input/blood-vessel-segmentation/test/kidney_5/images/0000.tif\")\n    print(image.shape)\n    print(\"MIN pixel value :{} Max Pixel value :{} \".format(np.min(image) , np.max(image)) )\n    hist, bins = exposure.histogram(image.flatten(), nbins=256)\n    plt.plot(bins, hist, color='gray')\n    plt.title('Pixel Intensity Histogram')\n    plt.xlabel('Pixel Intensity')\n    plt.ylabel('Frequency')\n    plt.show()\nexcept :\n    print('No img found')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:06:55.570201Z","iopub.execute_input":"2024-02-17T06:06:55.570601Z","iopub.status.idle":"2024-02-17T06:06:55.856789Z","shell.execute_reply.started":"2024-02-17T06:06:55.570571Z","shell.execute_reply":"2024-02-17T06:06:55.855910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:06:55.858158Z","iopub.execute_input":"2024-02-17T06:06:55.858862Z","iopub.status.idle":"2024-02-17T06:06:56.057958Z","shell.execute_reply.started":"2024-02-17T06:06:55.858826Z","shell.execute_reply":"2024-02-17T06:06:56.056925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## looping to create batches for images and masks ##\npath = \"/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/images/\"\nfiles = os.listdir(path)\nimgs  = np.zeros((len(files) ,height , width ,channel ) ,dtype = np.uint8 )\nfor i ,  id_ in enumerate(files) :\n    path_img = path + id_\n    img  = imread(path_img )\n    img = img_as_ubyte(exposure.rescale_intensity(img))\n    img  = resize(img , (height , width ) , mode = 'constant' , preserve_range = True)\n    imgs[i] = img[:, :, np.newaxis]\n    \npath = \"/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/labels/\"\nfiles = os.listdir(path)\nmasks = np.zeros((len(files) ,height , width ,channel ) ,dtype = bool)\nfor i ,  id_ in enumerate(files) :\n    path_img = path + id_\n    img  = imread(path_img )\n    img  = resize(img , (height , width ) , mode = 'constant' , preserve_range = True)\n    masks[i] = img[:, :, np.newaxis]","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:06:56.059213Z","iopub.execute_input":"2024-02-17T06:06:56.059496Z","iopub.status.idle":"2024-02-17T06:11:48.319356Z","shell.execute_reply.started":"2024-02-17T06:06:56.059471Z","shell.execute_reply":"2024-02-17T06:11:48.318309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(imgs.shape)\nprint(masks.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:11:48.322136Z","iopub.execute_input":"2024-02-17T06:11:48.322422Z","iopub.status.idle":"2024-02-17T06:11:48.327196Z","shell.execute_reply.started":"2024-02-17T06:11:48.322397Z","shell.execute_reply":"2024-02-17T06:11:48.326189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.max(imgs[0])","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:11:48.328550Z","iopub.execute_input":"2024-02-17T06:11:48.328815Z","iopub.status.idle":"2024-02-17T06:11:48.340702Z","shell.execute_reply.started":"2024-02-17T06:11:48.328792Z","shell.execute_reply":"2024-02-17T06:11:48.339652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## plotting \nimport matplotlib.pyplot as plt\nimport random\nix  = random.randint(0 , len(imgs)-1)\nplt.subplot(1, 2, 1 )\nplt.imshow(imgs[ix])\nplt.title('Original Image')\n\n\nplt.subplot(1, 2, 2 )\nplt.imshow(masks[ix] , cmap='gray')\nplt.title('Mask Image')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:11:48.341962Z","iopub.execute_input":"2024-02-17T06:11:48.342244Z","iopub.status.idle":"2024-02-17T06:11:48.911854Z","shell.execute_reply.started":"2024-02-17T06:11:48.342220Z","shell.execute_reply":"2024-02-17T06:11:48.910954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## looping to create batches for images and masks ##\npath = \"/kaggle/input/blood-vessel-segmentation/train/kidney_1_voi/images/\"\nfiles = os.listdir(path)\nimgs2  = np.zeros((len(files) ,height , width ,channel ) ,dtype = np.uint8 )\nfor i ,  id_ in enumerate(files) :\n    path_img = path + id_\n    img  = imread(path_img )\n    img = img_as_ubyte(exposure.rescale_intensity(img))\n    img  = resize(img , (height , width ) , mode = 'constant' , preserve_range = True)\n    imgs2[i] = img[:, :, np.newaxis]\nprint('Done')\n    \npath = \"/kaggle/input/blood-vessel-segmentation/train/kidney_1_voi/labels/\"\nfiles = os.listdir(path)\nmasks2 = np.zeros((len(files) ,height , width ,channel ) ,dtype = bool)\nfor i ,  id_ in enumerate(files) :\n    path_img = path + id_\n    img  = imread(path_img )\n    img  = resize(img , (height , width ) , mode = 'constant' , preserve_range = True)\n    masks2[i] = img[:, :, np.newaxis]","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:11:48.913199Z","iopub.execute_input":"2024-02-17T06:11:48.913536Z","iopub.status.idle":"2024-02-17T06:20:32.350366Z","shell.execute_reply.started":"2024-02-17T06:11:48.913506Z","shell.execute_reply":"2024-02-17T06:20:32.349180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## plotting \nimport matplotlib.pyplot as plt\nimport random\nix  = random.randint(0 , len(imgs2)-1)\nplt.subplot(1, 2, 1 )\nplt.imshow(imgs2[ix])\nplt.title('Original Image')\n\n\nplt.subplot(1, 2, 2 )\nplt.imshow(masks2[ix] , cmap='gray')\nplt.title('Mask Image')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:20:32.351654Z","iopub.execute_input":"2024-02-17T06:20:32.351999Z","iopub.status.idle":"2024-02-17T06:20:32.852298Z","shell.execute_reply.started":"2024-02-17T06:20:32.351972Z","shell.execute_reply":"2024-02-17T06:20:32.851409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Stacking and deleting the variables ##\nstack_img  = np.vstack((imgs , imgs2)).copy()\nstack_mask = np.vstack((masks , masks2)).copy()\ndel imgs \ndel imgs2\ndel masks\ndel masks2","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:20:32.853456Z","iopub.execute_input":"2024-02-17T06:20:32.853755Z","iopub.status.idle":"2024-02-17T06:20:38.010076Z","shell.execute_reply.started":"2024-02-17T06:20:32.853729Z","shell.execute_reply":"2024-02-17T06:20:38.009049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nix  = random.randint(0 , len(stack_img) - 1)\nprint(ix)\nplt.subplot(1, 2, 1 )\nplt.imshow(np.squeeze(stack_img[ix]) )\nplt.title('Original Image')\n\n\nplt.subplot(1, 2, 2 )\nplt.imshow(stack_mask[ix] , cmap='gray')\nplt.title('Mask Image')\n","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:20:38.011351Z","iopub.execute_input":"2024-02-17T06:20:38.011648Z","iopub.status.idle":"2024-02-17T06:20:38.518666Z","shell.execute_reply.started":"2024-02-17T06:20:38.011623Z","shell.execute_reply":"2024-02-17T06:20:38.517719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## looping to create batches for images and masks ##\npath = \"/kaggle/input/blood-vessel-segmentation/train/kidney_2/images/\"\nfiles = os.listdir(path)\nimgs3  = np.zeros((len(files) ,height , width ,channel ) ,dtype = np.uint8 )\nfor i ,  id_ in enumerate(files) :\n    path_img = path + id_\n    img  = imread(path_img )\n    img = img_as_ubyte(exposure.rescale_intensity(img))\n    img  = resize(img , (height , width ) , mode = 'constant' , preserve_range = True)\n    imgs3[i] = img[:, :, np.newaxis]\n\n    \npath = \"/kaggle/input/blood-vessel-segmentation/train/kidney_2/labels/\"\nfiles = os.listdir(path)\nmasks3 = np.zeros((len(files) ,height , width ,channel ) ,dtype = bool)\nfor i ,  id_ in enumerate(files) :\n    path_img = path + id_\n    img  = imread(path_img )\n    \n    img  = resize(img , (height , width ) , mode = 'constant' , preserve_range = True)\n    masks3[i] = img[:, :, np.newaxis]","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:20:38.519960Z","iopub.execute_input":"2024-02-17T06:20:38.520354Z","iopub.status.idle":"2024-02-17T06:25:28.471080Z","shell.execute_reply.started":"2024-02-17T06:20:38.520319Z","shell.execute_reply":"2024-02-17T06:25:28.469961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## plotting \nimport matplotlib.pyplot as plt\nimport random\nix  = random.randint(0 , len(imgs3)-1)\nplt.subplot(1, 2, 1 )\nplt.imshow(imgs3[ix])\nplt.title('Original Image')\n\n\nplt.subplot(1, 2, 2 )\nplt.imshow(masks3[ix] , cmap='gray')\nplt.title('Mask Image')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:25:28.472534Z","iopub.execute_input":"2024-02-17T06:25:28.473415Z","iopub.status.idle":"2024-02-17T06:25:28.993507Z","shell.execute_reply.started":"2024-02-17T06:25:28.473377Z","shell.execute_reply":"2024-02-17T06:25:28.992630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##stacking and deleting the variables\nstack_img = np.vstack((stack_img  , imgs3)).copy()\nstack_mask = np.vstack((stack_mask ,  masks3)).copy()\ndel imgs3\ndel masks3\n","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:25:28.994738Z","iopub.execute_input":"2024-02-17T06:25:28.995081Z","iopub.status.idle":"2024-02-17T06:25:36.063454Z","shell.execute_reply.started":"2024-02-17T06:25:28.995049Z","shell.execute_reply":"2024-02-17T06:25:36.062529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(stack_img.shape)\nprint(stack_img.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:25:36.064651Z","iopub.execute_input":"2024-02-17T06:25:36.064962Z","iopub.status.idle":"2024-02-17T06:25:36.069737Z","shell.execute_reply.started":"2024-02-17T06:25:36.064937Z","shell.execute_reply":"2024-02-17T06:25:36.068859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nix  = random.randint(0 , len(stack_img) - 1)\nprint(ix)\nplt.subplot(1, 2, 1 )\nplt.imshow(np.squeeze(stack_img[ix]) )\nplt.title('Original Image')\n\n\nplt.subplot(1, 2, 2 )\nplt.imshow(stack_mask[ix] , cmap='gray')\nplt.title('Mask Image')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:25:36.074426Z","iopub.execute_input":"2024-02-17T06:25:36.074719Z","iopub.status.idle":"2024-02-17T06:25:36.569548Z","shell.execute_reply.started":"2024-02-17T06:25:36.074696Z","shell.execute_reply":"2024-02-17T06:25:36.568697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:25:36.570573Z","iopub.execute_input":"2024-02-17T06:25:36.570870Z","iopub.status.idle":"2024-02-17T06:25:36.786940Z","shell.execute_reply.started":"2024-02-17T06:25:36.570845Z","shell.execute_reply":"2024-02-17T06:25:36.785720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## looping to create batches for images and masks ##\n\nfiles  = os.listdir(\"/kaggle/input/blood-vessel-segmentation/train/kidney_3_dense/labels/\")\nprint(len(files))\nfiles2  = os.listdir(\"/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse/labels/\")\nprint(len(files2))\n\ndire = \"/kaggle/input/blood-vessel-segmentation/train/\"\nmasks4 = np.zeros((len(files2) , height , width , 1 ) ,dtype = bool )\nfor i , id_ in enumerate(files2):\n    path =  dire + \"kidney_3_sparse\" + \"/labels/\" + id_\n    label  = imread(path )\n    label  = resize(label , (height , width ) ,mode = 'constant' , preserve_range = True )\n    \n    if id_ in files :\n        path2 = dire + \"kidney_3_dense/\" + \"labels/\" + id_\n       \n        label2  = imread(path2)\n        label2  = resize(label2 , (height , width ) , mode = 'constant' , preserve_range = True)\n        mask = np.maximum(label , label2)\n        masks4[i] = mask[: , : , np.newaxis ] \n    else :\n        masks4[i] = label[: , : , np.newaxis ]\n        \n\n        \npath = \"/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse/images/\"\nfiles = os.listdir(path)\nimgs4  = np.zeros((len(files) ,height , width ,channel ) ,dtype = np.uint8 )\nfor i ,  id_ in enumerate(files) :\n    path_img = path + id_\n    img  = imread(path_img )\n    img = img_as_ubyte(exposure.rescale_intensity(img))\n    img  = resize(img , (height , width ) , mode = 'constant' , preserve_range = True)\n    imgs4[i] = img[:, :, np.newaxis]","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:25:36.788338Z","iopub.execute_input":"2024-02-17T06:25:36.788690Z","iopub.status.idle":"2024-02-17T06:30:29.098263Z","shell.execute_reply.started":"2024-02-17T06:25:36.788642Z","shell.execute_reply":"2024-02-17T06:30:29.097201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nix  = random.randint(0 , len(imgs4) - 1)\nprint(ix)\nplt.subplot(1, 2, 1 )\nplt.imshow(imgs4[ix])\nplt.title('Original Image')\n\n\nplt.subplot(1, 2, 2 )\nplt.imshow(masks4[ix] , cmap='gray')\nplt.title('Mask Image')","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:29.099654Z","iopub.execute_input":"2024-02-17T06:30:29.100036Z","iopub.status.idle":"2024-02-17T06:30:29.682928Z","shell.execute_reply.started":"2024-02-17T06:30:29.100003Z","shell.execute_reply":"2024-02-17T06:30:29.681784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(imgs4.shape)\nprint(masks4.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:29.684087Z","iopub.execute_input":"2024-02-17T06:30:29.684389Z","iopub.status.idle":"2024-02-17T06:30:29.690117Z","shell.execute_reply.started":"2024-02-17T06:30:29.684361Z","shell.execute_reply":"2024-02-17T06:30:29.688936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stack_img = np.vstack((stack_img , imgs4))\nstack_mask = np.vstack((stack_mask , masks4))\ndel imgs4\ndel masks4","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:29.691507Z","iopub.execute_input":"2024-02-17T06:30:29.692132Z","iopub.status.idle":"2024-02-17T06:30:33.773087Z","shell.execute_reply.started":"2024-02-17T06:30:29.692091Z","shell.execute_reply":"2024-02-17T06:30:33.772182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(stack_img.shape)\nprint(stack_mask.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:33.774283Z","iopub.execute_input":"2024-02-17T06:30:33.774604Z","iopub.status.idle":"2024-02-17T06:30:33.779260Z","shell.execute_reply.started":"2024-02-17T06:30:33.774577Z","shell.execute_reply":"2024-02-17T06:30:33.778317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nix  = random.randint(0 , len(stack_img) - 1)\nprint(ix)\nplt.subplot(1, 2, 1 )\nplt.imshow(np.squeeze(stack_img[ix]) )\nplt.title('Original Image')\n\n\nplt.subplot(1, 2, 2 )\nplt.imshow(stack_mask[ix] , cmap='gray')\nplt.title('Mask Image')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:33.780696Z","iopub.execute_input":"2024-02-17T06:30:33.781152Z","iopub.status.idle":"2024-02-17T06:30:34.281627Z","shell.execute_reply.started":"2024-02-17T06:30:33.781098Z","shell.execute_reply":"2024-02-17T06:30:34.280700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:34.282907Z","iopub.execute_input":"2024-02-17T06:30:34.283223Z","iopub.status.idle":"2024-02-17T06:30:34.495861Z","shell.execute_reply.started":"2024-02-17T06:30:34.283197Z","shell.execute_reply":"2024-02-17T06:30:34.494730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## splitting train test\nfrom sklearn.model_selection import train_test_split\n\n# subset_size = 1200\n\n\ntrainx , testx , trainy , testy  = train_test_split(stack_img , stack_mask , test_size = 0.30  ,  random_state = 2)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:34.497271Z","iopub.execute_input":"2024-02-17T06:30:34.497805Z","iopub.status.idle":"2024-02-17T06:30:39.353976Z","shell.execute_reply.started":"2024-02-17T06:30:34.497765Z","shell.execute_reply":"2024-02-17T06:30:39.352968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(trainx.shape)\nprint(trainy.shape)\nprint(testx.shape)\nprint(testy.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:39.355197Z","iopub.execute_input":"2024-02-17T06:30:39.355527Z","iopub.status.idle":"2024-02-17T06:30:39.360973Z","shell.execute_reply.started":"2024-02-17T06:30:39.355493Z","shell.execute_reply":"2024-02-17T06:30:39.360103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainx  = trainx.copy()\ntrainy = trainy.copy()\ntestx = testx.copy()\ntesty = testy.copy()\ndel stack_img\ndel stack_mask","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:39.362571Z","iopub.execute_input":"2024-02-17T06:30:39.363013Z","iopub.status.idle":"2024-02-17T06:30:43.010480Z","shell.execute_reply.started":"2024-02-17T06:30:39.362979Z","shell.execute_reply":"2024-02-17T06:30:43.009706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:43.011553Z","iopub.execute_input":"2024-02-17T06:30:43.011831Z","iopub.status.idle":"2024-02-17T06:30:43.221348Z","shell.execute_reply.started":"2024-02-17T06:30:43.011806Z","shell.execute_reply":"2024-02-17T06:30:43.220408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Data Augmentation ##\nfrom tensorflow.keras.preprocessing import image\n\nseed = 8 \nBATCH_SIZE = 8\n\nimage_gen  = image.ImageDataGenerator(rescale = None ,   horizontal_flip =  True , vertical_flip  = True ,  rotation_range = 0.5\n                                                            , zoom_range = 0.2 , shear_range = 0.5 , width_shift_range = 0.2 , height_shift_range = 0.2 , fill_mode = 'reflect')\n\nmask_gen = image.ImageDataGenerator(rescale = None ,   horizontal_flip =  True , vertical_flip  = True ,  rotation_range = 0.5\n                                                            , zoom_range = 0.2 , shear_range = 0.5 , width_shift_range = 0.2 , height_shift_range = 0.2 , fill_mode = 'reflect' )\n\n\n# image_gen  = image.ImageDataGenerator()\n\n# mask_gen = image.ImageDataGenerator()\n\nimage_gen.fit(trainx , augment = False , seed = seed )\nmask_gen.fit(trainy , augment = False , seed   = seed)\n\nx = image_gen.flow(trainx , batch_size = BATCH_SIZE , shuffle = True , seed = seed )\ny = mask_gen.flow(trainy , batch_size = BATCH_SIZE ,shuffle = True  , seed = seed )\n\nimage_datagen_val = image.ImageDataGenerator()\nmask_datagen_val = image.ImageDataGenerator()\n\nimage_datagen_val.fit(testx , augment = False , seed = seed)\nmask_datagen_val.fit(testy , augment = False , seed = seed )\n\nx_val = image_datagen_val.flow(testx , batch_size = 1 ,shuffle = True , seed = seed)\ny_val = mask_datagen_val.flow(testy , batch_size = 1 , shuffle = True ,  seed = seed )\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:30:43.222796Z","iopub.execute_input":"2024-02-17T06:30:43.223430Z","iopub.status.idle":"2024-02-17T06:31:40.390719Z","shell.execute_reply.started":"2024-02-17T06:30:43.223392Z","shell.execute_reply":"2024-02-17T06:31:40.389738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:31:40.392052Z","iopub.execute_input":"2024-02-17T06:31:40.392366Z","iopub.status.idle":"2024-02-17T06:31:40.832517Z","shell.execute_reply.started":"2024-02-17T06:31:40.392338Z","shell.execute_reply":"2024-02-17T06:31:40.831565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## model ##\nfrom tensorflow.keras.layers import Input , Lambda , Conv2D , Conv2DTranspose , MaxPooling2D , Dropout , concatenate\nfrom tensorflow.keras.models import Model , load_model \n\ndef unet(input_shape = (height , width , 1)):\n    inp = Input(input_shape )\n    inp = Lambda(lambda x : x/ 255)(inp)\n    c1 =  Conv2D(32 ,kernel_size = (3 , 3) , padding ='same' , activation = 'relu'  , kernel_initializer = 'he_normal')(inp)\n    c1 = Dropout(0.1)(c1)\n    c1 = Conv2D(32 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal') (c1)\n    p1 = MaxPooling2D((2 ,2)) (c1)\n    \n    c2 = Conv2D(64 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(p1)\n    c2 = Dropout(0.1)(c2)\n    c2 = Conv2D(64 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(c2)\n    p2 = MaxPooling2D((2 ,2)) (c2)\n    \n    c3 = Conv2D(128 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(p2)\n    c3 = Dropout(0.1)(c3)\n    c3 = Conv2D(128 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(c3)\n    p3 = MaxPooling2D((2 ,2)) (c3)\n    \n    c4 = Conv2D(256 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(p3)\n    c4 = Dropout(0.1)(c4)\n    c4 = Conv2D(256 , kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(c4)\n    p4 = MaxPooling2D((2 ,2)) (c4)\n    \n    \n    ##Bridge Connection ##\n    \n    c5 = Conv2D(512, kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal')(p4)\n    c5 = Conv2D(512, kernel_size = (3 , 3) , padding = 'same' , activation ='relu' , kernel_initializer = 'he_normal') (c5)\n    c5 =  Dropout(0.1) (c5)\n    \n    up6 =  Conv2DTranspose(256 , (2 , 2) ,strides = ( 2,2 ) , padding = 'same' ) (c5)\n    up6 = concatenate([up6 ,c4] , axis = 3)\n    c6 = Conv2D(256 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' )(up6)\n    c6 = Dropout(0.1)(c6)\n    c6 = Conv2D(256 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' ) (c6)\n    \n\n    \n    up7 = Conv2DTranspose(128 , ( 2 , 2 ) ,strides = ( 2,2 ) , padding = 'same' ) (c6)\n    up7 = concatenate([up7 , c3] , axis = 3)\n    c7 = Conv2D(128 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' )(up7)\n    c7 = Dropout(0.1)(c7)\n    c7 = Conv2D(128 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' ) (c7)\n    \n    up8 = Conv2DTranspose(64 , (2 , 2 ) ,strides = ( 2,2 ) , padding = 'same'  ) (c7)\n    up8 = concatenate([up8, c2] , axis = 3)\n    c8 = Conv2D(64 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' )(up8)\n    c8 = Dropout(0.1)(c8)\n    c8 = Conv2D(64 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' ) (c8)\n    \n    up9 = Conv2DTranspose(32 , ( 2 , 2 ) ,strides = ( 2,2 ) , padding = 'same'  ) (c8)\n    up9 = concatenate([up9, c1] , axis = 3)\n    c9 = Conv2D(32, (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' )(up9)\n    c9 = Dropout(0.1)(c9)\n    c9 = Conv2D(32 , (3,3) , padding = 'same' , activation = 'relu' ,kernel_initializer = 'he_normal' ) (c9)\n    \n    c10 = Conv2D(1 , (1 , 1) , activation = 'sigmoid' ) (c9)\n    \n    model = Model(inputs = [inp], outputs = [c10] )\n    \n    return model\n    \n   \n    \n\nmodel  = unet()\nmodel.summary()    \n    \n","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:31:40.834123Z","iopub.execute_input":"2024-02-17T06:31:40.834717Z","iopub.status.idle":"2024-02-17T06:31:42.097700Z","shell.execute_reply.started":"2024-02-17T06:31:40.834680Z","shell.execute_reply":"2024-02-17T06:31:42.096716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Dice coeff for metrics ##\nfrom tensorflow.keras.losses import binary_crossentropy\nfrom tensorflow.keras import backend as k\n\n\ndef dice_coef(y_true , y_pred):\n    smooth = 1\n    y_true  = k.flatten(y_true)\n    y_pred = k.flatten(y_pred)\n    intersection  =  k.sum(y_true * y_pred)\n    return (2. * intersection + smooth ) /( k.sum(y_true ) + k.sum(y_pred)  + smooth )","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:31:42.099110Z","iopub.execute_input":"2024-02-17T06:31:42.099493Z","iopub.status.idle":"2024-02-17T06:31:42.107251Z","shell.execute_reply.started":"2024-02-17T06:31:42.099457Z","shell.execute_reply":"2024-02-17T06:31:42.106244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## model compilation ##\nfrom tensorflow.keras.optimizers import Adam\n\n\nmodel.compile(optimizer = 'adam' , loss = 'binary_crossentropy' , metrics = [dice_coef , \"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:31:42.108602Z","iopub.execute_input":"2024-02-17T06:31:42.108915Z","iopub.status.idle":"2024-02-17T06:31:42.131630Z","shell.execute_reply.started":"2024-02-17T06:31:42.108890Z","shell.execute_reply":"2024-02-17T06:31:42.130795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:31:42.133074Z","iopub.execute_input":"2024-02-17T06:31:42.133571Z","iopub.status.idle":"2024-02-17T06:31:42.358288Z","shell.execute_reply.started":"2024-02-17T06:31:42.133538Z","shell.execute_reply":"2024-02-17T06:31:42.357196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## checkpoints ##\nfrom tensorflow.keras.callbacks import EarlyStopping , ReduceLROnPlateau , ModelCheckpoint , CSVLogger , TensorBoard\nfrom datetime import datetime\nearly_stopping = EarlyStopping(patience = 3, verbose=2)\nreduce_lr  = ReduceLROnPlateau(factor=0.1, patience = 2, min_lr=0.0001, verbose=2)\nmodel_checkpoint = ModelCheckpoint('best_model.h5', save_best_only=True)\n# log_dir = \"logs/\" + datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n# tensorboard_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:31:42.359768Z","iopub.execute_input":"2024-02-17T06:31:42.360132Z","iopub.status.idle":"2024-02-17T06:31:42.368106Z","shell.execute_reply.started":"2024-02-17T06:31:42.360096Z","shell.execute_reply":"2024-02-17T06:31:42.367238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## merging x, y ##\ntrain_generator = zip(x,y)\nval_generator = zip(x_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:31:42.369315Z","iopub.execute_input":"2024-02-17T06:31:42.369623Z","iopub.status.idle":"2024-02-17T06:31:42.376457Z","shell.execute_reply.started":"2024-02-17T06:31:42.369596Z","shell.execute_reply":"2024-02-17T06:31:42.375640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## model training ##\nhistory = model.fit(train_generator , validation_data = ( val_generator ),  epochs = 16 , validation_steps= int(len(testy)), steps_per_epoch = int(len(trainy) / BATCH_SIZE) , verbose = 2 , callbacks = [ early_stopping , reduce_lr , model_checkpoint ])","metadata":{"execution":{"iopub.status.busy":"2024-02-17T06:31:42.377606Z","iopub.execute_input":"2024-02-17T06:31:42.377946Z","iopub.status.idle":"2024-02-17T10:01:16.706224Z","shell.execute_reply.started":"2024-02-17T06:31:42.377922Z","shell.execute_reply":"2024-02-17T10:01:16.702396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:16.715580Z","iopub.execute_input":"2024-02-17T10:01:16.715908Z","iopub.status.idle":"2024-02-17T10:01:16.978575Z","shell.execute_reply.started":"2024-02-17T10:01:16.715881Z","shell.execute_reply":"2024-02-17T10:01:16.977590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## loading best model ##\nfrom tensorflow.keras.utils import custom_object_scope\n\nwith custom_object_scope({'dice_coef': dice_coef}):\n    model = load_model(\"best_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:16.980289Z","iopub.execute_input":"2024-02-17T10:01:16.981087Z","iopub.status.idle":"2024-02-17T10:01:17.706185Z","shell.execute_reply.started":"2024-02-17T10:01:16.981048Z","shell.execute_reply":"2024-02-17T10:01:17.705303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:17.707489Z","iopub.execute_input":"2024-02-17T10:01:17.707873Z","iopub.status.idle":"2024-02-17T10:01:17.944173Z","shell.execute_reply.started":"2024-02-17T10:01:17.707836Z","shell.execute_reply":"2024-02-17T10:01:17.943181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ## Evaluating in x and y #\n# model.evaluate(x = testx ,y = testy ,verbose = 2 )","metadata":{"execution":{"iopub.status.busy":"2024-02-16T11:20:17.123724Z","iopub.execute_input":"2024-02-16T11:20:17.124487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T14:13:22.658974Z","iopub.execute_input":"2024-02-16T14:13:22.659646Z","iopub.status.idle":"2024-02-16T14:13:22.892959Z","shell.execute_reply.started":"2024-02-16T14:13:22.659600Z","shell.execute_reply":"2024-02-16T14:13:22.892053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ntf.keras.utils.plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:17.945425Z","iopub.execute_input":"2024-02-17T10:01:17.945776Z","iopub.status.idle":"2024-02-17T10:01:18.800682Z","shell.execute_reply.started":"2024-02-17T10:01:17.945749Z","shell.execute_reply":"2024-02-17T10:01:18.799696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## for test data inference ##\nimport glob\nDATASET_FOLDER = \"/kaggle/input/blood-vessel-segmentation\"\nls_images = glob.glob(os.path.join(DATASET_FOLDER, \"test\", \"*\", \"*\", \"*.tif\"))\nprint(f\"found images: {len(ls_images)}\")","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:18.802017Z","iopub.execute_input":"2024-02-17T10:01:18.802339Z","iopub.status.idle":"2024-02-17T10:01:18.830579Z","shell.execute_reply.started":"2024-02-17T10:01:18.802312Z","shell.execute_reply":"2024-02-17T10:01:18.829703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls_images","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:18.831625Z","iopub.execute_input":"2024-02-17T10:01:18.831922Z","iopub.status.idle":"2024-02-17T10:01:18.837819Z","shell.execute_reply.started":"2024-02-17T10:01:18.831897Z","shell.execute_reply":"2024-02-17T10:01:18.836716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## looping to create batches for images and masks ##\n## looping to create batches for images and masks ##\nimgs  = np.zeros((len(ls_images) , height , width , 1 ) , dtype = np.uint8)\nfor i , id_ in enumerate(ls_images):\n    print(id_)\n    img  = imread(path_img )\n    img = img_as_ubyte(exposure.rescale_intensity(img))\n    img  = resize(img , (height , width ) , mode = 'constant' , preserve_range = True)\n    imgs[i] = img[:, :, np.newaxis]","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:18.839204Z","iopub.execute_input":"2024-02-17T10:01:18.839625Z","iopub.status.idle":"2024-02-17T10:01:19.542170Z","shell.execute_reply.started":"2024-02-17T10:01:18.839589Z","shell.execute_reply":"2024-02-17T10:01:19.541388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()\nimgs.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:19.543248Z","iopub.execute_input":"2024-02-17T10:01:19.543547Z","iopub.status.idle":"2024-02-17T10:01:19.788946Z","shell.execute_reply.started":"2024-02-17T10:01:19.543520Z","shell.execute_reply":"2024-02-17T10:01:19.787969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict masks\npredicted_masks = model.predict(imgs , verbose = 2)\n# predicted_masks = model.predict(X_val)\n\n# Post-process masks: threshold and resize to original size\nthresholded_masks = (predicted_masks > 0.5).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:19.790153Z","iopub.execute_input":"2024-02-17T10:01:19.790448Z","iopub.status.idle":"2024-02-17T10:01:26.628067Z","shell.execute_reply.started":"2024-02-17T10:01:19.790423Z","shell.execute_reply":"2024-02-17T10:01:26.627035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:26.630133Z","iopub.execute_input":"2024-02-17T10:01:26.630445Z","iopub.status.idle":"2024-02-17T10:01:26.876184Z","shell.execute_reply.started":"2024-02-17T10:01:26.630417Z","shell.execute_reply":"2024-02-17T10:01:26.875269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nix = random.randint(0, len(imgs) - 1)\nplt.imshow(np.squeeze(imgs[ix]))\nplt.show()\n\nplt.imshow(np.squeeze(thresholded_masks[ix]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:26.877398Z","iopub.execute_input":"2024-02-17T10:01:26.877785Z","iopub.status.idle":"2024-02-17T10:01:27.443448Z","shell.execute_reply.started":"2024-02-17T10:01:26.877753Z","shell.execute_reply":"2024-02-17T10:01:27.442288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## rle encoding\ndef rle_encode(mask, bg = 0) -> dict:\n    vec = mask.flatten()\n    nb = len(vec)\n    where = np.flatnonzero\n    starts = np.r_[0, where(~np.isclose(vec[1:], vec[:-1], equal_nan=True)) + 1]\n    lengths = np.diff(np.r_[starts, nb])\n    values = vec[starts]\n    assert len(starts) == len(lengths) == len(values)\n    rle = []\n    for start, length, val in zip(starts, lengths, values):\n        if val == bg:\n            continue\n        rle += [str(start), length]\n    # post-processing\n    return \" \".join(map(str, rle))","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:27.444741Z","iopub.execute_input":"2024-02-17T10:01:27.445045Z","iopub.status.idle":"2024-02-17T10:01:27.452735Z","shell.execute_reply.started":"2024-02-17T10:01:27.445018Z","shell.execute_reply":"2024-02-17T10:01:27.451582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.auto import tqdm\n\nsubmission = []\nfor p_img in tqdm(ls_images):\n    path_ = p_img.split(os.path.sep)\n    # parse the submission ID\n    dataset = path_[-3]\n    slice_id, _ = os.path.splitext(path_[-1])\n    # load image to get dimension\n    img = plt.imread(p_img)\n    # sample mask with rectangle\n    mask = np.zeros(img.shape[:2])\n    i, j = int(img.shape[0] / 3), int(img.shape[1] / 4)\n    mask[i:i+50, j:j+100] = 1\n    # submission entry\n    submission.append({\n        \"id\": f\"{dataset}_{slice_id}\",\n        \"rle\": rle_encode(mask)\n    })","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:27.453951Z","iopub.execute_input":"2024-02-17T10:01:27.454224Z","iopub.status.idle":"2024-02-17T10:01:28.020283Z","shell.execute_reply.started":"2024-02-17T10:01:27.454200Z","shell.execute_reply":"2024-02-17T10:01:28.019311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = pd.DataFrame(submission)\ndisplay(df_sub.head())\ndf_sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:01:28.021495Z","iopub.execute_input":"2024-02-17T10:01:28.021840Z","iopub.status.idle":"2024-02-17T10:01:28.089045Z","shell.execute_reply.started":"2024-02-17T10:01:28.021812Z","shell.execute_reply":"2024-02-17T10:01:28.088236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport random\n\n\n\nix  = random.randint(0 , len(imgs)-1)\nplt.subplot(1, 2, 1 )\nplt.imshow(imgs[ix])\nplt.title('Original Image')\n\n\nplt.subplot(1, 2, 2 )\nplt.imshow(thresholded_masks[ix] , cmap='gray')\nplt.title('Mask Image')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:14:33.462267Z","iopub.execute_input":"2024-02-17T10:14:33.462653Z","iopub.status.idle":"2024-02-17T10:14:34.006608Z","shell.execute_reply.started":"2024-02-17T10:14:33.462618Z","shell.execute_reply":"2024-02-17T10:14:34.005706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ix  = random.randint(0 , len(imgs)-1)\nplt.subplot(1, 2, 1 )\nplt.imshow(imgs[ix])\nplt.title('Original Image')\n\n\nplt.subplot(1, 2, 2 )\nplt.imshow(thresholded_masks[ix] , cmap='gray')\nplt.title('Mask Image')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-17T10:15:27.470583Z","iopub.execute_input":"2024-02-17T10:15:27.470981Z","iopub.status.idle":"2024-02-17T10:15:27.954149Z","shell.execute_reply.started":"2024-02-17T10:15:27.470946Z","shell.execute_reply":"2024-02-17T10:15:27.953186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}