{"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":"import numpy as np\nimport pandas as pd \nimport os\nimport tifffile\nimport matplotlib.pyplot as plt\nimport random\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-19T11:00:36.676812Z","iopub.execute_input":"2022-07-19T11:00:36.677132Z","iopub.status.idle":"2022-07-19T11:00:36.823870Z","shell.execute_reply.started":"2022-07-19T11:00:36.677108Z","shell.execute_reply":"2022-07-19T11:00:36.822697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '../input/hubmap-organ-segmentation'\nprint(os.listdir(base_dir))","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:00:36.907460Z","iopub.execute_input":"2022-07-19T11:00:36.907820Z","iopub.status.idle":"2022-07-19T11:00:36.913751Z","shell.execute_reply.started":"2022-07-19T11:00:36.907790Z","shell.execute_reply":"2022-07-19T11:00:36.912688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')\nprint(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:00:37.128419Z","iopub.execute_input":"2022-07-19T11:00:37.128744Z","iopub.status.idle":"2022-07-19T11:00:37.520503Z","shell.execute_reply.started":"2022-07-19T11:00:37.128719Z","shell.execute_reply":"2022-07-19T11:00:37.519286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"prostate:前列腺\nspleen：脾脏\nlung：肺\nkidney：肾\nlarge intestine：大肠","metadata":{"execution":{"iopub.status.busy":"2022-07-18T05:37:45.257863Z","iopub.execute_input":"2022-07-18T05:37:45.258263Z","iopub.status.idle":"2022-07-18T05:37:45.265131Z","shell.execute_reply.started":"2022-07-18T05:37:45.258230Z","shell.execute_reply":"2022-07-18T05:37:45.263598Z"}}},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:00:37.546154Z","iopub.execute_input":"2022-07-19T11:00:37.546473Z","iopub.status.idle":"2022-07-19T11:00:37.570403Z","shell.execute_reply.started":"2022-07-19T11:00:37.546450Z","shell.execute_reply":"2022-07-19T11:00:37.568781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rle转图片\ndef rle2mask(mask_rle,shape):\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 =[\n        np.asarray(x,dtype=int) for x in (s[0:][::2], s[1:][::2])\n    ]\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":{"execution":{"iopub.status.busy":"2022-07-19T11:00:37.756949Z","iopub.execute_input":"2022-07-19T11:00:37.757333Z","iopub.status.idle":"2022-07-19T11:00:37.768355Z","shell.execute_reply.started":"2022-07-19T11:00:37.757305Z","shell.execute_reply":"2022-07-19T11:00:37.766760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_IMAGES_DIR = '../input/hubmap-organ-segmentation/train_images'\nTRAIN_IMAGE_NAMES = os.listdir(TRAIN_IMAGES_DIR)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:00:37.960402Z","iopub.execute_input":"2022-07-19T11:00:37.961340Z","iopub.status.idle":"2022-07-19T11:00:38.007364Z","shell.execute_reply.started":"2022-07-19T11:00:37.961310Z","shell.execute_reply":"2022-07-19T11:00:38.006285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images_and_masks(multiple=True, with_mask=True, img_id=None, organ=None):\n    plt.figure(figsize=(16, 16))\n    if multiple:\n        for ind, image_file_name in tqdm(enumerate(\n            train_df.sample(9)[\"id\"].values if not organ else train_df[train_df[\"organ\"]==organ].sample(9)[\"id\"].values\n        )):\n            plt.subplot(3, 3, ind + 1)\n            image = tifffile.imread(os.path.join(TRAIN_IMAGES_DIR, str(image_file_name)+\".tiff\"))\n            plt.imshow(image)\n            plt.title(str(image_file_name)+\" - \"+train_df[train_df[\"id\"]==image_file_name][\"organ\"].values[0])\n            if with_mask:\n                mask = rle2mask(train_df[train_df[\"id\"] == image_file_name][\"rle\"].values[0],\n                                (image.shape[1], image.shape[0]))\n                plt.imshow(mask, cmap=\"hot\", alpha=0.5)\n            plt.axis(\"off\")\n    else:\n        if not img_id:\n            img_id = random.choice(TRAIN_IMAGE_NAMES).split(\".\")[0]\n        image = tifffile.imread(os.path.join(TRAIN_IMAGES_DIR, img_id+\".tiff\"))\n        plt.imshow(image)\n        plt.title(img_id+\" - \"+train_df[train_df[\"id\"]==int(img_id)][\"organ\"].values[0])\n        if with_mask:\n            mask = rle2mask(train_df[train_df[\"id\"] == int(img_id)][\"rle\"].values[0],\n                            (image.shape[1], image.shape[0]))\n            plt.imshow(mask, cmap=\"hot\", alpha=0.5)\n        plt.axis(\"off\")\n    \n    plt.show()\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:00:38.184520Z","iopub.execute_input":"2022-07-19T11:00:38.184846Z","iopub.status.idle":"2022-07-19T11:00:38.195307Z","shell.execute_reply.started":"2022-07-19T11:00:38.184817Z","shell.execute_reply":"2022-07-19T11:00:38.194541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images_and_masks(multiple=True,with_mask=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:00:38.378404Z","iopub.execute_input":"2022-07-19T11:00:38.378729Z","iopub.status.idle":"2022-07-19T11:00:59.742923Z","shell.execute_reply.started":"2022-07-19T11:00:38.378705Z","shell.execute_reply":"2022-07-19T11:00:59.742265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images_and_masks(multiple=False, with_mask=False, img_id='27350')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:00:59.744557Z","iopub.execute_input":"2022-07-19T11:00:59.745091Z","iopub.status.idle":"2022-07-19T11:01:02.179197Z","shell.execute_reply.started":"2022-07-19T11:00:59.745057Z","shell.execute_reply":"2022-07-19T11:01:02.178044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images_and_masks(multiple=True, with_mask=False, organ='spleen')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:01:02.180579Z","iopub.execute_input":"2022-07-19T11:01:02.180837Z","iopub.status.idle":"2022-07-19T11:01:14.275098Z","shell.execute_reply.started":"2022-07-19T11:01:02.180810Z","shell.execute_reply":"2022-07-19T11:01:14.274062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 图像大小和像素值","metadata":{}},{"cell_type":"code","source":"img_shapes = {}\nPIXEL_MIN_VALUE = 255\nPIXEL_MAX_VALUE = 0\nfor image_file_name in tqdm(TRAIN_IMAGE_NAMES):\n    image = tifffile.imread(os.path.join(TRAIN_IMAGES_DIR, image_file_name))\n    img_shapes[image.shape] = img_shapes.get(image.shape, 0) + 1\n    PIXEL_MIN_VALUE = np.min(image) if np.min(image) < PIXEL_MIN_VALUE else PIXEL_MIN_VALUE\n    PIXEL_MAX_VALUE = np.max(image) if np.max(image) > PIXEL_MAX_VALUE else PIXEL_MAX_VALUE\nprint(\"\\n\\nImages shapes: \", img_shapes)\nprint(f\"\\n\\nPixel values are in range: {PIXEL_MIN_VALUE} - {PIXEL_MAX_VALUE}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:26:33.567541Z","iopub.execute_input":"2022-07-19T11:26:33.568091Z","iopub.status.idle":"2022-07-19T11:26:52.061074Z","shell.execute_reply.started":"2022-07-19T11:26:33.568065Z","shell.execute_reply":"2022-07-19T11:26:52.060001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[[\"img_height\",\"img_width\"]].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:23:32.423898Z","iopub.execute_input":"2022-07-19T11:23:32.424265Z","iopub.status.idle":"2022-07-19T11:23:32.498645Z","shell.execute_reply.started":"2022-07-19T11:23:32.424233Z","shell.execute_reply":"2022-07-19T11:23:32.497925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['organ'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:31:26.470591Z","iopub.execute_input":"2022-07-19T11:31:26.471702Z","iopub.status.idle":"2022-07-19T11:31:26.486982Z","shell.execute_reply.started":"2022-07-19T11:31:26.471628Z","shell.execute_reply":"2022-07-19T11:31:26.485772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/hubmap-organ-segmentation/test.csv')\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:39:08.161771Z","iopub.execute_input":"2022-07-19T11:39:08.162391Z","iopub.status.idle":"2022-07-19T11:39:08.192631Z","shell.execute_reply.started":"2022-07-19T11:39:08.162346Z","shell.execute_reply":"2022-07-19T11:39:08.191751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplot(1, 2, 2)\nimg_HuBMAP = tifffile.imread(os.path.join(base_dir, \"test_images\", \"10078.tiff\"))\nplt.imshow(img_HuBMAP)\nplt.title(\"10078\"+\" - \"+train_df[train_df[\"id\"]==26664][\"organ\"].values[0]+\" - HuBMAP\")\nplt.axis(\"off\")\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:40:29.138907Z","iopub.execute_input":"2022-07-19T11:40:29.139553Z","iopub.status.idle":"2022-07-19T11:40:30.114974Z","shell.execute_reply.started":"2022-07-19T11:40:29.139515Z","shell.execute_reply":"2022-07-19T11:40:30.113429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images_and_masks(multiple=True, with_mask=True, organ=\"spleen\")","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:40:53.494734Z","iopub.execute_input":"2022-07-19T11:40:53.495091Z","iopub.status.idle":"2022-07-19T11:41:15.469335Z","shell.execute_reply.started":"2022-07-19T11:40:53.495065Z","shell.execute_reply":"2022-07-19T11:41:15.468486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images_and_masks(multiple=False, with_mask=False, img_id='12471')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T11:46:35.182129Z","iopub.execute_input":"2022-07-19T11:46:35.182506Z","iopub.status.idle":"2022-07-19T11:46:37.402766Z","shell.execute_reply.started":"2022-07-19T11:46:35.182480Z","shell.execute_reply":"2022-07-19T11:46:37.401737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}