{"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":"This is the **inference notebook** for EfficientNetB0 UNet model.\n\nIf you are interested in how this model trained, please refer to [**training notebook**](https://www.kaggle.com/code/dingyan/hubmap-efficientnet-unet-with-data-augmentation). \nIn that notebook, we go through \n* How to write a custom data generator,\n* How to add augmentations on the fly.\n* How to finetune a pretrained EfficientNet UNet model.\n\nIf you feel this notebook is helpfule, please **upvote**! Thank you.","metadata":{}},{"cell_type":"markdown","source":"**1. Install segmentation models locally** as we cannot turn on Internet when we submit.","metadata":{}},{"cell_type":"code","source":"!pip install -q -U ../input/kerasapplications/Keras_Applications-1.0.8-py3-none-any.whl\n!pip install -q ../input/qubvel/efficientnet-1.0.0-py3-none-any.whl\n!pip install -q ../input/qubvel/image_classifiers-1.0.0-py3-none-any.whl\n!pip install -q ../input/qubvel/segmentation_models-1.0.0-py3-none-any.whl","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-16T05:20:56.230635Z","iopub.execute_input":"2022-08-16T05:20:56.231372Z","iopub.status.idle":"2022-08-16T05:22:55.480364Z","shell.execute_reply.started":"2022-08-16T05:20:56.231275Z","shell.execute_reply":"2022-08-16T05:22:55.479215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom PIL import Image\nimport os\nimport pandas as pd\nfrom pathlib import Path\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport segmentation_models as sm\nimport random\nimport math\nimport albumentations as A\n\nsm.set_framework('tf.keras')\nimg_size = 512\nthreshold = 0.2","metadata":{"execution":{"iopub.status.busy":"2022-08-16T05:23:55.975162Z","iopub.execute_input":"2022-08-16T05:23:55.976275Z","iopub.status.idle":"2022-08-16T05:23:55.984828Z","shell.execute_reply.started":"2022-08-16T05:23:55.976236Z","shell.execute_reply":"2022-08-16T05:23:55.983864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**2. Load our trained model.**","metadata":{}},{"cell_type":"code","source":"checkpoint_path = '../input/hubmap-efficientnet-unet-with-data-augmentation/efficient_unet_model'\nmodel = keras.models.load_model(checkpoint_path)","metadata":{"execution":{"iopub.status.busy":"2022-08-16T05:23:57.942818Z","iopub.execute_input":"2022-08-16T05:23:57.943454Z","iopub.status.idle":"2022-08-16T05:24:14.555051Z","shell.execute_reply.started":"2022-08-16T05:23:57.943415Z","shell.execute_reply":"2022-08-16T05:24:14.554059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**3. Visualize the prediction mask for test image.**","metadata":{}},{"cell_type":"code","source":"# ref.: https://www.kaggle.com/stainsby/fast-tested-rle\ndef rle_encode(img):\n    \"\"\" TBD\n    \n    Args:\n        img (np.array): \n            - 1 indicating mask\n            - 0 indicating background\n    \n    Returns: \n        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 preprocess_image(path):\n    image = keras.utils.load_img(path)\n    original_shape = keras.utils.img_to_array(image).shape\n    image = image.resize((img_size, img_size))\n    image_array = keras.utils.img_to_array(image)/255\n    return image_array, original_shape","metadata":{"execution":{"iopub.status.busy":"2022-08-16T05:24:32.715942Z","iopub.execute_input":"2022-08-16T05:24:32.716786Z","iopub.status.idle":"2022-08-16T05:24:32.727448Z","shell.execute_reply.started":"2022-08-16T05:24:32.716740Z","shell.execute_reply":"2022-08-16T05:24:32.724527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_test_path = '../input/hubmap-organ-segmentation/test_images/10078.tiff'\nimage, _ = preprocess_image(sample_test_path)\npred = model.predict(np.expand_dims(image, axis=0))\npred_mask = np.where(pred > threshold, 1, 0)[0]\nplt.figure(figsize=(6, 6))\nplt.axis('off')\nplt.imshow(image)\nplt.imshow(pred_mask, cmap='hot', alpha=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-08-16T05:34:41.578019Z","iopub.execute_input":"2022-08-16T05:34:41.578391Z","iopub.status.idle":"2022-08-16T05:34:41.983839Z","shell.execute_reply.started":"2022-08-16T05:34:41.578359Z","shell.execute_reply":"2022-08-16T05:34:41.982981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**4. Submission**","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv('../input/hubmap-organ-segmentation/test.csv')\ntest_ids = test_df['id']\ntest_dir = '../input/hubmap-organ-segmentation/test_images'\n\nids = []\nrles = []\nfor id in test_ids:\n    path = os.path.join(test_dir, f\"{id}.tiff\")\n    image, original_shape = preprocess_image(path)\n    pred = model.predict(np.expand_dims(image, axis=0))\n    pred_mask = np.where(pred > threshold, 1, 0)[0]\n    resized_pred_mask = keras.utils.array_to_img(pred_mask, scale=False).resize((original_shape[0], original_shape[1]), resample=0)\n    resized_pred_mask_array = keras.utils.img_to_array(resized_pred_mask, dtype='uint8')\n    rle = rle_encode(resized_pred_mask_array)\n    ids.append(id)\n    rles.append(rle)\n    \nsubmission_df = pd.DataFrame({'id':ids,'rle':rles})\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-16T05:24:49.858664Z","iopub.execute_input":"2022-08-16T05:24:49.859041Z","iopub.status.idle":"2022-08-16T05:24:50.050827Z","shell.execute_reply.started":"2022-08-16T05:24:49.859010Z","shell.execute_reply":"2022-08-16T05:24:50.049831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2022-08-16T05:24:56.043177Z","iopub.execute_input":"2022-08-16T05:24:56.043985Z","iopub.status.idle":"2022-08-16T05:24:56.057203Z","shell.execute_reply.started":"2022-08-16T05:24:56.043946Z","shell.execute_reply":"2022-08-16T05:24:56.056134Z"},"trusted":true},"execution_count":null,"outputs":[]}]}