{"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-27T13:27:59.116591Z","iopub.execute_input":"2022-08-27T13:27:59.117998Z","iopub.status.idle":"2022-08-27T13:27:59.150278Z","shell.execute_reply.started":"2022-08-27T13:27:59.117830Z","shell.execute_reply":"2022-08-27T13:27:59.148709Z"},"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-27T13:27:59.152374Z","iopub.execute_input":"2022-08-27T13:27:59.153602Z","iopub.status.idle":"2022-08-27T13:28:01.888491Z","shell.execute_reply.started":"2022-08-27T13:27:59.153556Z","shell.execute_reply":"2022-08-27T13:28:01.886604Z"},"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-27T13:28:01.891087Z","iopub.execute_input":"2022-08-27T13:28:01.891791Z","iopub.status.idle":"2022-08-27T13:28:03.026462Z","shell.execute_reply.started":"2022-08-27T13:28:01.891743Z","shell.execute_reply":"2022-08-27T13:28:03.021406Z"},"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/clahe-last-fold0.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-27T13:28:03.044308Z","iopub.execute_input":"2022-08-27T13:28:03.048946Z","iopub.status.idle":"2022-08-27T13:28:03.204349Z","shell.execute_reply.started":"2022-08-27T13:28:03.048864Z","shell.execute_reply":"2022-08-27T13:28:03.201085Z"},"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-27T13:28:03.209369Z","iopub.execute_input":"2022-08-27T13:28:03.210772Z","iopub.status.idle":"2022-08-27T13:28:03.227608Z","shell.execute_reply.started":"2022-08-27T13:28:03.210725Z","shell.execute_reply":"2022-08-27T13:28:03.224768Z"},"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-27T13:28:03.230682Z","iopub.execute_input":"2022-08-27T13:28:03.233324Z","iopub.status.idle":"2022-08-27T13:28:23.729845Z","shell.execute_reply.started":"2022-08-27T13:28:03.233280Z","shell.execute_reply":"2022-08-27T13:28:23.728427Z"},"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-27T13:29:58.598531Z","iopub.execute_input":"2022-08-27T13:29:58.599172Z","iopub.status.idle":"2022-08-27T13:29:58.614838Z","shell.execute_reply.started":"2022-08-27T13:29:58.599124Z","shell.execute_reply":"2022-08-27T13:29:58.613223Z"},"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-27T13:29:58.841549Z","iopub.execute_input":"2022-08-27T13:29:58.842097Z","iopub.status.idle":"2022-08-27T13:29:58.848182Z","shell.execute_reply.started":"2022-08-27T13:29:58.842053Z","shell.execute_reply":"2022-08-27T13:29:58.846748Z"},"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-27T13:29:59.155066Z","iopub.execute_input":"2022-08-27T13:29:59.155601Z","iopub.status.idle":"2022-08-27T13:29:59.169143Z","shell.execute_reply.started":"2022-08-27T13:29:59.155526Z","shell.execute_reply":"2022-08-27T13:29:59.167379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample","metadata":{"execution":{"iopub.status.busy":"2022-08-27T13:29:59.751237Z","iopub.execute_input":"2022-08-27T13:29:59.751802Z","iopub.status.idle":"2022-08-27T13:29:59.772299Z","shell.execute_reply.started":"2022-08-27T13:29:59.751753Z","shell.execute_reply":"2022-08-27T13:29:59.770871Z"},"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-27T13:30:01.197540Z","iopub.execute_input":"2022-08-27T13:30:01.198187Z","iopub.status.idle":"2022-08-27T13:30:01.211727Z","shell.execute_reply.started":"2022-08-27T13:30:01.198126Z","shell.execute_reply":"2022-08-27T13:30:01.209883Z"},"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    \n    # ヒストグラム平滑化\n    image = np.array(image, dtype=np.uint8)\n    clahe = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(8,8))\n    hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)\n    hsv[:,:,2] = clahe.apply(hsv[:,:,2]) \n    image = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)\n    \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-27T13:30:41.003942Z","iopub.execute_input":"2022-08-27T13:30:41.004435Z","iopub.status.idle":"2022-08-27T13:30:41.450440Z","shell.execute_reply.started":"2022-08-27T13:30:41.004403Z","shell.execute_reply":"2022-08-27T13:30:41.448950Z"},"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-27T13:30:44.351061Z","iopub.execute_input":"2022-08-27T13:30:44.351502Z","iopub.status.idle":"2022-08-27T13:30:46.206069Z","shell.execute_reply.started":"2022-08-27T13:30:44.351469Z","shell.execute_reply":"2022-08-27T13:30:46.204934Z"},"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-27T13:29:29.283192Z","iopub.execute_input":"2022-08-27T13:29:29.284113Z","iopub.status.idle":"2022-08-27T13:29:29.290220Z","shell.execute_reply.started":"2022-08-27T13:29:29.284057Z","shell.execute_reply":"2022-08-27T13:29:29.288613Z"},"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-27T13:29:29.292456Z","iopub.execute_input":"2022-08-27T13:29:29.293635Z","iopub.status.idle":"2022-08-27T13:29:29.303774Z","shell.execute_reply.started":"2022-08-27T13:29:29.293589Z","shell.execute_reply":"2022-08-27T13:29:29.302104Z"},"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-27T13:29:29.305891Z","iopub.execute_input":"2022-08-27T13:29:29.307401Z","iopub.status.idle":"2022-08-27T13:29:29.332426Z","shell.execute_reply.started":"2022-08-27T13:29:29.307351Z","shell.execute_reply":"2022-08-27T13:29:29.331134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-08-27T13:29:29.334124Z","iopub.execute_input":"2022-08-27T13:29:29.334594Z","iopub.status.idle":"2022-08-27T13:29:29.350703Z","shell.execute_reply.started":"2022-08-27T13:29:29.334552Z","shell.execute_reply":"2022-08-27T13:29:29.349110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}