{"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":"# 1. Prepare 準備","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport tifffile\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom itertools import chain\nfrom PIL import Image\nfrom PIL import ImageDraw","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-14T11:18:33.782802Z","iopub.execute_input":"2022-08-14T11:18:33.783306Z","iopub.status.idle":"2022-08-14T11:18:34.163275Z","shell.execute_reply.started":"2022-08-14T11:18:33.783260Z","shell.execute_reply":"2022-08-14T11:18:34.161933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/code/pestipeti/decoding-rle-masks/notebook\ndef mask2rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels= img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\n\ndef rle2mask(mask_rle, shape=(3000,3000)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0::2], s[1::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-14T11:18:34.165019Z","iopub.execute_input":"2022-08-14T11:18:34.165487Z","iopub.status.idle":"2022-08-14T11:18:34.177060Z","shell.execute_reply.started":"2022-08-14T11:18:34.165438Z","shell.execute_reply":"2022-08-14T11:18:34.175524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_image(idx,organ):\n    fig, ax = plt.subplots(1,3,figsize=(20,40))\n    df_organ = df_train[df_train['organ']==organ].reset_index()\n    image = plt.imread(f\"../input/hubmap-organ-segmentation/train_images/{df_organ.id[idx]}.tiff\")\n    mask = rle2mask(df_organ.rle[idx],shape=(df_organ.img_height[idx],df_organ.img_width[idx]))\n    display(pd.DataFrame(df_organ.loc[idx,['id','organ','age','sex']]).T)\n    ax[0].imshow(image)\n    ax[0].set_title(\"biopsy slide\")\n    ax[0].axis(\"off\")\n    ax[1].imshow(mask,alpha=0.3,cmap='gray')\n    ax[1].set_title(\"mask\")\n    ax[1].axis(\"off\")\n    ax[2].imshow(image)\n    ax[2].imshow(mask,alpha=0.3,cmap='gray')\n    ax[2].axis(\"off\")\n    plt.show()\n    print('-'*50)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-14T11:23:20.103548Z","iopub.execute_input":"2022-08-14T11:23:20.104028Z","iopub.status.idle":"2022-08-14T11:23:20.115054Z","shell.execute_reply.started":"2022-08-14T11:23:20.103989Z","shell.execute_reply":"2022-08-14T11:23:20.113456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check organ kinds 臓器の種類確認\ndf_train['organ'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T20:14:42.414339Z","iopub.execute_input":"2022-08-13T20:14:42.414713Z","iopub.status.idle":"2022-08-13T20:14:42.601037Z","shell.execute_reply.started":"2022-08-13T20:14:42.414662Z","shell.execute_reply":"2022-08-13T20:14:42.599931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Unnatural data 不自然なデータ","metadata":{}},{"cell_type":"code","source":"show_image(33,'lung')","metadata":{"execution":{"iopub.status.busy":"2022-08-14T11:23:22.060458Z","iopub.execute_input":"2022-08-14T11:23:22.061658Z","iopub.status.idle":"2022-08-14T11:23:26.677524Z","shell.execute_reply.started":"2022-08-14T11:23:22.061614Z","shell.execute_reply":"2022-08-14T11:23:26.676338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The top of the image is completely black.<br>\n画像の上部が切れている。","metadata":{}},{"cell_type":"markdown","source":"# 3. All train data 全学習データ","metadata":{}},{"cell_type":"markdown","source":"## 3-1. kidney 腎臓","metadata":{}},{"cell_type":"code","source":"organ='kidney'\nfor i in range(sum(df_train['organ']==organ)):\n    show_image(i,organ)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T20:48:04.691795Z","iopub.execute_input":"2022-08-13T20:48:04.692858Z","iopub.status.idle":"2022-08-13T20:48:09.184326Z","shell.execute_reply.started":"2022-08-13T20:48:04.692821Z","shell.execute_reply":"2022-08-13T20:48:09.183225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3-2. large intestine 大腸","metadata":{}},{"cell_type":"code","source":"organ='largeintestine'\nfor i in range(sum(df_train['organ']==organ)):\n    show_image(i,organ)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T20:39:16.696330Z","iopub.execute_input":"2022-08-13T20:39:16.697352Z","iopub.status.idle":"2022-08-13T20:39:25.526828Z","shell.execute_reply.started":"2022-08-13T20:39:16.697308Z","shell.execute_reply":"2022-08-13T20:39:25.525751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3-3. lung 肺臓","metadata":{}},{"cell_type":"code","source":"organ='lung'\nfor i in range(sum(df_train['organ']==organ)):\n    show_image(i,organ)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T20:43:01.472550Z","iopub.execute_input":"2022-08-13T20:43:01.473049Z","iopub.status.idle":"2022-08-13T20:43:12.034349Z","shell.execute_reply.started":"2022-08-13T20:43:01.473014Z","shell.execute_reply":"2022-08-13T20:43:12.032426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3-4. spleen 脾臓","metadata":{}},{"cell_type":"code","source":"organ='spleen'\nfor i in range(sum(df_train['organ']==organ)):\n    show_image(i,organ)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T20:45:59.682362Z","iopub.execute_input":"2022-08-13T20:45:59.683302Z","iopub.status.idle":"2022-08-13T20:46:09.321111Z","shell.execute_reply.started":"2022-08-13T20:45:59.683262Z","shell.execute_reply":"2022-08-13T20:46:09.320036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3-5. prostate 前立腺","metadata":{}},{"cell_type":"code","source":"organ='prostate'\nfor i in range(sum(df_train['organ']==organ)):\n    show_image(i,organ)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T20:45:25.907980Z","iopub.execute_input":"2022-08-13T20:45:25.909030Z","iopub.status.idle":"2022-08-13T20:45:35.032608Z","shell.execute_reply.started":"2022-08-13T20:45:25.908973Z","shell.execute_reply":"2022-08-13T20:45:35.031773Z"},"trusted":true},"execution_count":null,"outputs":[]}]}