{"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":"# YOLOX detections submission made on COTS dataset (PART 2 - DETECTION)\n\nThis notebook shows how to detect starfish objects (COTS dataset) using YOLOX ON  Kaggle. First part - Building Cutom Model on Kaggle using YOLOX I implmeneted in notebook called [YoloX full training pipeline for COTS dataset](https://www.kaggle.com/remekkinas/yolox-full-training-pipeline-for-cots-dataset). It could be good starting point for build own custom model based on YOLOX detector. Full github repository you can find here - [YOLOX](https://github.com/Megvii-BaseDetection/YOLOX)\n\n<div align = 'center'><img src='https://github.com/Megvii-BaseDetection/YOLOX/raw/main/assets/logo.png'/></div>\n\n<div class=\"alert alert-success\" role=\"alert\">\nThis work consists of two parts:     \n    <ul>\n        <li> <a href=\"https://www.kaggle.com/remekkinas/yolox-full-training-pipeline-for-cots-dataset\">YoloX full training pipeline for COTS dataset</a></li>\n        <li> YOLOX detections submission made on COTS dataset</li>\n    </ul>\n    \n</div>\n\n<div class=\"alert alert-warning\" role=\"alert\"><strong><ul><li>This is DEOMO only! What does it mean? Inference is made so far on weak model - trained only on 20 epochs.</li><li>I really appreciate if you <u>vote on both of these notebooks</u> - thank you! I just share my work to make competition fun and more interesting.</li></ul> </strong></div>\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport os\nimport torch\nimport importlib\nimport cv2 \nimport numpy as np\nimport pandas as pd\n\nfrom PIL import Image\nfrom IPython.display import display","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:35:38.288168Z","iopub.execute_input":"2022-02-06T17:35:38.288484Z","iopub.status.idle":"2022-02-06T17:35:39.756037Z","shell.execute_reply.started":"2022-02-06T17:35:38.288403Z","shell.execute_reply":"2022-02-06T17:35:39.755297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### INSTALL YOLOX \n<div class=\"alert alert-warning\" role=\"alert\"><strong>It unfortunately requires a lot of Kaggle enviroment hacking :) due to competition limitation - no internet access during submission.</strong></div>","metadata":{}},{"cell_type":"code","source":"# download required packages - first time when I created database (https://www.kaggle.com/remekkinas/yolox-cots-models) with required moduls for YOLOX\n# don't use this section of code until Kaggle doesn't change something in the environment (!!)\n\n\n#%mkdir /kaggle/working/yolox-dep\n#!pip download pip -d \"/kaggle/working/yolox-dep\"\n#!pip download loguru -d \"/kaggle/working/yolox-dep\"\n#!pip download ninja -d \"/kaggle/working/yolox-dep\"\n#!pip download onnx==\"1.8.1\" -d \"/kaggle/working/yolox-dep\"\n#!pip download onnxruntime==\"1.8.0\" -d \"/kaggle/working/yolox-dep\"\n#!pip download onnxoptimizer>=\"0.2.5\" -d \"/kaggle/working/yolox-dep\"\n#!pip download thop -d \"/kaggle/working/yolox-dep\"\n#!pip download tabulate -d \"/kaggle/working/yolox-dep\"\n#!pip download onnx-simplifier==0.3.5 -d \"/kaggle/working/yolox-dep\"","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-06T17:35:39.759516Z","iopub.execute_input":"2022-02-06T17:35:39.759743Z","iopub.status.idle":"2022-02-06T17:35:39.765611Z","shell.execute_reply.started":"2022-02-06T17:35:39.759711Z","shell.execute_reply":"2022-02-06T17:35:39.76493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy YOLOX and required modules from local repository (Kaggle dataset -> https://www.kaggle.com/remekkinas/yolox-cots-models)\n%cp -r /kaggle/input/yolox-cots-models /kaggle/working/\n%cd /kaggle/working/yolox-cots-models/yolox-dep","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:35:39.766832Z","iopub.execute_input":"2022-02-06T17:35:39.767127Z","iopub.status.idle":"2022-02-06T17:35:59.589Z","shell.execute_reply.started":"2022-02-06T17:35:39.767093Z","shell.execute_reply":"2022-02-06T17:35:59.588177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install YOLOX required modules\n\n!pip install pip-21.3.1-py3-none-any.whl -f ./ --no-index\n!pip install loguru-0.5.3-py3-none-any.whl -f ./ --no-index\n!pip install ninja-1.10.2.3-py2.py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl -f ./ --no-index\n!pip install onnx-1.8.1-cp37-cp37m-manylinux2010_x86_64.whl -f ./ --no-index\n!pip install onnxruntime-1.8.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -f ./ --no-index\n!pip install onnxoptimizer-0.2.6-cp37-cp37m-manylinux2014_x86_64.whl -f ./ --no-index\n!pip install thop-0.0.31.post2005241907-py3-none-any.whl -f ./ --no-index\n!pip install tabulate-0.8.9-py3-none-any.whl -f ./ --no-index\n#!pip install onnx-simplifier-0.3.6.tar.gz -f ./ --no-index","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-06T17:35:59.591561Z","iopub.execute_input":"2022-02-06T17:35:59.592011Z","iopub.status.idle":"2022-02-06T17:37:13.054936Z","shell.execute_reply.started":"2022-02-06T17:35:59.591969Z","shell.execute_reply":"2022-02-06T17:37:13.054098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install YOLOX\n%cd /kaggle/working/yolox-cots-models/YOLOX\n!pip install -r requirements.txt\n!pip install -v -e . ","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-06T17:37:13.057108Z","iopub.execute_input":"2022-02-06T17:37:13.05744Z","iopub.status.idle":"2022-02-06T17:38:27.08398Z","shell.execute_reply.started":"2022-02-06T17:37:13.057399Z","shell.execute_reply":"2022-02-06T17:38:27.08317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install CocoAPI tool\n%cd /kaggle/working/yolox-cots-models/yolox-dep/cocoapi/PythonAPI\n\n!make\n!make install\n!python setup.py install","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-06T17:38:27.085538Z","iopub.execute_input":"2022-02-06T17:38:27.085821Z","iopub.status.idle":"2022-02-06T17:38:45.948327Z","shell.execute_reply.started":"2022-02-06T17:38:27.085785Z","shell.execute_reply":"2022-02-06T17:38:45.947551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pycocotools","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:38:45.951585Z","iopub.execute_input":"2022-02-06T17:38:45.951813Z","iopub.status.idle":"2022-02-06T17:38:45.957675Z","shell.execute_reply.started":"2022-02-06T17:38:45.951787Z","shell.execute_reply":"2022-02-06T17:38:45.957002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cp -r /kaggle/input/ensemble-boxes /kaggle/working/\n%cd /kaggle/working/ensemble-boxes\n\n!python setup.py install","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:38:45.959153Z","iopub.execute_input":"2022-02-06T17:38:45.959625Z","iopub.status.idle":"2022-02-06T17:38:49.022128Z","shell.execute_reply.started":"2022-02-06T17:38:45.95959Z","shell.execute_reply":"2022-02-06T17:38:49.021119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### TEST MODEL - MAKE INFERENCE ON SAMPLE DATA\n","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working/yolox-cots-models/YOLOX\n# CHECKPOINT_FILES = '/kaggle/input/gbr-weights/exp020/fold0/yolo_m_fold0/latest_ckpt.pth'\nCHECKPOINT_FILE = ['/kaggle/input/gbr-weights/exp026/fold0/yolo_m_fold0/latest_ckpt.pth', '/kaggle/input/gbr-weights/exp026/fold1/yolo_m_fold1/latest_ckpt.pth', '/kaggle/input/gbr-weights/exp026/fold2/yolo_m_fold2/latest_ckpt.pth']\n","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:38:49.023961Z","iopub.execute_input":"2022-02-06T17:38:49.024594Z","iopub.status.idle":"2022-02-06T17:38:49.034222Z","shell.execute_reply.started":"2022-02-06T17:38:49.024546Z","shell.execute_reply":"2022-02-06T17:38:49.033336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config_file_template = '''\n\n#!/usr/bin/env python3\n# -*- coding:utf-8 -*-\n# Copyright (c) Megvii, Inc. and its affiliates.\n\n        \nimport os\n\nfrom torch import nn\n\nfrom yolox.exp import Exp as MyExp\n\nFOLD = 0\nE = \"001\"\nCOMMENT = \"add sync batchnorm\"\n\n\nclass Exp(MyExp):\n    def __init__(self):\n        super(Exp, self).__init__()\n        self.num_classes = 1\n        self.depth = 0.67\n        self.width = 0.75\n        self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(\".\")[0]\n        self.input_size = (2560, 2560)  # (height, width)\n        self.test_size = (2560, 2560)\n\n'''\n\nwith open('cots_config.py', 'w') as f:\n    f.write(config_file_template)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:38:49.037559Z","iopub.execute_input":"2022-02-06T17:38:49.038038Z","iopub.status.idle":"2022-02-06T17:38:49.045048Z","shell.execute_reply.started":"2022-02-06T17:38:49.037984Z","shell.execute_reply":"2022-02-06T17:38:49.044214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yolox.utils import postprocess\nfrom yolox.data.data_augment import ValTransform\n\nCOCO_CLASSES = (\n  \"starfish\",\n)\n\n# get YOLOX experiment\ncurrent_exp = importlib.import_module('cots_config')\n\n# set inference parameters\n# test_size = (5120, 5120)\n# test_size = (3840, 3840)\n# test_size = ((1920, 1920), (2560, 2560))\ntest_size = [(1920, 1920), (2560, 2560), (3840, 3840)]\n# test_size = [(2560, 2560), (3840, 3840)]\n# test_size = [(2560, 2560), (2560, 2560)]\nnum_classes = 1\nconfthre = 0.01\nconfthre2 = 0.1\nnmsthre = 0.5\nbbox_min_size = 400\n\n\n# get custom trained checkpoint\n# ckpt_files = CHECKPOINT_FILE\nmodels = []\nfor ckpt_file in CHECKPOINT_FILE:\n    # get YOLOX model\n    exp = current_exp.Exp()\n    model = exp.get_model()\n    model.cuda()\n    model.eval()\n    ckpt = torch.load(ckpt_file, map_location=\"cpu\")\n    model.load_state_dict(ckpt[\"model\"])\n    models.append(model)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:38:49.046467Z","iopub.execute_input":"2022-02-06T17:38:49.047032Z","iopub.status.idle":"2022-02-06T17:39:02.843786Z","shell.execute_reply.started":"2022-02-06T17:38:49.046941Z","shell.execute_reply":"2022-02-06T17:39:02.842933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/ensemble-boxes/\nimport ensemble_boxes","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:39:02.845272Z","iopub.execute_input":"2022-02-06T17:39:02.845548Z","iopub.status.idle":"2022-02-06T17:39:03.435591Z","shell.execute_reply.started":"2022-02-06T17:39:02.845509Z","shell.execute_reply":"2022-02-06T17:39:03.434862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ensemble_boxes import weighted_boxes_fusion\n\ndef opticalflow_postprocess(\n    last_img,\n    current_img,\n    last_dets,\n    current_dets,\n    score_coef=0.9,\n    # score_threshold=0.2\n):\n    if len(last_dets) == 0 or last_img is None:\n        return current_dets\n    current_gray = cv2.cvtColor(current_img, cv2.COLOR_RGB2GRAY)\n    last_gray = cv2.cvtColor(last_img, cv2.COLOR_RGB2GRAY)\n    flow = cv2.calcOpticalFlowFarneback(\n        last_gray, current_gray, None, 0.5, 3, 15, 3, 5, 1.1, 0\n    )\n    vx, vy = flow[..., 0], flow[..., 1]\n    pred_dets = last_dets.copy()\n    filter_ind = []\n    for i in range(len(pred_dets)):\n        bx1 = np.clip(pred_dets[i, 1], 0, last_img.shape[1] - 1)\n        by1 = np.clip(pred_dets[i, 2], 0, last_img.shape[0] - 1)\n        bx2 = np.clip(pred_dets[i, 3], 0, last_img.shape[1] - 1)\n        by2 = np.clip(pred_dets[i, 4], 0, last_img.shape[0] - 1)\n        pred_dets[i, 1] += vx[int(by1)][int(bx1)]\n        pred_dets[i, 2] += vy[int(by1)][int(bx1)]\n        pred_dets[i, 3] += vx[int(by2)][int(bx2)]\n        pred_dets[i, 4] += vy[int(by2)][int(bx2)]\n        pred_dets[i, 0] *= score_coef\n        bxc = (pred_dets[i, 1] + pred_dets[i, 3]) / 2\n        byc = (pred_dets[i, 2] + pred_dets[i, 4]) / 2\n        pred_dets[i, 1] = np.clip(pred_dets[i, 1], 0, last_img.shape[1])\n        pred_dets[i, 2] = np.clip(pred_dets[i, 2], 0, last_img.shape[0])\n        pred_dets[i, 3] = np.clip(pred_dets[i, 3], 0, last_img.shape[1])\n        pred_dets[i, 4] = np.clip(pred_dets[i, 4], 0, last_img.shape[0])\n        # if last_img.shape[1] > bxc > 0 and last_img.shape[0] > byc > 0:\n        # if (\n        #     pred_dets[i, 0] > score_threshold\n        #     and last_img.shape[1] - 5 > bxc > 5\n        #     and last_img.shape[0] - 5 > byc > 5\n        # ):\n        if last_img.shape[1] - 5 > bxc > 5 and last_img.shape[0] - 5 > byc > 5:\n            filter_ind.append(i)\n    pred_dets = np.asarray([pred_dets[f] for f in filter_ind])\n    if len(pred_dets) > 0:\n        if len(current_dets) > 0:\n            # pred_dets = np.concatenate([current_dets, pred_dets])\n            current_dets[:, 1] /= last_img.shape[1]\n            current_dets[:, 2] /= last_img.shape[0]\n            current_dets[:, 3] /= last_img.shape[1]\n            current_dets[:, 4] /= last_img.shape[0]\n            pred_dets[:, 1] /= last_img.shape[1]\n            pred_dets[:, 2] /= last_img.shape[0]\n            pred_dets[:, 3] /= last_img.shape[1]\n            pred_dets[:, 4] /= last_img.shape[0]\n            new_bboxes, new_scores, _ = weighted_boxes_fusion(\n                [current_dets[:, 1:].clip(0, 1), pred_dets[:, 1:].clip(0, 1)],\n                [current_dets[:, 0].clip(0, 1), pred_dets[:, 0]],\n                [\n                    np.zeros_like(current_dets[:, 0]),\n                    np.zeros_like(pred_dets[:, 0]),\n                ],\n                weights=(2, 1),\n                iou_thr=nmsthre,\n                skip_box_thr=0.0001,\n            )\n            new_bboxes[:, 0] *= last_img.shape[1]\n            new_bboxes[:, 1] *= last_img.shape[0]\n            new_bboxes[:, 2] *= last_img.shape[1]\n            new_bboxes[:, 3] *= last_img.shape[0]\n            result_dets = np.concatenate([new_scores[:, None], new_bboxes], -1)\n        else:\n            result_dets = pred_dets\n    else:\n        result_dets = current_dets\n    return result_dets","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:39:03.437079Z","iopub.execute_input":"2022-02-06T17:39:03.437345Z","iopub.status.idle":"2022-02-06T17:39:03.457642Z","shell.execute_reply.started":"2022-02-06T17:39:03.437309Z","shell.execute_reply":"2022-02-06T17:39:03.456889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def yolox_inference(img, models, test_size): \n    bboxes = []\n    bbclasses = []\n    scores = []\n    bboxes_models = []\n    bbclasses_models = []\n    scores_models = []\n    \n    preproc = ValTransform(legacy = False)\n    for i, ts in enumerate(test_size):\n        numpy_img, _ = preproc(img, None, ts)\n        if i % 2 ==0:\n            tensor_img = torch.from_numpy(numpy_img).flip(-1).unsqueeze(0)\n        else:\n            tensor_img = torch.from_numpy(numpy_img).unsqueeze(0)\n#         tensor_img = torch.from_numpy(numpy_img).unsqueeze(0)\n#         tensor_img = torch.from_numpy(numpy_img).flip(-1).unsqueeze(0)\n#         tensor_img = torch.cat([tensor_img, tensor_img_flip])\n        tensor_img = tensor_img.float()\n        tensor_img = tensor_img.cuda()\n        for model in models:\n            with torch.no_grad():\n                outputs = model(tensor_img)\n                outputs = postprocess(\n                            outputs, num_classes, confthre,\n                            nmsthre, class_agnostic=True\n                        )\n\n            if outputs[0] is None:\n                bboxes_models.append([])\n                bbclasses_models.append([])\n                scores_models.append([])\n            else:\n                outputs1 = outputs[0].cpu()\n                bboxes = outputs1[:, 0:4]\n\n                bboxes /= min(ts[0] / img.shape[0], ts[1] / img.shape[1])\n                if i % 2 == 0:\n                    bboxes[:, 0] = img.shape[1] - bboxes[:, 0]\n                    bboxes[:, 2] = img.shape[1] - bboxes[:, 2]\n                bbclasses = outputs1[:, 6]\n                scores = outputs1[:, 4] * outputs1[:, 5]\n                bboxes_models.append(bboxes)\n                bbclasses_models.append(bbclasses)\n                scores_models.append(scores)\n#             if outputs[1] is None:\n                \n#                 bboxes_models.append([])\n#                 bbclasses_models.append([])\n#                 scores_models.append([])\n#             else:\n#                 outputs2 = outputs[1].cpu()\n#                 bboxes = outputs2[:, 0:4]\n\n#                 bboxes /= min(ts[0] / img.shape[0], ts[1] / img.shape[1])\n#                 bboxes[:, 0] = img.shape[1] - bboxes[:, 0]\n#                 bboxes[:, 2] = img.shape[1] - bboxes[:, 2]\n#                 bbclasses = outputs2[:, 6]\n#                 scores = outputs2[:, 4] * outputs2[:, 5]\n#                 bboxes_models.append(bboxes)\n#                 bbclasses_models.append(bbclasses)\n#                 scores_models.append(scores)\n    \n    return bboxes_models, bbclasses_models, scores_models","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:39:03.459231Z","iopub.execute_input":"2022-02-06T17:39:03.460094Z","iopub.status.idle":"2022-02-06T17:39:03.470459Z","shell.execute_reply.started":"2022-02-06T17:39:03.460055Z","shell.execute_reply":"2022-02-06T17:39:03.469654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_yolox_predictions(img, bboxes, scores, bbclasses, confthre, classes_dict):\n    for i in range(len(bboxes)):\n            box = bboxes[i]\n            cls_id = int(bbclasses[i])\n            score = scores[i]\n            if score < confthre:\n                continue\n            x0 = int(box[0])\n            y0 = int(box[1])\n            x1 = int(box[2])\n            y1 = int(box[3])\n\n            cv2.rectangle(img, (x0, y0), (x1, y1), (0, 255, 0), 2)\n            cv2.putText(img, '{}:{:.1f}%'.format(classes_dict[cls_id], score * 100), (x0, y0 - 3), cv2.FONT_HERSHEY_PLAIN, 0.8, (0,255,0), thickness = 1)\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:39:03.471676Z","iopub.execute_input":"2022-02-06T17:39:03.472075Z","iopub.status.idle":"2022-02-06T17:39:03.482169Z","shell.execute_reply.started":"2022-02-06T17:39:03.472042Z","shell.execute_reply":"2022-02-06T17:39:03.481533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_IMAGE_PATH = \"/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/9674.jpg\"\nimg = cv2.imread(TEST_IMAGE_PATH)\n\n# Get predictions\n# bboxes, bbclasses, scores = yolox_inference(img, model, test_size)\nbboxes_models, bbclasses_models, scores_models = yolox_inference(img, models, test_size)\n# bboxes_models_norm = []\n# for bboxes in bboxes_models:\n#     if len(bboxes) \nbboxes_models = [np.stack([bboxes[:, 0] / img.shape[1], bboxes[:, 1] / img.shape[0], bboxes[:, 2] / img.shape[1], bboxes[:, 3] / img.shape[0]], 1).clip(0, 1) if len(bboxes) > 0 else np.asarray([]) for bboxes in bboxes_models]\nbboxes, scores, bbclasses = weighted_boxes_fusion(\n    bboxes_models,\n    scores_models,\n    bbclasses_models,\n    weights=np.ones(len(bboxes_models)),\n    iou_thr=0.65,\n    skip_box_thr=0.0001,\n)\n\nbboxes = np.stack([bboxes[:, 0] * img.shape[1], bboxes[:, 1] * img.shape[0], bboxes[:, 2] * img.shape[1], bboxes[:, 3] * img.shape[0]], 1)\n# Draw predictions\nout_image = draw_yolox_predictions(img, bboxes, scores, bbclasses, confthre2, COCO_CLASSES)\n\n# Since we load image using OpenCV we have to convert it \nout_image = cv2.cvtColor(out_image, cv2.COLOR_BGR2RGB)\ndisplay(Image.fromarray(out_image))","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:39:03.483473Z","iopub.execute_input":"2022-02-06T17:39:03.483758Z","iopub.status.idle":"2022-02-06T17:39:09.12653Z","shell.execute_reply.started":"2022-02-06T17:39:03.483723Z","shell.execute_reply":"2022-02-06T17:39:09.123771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SUBMIT PREDICTION TO COMPETITION","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:39:09.127534Z","iopub.execute_input":"2022-02-06T17:39:09.127779Z","iopub.status.idle":"2022-02-06T17:39:09.13489Z","shell.execute_reply.started":"2022-02-06T17:39:09.127749Z","shell.execute_reply":"2022-02-06T17:39:09.13418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\n\nenv = greatbarrierreef.make_env()   # initialize the environment\niter_test = env.iter_test()  ","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:39:09.136317Z","iopub.execute_input":"2022-02-06T17:39:09.136769Z","iopub.status.idle":"2022-02-06T17:39:09.178332Z","shell.execute_reply.started":"2022-02-06T17:39:09.136736Z","shell.execute_reply":"2022-02-06T17:39:09.177748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_dict = {\n    'id': [],\n    'prediction_string': [],\n}\ntest_df = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/test.csv\")\nlast_img = None\nlast_dets = None\nfor i, (image_np, sample_prediction_df) in enumerate(iter_test):\n    row = test_df.loc[i]\n    frame_id = row.sequence_frame\n    image_np = image_np[:,:,::-1] \n    bboxes_models, bbclasses_models, scores_models = yolox_inference(image_np, models, test_size)\n    bboxes_models = [np.stack([bboxes[:, 0] / img.shape[1], bboxes[:, 1] / img.shape[0], bboxes[:, 2] / img.shape[1], bboxes[:, 3] / img.shape[0]], 1).clip(0, 1) if len(bboxes) > 0 else np.asarray([]) for bboxes in bboxes_models]\n    if len(bboxes_models) > 1:\n        bboxes, scores, bbclasses = weighted_boxes_fusion(\n            bboxes_models,\n            scores_models,\n            bbclasses_models,\n            weights=np.ones(len(bboxes_models)),\n            iou_thr=0.65,\n            skip_box_thr=0.0001,\n        )\n    else:\n        bboxes, scores, bbclasses = bboxes_models[0], scores_models[0], bbclasses_models[0]\n    if len(bboxes) > 0:\n        bboxes = np.stack([bboxes[:, 0] * image_np.shape[1], bboxes[:, 1] * image_np.shape[0], bboxes[:, 2] * image_np.shape[1], bboxes[:, 3] * image_np.shape[0]], 1)\n    if len(bboxes) > 0:\n        current_dets = np.concatenate([np.asarray(scores)[:, None], np.asarray(bboxes)], -1)\n    else:\n        current_dets = []\n    if frame_id != 0:\n        current_dets = opticalflow_postprocess(\n            last_img,\n            image_np,\n            last_dets,\n            current_dets,\n            score_coef=0.9,\n        )\n    \n    predictions = []\n    for i in range(len(current_dets)):\n        box = current_dets[i, 1:]\n        score = current_dets[i, 0]\n        if score < confthre2 or ((box[2] - box[0]) * (box[3] - box[1]) < bbox_min_size):\n            continue\n        x_min = int(box[0])\n        y_min = int(box[1])\n        x_max = int(box[2])\n        y_max = int(box[3])\n        \n        bbox_width = x_max - x_min\n        bbox_height = y_max - y_min\n#         x_min += bbox_width * 0.1\n#         y_min += bbox_height * 0.1\n#         bbox_width *= 0.8\n#         bbox_height *= 0.8\n        predictions.append('{:.2f} {} {} {} {}'.format(score, x_min, y_min, bbox_width, bbox_height))\n    \n    prediction_str = ' '.join(predictions)\n    sample_prediction_df['annotations'] = prediction_str\n    env.predict(sample_prediction_df)\n    last_img = image_np\n    last_dets = current_dets\n    print('Prediction:', prediction_str)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:39:09.179594Z","iopub.execute_input":"2022-02-06T17:39:09.180193Z","iopub.status.idle":"2022-02-06T17:39:09.978211Z","shell.execute_reply.started":"2022-02-06T17:39:09.180155Z","shell.execute_reply":"2022-02-06T17:39:09.977025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv('submission.csv')\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T17:39:09.979423Z","iopub.status.idle":"2022-02-06T17:39:09.980459Z","shell.execute_reply.started":"2022-02-06T17:39:09.9802Z","shell.execute_reply":"2022-02-06T17:39:09.980227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-success\" role=\"alert\">\n    Find this notebook helpful? :) Please give me a vote ;) Thank you\n </div>","metadata":{}}]}