{"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":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport math\nimport glob\nimport gc\nimport tqdm\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torchvision\nimport torchvision.transforms as T\nimport torchvision.transforms.functional as TF\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom torch.utils.data import Dataset, DataLoader\nfrom fastai.vision.all import *\nfrom typing import Optional\nfrom torch.nn.functional import one_hot\nfrom sklearn.model_selection import KFold\nimport random\nimport skimage","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-15T20:42:39.594758Z","iopub.execute_input":"2024-01-15T20:42:39.595206Z","iopub.status.idle":"2024-01-15T20:42:49.380260Z","shell.execute_reply.started":"2024-01-15T20:42:39.595172Z","shell.execute_reply":"2024-01-15T20:42:49.379115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/code/limitz/pytorch-dataset-with-volumetric-augmentations\ndef load_volume(dataset, labeled=True, slice_range=None):\n    ''' Load slices into a volume. Keeps the memory requirement\n        as low as possible by using uint8 and uint16 in CPU memory.\n    '''\n    if labeled:\n        path = os.path.join(dataset, \"labels\", \"*.tif\")\n    else:\n        path = os.path.join(dataset, \"images\", \"*.tif\")\n        \n    dataset = sorted(glob.glob(path))\n    volume = None\n    target = None\n    keys = []\n    offset = 0 if slice_range is None else slice_range[0]\n    depth = len(dataset) if slice_range is None else slice_range[1]-slice_range[0]\n    \n    for z, path in enumerate(tqdm.tqdm(dataset)):\n        if slice_range is not None:\n            if z < slice_range[0]: continue\n            if z >= slice_range[1]: continue\n        \n        parts = path.split(os.path.sep)\n        key = parts[-3] + \"_\" + parts[-1].split(\".\")[0]\n        keys.append(key)\n                \n        if labeled:\n            label = cv2.imread(path, cv2.IMREAD_ANYDEPTH)\n            label = np.array(label,dtype=np.uint8)\n            if target is None:\n                target = np.zeros((1,depth, *label.shape[-2:]), dtype=np.uint8)\n            target[:,z-offset] = label\n        \n        path = path.replace(\"labels\",\"images\")\n        path = path.replace(\"kidney_3_dense\",\"kidney_3_sparse\")\n        image = cv2.imread(path, cv2.IMREAD_ANYDEPTH)\n        image = np.array(image,dtype=np.uint16)\n        \n        if volume is None:\n            volume = np.zeros((1,depth, *image.shape[-2:]), dtype=np.uint16)\n        volume[:,z-offset] = image\n    \n    return volume, target, keys","metadata":{"execution":{"iopub.status.busy":"2024-01-15T20:42:49.382800Z","iopub.execute_input":"2024-01-15T20:42:49.383450Z","iopub.status.idle":"2024-01-15T20:42:49.397309Z","shell.execute_reply.started":"2024-01-15T20:42:49.383410Z","shell.execute_reply":"2024-01-15T20:42:49.396079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_,target,_ = load_volume('/kaggle/input/blood-vessel-segmentation/train/kidney_2')","metadata":{"execution":{"iopub.status.busy":"2024-01-15T20:42:49.398743Z","iopub.execute_input":"2024-01-15T20:42:49.399108Z","iopub.status.idle":"2024-01-15T20:45:40.424843Z","shell.execute_reply.started":"2024-01-15T20:42:49.399077Z","shell.execute_reply":"2024-01-15T20:45:40.423587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install connected-components-3d","metadata":{"execution":{"iopub.status.busy":"2024-01-15T20:45:40.426565Z","iopub.execute_input":"2024-01-15T20:45:40.427097Z","iopub.status.idle":"2024-01-15T20:45:57.298301Z","shell.execute_reply.started":"2024-01-15T20:45:40.427050Z","shell.execute_reply":"2024-01-15T20:45:57.297179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cc3d\nZ,X,Y = target[0].shape\ncore = np.zeros_like(target[0])\nfor axis in [0,1,2]:\n    for i in tqdm.tqdm(range([Z,X,Y][axis])):\n        if axis == 0:\n            PLANE = target[0,i,:,:]\n        elif axis == 1:\n            PLANE = target[0,:,i,:]\n        else:\n            PLANE = target[0,:,:,i]\n            \n        if np.sum(PLANE > 0) > 0:            \n            labels_out = cc3d.connected_components(PLANE)\n            stats = cc3d.statistics(labels_out,no_slice_conversion = True)\n            for k in range(len(stats['centroids'])):\n                c = stats['centroids'][k]\n                if axis == 0:\n                    core[i,int(c[0]),int(c[1])] = stats['voxel_counts'][k]\n                elif axis == 1:\n                    core[int(c[0]),i,int(c[1])] = stats['voxel_counts'][k]\n                else:\n                    core[int(c[0]),int(c[1]),i] = stats['voxel_counts'][k]\n                    \n            no_mask = np.argwhere(PLANE == 0)\n            h,w = np.sum(no_mask,axis=0)//len(no_mask)\n            if axis == 0:\n                core[i,h-1:h+2,w-1:w+2] = 0\n            elif axis == 1:\n                core[h-1:h+2,i,w-1:w+2] = 0\n            else:\n                core[h-1:h+2,w-1:w+2,i] = 0","metadata":{"execution":{"iopub.status.busy":"2024-01-15T21:50:03.838068Z","iopub.execute_input":"2024-01-15T21:50:03.839773Z","iopub.status.idle":"2024-01-15T21:57:55.296616Z","shell.execute_reply.started":"2024-01-15T21:50:03.839643Z","shell.execute_reply":"2024-01-15T21:57:55.295378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for axis in [0,1,2]:\n    plt.imshow(core.max(axis))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T21:59:35.767134Z","iopub.execute_input":"2024-01-15T21:59:35.767679Z","iopub.status.idle":"2024-01-15T21:59:38.611819Z","shell.execute_reply.started":"2024-01-15T21:59:35.767623Z","shell.execute_reply":"2024-01-15T21:59:38.610679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for axis in [0,1,2]:\n    f, axarr = plt.subplots(1,2)\n    axarr[0].imshow(target[0].sum(axis))\n    axarr[1].imshow(core.max(axis))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T22:00:03.113755Z","iopub.execute_input":"2024-01-15T22:00:03.114343Z","iopub.status.idle":"2024-01-15T22:00:16.625946Z","shell.execute_reply.started":"2024-01-15T22:00:03.114293Z","shell.execute_reply":"2024-01-15T22:00:16.624716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values,counts = np.unique(core,return_counts=True)\nplt.plot(values,counts,'.')","metadata":{"execution":{"iopub.status.busy":"2024-01-15T22:01:30.609202Z","iopub.execute_input":"2024-01-15T22:01:30.609707Z","iopub.status.idle":"2024-01-15T22:02:52.444155Z","shell.execute_reply.started":"2024-01-15T22:01:30.609639Z","shell.execute_reply":"2024-01-15T22:02:52.442893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MAX_VALUES = 50000\nTH = -1\nTOTAL_VALUES = 0\nwhile TOTAL_VALUES < MAX_VALUES:\n    TOTAL_VALUES += counts[TH]\n    TH -= 1\nTH = values[TH]\nfor axis in [0,1,2]:\n    plt.imshow((core > TH).max(axis))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T22:21:20.166829Z","iopub.execute_input":"2024-01-15T22:21:20.167257Z","iopub.status.idle":"2024-01-15T22:21:26.276191Z","shell.execute_reply.started":"2024-01-15T22:21:20.167224Z","shell.execute_reply":"2024-01-15T22:21:26.274755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(np.argwhere(core > TH))","metadata":{"execution":{"iopub.status.busy":"2024-01-15T22:21:30.118743Z","iopub.execute_input":"2024-01-15T22:21:30.119215Z","iopub.status.idle":"2024-01-15T22:21:41.267090Z","shell.execute_reply.started":"2024-01-15T22:21:30.119177Z","shell.execute_reply":"2024-01-15T22:21:41.265869Z"},"trusted":true},"execution_count":null,"outputs":[]}]}