{"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":"# Naive Model Baseline\n\nA naive model baseline for the contrails competition.\n\nThis model predicts that every pixel is a contrail. If we predict no contrail at every pixel, the model scores a 0 because of the Dice scoring (no non-zero intersections means a numerator of 0).\n    \n| Model                      | Dice Score  |\n| -------------------------- | ----------: |\n| Naive - All contrails      | 0.002       |\n| Naive - No contrails       | 0.0         |\n    ","metadata":{}},{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-18T23:05:34.143678Z","iopub.execute_input":"2023-05-18T23:05:34.144863Z","iopub.status.idle":"2023-05-18T23:05:34.190258Z","shell.execute_reply.started":"2023-05-18T23:05:34.144805Z","shell.execute_reply":"2023-05-18T23:05:34.188686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run-Length Code\n\nThe following is code to both encode and decode RLE format.\n\nIMPORTANT: Unlike many previous Kaggle competition, empty predictions must be encoded as `'-'`. Empty string / null predictions will cause an error in scoring. The code below handles this change.","metadata":{}},{"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","metadata":{"execution":{"iopub.status.busy":"2023-05-18T23:05:34.19236Z","iopub.execute_input":"2023-05-18T23:05:34.192787Z","iopub.status.idle":"2023-05-18T23:05:34.203293Z","shell.execute_reply.started":"2023-05-18T23:05:34.192746Z","shell.execute_reply":"2023-05-18T23:05:34.201837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create a naive submission\n\nPredict that there are no contrail pixels at all!","metadata":{}},{"cell_type":"code","source":"test_recs = os.listdir(data_path / 'test')\nprint(test_recs)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T23:05:34.205203Z","iopub.execute_input":"2023-05-18T23:05:34.205708Z","iopub.status.idle":"2023-05-18T23:05:34.224222Z","shell.execute_reply.started":"2023-05-18T23:05:34.205668Z","shell.execute_reply":"2023-05-18T23:05:34.222041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict contrails everywhere\nmask = np.ones((256, 256))\n\nplt.imshow(mask, cmap='Greys')\nplt.title(\"Contrails Everywhere\", fontsize='16')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-18T23:05:46.728286Z","iopub.execute_input":"2023-05-18T23:05:46.72887Z","iopub.status.idle":"2023-05-18T23:05:47.011551Z","shell.execute_reply.started":"2023-05-18T23:05:46.728815Z","shell.execute_reply":"2023-05-18T23:05:47.010471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert to RLE","metadata":{}},{"cell_type":"code","source":"list_to_string(rle_encode(mask))","metadata":{"execution":{"iopub.status.busy":"2023-05-18T23:05:34.529412Z","iopub.execute_input":"2023-05-18T23:05:34.529776Z","iopub.status.idle":"2023-05-18T23:05:34.583329Z","shell.execute_reply.started":"2023-05-18T23:05:34.529741Z","shell.execute_reply":"2023-05-18T23:05:34.582303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Now let's automate a submission","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='record_id')\n\nfor rec in test_recs:\n    mask = np.ones((256, 256))\n    submission.loc[int(rec), 'encoded_pixels'] = list_to_string(rle_encode(mask))\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-18T23:06:14.629752Z","iopub.execute_input":"2023-05-18T23:06:14.630168Z","iopub.status.idle":"2023-05-18T23:06:14.741627Z","shell.execute_reply.started":"2023-05-18T23:06:14.630129Z","shell.execute_reply":"2023-05-18T23:06:14.740065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-18T23:05:34.639769Z","iopub.execute_input":"2023-05-18T23:05:34.640148Z","iopub.status.idle":"2023-05-18T23:05:34.652264Z","shell.execute_reply.started":"2023-05-18T23:05:34.640111Z","shell.execute_reply":"2023-05-18T23:05:34.650991Z"},"trusted":true},"execution_count":null,"outputs":[]}]}