{"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-08-26T10:54:55.960423Z","iopub.execute_input":"2022-08-26T10:54:55.961307Z","iopub.status.idle":"2022-08-26T10:54:55.986226Z","shell.execute_reply.started":"2022-08-26T10:54:55.961190Z","shell.execute_reply":"2022-08-26T10:54:55.985206Z"},"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-08-26T10:54:55.988417Z","iopub.execute_input":"2022-08-26T10:54:55.988865Z","iopub.status.idle":"2022-08-26T10:54:57.947357Z","shell.execute_reply.started":"2022-08-26T10:54:55.988828Z","shell.execute_reply":"2022-08-26T10:54:57.946059Z"},"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-08-26T10:54:57.949798Z","iopub.execute_input":"2022-08-26T10:54:57.950432Z","iopub.status.idle":"2022-08-26T10:54:58.829226Z","shell.execute_reply.started":"2022-08-26T10:54:57.950388Z","shell.execute_reply":"2022-08-26T10:54:58.827973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA = '../input/hubmap-organ-segmentation/test_images/'\nCONFIG = '../input/hubmapsegformerb5/mit-b5.pickle'\nMODEL = \"../input/hubmapsegformerb5/last-fold0-3e-4.pth\"\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-08-26T10:54:58.831927Z","iopub.execute_input":"2022-08-26T10:54:58.833175Z","iopub.status.idle":"2022-08-26T10:54:58.926682Z","shell.execute_reply.started":"2022-08-26T10:54:58.833134Z","shell.execute_reply":"2022-08-26T10:54:58.925247Z"},"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-08-26T10:54:58.930394Z","iopub.execute_input":"2022-08-26T10:54:58.930680Z","iopub.status.idle":"2022-08-26T10:54:58.937279Z","shell.execute_reply.started":"2022-08-26T10:54:58.930654Z","shell.execute_reply":"2022-08-26T10:54:58.935395Z"},"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)\n    \nmodel = SegformerForSemanticSegmentation(config)\nmodel_path = MODEL\nmodel.load_state_dict(torch.load(model_path))\nmodel = model.cuda()","metadata":{"execution":{"iopub.status.busy":"2022-08-26T10:54:58.939906Z","iopub.execute_input":"2022-08-26T10:54:58.941148Z","iopub.status.idle":"2022-08-26T10:55:15.926638Z","shell.execute_reply.started":"2022-08-26T10:54:58.941111Z","shell.execute_reply":"2022-08-26T10:55:15.925464Z"},"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-08-26T10:55:15.928148Z","iopub.execute_input":"2022-08-26T10:55:15.929113Z","iopub.status.idle":"2022-08-26T10:55:15.946661Z","shell.execute_reply.started":"2022-08-26T10:55:15.929075Z","shell.execute_reply":"2022-08-26T10:55:15.945771Z"},"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-08-26T10:55:15.948994Z","iopub.execute_input":"2022-08-26T10:55:15.949692Z","iopub.status.idle":"2022-08-26T10:55:15.954044Z","shell.execute_reply.started":"2022-08-26T10:55:15.949652Z","shell.execute_reply":"2022-08-26T10:55:15.952787Z"},"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-08-26T10:55:15.955801Z","iopub.execute_input":"2022-08-26T10:55:15.956168Z","iopub.status.idle":"2022-08-26T10:55:15.965233Z","shell.execute_reply.started":"2022-08-26T10:55:15.956135Z","shell.execute_reply":"2022-08-26T10:55:15.964167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample","metadata":{"execution":{"iopub.status.busy":"2022-08-26T10:55:15.969791Z","iopub.execute_input":"2022-08-26T10:55:15.970139Z","iopub.status.idle":"2022-08-26T10:55:15.990840Z","shell.execute_reply.started":"2022-08-26T10:55:15.970114Z","shell.execute_reply":"2022-08-26T10:55:15.989707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_resize(w, p) :\n    a = 3000/512\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-08-26T10:55:15.994351Z","iopub.execute_input":"2022-08-26T10:55:15.994641Z","iopub.status.idle":"2022-08-26T10:55:16.005129Z","shell.execute_reply.started":"2022-08-26T10:55:15.994615Z","shell.execute_reply":"2022-08-26T10:55:16.003934Z"},"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\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        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    \n    pred = mask.detach().cpu().numpy().astype(np.uint8)\n\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-08-26T10:55:16.007092Z","iopub.execute_input":"2022-08-26T10:55:16.008755Z","iopub.status.idle":"2022-08-26T10:55:22.383309Z","shell.execute_reply.started":"2022-08-26T10:55:16.008703Z","shell.execute_reply":"2022-08-26T10:55:22.382152Z"},"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-08-26T10:55:22.385077Z","iopub.execute_input":"2022-08-26T10:55:22.385903Z","iopub.status.idle":"2022-08-26T10:55:24.457286Z","shell.execute_reply.started":"2022-08-26T10:55:22.385844Z","shell.execute_reply":"2022-08-26T10:55:24.456223Z"},"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-08-26T10:55:24.458318Z","iopub.execute_input":"2022-08-26T10:55:24.458642Z","iopub.status.idle":"2022-08-26T10:55:24.463803Z","shell.execute_reply.started":"2022-08-26T10:55:24.458612Z","shell.execute_reply":"2022-08-26T10:55:24.462673Z"},"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-08-26T10:55:24.465631Z","iopub.execute_input":"2022-08-26T10:55:24.466363Z","iopub.status.idle":"2022-08-26T10:55:24.478334Z","shell.execute_reply.started":"2022-08-26T10:55:24.466327Z","shell.execute_reply":"2022-08-26T10:55:24.477234Z"},"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-08-26T10:55:24.480212Z","iopub.execute_input":"2022-08-26T10:55:24.480984Z","iopub.status.idle":"2022-08-26T10:55:24.493753Z","shell.execute_reply.started":"2022-08-26T10:55:24.480944Z","shell.execute_reply":"2022-08-26T10:55:24.492534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-08-26T10:55:24.495445Z","iopub.execute_input":"2022-08-26T10:55:24.495938Z","iopub.status.idle":"2022-08-26T10:55:24.504916Z","shell.execute_reply.started":"2022-08-26T10:55:24.495899Z","shell.execute_reply":"2022-08-26T10:55:24.503869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}