{"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":"code","source":"import os\nimport numpy as np\nfrom matplotlib import animation\nimport matplotlib.pyplot as plt\nfrom IPython import display\nimport pandas as pd\n\nfrom tqdm import tqdm\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import models\nfrom tensorflow.keras import losses\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras import metrics\nimport tensorflow.keras.backend as K","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-05T10:33:45.449483Z","iopub.execute_input":"2023-06-05T10:33:45.449745Z","iopub.status.idle":"2023-06-05T10:33:53.825949Z","shell.execute_reply.started":"2023-06-05T10:33:45.44972Z","shell.execute_reply":"2023-06-05T10:33:53.824848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_range(data, bounds):\n    \"\"\"Maps data to the range [0, 1].\"\"\"\n    return (data - bounds[0]) / (bounds[1] - bounds[0])\n\ndef get_image(BASE_DIR, record_id):\n    N_TIMES_BEFORE = 4\n    with open(os.path.join(BASE_DIR, record_id, 'band_11.npy'), 'rb') as f:\n        band11 = np.load(f)\n    with open(os.path.join(BASE_DIR, record_id, 'band_14.npy'), 'rb') as f:\n        band14 = np.load(f)\n    with open(os.path.join(BASE_DIR, record_id, 'band_15.npy'), 'rb') as f:\n        band15 = np.load(f)\n        \n    _T11_BOUNDS = (243, 303)\n    _CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n    _TDIFF_BOUNDS = (-4, 2)\n\n    r = normalize_range(band15 - band14, _TDIFF_BOUNDS)\n    g = normalize_range(band14 - band11, _CLOUD_TOP_TDIFF_BOUNDS)\n    b = normalize_range(band14, _T11_BOUNDS)\n    false_color = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n    \n    img = false_color[..., N_TIMES_BEFORE]\n    \n    return img","metadata":{"execution":{"iopub.status.busy":"2023-06-05T10:33:53.827996Z","iopub.execute_input":"2023-06-05T10:33:53.828751Z","iopub.status.idle":"2023-06-05T10:33:53.837948Z","shell.execute_reply.started":"2023-06-05T10:33:53.828716Z","shell.execute_reply":"2023-06-05T10:33:53.836918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dice_loss(y_true, y_pred):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f* y_pred_f)\n    val = (2. * intersection + K.epsilon()) / (K.sum(y_true_f) + K.sum(y_pred_f) + K.epsilon())\n    return 1. - val\n\nmodel = models.load_model('/kaggle/input/gric-eda-baseline/model-epoch_29.h5', custom_objects={'dice_loss': dice_loss})","metadata":{"execution":{"iopub.status.busy":"2023-06-05T10:33:53.839203Z","iopub.execute_input":"2023-06-05T10:33:53.840045Z","iopub.status.idle":"2023-06-05T10:33:57.326093Z","shell.execute_reply.started":"2023-06-05T10:33:53.840013Z","shell.execute_reply":"2023-06-05T10:33:57.325024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(x, fg_val=1):\n    \"\"\"\n    Args:\n        x:  numpy array of shape (height, width), 1 - mask, 0 - background\n    Returns: run length encoding as list\n    \"\"\"\n\n    dots = np.where(\n        x.T.flatten() == fg_val)[0]  # .T sets Fortran order down-then-right\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if b > prev + 1:\n            run_lengths.extend((b + 1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return run_lengths\n\n\ndef list_to_string(x):\n    \"\"\"\n    Converts list to a string representation\n    Empty list returns '-'\n    \"\"\"\n    if x: # non-empty list\n        s = str(x).replace(\"[\", \"\").replace(\"]\", \"\").replace(\",\", \"\")\n    else:\n        s = '-'\n    return s\n\n\ndef rle_decode(mask_rle, shape=(256, 256)):\n    '''\n    mask_rle: run-length as string formatted (start length)\n              empty predictions need to be encoded with '-'\n    shape: (height, width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    '''\n\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    if mask_rle != '-': \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        for lo, hi in zip(starts, ends):\n            img[lo:hi] = 1\n    return img.reshape(shape, order='F')  # Needed to align to RLE direction","metadata":{"execution":{"iopub.status.busy":"2023-06-05T10:33:57.32838Z","iopub.execute_input":"2023-06-05T10:33:57.32872Z","iopub.status.idle":"2023-06-05T10:33:57.339944Z","shell.execute_reply.started":"2023-06-05T10:33:57.328683Z","shell.execute_reply":"2023-06-05T10:33:57.339094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_DIR = '/kaggle/input/google-research-identify-contrails-reduce-global-warming/test'\nthreshold = 0.5\nall_test_records = os.listdir(BASE_DIR)\npredicted_masks = []\nfor test_record in all_test_records:\n    image = get_image(BASE_DIR, str(test_record))\n    mask = model.predict(image.reshape(1,256,256,3),verbose=0).reshape(256,256)\n    mask[mask>=threshold] = 1\n    mask[mask<threshold] = 0\n    mask = list_to_string(rle_encode(mask))\n    predicted_masks.append(mask)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T10:33:57.34215Z","iopub.execute_input":"2023-06-05T10:33:57.342749Z","iopub.status.idle":"2023-06-05T10:34:02.832926Z","shell.execute_reply.started":"2023-06-05T10:33:57.342711Z","shell.execute_reply":"2023-06-05T10:34:02.831984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'record_id':all_test_records, 'encoded_pixels':predicted_masks})\ndf","metadata":{"execution":{"iopub.status.busy":"2023-06-05T10:34:02.834467Z","iopub.execute_input":"2023-06-05T10:34:02.834822Z","iopub.status.idle":"2023-06-05T10:34:02.853416Z","shell.execute_reply.started":"2023-06-05T10:34:02.834787Z","shell.execute_reply":"2023-06-05T10:34:02.852282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T10:34:02.85481Z","iopub.execute_input":"2023-06-05T10:34:02.855291Z","iopub.status.idle":"2023-06-05T10:34:02.864137Z","shell.execute_reply.started":"2023-06-05T10:34:02.855258Z","shell.execute_reply":"2023-06-05T10:34:02.863299Z"},"trusted":true},"execution_count":null,"outputs":[]}]}