{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom pathlib import Path","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-18T12:29:10.717512Z","iopub.execute_input":"2023-05-18T12:29:10.71823Z","iopub.status.idle":"2023-05-18T12:29:10.731844Z","shell.execute_reply.started":"2023-05-18T12:29:10.718186Z","shell.execute_reply":"2023-05-18T12:29:10.730627Z"},"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(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","metadata":{"execution":{"iopub.status.busy":"2023-05-18T12:29:10.733149Z","iopub.execute_input":"2023-05-18T12:29:10.733505Z","iopub.status.idle":"2023-05-18T12:29:10.74876Z","shell.execute_reply.started":"2023-05-18T12:29:10.733476Z","shell.execute_reply":"2023-05-18T12:29:10.747609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_mask = np.ones((256,256))","metadata":{"execution":{"iopub.status.busy":"2023-05-18T12:29:10.751079Z","iopub.execute_input":"2023-05-18T12:29:10.752081Z","iopub.status.idle":"2023-05-18T12:29:10.762658Z","shell.execute_reply.started":"2023-05-18T12:29:10.752043Z","shell.execute_reply":"2023-05-18T12:29:10.761532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict Entire Image as Contrails","metadata":{}},{"cell_type":"code","source":"submission_string = list_to_string(rle_encode(predicted_mask))\nsubmission_string","metadata":{"execution":{"iopub.status.busy":"2023-05-18T12:29:10.765203Z","iopub.execute_input":"2023-05-18T12:29:10.76563Z","iopub.status.idle":"2023-05-18T12:29:10.819683Z","shell.execute_reply.started":"2023-05-18T12:29:10.765584Z","shell.execute_reply":"2023-05-18T12:29:10.818511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_test_paths = list(Path('/kaggle/input/google-research-identify-contrails-reduce-global-warming/test').glob(\"*\"))","metadata":{"execution":{"iopub.status.busy":"2023-05-18T12:29:10.820908Z","iopub.execute_input":"2023-05-18T12:29:10.821231Z","iopub.status.idle":"2023-05-18T12:29:10.827052Z","shell.execute_reply.started":"2023-05-18T12:29:10.821204Z","shell.execute_reply":"2023-05-18T12:29:10.825904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.DataFrame({'record_id': [path.stem for path in all_test_paths], 'encoded_pixels': [submission_string]*len(all_test_paths)})\nsub_df","metadata":{"execution":{"iopub.status.busy":"2023-05-18T12:29:10.828734Z","iopub.execute_input":"2023-05-18T12:29:10.829078Z","iopub.status.idle":"2023-05-18T12:29:10.847223Z","shell.execute_reply.started":"2023-05-18T12:29:10.829048Z","shell.execute_reply":"2023-05-18T12:29:10.846096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T12:29:10.849174Z","iopub.execute_input":"2023-05-18T12:29:10.850006Z","iopub.status.idle":"2023-05-18T12:29:10.863176Z","shell.execute_reply.started":"2023-05-18T12:29:10.849962Z","shell.execute_reply":"2023-05-18T12:29:10.862165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}