{"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":"###### <center>\n    <h1>[Inference] - FastAI Baseline</h1>\n<center>","metadata":{}},{"cell_type":"markdown","source":"<center>\n<img src=\"https://hubmapconsortium.org/wp-content/uploads/2019/01/HuBMAP-Retina-Logo-Color.png\">\n</center>","metadata":{}},{"cell_type":"code","source":"# import sys\n# sys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\n# import timm","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:06.067892Z","iopub.execute_input":"2022-09-15T14:24:06.068531Z","iopub.status.idle":"2022-09-15T14:24:06.093595Z","shell.execute_reply.started":"2022-09-15T14:24:06.068425Z","shell.execute_reply":"2022-09-15T14:24:06.092383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nLovasz-Softmax and Jaccard hinge loss in PyTorch\nMaxim Berman 2018 ESAT-PSI KU Leuven (MIT License)\n\"\"\"\nimport torch\nfrom torch.autograd import Variable\nimport torch.nn.functional as F\nimport torch.nn as nn\nimport numpy as np\ntry:\n    from itertools import  ifilterfalse\nexcept ImportError: # py3k\n    from itertools import  filterfalse\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-15T14:24:06.095771Z","iopub.execute_input":"2022-09-15T14:24:06.096130Z","iopub.status.idle":"2022-09-15T14:24:07.915366Z","shell.execute_reply.started":"2022-09-15T14:24:06.096094Z","shell.execute_reply":"2022-09-15T14:24:07.914304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport cv2\nimport rasterio\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nfrom tqdm import tqdm\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\nfrom rasterio.windows import Window\nfrom torch.utils.data import Dataset, DataLoader\nimport warnings; warnings.filterwarnings(\"ignore\")","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":3.066435,"end_time":"2021-03-12T06:33:17.956368","exception":false,"start_time":"2021-03-12T06:33:14.889933","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-15T14:24:07.919541Z","iopub.execute_input":"2022-09-15T14:24:07.920022Z","iopub.status.idle":"2022-09-15T14:24:08.753662Z","shell.execute_reply.started":"2022-09-15T14:24:07.919991Z","shell.execute_reply":"2022-09-15T14:24:08.752349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TH = 0.20\n\nTHs = {'lung':TH, 'kidney':TH, 'largeintestine':TH, 'prostate':TH, 'spleen':TH}\n\nDATA = '../input/hubmap-organ-segmentation/test_images/'\nCONFIG = '../input/hubmapsegformer/mit-b5.pickle'\nMODELS = [\n    \"../input/hubmapsegformerlast/768-merge-fold0.pth\",\n    \"../input/hubmapsegformerlast/768-merge-fold1.pth\",\n    \"../input/hubmapsegformerlast/768-merge-fold2.pth\",\n    \"../input/hubmapsegformerlast/768-model-b5.pth\",\n    \"../input/hubmapsegformerlast/768-model-b5-2.pth\",\n]\n# MODELS = [f'../input/hubmapunext50base/model_{i}.pth' for i in range(4)]\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/sample_submission.csv')\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"papermill":{"duration":0.024698,"end_time":"2021-03-12T06:33:17.991398","exception":false,"start_time":"2021-03-12T06:33:17.9667","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-15T14:24:08.761127Z","iopub.execute_input":"2022-09-15T14:24:08.763853Z","iopub.status.idle":"2022-09-15T14:24:08.857115Z","shell.execute_reply.started":"2022-09-15T14:24:08.763793Z","shell.execute_reply":"2022-09-15T14:24:08.856105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{"papermill":{"duration":0.008902,"end_time":"2021-03-12T06:33:18.153045","exception":false,"start_time":"2021-03-12T06:33:18.144143","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class2idx = {'prostate': 1,\n  'spleen': 2,\n  'lung': 3,\n  'kidney': 4,\n  'largeintestine': 5,\n  'none': 0}\nidx2class = {1: 'prostate',\n  2: 'spleen',\n  3: 'lung',\n  4: 'kidney',\n  5: 'largeintestine',\n  0: 'none'}","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:08.861794Z","iopub.execute_input":"2022-09-15T14:24:08.864350Z","iopub.status.idle":"2022-09-15T14:24:08.872026Z","shell.execute_reply.started":"2022-09-15T14:24:08.864310Z","shell.execute_reply":"2022-09-15T14:24:08.870743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import SegformerForSemanticSegmentation\n\nimport pickle\n\nconfig = {}\nwith open(CONFIG, mode=\"rb\") as f:\n    config = pickle.load(f)\n    \n    \n# from transformers import SegformerForSemanticSegmentation\n# from transformers import SegformerModel, SegformerConfig\n# MODEL_NAME=\"nvidia/segformer-b2-finetuned-ade-512-512\"\n# config = SegformerConfig.from_pretrained(MODEL_NAME,\n#                         num_labels=len(class2idx), \n#                         id2label=idx2class, \n#                         label2id=class2idx,\n# )\n\n# with open(\"mit-b2.pickle\", mode=\"wb\") as f :\n#     pickle.dump(config, f)\nmodels = []\nfor MODEL in MODELS:\n    model = SegformerForSemanticSegmentation(config)\n    model_path = MODEL\n    model.load_state_dict(torch.load(model_path))\n    model = model.cuda()\n    model.eval()\n    models.append(model)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:08.873957Z","iopub.execute_input":"2022-09-15T14:24:08.874634Z","iopub.status.idle":"2022-09-15T14:24:32.652075Z","shell.execute_reply.started":"2022-09-15T14:24:08.874598Z","shell.execute_reply":"2022-09-15T14:24:32.651045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{"papermill":{"duration":0.009141,"end_time":"2021-03-12T06:33:32.112738","exception":false,"start_time":"2021-03-12T06:33:32.103597","status":"completed"},"tags":[]}},{"cell_type":"code","source":"DATA = '../input/hubmap-organ-segmentation/test_images/'\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/test.csv').set_index('id')","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:32.653551Z","iopub.execute_input":"2022-09-15T14:24:32.654257Z","iopub.status.idle":"2022-09-15T14:24:32.672768Z","shell.execute_reply.started":"2022-09-15T14:24:32.654216Z","shell.execute_reply":"2022-09-15T14:24:32.671900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DATA = '../input/hubmap-organ-segmentation/train_images/'\n# df_sample = pd.read_csv('../input/hubmap-organ-segmentation/train.csv').set_index('id')","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:25:51.304629Z","iopub.execute_input":"2022-09-15T14:25:51.305007Z","iopub.status.idle":"2022-09-15T14:25:51.638349Z","shell.execute_reply.started":"2022-09-15T14:25:51.304975Z","shell.execute_reply":"2022-09-15T14:25:51.637345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode_less_memory(img):\n    pixels = img.T.flatten()\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:32.681480Z","iopub.execute_input":"2022-09-15T14:24:32.681990Z","iopub.status.idle":"2022-09-15T14:24:32.690580Z","shell.execute_reply.started":"2022-09-15T14:24:32.681951Z","shell.execute_reply":"2022-09-15T14:24:32.689127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample[:10]","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:32.694457Z","iopub.execute_input":"2022-09-15T14:24:32.694835Z","iopub.status.idle":"2022-09-15T14:24:32.713993Z","shell.execute_reply.started":"2022-09-15T14:24:32.694796Z","shell.execute_reply":"2022-09-15T14:24:32.713052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_resize(w, p) :\n    a = 3000/768\n    target = int((w*p/0.4)/a + 0.5)\n    target = (target+31)//32 * 32\n    return target\n\ncalc_resize(2023, 0.4945), calc_resize(3000, 0.4)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:32.715707Z","iopub.execute_input":"2022-09-15T14:24:32.716180Z","iopub.status.idle":"2022-09-15T14:24:32.725097Z","shell.execute_reply.started":"2022-09-15T14:24:32.716142Z","shell.execute_reply":"2022-09-15T14:24:32.723786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # TH = 0.225\n# from transformers import SegformerFeatureExtractor\n\n# names,preds = [],[]\n# imgs, pd_mks = [],[]\n# debug = len(df_sample)<2\n# # debug = True\n# # tta = True\n\n# model.eval()\n\n# cnt = 0\n# file = \"\"\n# for idx,row in tqdm(df_sample.iterrows(),total=len(df_sample)):\n#     image = Image.open(os.path.join(DATA,str(idx)+'.tiff'))\n#     size = calc_resize(row.img_height, row.pixel_size)\n#     feature_extractor = SegformerFeatureExtractor(reduce_labels=False, size=(size,size))\n#     encoding = feature_extractor(image, return_tensors=\"pt\")\n# #     print(idx)\n#     pixel_values = encoding.pixel_values.cuda()\n# #     print(pixel_values.shape)\n#     organ = row.organ\n#     height, width = row.img_height, row.img_width\n#     index = class2idx[organ]\n#     with torch.no_grad():\n#         pred = []\n#         for model in models:\n#             outputs = model(pixel_values=pixel_values)\n#             upsampled_logits = nn.functional.interpolate(outputs['logits'],\n#                         # size=image.size[::-1], # (height, width)\n#                         (height, width),\n#                         mode='bilinear',\n#                         align_corners=False)\n#             mask = upsampled_logits.argmax(dim=1)[0]\n#             mask[mask != index] = 0\n#             mask[mask == index] = 1\n#             if len(pred) == 0 :\n#                 pred = mask.detach().cpu().numpy().astype(np.uint8)\n#             else :\n#                 pred += mask.detach().cpu().numpy().astype(np.uint8)\n                \n#     pred[pred < TH] = 0\n#     pred[pred >= TH] = 1\n#     rle = rle_encode_less_memory(pred)\n\n#     names.append(str(idx))\n#     preds.append(rle)\n#     if debug:\n#         imgs.append(image)\n#         pd_mks.append(pred)\n    \n#     if debug and cnt == 10:\n#         file = os.path.join(DATA,str(idx)+'.tiff')\n#         break\n#     cnt+=1\n\n#     del image, mask, rle, idx, row, pred\n#     gc.collect()    \n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:32.727095Z","iopub.execute_input":"2022-09-15T14:24:32.727639Z","iopub.status.idle":"2022-09-15T14:24:32.735474Z","shell.execute_reply.started":"2022-09-15T14:24:32.727598Z","shell.execute_reply":"2022-09-15T14:24:32.734142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TH = 0.225\nfrom transformers import SegformerFeatureExtractor\n\nnames,preds = [],[]\nimgs, pd_mks = [],[]\ndebug = len(df_sample)<2\n# debug = True\n# tta = True\n\nmodel.eval()\n\ncnt = 0\nfile = \"\"\nfor idx,row in tqdm(df_sample.iterrows(),total=len(df_sample)):\n    image = Image.open(os.path.join(DATA,str(idx)+'.tiff'))\n    size = calc_resize(row.img_height, row.pixel_size)\n    feature_extractor = SegformerFeatureExtractor(reduce_labels=False, size=(size,size))\n    encoding = feature_extractor(image, return_tensors=\"pt\")\n#     print(idx)\n    pixel_values = encoding.pixel_values.cuda()\n#     print(pixel_values.shape)\n    organ = row.organ\n    height, width = row.img_height, row.img_width\n    index = class2idx[organ]\n    with torch.no_grad():\n        pred = []\n        for model in models:\n            model.eval()\n            with torch.no_grad() :\n                outputs = model(pixel_values=pixel_values)\n            upsampled_logits = nn.functional.interpolate(outputs['logits'],\n                        # size=image.size[::-1], # (height, width)\n                        (height, width),\n                        mode='bilinear',\n                        align_corners=False)\n            mask = upsampled_logits.argmax(dim=1)[0]\n            mask[mask != index] = 0\n            mask[mask == index] = 1\n            mask = mask * F.sigmoid(upsampled_logits[0][index])\n#             print(mask.shape, upsampled_logits.shape, mask.min(), mask.max())\n#             plt.imshow(mask.cpu().numpy())\n#             plt.show()\n            if len(pred) == 0 :\n                pred = mask.detach().cpu().numpy()\n            else :\n#                 pred += mask.detach().cpu().numpy()\n                pred = np.fmax(pred, mask.detach().cpu().numpy())\n#     pred /= len(models)\n#     plt.imshow(pred)\n#     plt.show()\n    pred[pred <= THs[row.organ]] = 0\n    pred[pred > THs[row.organ]] = 1\n#     plt.imshow(pred)\n#     plt.show()\n    rle = rle_encode_less_memory(pred)\n\n    names.append(str(idx))\n    preds.append(rle)\n    if debug:\n        imgs.append(image)\n        pd_mks.append(pred)\n    \n    if debug and cnt == 10:\n        file = os.path.join(DATA,str(idx)+'.tiff')\n        break\n    cnt+=1\n\n    del image, mask, rle, idx, row, pred\n    gc.collect()    \n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:25:59.792307Z","iopub.execute_input":"2022-09-15T14:25:59.792962Z","iopub.status.idle":"2022-09-15T14:26:12.369179Z","shell.execute_reply.started":"2022-09-15T14:25:59.792914Z","shell.execute_reply":"2022-09-15T14:26:12.368025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#debug = True\nif debug:\n    import matplotlib.pyplot as plt\n    for img, mask in zip(imgs, pd_mks):\n        plt.figure(figsize=(12, 7))\n        plt.subplot(1, 3, 1); plt.imshow(img); plt.axis('OFF'); plt.title('image')\n        plt.subplot(1, 3, 2); plt.imshow(mask*255); plt.axis('OFF'); plt.title('mask')\n        plt.subplot(1, 3, 3); plt.imshow(img); plt.imshow(mask*255, alpha=0.4); plt.axis('OFF'); plt.title('overlay')\n        plt.tight_layout()\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:26:12.371734Z","iopub.execute_input":"2022-09-15T14:26:12.372647Z","iopub.status.idle":"2022-09-15T14:26:56.794646Z","shell.execute_reply.started":"2022-09-15T14:26:12.372603Z","shell.execute_reply":"2022-09-15T14:26:56.793123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n# plt.imshow(pred)\n# pred.max()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:41.308088Z","iopub.execute_input":"2022-09-15T14:24:41.308400Z","iopub.status.idle":"2022-09-15T14:24:41.313870Z","shell.execute_reply.started":"2022-09-15T14:24:41.308371Z","shell.execute_reply":"2022-09-15T14:24:41.312824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img  = cv2.imread(file)\n# img[:,:,0] = pred*255\n# plt.imshow(img)\n# img.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:41.316163Z","iopub.execute_input":"2022-09-15T14:24:41.316540Z","iopub.status.idle":"2022-09-15T14:24:41.325171Z","shell.execute_reply.started":"2022-09-15T14:24:41.316502Z","shell.execute_reply":"2022-09-15T14:24:41.322491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'id':names,'rle':preds})\ndf.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:41.327307Z","iopub.execute_input":"2022-09-15T14:24:41.328115Z","iopub.status.idle":"2022-09-15T14:24:41.337474Z","shell.execute_reply.started":"2022-09-15T14:24:41.328057Z","shell.execute_reply":"2022-09-15T14:24:41.336455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_sample = pd.read_csv('../input/hubmap-organ-segmentation/test.csv')\n# df.id = df.id.astype(str)\n# df_sample.id = df_sample.id.astype(str)\n# df = pd.merge(df, df_sample, on=\"id\")\n\n# RLE = []\n# for i in range(len(df)):\n#     rle = df.iloc[i].rle\n#     src = df.iloc[i].data_source\n#     if src != \"Hubmap\" :\n#         RLE.append(\"\")\n#         continue\n#     RLE.append(rle)\n\n# df['rle'] = RLE\n# df[['id','rle']].to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:24:41.339121Z","iopub.execute_input":"2022-09-15T14:24:41.339866Z","iopub.status.idle":"2022-09-15T14:24:41.344948Z","shell.execute_reply.started":"2022-09-15T14:24:41.339828Z","shell.execute_reply":"2022-09-15T14:24:41.343821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}