{"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":"# Since the evaluation method for this competition is \"instance segmentation\", this notebook cannot be used directly for submission.\nPlease use this notebook as a reference for semantic segmentation.\n\nThe codes in this notebook refer to https://github.com/YutaroOgawa/pytorch_advanced/tree/master/3_semantic_segmentation, https://www.kaggle.com/inversion/run-length-decoding-quick-start and https://www.kaggle.com/ihelon/cell-segmentation-run-length-decoding\n\nPlease upvote the notebooks.","metadata":{}},{"cell_type":"markdown","source":"Copyright (c) 2019 Yutaro Ogawa\n\nReleased under the MIT license https://github.com/YutaroOgawa/pytorch_advanced/blob/master/LICENSE","metadata":{}},{"cell_type":"markdown","source":"# Training notebook is [here](https://www.kaggle.com/kurokia/semantic-segmentation-by-pspnet-train).","metadata":{}},{"cell_type":"code","source":"from PIL import Image, ImageOps\nimport cv2\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport torch\n\nimport os\nimport sys","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:15.733398Z","iopub.execute_input":"2021-11-08T12:26:15.733681Z","iopub.status.idle":"2021-11-08T12:26:15.740457Z","shell.execute_reply.started":"2021-11-08T12:26:15.733653Z","shell.execute_reply":"2021-11-08T12:26:15.739681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./utils')\nsys.path.append('./utils')","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:15.742655Z","iopub.execute_input":"2021-11-08T12:26:15.743541Z","iopub.status.idle":"2021-11-08T12:26:15.859256Z","shell.execute_reply.started":"2021-11-08T12:26:15.743497Z","shell.execute_reply":"2021-11-08T12:26:15.857697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from shutil import copyfile\ncopyfile(src = \"../input/utils-inf/data_augumentation.py\", dst = \"./utils/data_augumentation.py\")\ncopyfile(src = \"../input/utils-inf/dataloader.py\", dst = \"./utils/dataloader.py\")\ncopyfile(src = \"../input/utils-inf/pspnet.py\", dst = \"./utils/pspnet.py\")\n\nfrom dataloader import make_datapath_list, DataTransform","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:22.209395Z","iopub.execute_input":"2021-11-08T12:26:22.210179Z","iopub.status.idle":"2021-11-08T12:26:22.22219Z","shell.execute_reply.started":"2021-11-08T12:26:22.210123Z","shell.execute_reply":"2021-11-08T12:26:22.22154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rootpath = \"../input/sartorius-cell-instance-segmentation/\"\nval_anno_list = make_datapath_list(\n    rootpath=rootpath)","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:22.541464Z","iopub.execute_input":"2021-11-08T12:26:22.541914Z","iopub.status.idle":"2021-11-08T12:26:22.547008Z","shell.execute_reply.started":"2021-11-08T12:26:22.541862Z","shell.execute_reply":"2021-11-08T12:26:22.546232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pspnet import PSPNet\n\nnet = PSPNet(n_classes=1)\n\nstate_dict = torch.load(\"../input/weights/pspnet50_40.pth\",\n                        map_location={'cuda:0': 'cpu'})\nnet.load_state_dict(state_dict)","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:22.822537Z","iopub.execute_input":"2021-11-08T12:26:22.823147Z","iopub.status.idle":"2021-11-08T12:26:27.45073Z","shell.execute_reply.started":"2021-11-08T12:26:22.823085Z","shell.execute_reply":"2021-11-08T12:26:27.449925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub_df = pd.read_csv('../input/sartorius-cell-instance-segmentation/sample_submission.csv')\nsample_sub_df","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:27.452361Z","iopub.execute_input":"2021-11-08T12:26:27.452648Z","iopub.status.idle":"2021-11-08T12:26:27.466729Z","shell.execute_reply.started":"2021-11-08T12:26:27.452612Z","shell.execute_reply":"2021-11-08T12:26:27.465922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_id_list = list(sample_sub_df['id'].values)\ntest_id_list","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:27.468231Z","iopub.execute_input":"2021-11-08T12:26:27.469184Z","iopub.status.idle":"2021-11-08T12:26:27.4759Z","shell.execute_reply.started":"2021-11-08T12:26:27.469141Z","shell.execute_reply":"2021-11-08T12:26:27.475139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(mask_rle, shape, color=1):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height, width, channels) of array to return \n    color: color for the mask\n    Returns numpy array (mask)\n\n    '''\n    s = mask_rle.split()\n    \n    starts = list(map(lambda x: int(x) - 1, s[0::2]))\n    lengths = list(map(int, s[1::2]))\n    ends = [x + y for x, y in zip(starts, lengths)]\n    \n    img = np.zeros((shape[0] * shape[1], shape[2]), dtype=np.float32)\n            \n    for start, end in zip(starts, ends):\n        img[start : end] = color\n    \n    return img.reshape(shape)\n\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.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)","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:27.477798Z","iopub.execute_input":"2021-11-08T12:26:27.478091Z","iopub.status.idle":"2021-11-08T12:26:27.490212Z","shell.execute_reply.started":"2021-11-08T12:26:27.478061Z","shell.execute_reply":"2021-11-08T12:26:27.489408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"color_mean = (1.0, 1.0, 1.0)\ncolor_std = (1.0, 1.0, 1.0)","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:27.491214Z","iopub.execute_input":"2021-11-08T12:26:27.491538Z","iopub.status.idle":"2021-11-08T12:26:27.501752Z","shell.execute_reply.started":"2021-11-08T12:26:27.491512Z","shell.execute_reply":"2021-11-08T12:26:27.501062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_test_mask(test_id):\n\n    image_file_path = f\"../input/sartorius-cell-instance-segmentation/test/{test_id}.png\"\n\n    img = Image.open(image_file_path)\n    img = img.convert(\"RGB\")\n    img_width, img_height = img.size\n\n    transform = DataTransform(\n        input_size=520, color_mean=color_mean, color_std=color_std)\n\n    anno_file_path = val_anno_list[0]\n    anno_class_img = Image.open(anno_file_path)  \n    anno_class_img = anno_class_img.convert(\"L\")\n    anno_class_img = ImageOps.invert(anno_class_img)\n    anno_class_img = anno_class_img.quantize()\n    p_palette = anno_class_img.getpalette()\n    phase = \"val\"\n    img, anno_class_img = transform(phase, img, anno_class_img)\n\n    net.eval()\n    x = img.unsqueeze(0) \n    outputs = net(x)\n    y = outputs[0]\n\n    y = y.detach().numpy()[0][0]\n    anno_class_img = Image.fromarray(np.uint8(y), mode=\"P\")\n    anno_class_img = anno_class_img.resize((img_width, img_height), Image.NEAREST)\n    anno_class_img.putpalette(p_palette)\n\n    anno_class_img = anno_class_img.convert('I')\n    n = np.array(anno_class_img).astype(np.uint8)\n    n = np.clip(n, 0, 1)\n\n    test_mask = rle_encode(n)\n\n    sample_sub_df.loc[sample_sub_df['id'] == test_id, \"predicted\"] = test_mask\n    \n    return","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:27.502828Z","iopub.execute_input":"2021-11-08T12:26:27.503154Z","iopub.status.idle":"2021-11-08T12:26:27.513371Z","shell.execute_reply.started":"2021-11-08T12:26:27.503127Z","shell.execute_reply":"2021-11-08T12:26:27.512605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for test_id in test_id_list:\n    make_test_mask(test_id)","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:27.514469Z","iopub.execute_input":"2021-11-08T12:26:27.514708Z","iopub.status.idle":"2021-11-08T12:26:39.690612Z","shell.execute_reply.started":"2021-11-08T12:26:27.514673Z","shell.execute_reply":"2021-11-08T12:26:39.689857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:39.691989Z","iopub.execute_input":"2021-11-08T12:26:39.692382Z","iopub.status.idle":"2021-11-08T12:26:39.700942Z","shell.execute_reply.started":"2021-11-08T12:26:39.692331Z","shell.execute_reply":"2021-11-08T12:26:39.700045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_masks(image_id, colors=True):\n    labels = sample_sub_df[sample_sub_df[\"id\"] == image_id][\"predicted\"].tolist()\n\n    if colors:\n        mask = np.zeros((520, 704, 3))\n        for label in labels:\n            mask += rle_decode(label, shape=(520, 704, 3), color=np.random.rand(3))\n    else:\n        mask = np.zeros((520, 704, 1))\n        for label in labels:\n            mask += rle_decode(label, shape=(520, 704, 1))\n    mask = mask.clip(0, 1)\n\n    image = cv2.imread(f\"../input/sartorius-cell-instance-segmentation/test/{image_id}.png\")\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n    plt.figure(figsize=(16, 32))\n    plt.subplot(3, 1, 1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    plt.subplot(3, 1, 2)\n    plt.imshow(image)\n    plt.imshow(mask, alpha=0.5)\n    plt.axis(\"off\")\n    plt.subplot(3, 1, 3)\n    plt.imshow(mask)\n    plt.axis(\"off\")\n    \n    plt.show();","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:39.702655Z","iopub.execute_input":"2021-11-08T12:26:39.702924Z","iopub.status.idle":"2021-11-08T12:26:39.71423Z","shell.execute_reply.started":"2021-11-08T12:26:39.702894Z","shell.execute_reply":"2021-11-08T12:26:39.713548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_masks(\"7ae19de7bc2a\", colors=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-08T12:26:39.716194Z","iopub.execute_input":"2021-11-08T12:26:39.716513Z","iopub.status.idle":"2021-11-08T12:26:40.59401Z","shell.execute_reply.started":"2021-11-08T12:26:39.716481Z","shell.execute_reply":"2021-11-08T12:26:40.593105Z"},"trusted":true},"execution_count":null,"outputs":[]}]}