{"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 matplotlib.pyplot as plt\nimport cv2\nimport pandas as pd\nimport random\nimport tqdm\nimport imgaug.augmenters as iaa\nimport imgaug\nimport os\nimport torch\nfrom torch import nn\nimport glob\nimport copy\nfrom skimage.transform import rescale, resize\nimport pytorch_lightning as pl\nimport sys\nsys.path.append('../input/coat-train-env/package')\nsys.path.append('../input/coat-train-env')","metadata":{"papermill":{"duration":8.786666,"end_time":"2022-08-11T12:20:58.216157","exception":false,"start_time":"2022-08-11T12:20:49.429491","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-07T09:07:12.757105Z","iopub.execute_input":"2022-09-07T09:07:12.757668Z","iopub.status.idle":"2022-09-07T09:07:18.331681Z","shell.execute_reply.started":"2022-09-07T09:07:12.757549Z","shell.execute_reply":"2022-09-07T09:07:18.330612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev = torch.device('cuda')","metadata":{"execution":{"iopub.status.busy":"2022-09-07T09:07:21.310037Z","iopub.execute_input":"2022-09-07T09:07:21.311525Z","iopub.status.idle":"2022-09-07T09:07:21.316961Z","shell.execute_reply.started":"2022-09-07T09:07:21.311476Z","shell.execute_reply":"2022-09-07T09:07:21.315885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import model\n#model = model.Model().load_from_checkpoint('../input/coat-e70-dice07974-fold0/epoch70-val_dice0.7974.ckpt').to(dev)\n#model = model.Model().load_from_checkpoint('../input/coat-e96-dice08004-aspp-fold1/768_epoch96-val_dice0.8004.ckpt').to(dev)\nmodel = model.Model().load_from_checkpoint('../input/coat-no-aspp-768-fold3-07838/768_epoch78-val_dice0.7838_fold3.ckpt').to(dev)\nmodel.decoder_mask.dropout.p = 0","metadata":{"execution":{"iopub.status.busy":"2022-09-07T09:07:22.711266Z","iopub.execute_input":"2022-09-07T09:07:22.711811Z","iopub.status.idle":"2022-09-07T09:07:34.490243Z","shell.execute_reply.started":"2022-09-07T09:07:22.711775Z","shell.execute_reply":"2022-09-07T09:07:34.489068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_PATH = '../input/hubmap-organ-segmentation'\nINPUT_SHAPE = [768, 768]","metadata":{"papermill":{"duration":0.014816,"end_time":"2022-08-11T12:20:58.23548","exception":false,"start_time":"2022-08-11T12:20:58.220664","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-07T09:07:37.682746Z","iopub.execute_input":"2022-09-07T09:07:37.683437Z","iopub.status.idle":"2022-09-07T09:07:37.688252Z","shell.execute_reply.started":"2022-09-07T09:07:37.683399Z","shell.execute_reply":"2022-09-07T09:07:37.686992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle2mask(rle, shape):\n    '''\n    mask_rle: run-length as string formatted (start length)\n    shape: (height, width) of array to return \n    Returns numpy array <- 1(mask), 0(background)\n    '''\n    s = 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, order='F')  # Needed to align to RLE direction\n\n\ndef mask2rle(img, shape, small_mask_threshold):\n    '''\n    Convert mask to rle.\n    img: numpy array <- 1(mask), 0(background)\n    Returns run length as string formated\n    \n    pixels = np.array([1,1,1,0,0,1,0,1,1]) #-> rle = '1 3 6 1 8 2'\n    pixels = np.concatenate([[0], pixels, [0]]) #[0,1,1,1,0,0,1,0,1,1,0]\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1 #[ 1  4  6  7  8 10] bit change points\n    print(runs[1::2]) #[4 7 10]\n    print(runs[::2]) #[1 6 8]\n    runs[1::2] -= runs[::2]\n    print(runs) #[1 3 6 1 8 2]\n    '''\n    if img.shape != shape:\n        h,w = shape\n        img = cv2.resize(img, dsize=(w,h), interpolation=cv2.INTER_LINEAR)\n    img = img.astype(np.int8) \n    pixels = img.T.flatten()\n    #pixels = np.concatenate([[0], pixels, [0]])\n    pixels = np.pad(pixels, ((1, 1), ))\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    if runs[1::2].sum() <= small_mask_threshold:\n        return ''\n    else:\n        return ' '.join(str(x) for x in runs)","metadata":{"papermill":{"duration":0.018663,"end_time":"2022-08-11T12:20:58.257466","exception":false,"start_time":"2022-08-11T12:20:58.238803","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-07T09:07:40.041740Z","iopub.execute_input":"2022-09-07T09:07:40.042457Z","iopub.status.idle":"2022-09-07T09:07:40.053136Z","shell.execute_reply.started":"2022-09-07T09:07:40.042417Z","shell.execute_reply":"2022-09-07T09:07:40.052150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_img(i, df, train=True):\n    idx = df.iloc[i].id\n    if train:\n        rle = df.iloc[i].rle\n        h, w = df.iloc[i].img_height, train_df.iloc[i].img_width\n        organ = df.iloc[i].organ\n        img = cv2.imread(f'{DATA_PATH}/train_images/{idx}.tiff')\n        mask = rle2mask(rle, (h, w))\n        return img, mask\n    else:\n        img = cv2.imread(f'{DATA_PATH}/test_images/{idx}.tiff')\n        return img","metadata":{"papermill":{"duration":0.01464,"end_time":"2022-08-11T12:20:58.300055","exception":false,"start_time":"2022-08-11T12:20:58.285415","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-07T09:07:42.038293Z","iopub.execute_input":"2022-09-07T09:07:42.038749Z","iopub.status.idle":"2022-09-07T09:07:42.046359Z","shell.execute_reply.started":"2022-09-07T09:07:42.038709Z","shell.execute_reply":"2022-09-07T09:07:42.045339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/hubmap-organ-segmentation/test.csv')\ntrain_df = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')","metadata":{"papermill":{"duration":0.383325,"end_time":"2022-08-11T12:20:58.68682","exception":false,"start_time":"2022-08-11T12:20:58.303495","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-07T09:07:43.263793Z","iopub.execute_input":"2022-09-07T09:07:43.264753Z","iopub.status.idle":"2022-09-07T09:07:43.545645Z","shell.execute_reply.started":"2022-09-07T09:07:43.264703Z","shell.execute_reply":"2022-09-07T09:07:43.544692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ref = dict()\nfor organ in ['prostate', 'spleen', 'lung', 'kidney', 'largeintestine']:\n    organ_ref = train_df[train_df['organ']==organ].iloc[0].id\n    organ_ref = cv2.imread(f'{DATA_PATH}/train_images/{organ_ref}.tiff')[..., ::-1]\n    ref[organ] = organ_ref","metadata":{"papermill":{"duration":2.440194,"end_time":"2022-08-11T12:21:19.593455","exception":false,"start_time":"2022-08-11T12:21:17.153261","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-07T09:07:44.405436Z","iopub.execute_input":"2022-09-07T09:07:44.405808Z","iopub.status.idle":"2022-09-07T09:07:46.432274Z","shell.execute_reply.started":"2022-09-07T09:07:44.405777Z","shell.execute_reply":"2022-09-07T09:07:46.426598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_image(img):\n    with torch.no_grad():\n        resized = cv2.resize(img, tuple(INPUT_SHAPE)).astype(np.float32) / 255\n        resized = np.stack([\n            resized,\n            resized[::-1],\n            resized[:, ::-1],\n            resized[::-1, ::-1],\n            np.transpose(resized, (1, 0, 2))\n        ], axis=0)\n        pred = model.forward_seg(\n            torch.tensor(resized).to(model.device).permute(0, 3, 1, 2)\n        ).sigmoid().detach().cpu().numpy()[:, 0]\n        pred = (pred[0] + pred[1, ::-1] + pred[2, :, ::-1] + pred[3, ::-1, ::-1] + np.transpose(pred[4], (1, 0))) * 0.2\n        #pred = cv2.resize(pred[0,0], img.shape[:2])\n    return pred","metadata":{"execution":{"iopub.status.busy":"2022-09-07T09:07:46.434072Z","iopub.execute_input":"2022-09-07T09:07:46.434463Z","iopub.status.idle":"2022-09-07T09:07:46.451102Z","shell.execute_reply.started":"2022-09-07T09:07:46.434426Z","shell.execute_reply":"2022-09-07T09:07:46.450204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"organ_threshold = {\n    'Hubmap': {\n        'kidney'        : 0.4,\n        'prostate'      : 0.4,\n        'largeintestine': 0.4,\n        'spleen'        : 0.4,\n        'lung'          : 0.1,\n    },\n    'HPA': {\n        'kidney'        : 0.50,\n        'prostate'      : 0.50,\n        'largeintestine': 0.50,\n        'spleen'        : 0.50,\n        'lung'          : 0.10,\n    },\n}\n\ndata_sources = ['Hubmap']\ndf = test_df\nres_df = pd.DataFrame(columns=['id', 'rle'])\nfor i in tqdm.tqdm(list(range(df.shape[0]))):\n    idx = df.iloc[i].id\n    organ = df.iloc[i].organ\n    source = df.iloc[i].data_source\n    threshold = organ_threshold[source][organ]\n    if source in data_sources:\n        #img_init, _ = read_img(i, df, train=True)\n        img_init = read_img(i, df, train=False)\n        img_init = img_init[..., ::-1]\n\n\n        scale =  df.iloc[i].pixel_size / 0.4\n\n        img_rescaled = cv2.resize(img_init, None, fx=scale, fy=scale)\n        \n        r_mask = predict_image(img_rescaled)\n        r_mask = resize(r_mask, img_init.shape[:2], order=0)\n        if source == 'HPA':\n            mask = np.zeros_like(r_mask)\n            mask = cv2.circle(mask, (mask.shape[1] // 2, mask.shape[0] // 2), mask.shape[0] // 2, (1.,), thickness=-1)\n            r_mask *= mask\n        rle = mask2rle((r_mask > threshold).astype(np.uint8), img_init.shape[:2], 0)\n    else:\n        rle = ''\n    tmp_res = {'id': [idx], 'rle': [rle]}\n    tmp_df = pd.DataFrame.from_dict(tmp_res)\n    res_df = pd.concat([res_df, tmp_df], ignore_index=True)\n    \nres_df.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"papermill":{"duration":19.683067,"end_time":"2022-08-11T12:21:39.314507","exception":false,"start_time":"2022-08-11T12:21:19.63144","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-07T09:10:07.569500Z","iopub.execute_input":"2022-09-07T09:10:07.570384Z","iopub.status.idle":"2022-09-07T09:10:08.315242Z","shell.execute_reply.started":"2022-09-07T09:10:07.570338Z","shell.execute_reply":"2022-09-07T09:10:08.314048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nmasks = list()\ndata_sources = ['Hubmap', 'HPA']\ndf = train_df\nres_df = pd.DataFrame(columns=['id', 'rle'])\nfor i in tqdm.tqdm(range(len(df))):\n    idx = df.iloc[i].id\n    organ = df.iloc[i].organ\n    source = df.iloc[i].data_source\n    threshold = organ_threshold[source][organ]\n    if source in data_sources:\n        img_init, mask_true = read_img(i, df, train=True)\n        img_init = img_init[..., ::-1]\n\n\n        scale =  df.iloc[i].pixel_size / 0.4\n\n        # scale image by pixel size\n        #img_rescaled = rescale(img_init, scale, channel_axis=2, order=1, anti_aliasing=True) * 255\n        img_rescaled = cv2.resize(img_init, None, fx=scale, fy=scale)\n        \n        r_mask = predict_image(img_rescaled, inner_size=256)\n        masks.append(cv2.resize(r_mask), dsize=None, fx=1/3, fy=1/3)\n        break\n        rle = mask2rle((r_mask>0).astype(np.uint8), img_init.shape[:2], 0)\n    else:\n        rle = ''\n    tmp_res = {'id': [idx], 'rle': [rle]}\n    tmp_df = pd.DataFrame.from_dict(tmp_res)\n    res_df = pd.concat([res_df, tmp_df], ignore_index=True)\n    \n#res_df.to_csv('/kaggle/working/submission.csv', index=False)\n\"\"\"","metadata":{"papermill":{"duration":0.017699,"end_time":"2022-08-11T12:21:39.338117","exception":false,"start_time":"2022-08-11T12:21:39.320418","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-08-17T08:15:37.376733Z","iopub.execute_input":"2022-08-17T08:15:37.37712Z","iopub.status.idle":"2022-08-17T08:15:37.38459Z","shell.execute_reply.started":"2022-08-17T08:15:37.377088Z","shell.execute_reply":"2022-08-17T08:15:37.383484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\ndef dice(thresh):\n    dice_sum = 0\n    for mask, (_, row) in zip((masks), test_data.iterrows()):\n        true_mask = (cv2.resize(rle2mask(row.rle, (row.img_width, row.img_height)), dsize=None, fx=1/3, fy=1/3) > 0).reshape(-1)\n        pred_mask = (mask > thresh).reshape(-1)\n        dice_sum += 2 * (true_mask & pred_mask).sum() / (true_mask.sum() + pred_mask.sum())\n    return dice_sum / len(masks)\n\ndice(0.5)\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2022-08-11T12:21:39.349435Z","iopub.status.busy":"2022-08-11T12:21:39.347304Z","iopub.status.idle":"2022-08-11T12:21:39.356263Z","shell.execute_reply":"2022-08-11T12:21:39.354848Z"},"papermill":{"duration":0.016498,"end_time":"2022-08-11T12:21:39.358799","exception":false,"start_time":"2022-08-11T12:21:39.342301","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.004393,"end_time":"2022-08-11T12:21:39.367672","exception":false,"start_time":"2022-08-11T12:21:39.363279","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}