{"cells":[{"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow import keras\nimport pandas as pd\nimport gc\nimport numpy as np\nimport cv2\n\nimport rasterio\nfrom rasterio.windows import Window\nimport pathlib\nfrom tqdm.notebook import tqdm\n\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# https://www.kaggle.com/bguberfain/memory-aware-rle-encoding\ndef 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)\n\n# thanks to @igor14497 for helping me debug\ndef make_grid(shape, window=512):\n    \"\"\"\n        Return Array of size (N,4), where N - number of tiles,\n        2nd axis represente slices: x1,x2,y1,y2 \n    \"\"\"\n    x, y = shape\n\n    if x % WINDOW == 0:\n        nx = x // window\n    else:\n        nx = x // window + 1\n\n    x1 = list(range(0, x, window))\n    x2 = [(x + window) for x in x1][:-1]\n    x2.append(x)\n\n    if y % WINDOW == 0:\n        ny = y // window\n    else:\n        ny = y // window + 1\n\n    y1 = list(range(0, y, window))\n    y2 = [(y + window) for y in y1][:-1]\n    y2.append(y)\n\n    slices = np.zeros((nx, ny, 4), dtype=np.int64)\n    \n    for i in range(nx):\n        for j in range(ny):\n            slices[i, j] = x1[i], x2[i], y1[j], y2[j]    \n    return slices.reshape(nx*ny, 4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"THRESHOLD = 0.4\nWINDOW = 4*512\nTILE_SIZE = 512","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"MODEL = '../input/notebook4ca5c6eb1c/alina_resnet34.h5'\nmodel = keras.models.load_model(MODEL, compile=False)\nprint(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# https://www.kaggle.com/wrrosa/hubmap-tf-with-tpu-efficientunet-512x512-subm\n\nsub = {}\n\np = pathlib.Path('../input/hubmap-kidney-segmentation')\nidentity = rasterio.Affine(1, 0, 0, 0, 1, 0)\n\nfor i, filename in tqdm(enumerate(p.glob('test/*.tiff')), \n                        total = len(list(p.glob('test/*.tiff')))):\n    print(f'{i+1} Predicting {filename.stem}')\n    \n    dataset = rasterio.open(filename.as_posix(), transform=identity)\n    preds = np.zeros(dataset.shape, dtype=np.uint8)    \n\n    slices = make_grid(dataset.shape, window=WINDOW)\n\n    for (x1,x2,y1,y2) in slices:\n        if (x2-x1) == WINDOW and (y2-y1) == WINDOW:\n            image = dataset.read([1,2,3],\n                                 window=Window.from_slices((x1,x2),(y1,y2)))\n            image = np.moveaxis(image, 0, -1)\n            image = cv2.resize(image, (TILE_SIZE, TILE_SIZE), interpolation=cv2.INTER_AREA)\n            # image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n            image = np.expand_dims(image, 0)\n\n            pred =  np.squeeze(model.predict(image))\n            pred = cv2.resize(pred, (WINDOW, WINDOW), interpolation=cv2.INTER_NEAREST)\n            preds[x1:x2,y1:y2] += (pred > THRESHOLD).astype(np.uint8)\n        else:\n            continue\n\n    # preds = (preds > 0.5).astype(np.uint8)\n    \n    sub[i] = {'id':filename.stem, 'predicted': rle_encode_less_memory(preds)}\n    \n    del preds\n    gc.collect();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame.from_dict(sub, orient='index')\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","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}