{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from IPython.display import clear_output\n\n!pip install ../input/packages/pretrainedmodels-0.7.4-py3-none-any.whl\n!pip install ../input/segmentationmodelspytorch/segmentation_models/timm-0.1.20-py3-none-any.whl\n!pip install ../input/packages/efficientnet_pytorch-0.6.3-py2.py3-none-any.whl\n!pip install ../input/segmentationmodelspytorch/segmentation_models/segmentation_models_pytorch-0.1.2-py3-none-any.whl\n\nclear_output()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from PIL import Image\nimport tifffile as tiff\nimport subprocess\nimport pandas as pd\nfrom IPython.display import clear_output\nimport matplotlib.pyplot as plt\nimport glob\n\nimport numpy as np\nimport cv2\nimport os\nfrom tqdm.notebook import tqdm\nimport zipfile\nimport gc\nimport segmentation_models_pytorch as smp\nimport torchvision\nimport torchvision.transforms as transforms\nimport torch\nfrom torch.utils.data import TensorDataset, DataLoader,Dataset\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data.sampler import SubsetRandomSampler\nfrom torch.optim.lr_scheduler import StepLR, ReduceLROnPlateau, CosineAnnealingLR\nsample_submission = pd.read_csv('../input/hubmap-kidney-segmentation/sample_submission.csv')\nsample_submission = sample_submission.set_index('id')\nseed = 1015\nnp.random.seed(seed)\ntorch.manual_seed(seed)\ntorch.cuda.manual_seed(seed)\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PATH = \"../input/modelsfpn/\"\nbest_model = torch.load(f\"{PATH}512_FPN_effib3.pth\")\n\nsz = 512","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def mask2rle(img):\n    \n   \n    pixels = img.T.flatten()\n    pixels = np.pad(pixels, ((1, 1), ))\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\ndef rle_encode_less_memory(img):\n    #watch out for the bug\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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_files = sample_submission.index.tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_files","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_path = '../input/hubmap-kidney-segmentation/test/'\n\nsubmission_df = pd.DataFrame(columns = ['id','predicted'])\n\nfor step, person_idx in enumerate(test_files):\n    print(f'load {step+1}/{len(test_files)} data...')\n    img = tiff.imread(test_path + person_idx + '.tiff').squeeze()\n    if img.shape[0] == 3:\n        img = img.transpose(1,2,0)\n    \n    \n    predict_mask = np.zeros((img.shape[0], img.shape[1]), dtype = bool)\n    landscape =img.shape[0]//(256*4)\n    portrait = img.shape[1]//(256*4)\n\n\n    s_th = 40  #saturation blancking threshold\n    p_th = 200*sz//256 #threshold for the minimum number of pixels\n\n    print('predict mask...')\n    for x in tqdm(range(landscape+1)):\n        for y in range(portrait+1):\n            start_x = (256*4)*x\n            end_x   = (256*4)*(x+1)\n            start_y = (256*4)*y\n            end_y   = (256*4)*(y+1)\n\n            if x == landscape:\n                start_x = (256*4)*(-1)\n                end_x   = None\n            if y == portrait:\n                start_y = (256*4)*(-1)\n                end_y   = None\n\n            sample_img = img[start_x : end_x, start_y : end_y,:]\n\n            hsv = cv2.cvtColor(sample_img, cv2.COLOR_BGR2HSV)\n            h, s, v = cv2.split(hsv)\n            if (s>s_th).sum() <= p_th or sample_img.sum() <= p_th: continue\n\n            sample_img = cv2.resize(sample_img,(sz,sz),interpolation = cv2.INTER_AREA)\n            sample_img = torch.cuda.FloatTensor(sample_img.transpose([2,0,1])[np.newaxis,...])\n            sample_pred = best_model.predict(sample_img).cpu().numpy()[0,0,:,:]\n            sample_pred = cv2.resize(sample_pred,(1024,1024),interpolation = cv2.INTER_AREA)\n            predict_mask[start_x : end_x, start_y : end_y] = sample_pred\n\n    del sample_img\n    del sample_pred\n    del img\n    gc.collect()  \n    gc.collect()\n\n    print('convert mask to rle \\n\\n')\n    predict_rle = rle_encode_less_memory(predict_mask) \n    sample_submission.loc[person_idx,'predicted'] = predict_rle\n\n    del predict_rle\n    del predict_mask\n    gc.collect()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission = sample_submission.reset_index()\nsample_submission.to_csv('/kaggle/working/submission.csv',index=False)\nsample_submission","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}