{"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)\nimport os\nimport gc\nimport sys\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-22T12:15:25.710473Z","iopub.execute_input":"2023-08-22T12:15:25.71077Z","iopub.status.idle":"2023-08-22T12:15:25.8785Z","shell.execute_reply.started":"2023-08-22T12:15:25.710743Z","shell.execute_reply":"2023-08-22T12:15:25.877617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --no-index --find-links=/kaggle/input/mmdetpkgs/openmim openmim > /dev/null 2>&1\n!pip install --no-index --find-links=/kaggle/input/mmdetpkgs/mmengine mmengine > /dev/null 2>&1\n!pip install --no-index --find-links=/kaggle/input/mmdetpkgs/mmcvwheel mmcv > /dev/null 2>&1\n!pip install --no-index --find-links=/kaggle/input/mmdetpkgs/pycocotoolswheel pycocotools > /dev/null 2>&1\n# !pip install --no-index --find-links=/kaggle/input/mmdetpkgs/mmdet mmdet > /dev/null 2>&1","metadata":{"execution":{"iopub.status.busy":"2023-08-22T12:15:25.880387Z","iopub.execute_input":"2023-08-22T12:15:25.88076Z","iopub.status.idle":"2023-08-22T12:16:14.596025Z","shell.execute_reply.started":"2023-08-22T12:15:25.880728Z","shell.execute_reply":"2023-08-22T12:16:14.594706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ[\"PYTHONPATH\"] = \"/kaggle/input/contrail-comp-source\"\nos.environ[\"MKL_NUM_THREADS\"] = \"1\"\nos.environ[\"OMP_NUM_THREADS\"] = \"1\"\nos.environ[\"NUMEXPR_NUM_THREADS\"] = \"1\"\n\n!export PYTHONPATH=\".\"\n!export MKL_NUM_THREADS=\"1\"\n!export OMP_NUM_THREADS=\"1\"\n!export NUMEXPR_NUM_THREADS=\"1\"\n\n!find /kaggle/input/contrail-comp-source/dependencies -type f -print | xargs pip install --force-reinstall --no-deps --no-index","metadata":{"execution":{"iopub.status.busy":"2023-08-22T12:16:14.598709Z","iopub.execute_input":"2023-08-22T12:16:14.599018Z","iopub.status.idle":"2023-08-22T12:16:23.647951Z","shell.execute_reply.started":"2023-08-22T12:16:14.598989Z","shell.execute_reply":"2023-08-22T12:16:23.646769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_names = {    \n    # model_name, weight\n    \"exp166-maxvit-base-512-crop-5e4-25ep-gc\": [\"exp166-maxvit-base-512-crop-5e4-2\", 0.08510638],\n#     \"exp164-maxvit-large-512-crop-5e4-gc\": [\"exp164-maxvit-large-512-crop-5e4-\", 0.04255319],\n    \"exp228-effv2l-512-5e4-knpl-f4s-100ep\": [\"exp228-effv2l-512-5e4-knpl-f4s-10\", 0.04255319],\n    \"exp223-resnest269e-512-knpl-50ep\": [\"exp223-resnest269e-512-knpl-50ep\", 0.04255319],\n    \"exp227v2-maxvit-base-512-5e4-knpl-f4s-50ep\": [\"exp227v2-maxvit-base-512-5e4-knpl\", 0.04255319],\n    \"exp241-maxvit-base-512-1e4-233-15ep\": [\"exp241-maxvit-base-512-1e4-233-15\", 0.21276596],\n    \"exp243-effv2l-512-1e4-228-15ep\": [\"exp243-effv2l-512-1e4-228-15ep\", 0.08510638],\n    \"exp237-maxvit-base-512-1e4-15ep\": [\"exp237-maxvit-base-512-1e4-15ep\", 0.21276596],\n    \"exp240-resnest269e-512-1e4-p223\": [\"exp240-resnest269e-512-1e4-p223\", 0.08510638],\n    \"exp234-resnest269e-512-5e4-p223\": [\"exp234-resnest269e-512-5e4-p223\", 0.08510638],\n    \"exp247-effv2l-512-3e5-228-15ep\": [\"exp247-effv2l-512-3e5-228-15ep\", 0.10638298],\n}","metadata":{"execution":{"iopub.status.busy":"2023-08-22T12:21:13.877054Z","iopub.execute_input":"2023-08-22T12:21:13.877413Z","iopub.status.idle":"2023-08-22T12:21:13.884013Z","shell.execute_reply.started":"2023-08-22T12:21:13.877386Z","shell.execute_reply":"2023-08-22T12:21:13.882762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, (exp_name, (model_name, weight)) in enumerate(exp_names.items()):\n    fold = 0\n\n    !python /kaggle/input/contrail-comp-source/scripts/training/run.py predict_test \\\n    --config_path=\"/kaggle/input/contrail-comp-source/scripts/training/config/{exp_name}.yaml\" \\\n    num_workers=2 gpus=1 debug=False test_batch_size=12 \\\n    test_image_dir=\"/kaggle/input/google-research-identify-contrails-reduce-global-warming/test/\" \\\n    train_on_multi_frames=False \\\n    --ckpt_path=\"/kaggle/input/{model_name}/best-ckpt{fold}.ckpt\" \\\n    --output_name=\"{exp_name}-{fold}\" \\\n    --in_kaggle=True\n\n    preds = pd.read_pickle(f\"/kaggle/working/{exp_name}-0.pkl\")\n    preds = preds * weight\n\n    if idx == 0:\n        ens_preds = preds.copy()\n    else:\n        ens_preds += preds        \n        \n    !rm \"/kaggle/working/{exp_name}-0.pkl\"\n#     !rm \"/kaggle/working/{exp_name}-0_0.pkl\"","metadata":{"execution":{"iopub.status.busy":"2023-08-22T12:21:19.44808Z","iopub.execute_input":"2023-08-22T12:21:19.449012Z","iopub.status.idle":"2023-08-22T12:22:26.059855Z","shell.execute_reply.started":"2023-08-22T12:21:19.44897Z","shell.execute_reply":"2023-08-22T12:22:26.058542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(y_pred, fg_val=1):\n    def list_to_string(x):\n        if x:\n            s = str(x).replace(\"[\", \"\").replace(\"]\", \"\").replace(\",\", \"\")\n        else:\n            s = '-'\n        return s\n    dots = np.where(\n        y_pred.T.flatten() == fg_val)[0]\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 list_to_string(run_lengths)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:33:33.859051Z","iopub.execute_input":"2023-08-08T16:33:33.859796Z","iopub.status.idle":"2023-08-08T16:33:33.868023Z","shell.execute_reply.started":"2023-08-08T16:33:33.859752Z","shell.execute_reply":"2023-08-08T16:33:33.866689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming/test/\"\n\nrecord_ids = sorted([img_path for img_path in os.listdir(image_dir)])\nsub_df = pd.DataFrame(\n    {\n        \"record_id\": record_ids,\n    }\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:33:33.869778Z","iopub.execute_input":"2023-08-08T16:33:33.870167Z","iopub.status.idle":"2023-08-08T16:33:33.887162Z","shell.execute_reply.started":"2023-08-08T16:33:33.870117Z","shell.execute_reply":"2023-08-08T16:33:33.886184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_encoded = []\nthresh = 0.44\nbinary_preds = (ens_preds > thresh).astype(int).transpose(0, 2, 3, 1)\n\nfor bi_pred in binary_preds:\n    y_encoded.append(rle_encode(bi_pred))\n    \nsub_df[\"encoded_pixels\"] = y_encoded        ","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:33:33.889665Z","iopub.execute_input":"2023-08-08T16:33:33.890031Z","iopub.status.idle":"2023-08-08T16:33:33.899146Z","shell.execute_reply.started":"2023-08-08T16:33:33.889999Z","shell.execute_reply":"2023-08-08T16:33:33.898203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:33:33.904105Z","iopub.execute_input":"2023-08-08T16:33:33.906721Z","iopub.status.idle":"2023-08-08T16:33:33.927133Z","shell.execute_reply.started":"2023-08-08T16:33:33.90657Z","shell.execute_reply":"2023-08-08T16:33:33.926114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2023-08-08T16:35:06.012223Z","iopub.execute_input":"2023-08-08T16:35:06.012899Z","iopub.status.idle":"2023-08-08T16:35:06.02883Z","shell.execute_reply.started":"2023-08-08T16:35:06.012866Z","shell.execute_reply":"2023-08-08T16:35:06.027914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}