{"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":"markdown","source":"Let's take a look at the functional tissue units (FTUs) from each organ.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport torch\nimport torchvision\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:33:19.32046Z","iopub.execute_input":"2022-06-27T03:33:19.320843Z","iopub.status.idle":"2022-06-27T03:33:19.325808Z","shell.execute_reply.started":"2022-06-27T03:33:19.320812Z","shell.execute_reply":"2022-06-27T03:33:19.324921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')\nplt.ylabel('# of images')\ndf.organ.hist();","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:35:31.157243Z","iopub.execute_input":"2022-06-27T03:35:31.157639Z","iopub.status.idle":"2022-06-27T03:35:31.47309Z","shell.execute_reply.started":"2022-06-27T03:35:31.157608Z","shell.execute_reply":"2022-06-27T03:35:31.471897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_ROOT = '../input/hubmap-organ-segmentation/train_images'\nto_tensor = torchvision.transforms.ToTensor()\n\n\ndef get_image(row):\n    id = row.id\n    img_pil = Image.open(f'{TRAIN_ROOT}/{id}.tiff')\n    return to_tensor(img_pil)\n\n\ndef get_mask(row):\n    width = row.img_width\n    height = row.img_height\n    mask = torch.zeros(height * width)\n    for p, length in torch.tensor([int(x) for x in row.rle.split(' ')]).reshape(-1, 2):\n        mask[p:p+length] = 1.0\n    mask = mask.reshape(height, -1).T\n    return mask\n\n\ndef get_image_with_mask(row):\n    img = get_image(row)\n    mask = get_mask(row)\n    height, width = mask.shape\n    img_with_mask = torch.zeros(3, height, width)\n    img_with_mask[0] = mask\n    img_with_mask[1:3] = img.mean(0)\n    return img_with_mask\n\n\nto_pil = torchvision.transforms.ToPILImage()\nresize = torchvision.transforms.Resize((250, 250))\nfor organ in df.organ.unique():\n    print('\\n' + organ)\n    display(to_pil(torchvision.utils.make_grid(\n        [resize(get_image_with_mask(df[df.organ == organ].iloc[i])) for i in range(16)],\n        nrow=4\n    )))","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:31:08.017734Z","iopub.execute_input":"2022-06-27T03:31:08.018582Z","iopub.status.idle":"2022-06-27T03:31:37.659082Z","shell.execute_reply.started":"2022-06-27T03:31:08.018533Z","shell.execute_reply":"2022-06-27T03:31:37.65777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}