{"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":"### PSPNet\n\n|  hyperparameter  | Setting  |\n|  ----            | :----:     |\n|resize            | 768x768 |\n|batch_size        | 8       |\n|augmentation      | stain normalization + several methods of albumentations|\n|loss              | focal loss + dice loss|\n|epoch             |500      |\n|cross validation  | no      |\n|train:validation  | 4:1     |\n\nThe IOU of validation: about 0.72\n\nThe dice of test: **0.65**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport gc\nimport torch\nimport os\nimport cv2\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nfrom tqdm import tqdm\n\ntransform = A.Compose([\n    A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),\n    ToTensorV2(),\n])","metadata":{"execution":{"iopub.status.busy":"2022-08-20T04:36:26.322311Z","iopub.execute_input":"2022-08-20T04:36:26.322689Z","iopub.status.idle":"2022-08-20T04:36:30.785644Z","shell.execute_reply.started":"2022-08-20T04:36:26.322612Z","shell.execute_reply":"2022-08-20T04:36:30.784423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('../input/torch-seg-model/pytorch-segmentation-master')\nfrom models import PSPNet\n\nmodel = PSPNet(num_classes=2, in_channels=3, backbone='resnet101', pretrained=False, use_aux=False).cuda()\nmodel.load_state_dict(torch.load('../input/model-weight/PSPNet_human_n_101.pth'))\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2022-08-20T04:36:30.792964Z","iopub.execute_input":"2022-08-20T04:36:30.796952Z","iopub.status.idle":"2022-08-20T04:36:39.631191Z","shell.execute_reply.started":"2022-08-20T04:36:30.796910Z","shell.execute_reply":"2022-08-20T04:36:39.630262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA = '../input/hubmap-organ-segmentation/test_images/'\ndf_sample = pd.read_csv('../input/hubmap-organ-segmentation/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-20T04:36:39.632929Z","iopub.execute_input":"2022-08-20T04:36:39.633675Z","iopub.status.idle":"2022-08-20T04:36:39.655952Z","shell.execute_reply.started":"2022-08-20T04:36:39.633638Z","shell.execute_reply":"2022-08-20T04:36:39.653679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode_less_memory(img):\n    #the image should be transposed\n    pixels = img.T.flatten()\n    \n    # This simplified method requires first and last pixel to be zero\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    \n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-08-20T04:36:39.657108Z","iopub.execute_input":"2022-08-20T04:36:39.657481Z","iopub.status.idle":"2022-08-20T04:36:39.669137Z","shell.execute_reply.started":"2022-08-20T04:36:39.657442Z","shell.execute_reply":"2022-08-20T04:36:39.667211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names, preds = [], []","metadata":{"execution":{"iopub.status.busy":"2022-08-20T04:36:39.672583Z","iopub.execute_input":"2022-08-20T04:36:39.673271Z","iopub.status.idle":"2022-08-20T04:36:39.690359Z","shell.execute_reply.started":"2022-08-20T04:36:39.673206Z","shell.execute_reply":"2022-08-20T04:36:39.689068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx,row in tqdm(df_sample.iterrows(),total=len(df_sample)):\n    idx = str(row['id'])\n    img_path = f'{DATA}{idx}.tiff'\n    img = cv2.imread(img_path)\n    original_shape = img.shape[:2]\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (768, 768))\n    input_img = torch.stack([transform(image=img)['image']]).cuda()\n    with torch.no_grad():\n        pred = model(input_img)\n        mask = pred.argmax(dim=1)\n        mask = mask.squeeze().cpu().numpy()\n        mask = cv2.resize(mask, original_shape, interpolation=cv2.INTER_NEAREST)\n        names.append(idx)\n        preds.append(rle_encode_less_memory(mask))\n        del mask, pred, input_img\n        gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-20T04:36:39.691893Z","iopub.execute_input":"2022-08-20T04:36:39.692515Z","iopub.status.idle":"2022-08-20T04:36:45.325494Z","shell.execute_reply.started":"2022-08-20T04:36:39.692457Z","shell.execute_reply":"2022-08-20T04:36:45.324488Z"},"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-20T04:36:45.326751Z","iopub.execute_input":"2022-08-20T04:36:45.327400Z","iopub.status.idle":"2022-08-20T04:36:45.336001Z","shell.execute_reply.started":"2022-08-20T04:36:45.327361Z","shell.execute_reply":"2022-08-20T04:36:45.334970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}