{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from pathlib import Path\n\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nimport torch\n\nfrom PIL import Image\n\nfrom torch.utils.data import Dataset\n\nimport albumentations as A","metadata":{"execution":{"iopub.status.busy":"2022-08-25T15:10:31.963873Z","iopub.execute_input":"2022-08-25T15:10:31.964375Z","iopub.status.idle":"2022-08-25T15:10:36.196870Z","shell.execute_reply.started":"2022-08-25T15:10:31.964277Z","shell.execute_reply":"2022-08-25T15:10:36.195547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR = Path('../input/hubmap-organ-segmentation')\n\nTRAIN_CSV = ROOT_DIR / Path('train.csv')\nTEST_CSV = ROOT_DIR / Path('test.csv')\nTRAIN_IMAGE_DIR = ROOT_DIR / Path('train_images')\nTEST_IMAGE_DIR = ROOT_DIR / Path('test_images')","metadata":{"execution":{"iopub.status.busy":"2022-08-25T15:10:36.199445Z","iopub.execute_input":"2022-08-25T15:10:36.200723Z","iopub.status.idle":"2022-08-25T15:10:36.209129Z","shell.execute_reply.started":"2022-08-25T15:10:36.200673Z","shell.execute_reply":"2022-08-25T15:10:36.208251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(TRAIN_CSV)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T15:10:36.210303Z","iopub.execute_input":"2022-08-25T15:10:36.211572Z","iopub.status.idle":"2022-08-25T15:10:36.555801Z","shell.execute_reply.started":"2022-08-25T15:10:36.211524Z","shell.execute_reply":"2022-08-25T15:10:36.553646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle2mask(rle: str, shape: tuple, mask = 1):\n    rle_list = rle.split() # split the rle string\n    start, length = [np.asarray(vals, dtype = int) for vals in (rle_list[0::2],rle_list[1::2])]\n    start -= 1\n    \n    img_mask = np.zeros(shape[0] * shape[1],dtype = np.uint8)\n    \n    for st, ln in zip(start, length):\n        img_mask[st:st+ln] = mask\n    img_mask = img_mask.reshape(shape).T[:,:,None]\n    \n    return img_mask","metadata":{"execution":{"iopub.status.busy":"2022-08-25T15:10:36.557872Z","iopub.execute_input":"2022-08-25T15:10:36.558488Z","iopub.status.idle":"2022-08-25T15:10:36.568516Z","shell.execute_reply.started":"2022-08-25T15:10:36.558453Z","shell.execute_reply":"2022-08-25T15:10:36.567233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mask2rle(mask):\n    img_mask = mask.T.reshape(mask.shape[0]* mask.shape[1])\n    img_mask = np.concatenate([[0],img_mask,[0]])\n    \n    run = np.where(img_mask[1:] != img_mask[:-1])[0] + 1\n    \n    run[1::2]  -= run[::2]\n    \n    rle = ' '.join(str(r) for r in run)\n    \n    return rle\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-25T15:10:36.570379Z","iopub.execute_input":"2022-08-25T15:10:36.570729Z","iopub.status.idle":"2022-08-25T15:10:36.583445Z","shell.execute_reply.started":"2022-08-25T15:10:36.570700Z","shell.execute_reply":"2022-08-25T15:10:36.582077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = rle2mask(train_df.rle.iloc[1], shape = (3000,3000))","metadata":{"execution":{"iopub.status.busy":"2022-08-25T15:10:36.584837Z","iopub.execute_input":"2022-08-25T15:10:36.585539Z","iopub.status.idle":"2022-08-25T15:10:36.612892Z","shell.execute_reply.started":"2022-08-25T15:10:36.585505Z","shell.execute_reply":"2022-08-25T15:10:36.611180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-25T15:10:46.291833Z","iopub.execute_input":"2022-08-25T15:10:46.293091Z","iopub.status.idle":"2022-08-25T15:10:46.304418Z","shell.execute_reply.started":"2022-08-25T15:10:46.293036Z","shell.execute_reply":"2022-08-25T15:10:46.303211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask2rle(mask) == train_df.rle.iloc[1]","metadata":{"execution":{"iopub.status.busy":"2022-08-19T16:24:14.534012Z","iopub.execute_input":"2022-08-19T16:24:14.535295Z","iopub.status.idle":"2022-08-19T16:24:14.599076Z","shell.execute_reply.started":"2022-08-19T16:24:14.535253Z","shell.execute_reply":"2022-08-19T16:24:14.598224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T16:24:14.609549Z","iopub.execute_input":"2022-08-19T16:24:14.610009Z","iopub.status.idle":"2022-08-19T16:24:14.634239Z","shell.execute_reply.started":"2022-08-19T16:24:14.609973Z","shell.execute_reply":"2022-08-19T16:24:14.633127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class HBDataset(Dataset):\n    def __init__(self,image_path, csv, transform):\n        self.df = pd.read_csv(csv)\n        self.transform = transform\n        self.image_path = image_path\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        image_id = str(self.df.id.iloc[idx]) +'.tiff'\n        image = np.array(Image.open(self.image_path / image_id), dtype=np.uint8)\n        mask = rle2mask(self.df.rle.iloc[idx], shape = (self.df.img_height.iloc[idx],self.df.img_width.iloc[idx]))\n        \n        if self.transform is not None:\n            transformed = transform(image=image, mask=mask)\n            image = transformed['image']\n            mask = transformed['mask']\n        \n        return str(self.df.id.iloc[idx]), image, mask\n            \n        \n        \n        \n        ","metadata":{"execution":{"iopub.status.busy":"2022-08-19T16:24:20.304848Z","iopub.execute_input":"2022-08-19T16:24:20.305475Z","iopub.status.idle":"2022-08-19T16:24:20.313966Z","shell.execute_reply.started":"2022-08-19T16:24:20.305438Z","shell.execute_reply":"2022-08-19T16:24:20.313133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.Compose([\n    A.Resize(width=1024, height=1024),\n    \n])","metadata":{"execution":{"iopub.status.busy":"2022-08-19T16:24:20.314980Z","iopub.execute_input":"2022-08-19T16:24:20.315990Z","iopub.status.idle":"2022-08-19T16:24:20.334272Z","shell.execute_reply.started":"2022-08-19T16:24:20.315957Z","shell.execute_reply":"2022-08-19T16:24:20.333327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hb_ds = HBDataset(TRAIN_IMAGE_DIR, TRAIN_CSV, transform)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T16:24:20.336101Z","iopub.execute_input":"2022-08-19T16:24:20.336621Z","iopub.status.idle":"2022-08-19T16:24:20.486559Z","shell.execute_reply.started":"2022-08-19T16:24:20.336589Z","shell.execute_reply":"2022-08-19T16:24:20.485476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_path = Path('./image')\ntrain_mask_path = Path('./mask')","metadata":{"execution":{"iopub.status.busy":"2022-08-19T16:33:17.067347Z","iopub.execute_input":"2022-08-19T16:33:17.067775Z","iopub.status.idle":"2022-08-19T16:33:17.074272Z","shell.execute_reply.started":"2022-08-19T16:33:17.067738Z","shell.execute_reply":"2022-08-19T16:33:17.072945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir mask\n!mkdir image\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-19T16:30:42.946067Z","iopub.execute_input":"2022-08-19T16:30:42.946472Z","iopub.status.idle":"2022-08-19T16:30:45.237716Z","shell.execute_reply.started":"2022-08-19T16:30:42.946437Z","shell.execute_reply":"2022-08-19T16:30:45.236306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx in range(len(hb_ds)):\n    image_id, image, mask = hb_ds[idx]\n    pil_image = Image.fromarray(image)\n    pil_mask = Image.fromarray(mask.astype(bool).squeeze(-1))\n    \n    pil_image.save(train_image_path / (image_id + '.png'))\n    pil_mask.save(train_mask_path / (image_id + '_mask.png'))\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-19T16:30:46.189373Z","iopub.execute_input":"2022-08-19T16:30:46.190134Z","iopub.status.idle":"2022-08-19T16:30:46.336479Z","shell.execute_reply.started":"2022-08-19T16:30:46.190088Z","shell.execute_reply":"2022-08-19T16:30:46.335441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image)\nplt.imshow(mask, cmap = 'jet', alpha = 0.5)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T12:15:46.334311Z","iopub.execute_input":"2022-08-19T12:15:46.334686Z","iopub.status.idle":"2022-08-19T12:15:46.556119Z","shell.execute_reply.started":"2022-08-19T12:15:46.334658Z","shell.execute_reply":"2022-08-19T12:15:46.555013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}