{"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":"    \n<center><img src=https://hubmapconsortium.org/wp-content/uploads/2019/01/HuBMAP-Retina-Logo-Color.png></center>","metadata":{}},{"cell_type":"markdown","source":"This is the Inference notebook for the starter training notebook: \nhttps://www.kaggle.com/vmuzhichenko/hubmap-hpa-tf-unet-train\n\n#### Credits:\n* https://www.kaggle.com/code/wrrosa/hubmap-tf-with-tpu-efficientunet-512x512-train\n* https://www.kaggle.com/code/susnato/hubmap-hpa-fpn-starter-tf\n* https://www.kaggle.com/code/awsaf49/uwmgi-2-5d-train-pytorch","metadata":{}},{"cell_type":"code","source":"import os\nimport gc\nimport cv2\nimport glob\nfrom tqdm import notebook\nimport tifffile as tiff \nimport numpy as np \nimport pandas as pd \nimport tensorflow as tf\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.utils import get_custom_objects\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import KFold\nimport albumentations as A\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-15T10:55:53.112522Z","iopub.execute_input":"2022-07-15T10:55:53.113797Z","iopub.status.idle":"2022-07-15T10:56:01.692296Z","shell.execute_reply.started":"2022-07-15T10:55:53.113151Z","shell.execute_reply":"2022-07-15T10:56:01.691381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config = {\n'backbone':'efficientnetb4',\n'img_size': 512,\n'n_folds' : 3,\n'seed' : 142,\n'epochs': 20,\n'LR_MAX': 0.001,\n'LR_MIN':1e-5,\n'LR_DECAY': 0.8, \n'plot_history': True\n}\n\nDEBUG = False\nTRAIN_ONE_EPOCH = True\n\ntrain = pd.read_csv(\"../input/hubmap-organ-segmentation/train.csv\")\ntest = pd.read_csv(\"../input/hubmap-organ-segmentation/test.csv\")\nsample_submission = pd.read_csv(\"../input/hubmap-organ-segmentation/sample_submission.csv\")\n\nimages_dir = '../input/hubmap-hpa-numpy-512x512/train_images/'\nmasks_dir = '../input/hubmap-hpa-numpy-512x512/train_masks/'\n\nif DEBUG:\n    train = train.head(50)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:01:32.351962Z","iopub.execute_input":"2022-07-15T11:01:32.352327Z","iopub.status.idle":"2022-07-15T11:01:32.486838Z","shell.execute_reply.started":"2022-07-15T11:01:32.352295Z","shell.execute_reply":"2022-07-15T11:01:32.485900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://tensorlayer.readthedocs.io/en/latest/_modules/tensorlayer/cost.html#dice_coe\ndef dice_coe(output, target, axis = None, smooth=1e-10):\n    output = tf.dtypes.cast( tf.math.greater(output, 0.5), tf. float32 )\n    target = tf.dtypes.cast( tf.math.greater(target, 0.5), tf. float32 )\n    inse = tf.reduce_sum(output * target, axis=axis)\n    l = tf.reduce_sum(output, axis=axis)\n    r = tf.reduce_sum(target, axis=axis)\n\n    dice = (2. * inse + smooth) / (l + r + smooth)\n    dice = tf.reduce_mean(dice, name='dice_coe')\n    return dice","metadata":{"execution":{"iopub.status.busy":"2022-07-15T10:56:01.999409Z","iopub.execute_input":"2022-07-15T10:56:02.000101Z","iopub.status.idle":"2022-07-15T10:56:02.007777Z","shell.execute_reply.started":"2022-07-15T10:56:02.000059Z","shell.execute_reply":"2022-07-15T10:56:02.006792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/code/paulorzp/rle-functions-run-lenght-encode-decode\n\ndef mask2rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels= img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\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 rle2mask(mask_rle, shape):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-15T10:56:02.010163Z","iopub.execute_input":"2022-07-15T10:56:02.011861Z","iopub.status.idle":"2022-07-15T10:56:02.022013Z","shell.execute_reply.started":"2022-07-15T10:56:02.011832Z","shell.execute_reply":"2022-07-15T10:56:02.021144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = []\nfor i in range(config['n_folds']):\n    model = tf.keras.models.load_model(f'../input/hubmap-hpa-tf-unet-train/{config[\"backbone\"]}_UNet_512x512_best_fold_{i+1}', custom_objects={'dice_coe':dice_coe})\n    models.append(model)\n    print(f'Model #{i+1} loaded')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:01:35.551686Z","iopub.execute_input":"2022-07-15T11:01:35.552026Z","iopub.status.idle":"2022-07-15T11:02:48.755198Z","shell.execute_reply.started":"2022-07-15T11:01:35.551997Z","shell.execute_reply":"2022-07-15T11:02:48.754049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/code/susnato/hubmap-hpa-fpn-starter-tf\n    \nids = []\npreds = []\n\nfor each_id in sample_submission.id.values:\n    print(each_id)\n    img_path = \"../input/hubmap-organ-segmentation/test_images/{}.tiff\".format(each_id)\n    img = tiff.imread(img_path)\n    if img==[]:\n        ids.append(each_id)\n        preds.append('')\n    else:\n        img_real_shape  = img.shape\n        img = cv2.resize(img, (config['img_size'], config['img_size']))\n        \n        ###Prediction\n        img = np.expand_dims(img, 0)\n        img = img/255.0\n        \n        pred_masks=[]\n        for model in models:\n            pred_mask = model.predict(img)[0, :, :, 0]\n            pred_masks.append(pred_mask)\n        pred_mask = np.mean(np.array(pred_masks), axis=0)\n        \n        pred_mask = cv2.resize(pred_mask, (img_real_shape[1], img_real_shape[0]))\n        pred_mask[pred_mask>=0.5] = 1\n        pred_mask[pred_mask<0.5] = 0\n        \n        pred_rle  = mask2rle(pred_mask)\n        ids.append(each_id)\n        preds.append(pred_rle)\n        \n\nsub = pd.DataFrame({'id':ids,'rle':preds})\nsub.to_csv('submission.csv', index=False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:02:48.758587Z","iopub.execute_input":"2022-07-15T11:02:48.759047Z","iopub.status.idle":"2022-07-15T11:03:07.642083Z","shell.execute_reply.started":"2022-07-15T11:02:48.759009Z","shell.execute_reply":"2022-07-15T11:03:07.641107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}