{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Installation"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install segmentation-models-pytorch\n!git clone https://github.com/NVIDIA/apex\n!mv apex to_delete\n!mv to_delete/* .\n!rm -r to_delete\n!pip install -v --disable-pip-version-check --no-cache-dir ./\n!mkdir testing_output\n!mkdir masked_output","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Imports"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\nimport torch\nfrom torch import nn\nimport segmentation_models_pytorch as smp\nfrom torch.utils.data import DataLoader\nfrom tqdm import tqdm\nfrom apex import amp\nfrom albumentations import Resize, Normalize, Compose\nfrom albumentations.pytorch import ToTensorV2\nfrom torch.utils.data import Dataset\nimport cv2\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Crop function"},{"metadata":{"trusted":true},"cell_type":"code","source":"def crop_image_only_outside(img,tol=0):\n    # img is 2D image data\n    # tol  is tolerance\n    mask = img>tol\n    m,n = img.shape\n    mask0,mask1 = mask.any(0),mask.any(1)\n    col_start,col_end = mask0.argmax(),n-mask0[::-1].argmax()\n    row_start,row_end = mask1.argmax(),m-mask1[::-1].argmax()\n    return row_start,row_end,col_start,col_end","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Dataloader"},{"metadata":{"trusted":true},"cell_type":"code","source":"class AlbuAugment():\n    def __init__(self):\n        transformation = [\n            Resize(256, 256),\n            Normalize(),\n            ToTensorV2()\n        ]\n        self.transform = Compose(transformation)\n    \n    def __call__(self, image):\n        transformed = self.transform(image=image)\n        return transformed['image']        \n\nclass SegmentationDataset(Dataset):\n    def __init__(self, img_dir, names):\n        self.images_src = img_dir\n        self.names = names\n        self.transform = AlbuAugment()\n    def __len__(self):\n        return len(self.names)\n\n    def __getitem__(self, idx):\n        image = cv2.imread(os.path.join(self.images_src, self.names[idx]))\n        image = self.transform(image=image)\n        return image, self.names[idx]\n\n# Image folder    \nfdir = \"../input/vinbigdata-chest-xray-resized-png-1024x1024/test\"\nloader = DataLoader(SegmentationDataset(fdir, os.listdir(fdir)), batch_size=32, pin_memory=True, shuffle=False, num_workers=4)    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Segmentation model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = smp.UnetPlusPlus('resnet50',\n    encoder_weights=None,\n    classes=1,    \n    ).cuda()\nmodel.load_state_dict(torch.load(\"/kaggle/input/lungfield-segmetation/segmentation-checkpoint.pth\", \"cpu\"))\nmodel.eval();","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Make crops"},{"metadata":{"trusted":true},"cell_type":"code","source":"tbar = tqdm(loader)\noutputs = []\nwith torch.no_grad():\n    for image, names in tbar:\n        image = image.cuda()\n        # Threshold 0.5, change as you wish\n        output = (torch.sigmoid(model(image).cpu()).permute(0,2,3,1).numpy() > 0.5).astype(np.uint8)\n        \n        for crop, name in zip(output, names):\n            src = cv2.imread(f\"{fdir}/{name}\")\n            # Imagesize 1024x1024, change as you wish\n            crop = cv2.resize(crop, (1024, 1024))\n            \n            src_w_mask = np.array(src)\n            src_w_mask[:,:,0] += (crop * 255 * 0.3).astype(np.uint8).squeeze()\n            cv2.imwrite(f\"./testing_output/{name}\", src_w_mask)\n            \n            rs, re, cs, ce = crop_image_only_outside(crop)\n            # Padsize 100, change as you wish\n            padsize = 100\n            rs = max(0, rs-padsize)\n            re = min(src.shape[0], re+padsize)\n            cs = max(0, cs-padsize)\n            ce = min(src.shape[1], ce+padsize)\n            src_wo_mask = np.array(src)\n            src_wo_mask = src[rs:re, cs:ce]\n            cv2.imwrite(f\"./masked_output/{name}\", src_wo_mask)\n        \n        # To run on full set, remove this\n        break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fname = os.listdir(\"./testing_output\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"idxes = np.random.choice(fname, 5)\nfor f in idxes:\n    plt.figure(figsize=(10,10))\n    plt.subplot(121)\n    plt.imshow(cv2.imread(f\"./testing_output/{f}\"))\n    plt.subplot(122)\n    plt.imshow(cv2.imread(f\"./masked_output/{f}\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}