{"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\nDIR_INPUT_MINE_SUB = '../input/hubmap-pred-stitch-test-transfer-tune-enc-weighted'\nDIR_INPUT_PROJ = '/kaggle/input/hubmap-kidney-segmentation'\nDIR_INPUT_REF_SUB = '../input/global-mask-shift'\nimport os\nfor dirname, _, filenames in os.walk(DIR_INPUT_MINE_SUB):\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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_mine_sub = pd.read_csv(os.path.join(DIR_INPUT_MINE_SUB, 'submission.csv'), index_col=0)\ndf_ref_sub = pd.read_csv(os.path.join(DIR_INPUT_REF_SUB, 'submission.csv'), index_col=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras import backend as K\n\nsmooth = 1\n\ndef dice_coef_bin(y_true, y_pred):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    y_pred_f = K.greater(y_pred_f, 0.5)\n    y_pred_f = K.cast(y_pred_f, y_true_f.dtype)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\nimport tifffile\ndef read_raw_tiff(img_path, mask_bin=None):\n    img_raw = tifffile.imread(img_path)\n    if len(img_raw.shape) > 3:\n        img_raw = np.transpose(img_raw.squeeze(), (1,2,0))\n    if mask_bin is None:\n        return img_raw\n    else:\n        assert img_raw.shape[:2] == mask_bin.shape[:2]\n    return img_raw\n\n\ndef get_mask_bin(rle_raw, n_pix_height, n_pix_width):\n    '''convert the raw rle data of one image into a binary mask'''\n    rle_list = np.array(rle_raw.split(' ')).astype(int)\n    rle_paired = np.array([rle_list[::2] - 1, rle_list[1::2]]).T\n    mask_bin = np.zeros(n_pix_width * n_pix_height)\n    for start, run_length in rle_paired:\n        mask_bin[start: start + run_length] = 1\n    mask_bin = mask_bin.reshape(n_pix_width, n_pix_height).T.astype(int).reshape(n_pix_height, n_pix_width, 1)\n    mask_bin = mask_bin.astype(np.uint8)\n    return mask_bin","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nfrom tqdm.auto import tqdm\nfor img_whole_id in tqdm(df_mine_sub.index):\n    img_whole_filename = img_whole_id + '.tiff'\n    im_whole_path = os.path.join(os.path.join(DIR_INPUT_PROJ, 'test',img_whole_filename))\n    img_whole = read_raw_tiff(im_whole_path, mask_bin=None)\n    img_whole_h, img_whole_w = img_whole.shape[:2]\n\n    scale_factor = 0.25\n    img_whole_h_scaled, img_whole_w_scaled = np.round(\n        np.array([img_whole_h, img_whole_w])* scale_factor\n    ).astype(int)\n    img_whole_scaled = cv2.resize(\n        img_whole.astype(np.uint8), \n        (img_whole_w_scaled, img_whole_h_scaled), \n        interpolation=cv2.INTER_NEAREST\n    )\n    del img_whole\n    print(img_whole_id)\n    print(img_whole_h, img_whole_w)\n    \n    mask_rle_str = df_mine_sub.loc[img_whole_id,'predicted']\n    mask_bin = get_mask_bin(mask_rle_str,img_whole_h, img_whole_w).reshape((img_whole_h, img_whole_w)) \n    mask_bin_scaled_mine = cv2.resize(\n        mask_bin.astype(np.uint8), \n        (img_whole_w_scaled, img_whole_h_scaled), \n        interpolation=cv2.INTER_NEAREST\n    )\n    del mask_bin\n    \n    mask_rle_str = df_ref_sub.loc[img_whole_id,'predicted']\n    mask_bin = get_mask_bin(mask_rle_str, img_whole_h, img_whole_w).reshape((img_whole_h, img_whole_w))\n    mask_bin_scaled_ref = cv2.resize(\n        mask_bin.astype(np.uint8), \n        (img_whole_w_scaled, img_whole_h_scaled), \n        interpolation=cv2.INTER_NEAREST\n    )\n    del mask_bin\n    print(\n        dice_coef_bin(mask_bin_scaled_mine.astype(np.float32), mask_bin_scaled_ref.astype(np.float32))\n    )\n    \n    cv2.imwrite(f'./{img_whole_id}_img_whole_scaled.tiff', img_whole_scaled)\n    del img_whole_scaled\n    cv2.imwrite(f'./{img_whole_id}_mask_bin_scaled_pred.tiff', mask_bin_scaled_mine*255)\n    del mask_bin_scaled_mine\n    cv2.imwrite(f'./{img_whole_id}_mask_bin_scaled_ref.tiff', mask_bin_scaled_ref*255)\n    del mask_bin_scaled_ref\n    \n    \n    ","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}