{"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\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nfrom pathlib import Path\ndata_path = Path('/kaggle/input/google-research-identify-contrails-reduce-global-warming')","metadata":{"execution":{"iopub.status.busy":"2023-07-08T07:21:17.659856Z","iopub.execute_input":"2023-07-08T07:21:17.661013Z","iopub.status.idle":"2023-07-08T07:21:17.696933Z","shell.execute_reply.started":"2023-07-08T07:21:17.660962Z","shell.execute_reply":"2023-07-08T07:21:17.695732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print((data_path))","metadata":{"execution":{"iopub.status.busy":"2023-07-08T09:27:08.469685Z","iopub.execute_input":"2023-07-08T09:27:08.470056Z","iopub.status.idle":"2023-07-08T09:27:08.474493Z","shell.execute_reply.started":"2023-07-08T09:27:08.470025Z","shell.execute_reply":"2023-07-08T09:27:08.473683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# functions \ndef list_to_string(x):\n    if x: \n        s = str(x).replace(\"[\", \"\").replace(\"]\", \"\").replace(\",\", \"\")\n    else:\n        s = '-'\n    return s\n\n\ndef rle_encode(x, fg_val=1):\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\ndef rle_decode(mask_rle, shape=(256, 256)):\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')  #","metadata":{"execution":{"iopub.status.busy":"2023-07-08T09:13:16.969182Z","iopub.execute_input":"2023-07-08T09:13:16.969552Z","iopub.status.idle":"2023-07-08T09:13:16.978318Z","shell.execute_reply.started":"2023-07-08T09:13:16.969526Z","shell.execute_reply":"2023-07-08T09:13:16.977462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_recs = os.listdir(data_path / 'test')\nprint(test_recs)","metadata":{"execution":{"iopub.status.busy":"2023-07-08T08:52:35.0551Z","iopub.execute_input":"2023-07-08T08:52:35.055448Z","iopub.status.idle":"2023-07-08T08:52:35.068828Z","shell.execute_reply.started":"2023-07-08T08:52:35.055418Z","shell.execute_reply":"2023-07-08T08:52:35.068094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"n = 1000\nband_08 = np.load(data_path / 'test' / test_recs[0] / 'band_08.npy').sum(axis=2)\npreds = np.unravel_index(np.argpartition(band_08.ravel(), -n)[-n:], band_08.shape)\nmask = np.zeros((256, 266))\nmask[preds[0], preds[1]] = 1\n\nplt.imshow(mask, cmap='bone')\nplt.title(\"not contrails\", fontsize='16')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-08T09:33:03.110021Z","iopub.execute_input":"2023-07-08T09:33:03.110336Z","iopub.status.idle":"2023-07-08T09:33:03.312361Z","shell.execute_reply.started":"2023-07-08T09:33:03.110311Z","shell.execute_reply":"2023-07-08T09:33:03.311022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_rec = os.listdir(data_path / 'test')\nprint(test_rec)","metadata":{"execution":{"iopub.status.busy":"2023-07-08T09:09:34.001679Z","iopub.execute_input":"2023-07-08T09:09:34.002026Z","iopub.status.idle":"2023-07-08T09:09:34.008612Z","shell.execute_reply.started":"2023-07-08T09:09:34.002001Z","shell.execute_reply":"2023-07-08T09:09:34.00707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_to_string(rle_encode(mask))","metadata":{"execution":{"iopub.status.busy":"2023-07-08T09:11:25.804169Z","iopub.execute_input":"2023-07-08T09:11:25.804529Z","iopub.status.idle":"2023-07-08T09:11:25.812991Z","shell.execute_reply.started":"2023-07-08T09:11:25.804499Z","shell.execute_reply":"2023-07-08T09:11:25.811852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch \nimport torchvision\nimport seaborn as sns\nimport tensorflow as tf\n","metadata":{"execution":{"iopub.status.busy":"2023-07-08T10:25:35.82395Z","iopub.execute_input":"2023-07-08T10:25:35.824307Z","iopub.status.idle":"2023-07-08T10:25:44.602646Z","shell.execute_reply.started":"2023-07-08T10:25:35.824283Z","shell.execute_reply":"2023-07-08T10:25:44.601488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}