{"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 warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:20:39.821611Z","iopub.execute_input":"2022-02-14T14:20:39.821915Z","iopub.status.idle":"2022-02-14T14:20:39.844765Z","shell.execute_reply.started":"2022-02-14T14:20:39.821833Z","shell.execute_reply":"2022-02-14T14:20:39.844061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!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","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:20:39.846461Z","iopub.execute_input":"2022-02-14T14:20:39.846770Z","iopub.status.idle":"2022-02-14T14:20:52.176308Z","shell.execute_reply.started":"2022-02-14T14:20:39.846734Z","shell.execute_reply":"2022-02-14T14:20:52.175467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# norfair dependencies\n%cd /kaggle/input/norfair031py3/\n!pip install commonmark-0.9.1-py2.py3-none-any.whl -f ./ --no-index\n!pip install rich-9.13.0-py3-none-any.whl\n\n!mkdir /kaggle/working/tmp\n!cp -r /kaggle/input/norfair031py3/filterpy-1.4.5/filterpy-1.4.5/ /kaggle/working/tmp/\n%cd /kaggle/working/tmp/filterpy-1.4.5/\n!pip install .\n!rm -rf /kaggle/working/tmp\n\n# norfair\n%cd /kaggle/input/norfair031py3/\n!pip install norfair-0.3.1-py3-none-any.whl -f ./ --no-index","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:20:52.179710Z","iopub.execute_input":"2022-02-14T14:20:52.179927Z","iopub.status.idle":"2022-02-14T14:22:07.881295Z","shell.execute_reply.started":"2022-02-14T14:20:52.179901Z","shell.execute_reply":"2022-02-14T14:22:07.880408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📚 Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport glob\nimport shutil\nimport sys\nsys.path.append('../input/tensorflow-great-barrier-reef')\nimport torch\nfrom PIL import Image\nimport ast\nimport albumentations as albu\n# import pycocotools\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport importlib\nfrom PIL import Image\nfrom IPython.display import display\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-14T14:22:07.884880Z","iopub.execute_input":"2022-02-14T14:22:07.885135Z","iopub.status.idle":"2022-02-14T14:22:11.281722Z","shell.execute_reply.started":"2022-02-14T14:22:07.885107Z","shell.execute_reply":"2022-02-14T14:22:11.280991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR  = '/kaggle/input/tensorflow-great-barrier-reef/'\nlist_yolov5_checkpoint = [\n    '/kaggle/input/yolov5m-1664-fold0/best(17).pt',\n    '/kaggle/input/yolov5m-1664-fold1/best(16).pt',\n    '/kaggle/input/yolov5m-1664-fold2-1422022/best(18).pt'\n]\ntest_size_yolov5  = int(1664*2)\nlist_yolov5_conf  = [0.29,0.229,0.521]\nyolov5_iou       = 0.5\n\nAUGMENT   = False\nFDA_aug = False\n\n# ensemble WBF seting\nweighted_ensemble = [1,1,1.3]\niou_ensemble = 0.5\nskip_box_thr = 0.0001\nsigma = 0.1","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:11.284329Z","iopub.execute_input":"2022-02-14T14:22:11.284612Z","iopub.status.idle":"2022-02-14T14:22:11.293017Z","shell.execute_reply.started":"2022-02-14T14:22:11.284578Z","shell.execute_reply":"2022-02-14T14:22:11.292229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_path(row):\n    row['image_path'] = f'{ROOT_DIR}/train_images/video_{row.video_id}/{row.video_frame}.jpg'\n    return row","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:11.294365Z","iopub.execute_input":"2022-02-14T14:22:11.294665Z","iopub.status.idle":"2022-02-14T14:22:11.310928Z","shell.execute_reply.started":"2022-02-14T14:22:11.294629Z","shell.execute_reply":"2022-02-14T14:22:11.310177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Data\ndf = pd.read_csv(f'{ROOT_DIR}/train.csv')\ndf = df.progress_apply(get_path, axis=1)\ndf['annotations'] = df['annotations'].progress_apply(lambda x: ast.literal_eval(x))\ndisplay(df.head(2))","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:11.312365Z","iopub.execute_input":"2022-02-14T14:22:11.312699Z","iopub.status.idle":"2022-02-14T14:22:26.995780Z","shell.execute_reply.started":"2022-02-14T14:22:11.312659Z","shell.execute_reply":"2022-02-14T14:22:26.995139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FDA_reference = df[df['annotations']!='[]']","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:26.996980Z","iopub.execute_input":"2022-02-14T14:22:26.997318Z","iopub.status.idle":"2022-02-14T14:22:27.012920Z","shell.execute_reply.started":"2022-02-14T14:22:26.997278Z","shell.execute_reply":"2022-02-14T14:22:27.012132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FDA_trans = albu.FDA(FDA_reference['image_path'].values)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:27.014450Z","iopub.execute_input":"2022-02-14T14:22:27.015476Z","iopub.status.idle":"2022-02-14T14:22:27.019397Z","shell.execute_reply.started":"2022-02-14T14:22:27.015438Z","shell.execute_reply":"2022-02-14T14:22:27.018735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Number of BBoxes","metadata":{}},{"cell_type":"code","source":"df['num_bbox'] = df['annotations'].progress_apply(lambda x: len(x))\ndata = (df.num_bbox>0).value_counts()/len(df)*100\nprint(f\"No BBox: {data[0]:0.2f}% | With BBox: {data[1]:0.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:27.020919Z","iopub.execute_input":"2022-02-14T14:22:27.021443Z","iopub.status.idle":"2022-02-14T14:22:27.123450Z","shell.execute_reply.started":"2022-02-14T14:22:27.021405Z","shell.execute_reply":"2022-02-14T14:22:27.121757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔨 Helper","metadata":{}},{"cell_type":"code","source":"def voc2yolo(bboxes, image_height=720, image_width=1280):\n    \"\"\"\n    voc  => [x1, y1, x2, y1]\n    yolo => [xmid, ymid, w, h] (normalized)\n    \"\"\"\n    \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\n    \n    bboxes[..., [0, 2]] = bboxes[..., [0, 2]]/ image_width\n    bboxes[..., [1, 3]] = bboxes[..., [1, 3]]/ image_height\n    \n    w = bboxes[..., 2] - bboxes[..., 0]\n    h = bboxes[..., 3] - bboxes[..., 1]\n    \n    bboxes[..., 0] = bboxes[..., 0] + w/2\n    bboxes[..., 1] = bboxes[..., 1] + h/2\n    bboxes[..., 2] = w\n    bboxes[..., 3] = h\n    \n    return bboxes\n\ndef yolo2voc(bboxes, image_height=720, image_width=1280):\n    \"\"\"\n    yolo => [xmid, ymid, w, h] (normalized)\n    voc  => [x1, y1, x2, y1]\n    \n    \"\"\" \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\n    \n    bboxes[..., [0, 2]] = bboxes[..., [0, 2]]* image_width\n    bboxes[..., [1, 3]] = bboxes[..., [1, 3]]* image_height\n    \n    bboxes[..., [0, 1]] = bboxes[..., [0, 1]] - bboxes[..., [2, 3]]/2\n    bboxes[..., [2, 3]] = bboxes[..., [0, 1]] + bboxes[..., [2, 3]]\n    \n    return bboxes\n\ndef coco2yolo(bboxes, image_height=720, image_width=1280):\n    \"\"\"\n    coco => [xmin, ymin, w, h]\n    yolo => [xmid, ymid, w, h] (normalized)\n    \"\"\"\n    \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\n    \n    # normolizinig\n    bboxes[..., [0, 2]]= bboxes[..., [0, 2]]/ image_width\n    bboxes[..., [1, 3]]= bboxes[..., [1, 3]]/ image_height\n    \n    # converstion (xmin, ymin) => (xmid, ymid)\n    bboxes[..., [0, 1]] = bboxes[..., [0, 1]] + bboxes[..., [2, 3]]/2\n    \n    return bboxes\n\ndef yolo2coco(bboxes, image_height=720, image_width=1280):\n    \"\"\"\n    yolo => [xmid, ymid, w, h] (normalized)\n    coco => [xmin, ymin, w, h]\n    \n    \"\"\" \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\n    \n    # denormalizing\n    bboxes[..., [0, 2]]= bboxes[..., [0, 2]]* image_width\n    bboxes[..., [1, 3]]= bboxes[..., [1, 3]]* image_height\n    \n    # converstion (xmid, ymid) => (xmin, ymin) \n    bboxes[..., [0, 1]] = bboxes[..., [0, 1]] - bboxes[..., [2, 3]]/2\n    \n    return bboxes\n\ndef voc2coco(bboxes, image_height=720, image_width=1280):\n    bboxes  = voc2yolo(bboxes, image_height, image_width)\n    bboxes  = yolo2coco(bboxes, image_height, image_width)\n    return bboxes\n\n\ndef load_image(image_path):\n    return cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\n\n\ndef plot_one_box(x, img, color=None, label=None, line_thickness=None):\n    # Plots one bounding box on image img\n    tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness\n    color = color or [random.randint(0, 255) for _ in range(3)]\n    c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))\n    cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)\n    if label:\n        tf = max(tl - 1, 1)  # font thickness\n        t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]\n        c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3\n        cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled\n        cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)\n\ndef draw_bboxes(img, bboxes, classes, class_ids, colors = None, show_classes = None, bbox_format = 'yolo', class_name = False, line_thickness = 2):  \n     \n    image = img.copy()\n    show_classes = classes if show_classes is None else show_classes\n    colors = (0, 255 ,0) if colors is None else colors\n    \n    if bbox_format == 'yolo':\n        \n        for idx in range(len(bboxes)):  \n            \n            bbox  = bboxes[idx]\n            cls   = classes[idx]\n            cls_id = class_ids[idx]\n            color = colors[cls_id] if type(colors) is list else colors\n            \n            if cls in show_classes:\n            \n                x1 = round(float(bbox[0])*image.shape[1])\n                y1 = round(float(bbox[1])*image.shape[0])\n                w  = round(float(bbox[2])*image.shape[1]/2) #w/2 \n                h  = round(float(bbox[3])*image.shape[0]/2)\n\n                voc_bbox = (x1-w, y1-h, x1+w, y1+h)\n                plot_one_box(voc_bbox, \n                             image,\n                             color = color,\n                             label = cls if class_name else str(get_label(cls)),\n                             line_thickness = line_thickness)\n            \n    elif bbox_format == 'coco':\n        \n        for idx in range(len(bboxes)):  \n            \n            bbox  = bboxes[idx]\n            cls   = classes[idx]\n            cls_id = class_ids[idx]\n            color = colors[cls_id] if type(colors) is list else colors\n            \n            if cls in show_classes:            \n                x1 = int(round(bbox[0]))\n                y1 = int(round(bbox[1]))\n                w  = int(round(bbox[2]))\n                h  = int(round(bbox[3]))\n\n                voc_bbox = (x1, y1, x1+w, y1+h)\n                plot_one_box(voc_bbox, \n                             image,\n                             color = color,\n                             label = cls if class_name else str(cls_id),\n                             line_thickness = line_thickness)\n\n    elif bbox_format == 'voc_pascal':\n        \n        for idx in range(len(bboxes)):  \n            \n            bbox  = bboxes[idx]\n            cls   = classes[idx]\n            cls_id = class_ids[idx]\n            color = colors[cls_id] if type(colors) is list else colors\n            \n            if cls in show_classes: \n                x1 = int(round(bbox[0]))\n                y1 = int(round(bbox[1]))\n                x2 = int(round(bbox[2]))\n                y2 = int(round(bbox[3]))\n                voc_bbox = (x1, y1, x2, y2)\n                plot_one_box(voc_bbox, \n                             image,\n                             color = color,\n                             label = cls if class_name else str(cls_id),\n                             line_thickness = line_thickness)\n    else:\n        raise ValueError('wrong bbox format')\n\n    return image\n\ndef get_bbox(annots):\n    bboxes = [list(annot.values()) for annot in annots]\n    return bboxes\n\ndef get_imgsize(row):\n    row['width'], row['height'] = imagesize.get(row['image_path'])\n    return row\n\nnp.random.seed(32)\ncolors = [(np.random.randint(255), np.random.randint(255), np.random.randint(255))\\\n          for idx in range(1)]","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T14:22:27.124992Z","iopub.execute_input":"2022-02-14T14:22:27.125631Z","iopub.status.idle":"2022-02-14T14:22:27.163676Z","shell.execute_reply.started":"2022-02-14T14:22:27.125593Z","shell.execute_reply":"2022-02-14T14:22:27.162844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##############################################################\n#                      Tracking helpers                      #\n##############################################################\n\nimport numpy as np\nfrom norfair import Detection, Tracker\n\n# Helper to convert bbox in format [x_min, y_min, x_max, y_max, score] to norfair.Detection class\ndef to_norfair(detects, frame_id):\n    result = []\n    for x_min, y_min, x_max, y_max, score in detects:\n        xc, yc = (x_min + x_max) / 2, (y_min + y_max) / 2\n        w, h = x_max - x_min, y_max - y_min\n        result.append(Detection(points=np.array([xc, yc]), scores=np.array([score]), data=np.array([w, h, frame_id])))\n        \n    return result\n\n# Euclidean distance function to match detections on this frame with tracked_objects from previous frames\ndef euclidean_distance(detection, tracked_object):\n    return np.linalg.norm(detection.points - tracked_object.estimate)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:27.166768Z","iopub.execute_input":"2022-02-14T14:22:27.167014Z","iopub.status.idle":"2022-02-14T14:22:27.222976Z","shell.execute_reply.started":"2022-02-14T14:22:27.166986Z","shell.execute_reply":"2022-02-14T14:22:27.222220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# install yolov5 #\n!mkdir -p /root/.config/Ultralytics\n!cp /kaggle/input/yolov5-font/Arial.ttf /root/.config/Ultralytics/","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T14:22:27.224208Z","iopub.execute_input":"2022-02-14T14:22:27.224852Z","iopub.status.idle":"2022-02-14T14:22:28.564908Z","shell.execute_reply.started":"2022-02-14T14:22:27.224814Z","shell.execute_reply":"2022-02-14T14:22:28.563967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model(ckpt_path, conf=0.25, iou=0.50):\n    model = torch.hub.load('/kaggle/input/yolov5-lib-ds',\n                           'custom',\n                           path=ckpt_path,\n                           source='local',\n                           force_reload=True)  # local repo\n    model.conf = conf  # NMS confidence threshold\n    model.iou  = iou  # NMS IoU threshold\n    model.classes = None   # (optional list) filter by class, i.e. = [0, 15, 16] for persons, cats and dogs\n    model.multi_label = False  # NMS multiple labels per box\n    model.max_det = 1000  # maximum number of detections per image\n    return model","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T14:22:28.569044Z","iopub.execute_input":"2022-02-14T14:22:28.569584Z","iopub.status.idle":"2022-02-14T14:22:28.575593Z","shell.execute_reply.started":"2022-02-14T14:22:28.569551Z","shell.execute_reply":"2022-02-14T14:22:28.574463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔭 Inference","metadata":{}},{"cell_type":"markdown","source":"## Helper","metadata":{}},{"cell_type":"code","source":"def predict_yolov5(model, img, size=768, augment=False):\n    height, width = img.shape[:2]\n    results = model(img, size=size, augment=augment)  # custom inference size\n    preds   = results.pandas().xyxy[0]\n    bboxes  = preds[['xmin','ymin','xmax','ymax']].values\n    if len(bboxes):\n        bboxes  = voc2coco(bboxes,height,width).astype(int)\n        confs   = preds.confidence.values\n        return bboxes, confs\n    else:\n        return [],[]\n    \ndef format_prediction(bboxes, confs):\n    annot = ''\n    if len(bboxes)>0:\n        for idx in range(len(bboxes)):\n            xmin, ymin, w, h = bboxes[idx]\n            conf             = confs[idx]\n            annot += f'{conf} {xmin} {ymin} {w} {h}'\n            annot +=' '\n        annot = annot.strip(' ')\n    return annot\n\ndef show_img(img, bboxes, bbox_format='yolo'):\n    names  = ['starfish']*len(bboxes)\n    labels = [0]*len(bboxes)\n    img    = draw_bboxes(img = img,\n                           bboxes = bboxes, \n                           classes = names,\n                           class_ids = labels,\n                           class_name = True, \n                           colors = colors, \n                           bbox_format = bbox_format,\n                           line_thickness = 2)\n    return Image.fromarray(img).resize((800, 400))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T14:22:28.577227Z","iopub.execute_input":"2022-02-14T14:22:28.577759Z","iopub.status.idle":"2022-02-14T14:22:28.590069Z","shell.execute_reply.started":"2022-02-14T14:22:28.577721Z","shell.execute_reply":"2022-02-14T14:22:28.589311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tracking_function(tracker, frame_id, bboxes, scores):\n    \n    detects = []\n    predictions = []\n    \n    if len(scores)>0:\n        for i in range(len(bboxes)):\n            box = bboxes[i]\n            score = scores[i]\n            x_min = int(box[0])\n            y_min = int(box[1])\n            bbox_width = int(box[2])\n            bbox_height = int(box[3])\n            detects.append([x_min, y_min, x_min+bbox_width, y_min+bbox_height, score])\n            predictions.append('{:.2f} {} {} {} {}'.format(score, x_min, y_min, bbox_width, bbox_height))\n#             print(predictions[:-1])\n    # Update tracks using detects from current frame\n    tracked_objects = tracker.update(detections=to_norfair(detects, frame_id))\n    for tobj in tracked_objects:\n        bbox_width, bbox_height, last_detected_frame_id = tobj.last_detection.data\n        if last_detected_frame_id == frame_id:  # Skip objects that were detected on current frame\n            continue\n        # Add objects that have no detections on current frame to predictions\n        xc, yc = tobj.estimate[0]\n        x_min, y_min = int(round(xc - bbox_width / 2)), int(round(yc - bbox_height / 2))\n        score = tobj.last_detection.scores[0]\n\n        predictions.append('{:.2f} {} {} {} {}'.format(score, x_min, y_min, bbox_width, bbox_height))\n        \n    return predictions","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:28.591388Z","iopub.execute_input":"2022-02-14T14:22:28.591803Z","iopub.status.idle":"2022-02-14T14:22:28.603730Z","shell.execute_reply.started":"2022-02-14T14:22:28.591766Z","shell.execute_reply":"2022-02-14T14:22:28.602977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run Inference on **Train**","metadata":{}},{"cell_type":"code","source":"import sys \nsys.path.append('/kaggle/input/wbf-ensemble/Weighted-Boxes-Fusion-master')\nfrom ensemble_boxes import *","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:28.605530Z","iopub.execute_input":"2022-02-14T14:22:28.606225Z","iopub.status.idle":"2022-02-14T14:22:29.252633Z","shell.execute_reply.started":"2022-02-14T14:22:28.606188Z","shell.execute_reply":"2022-02-14T14:22:29.251895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_and_clip(bboxes, test_size = (int(1280*1.3*2),int(720*1.3*2))):\n    bboxes = bboxes.astype(float)\n    bboxes[...,[0,2]] = bboxes[...,[0,2]]/test_size[0]\n    bboxes[...,[1,3]] = bboxes[...,[1,3]]/test_size[1]\n    bboxes = np.clip(bboxes,0,1)\n    return bboxes\n\ndef denormalize_and_transform_to_tracking_form(bboxes,test_size=(1280*1.3*2,720*1.3*2)):\n    \n    bboxes[...,[0,2]] = bboxes[...,[0,2]]*test_size[0]\n    bboxes[...,[1,3]] = bboxes[...,[1,3]]*test_size[1]\n    \n    bboxes[...,[2,3]] = bboxes[...,[2,3]] - bboxes[...,[0,1]] \n    \n    return bboxes","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:29.254714Z","iopub.execute_input":"2022-02-14T14:22:29.254905Z","iopub.status.idle":"2022-02-14T14:22:29.262627Z","shell.execute_reply.started":"2022-02-14T14:22:29.254882Z","shell.execute_reply":"2022-02-14T14:22:29.261986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_all_stage(tracker, img):\n    \n    bboxes_yolov5_fold0, scores_yolov5_fold0 = predict_yolov5(model_yolov5_fold0, img, size=test_size_yolov5, augment=AUGMENT)\n    if len(bboxes_yolov5_fold0)>0:\n        \n        bboxes_yolov5_fold0[...,[2,3]] = bboxes_yolov5_fold0[...,[0,1]] + bboxes_yolov5_fold0[...,[2,3]]\n#         print('bboxes_yolov5_fold0 before normalize', bboxes_yolov5_fold0)\n        bboxes_yolov5_fold0 = normalize_and_clip(bboxes_yolov5_fold0)\n#         print('bboxes_yolov5_fold0 after normalize', bboxes_yolov5_fold0)\n    bboxes_yolov5_fold1, scores_yolov5_fold1 = predict_yolov5(model_yolov5_fold1, img, size=test_size_yolov5, augment=AUGMENT)\n    if len(bboxes_yolov5_fold1)>0:\n#         print('bboxes_yolov5_fold1')\n        bboxes_yolov5_fold1[...,[2,3]] = bboxes_yolov5_fold1[...,[0,1]] + bboxes_yolov5_fold1[...,[2,3]]\n        bboxes_yolov5_fold1 = normalize_and_clip(bboxes_yolov5_fold1)\n    \n    bboxes_yolov5_fold2, scores_yolov5_fold2 = predict_yolov5(model_yolov5_fold2, img, size=test_size_yolov5, augment=AUGMENT)\n    if len(bboxes_yolov5_fold2)>0:\n#         print('bboxes_yolov5_fold2')\n        bboxes_yolov5_fold2[...,[2,3]] = bboxes_yolov5_fold2[...,[0,1]] + bboxes_yolov5_fold2[...,[2,3]]\n        bboxes_yolov5_fold2 = normalize_and_clip(bboxes_yolov5_fold2)\n    \n    bboxes = [bboxes_yolov5_fold0, bboxes_yolov5_fold1, bboxes_yolov5_fold2]\n    scores = [scores_yolov5_fold0, scores_yolov5_fold1, scores_yolov5_fold2]\n#     print('last bboxes_yolov5_fold0',bboxes_yolov5_fold0)\n    \n    label = [[0]*len(bboxes_yolov5_fold0), [0]*len(bboxes_yolov5_fold1), [0]*len(bboxes_yolov5_fold2)]\n    # xmin, ymin, xmax, ymax  -> normlize\n    bboxes, scores, labels = weighted_boxes_fusion(bboxes, scores, label, weights=weighted_ensemble, iou_thr=iou_ensemble, skip_box_thr=skip_box_thr)\n    \n    # bboxes -> xmin , ymin, width, height #\n    bboxes = denormalize_and_transform_to_tracking_form(bboxes)\n    \n    predict_box = tracking_function(tracker, frame_id, bboxes, scores)\n    return predict_box","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:29.264657Z","iopub.execute_input":"2022-02-14T14:22:29.264923Z","iopub.status.idle":"2022-02-14T14:22:29.279398Z","shell.execute_reply.started":"2022-02-14T14:22:29.264888Z","shell.execute_reply":"2022-02-14T14:22:29.278708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /root/.config/Ultralytics\n!cp /kaggle/input/yolov5-font/Arial.ttf /root/.config/Ultralytics/","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:29.280896Z","iopub.execute_input":"2022-02-14T14:22:29.281437Z","iopub.status.idle":"2022-02-14T14:22:30.620663Z","shell.execute_reply.started":"2022-02-14T14:22:29.281400Z","shell.execute_reply":"2022-02-14T14:22:30.619566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/woking","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:30.623405Z","iopub.execute_input":"2022-02-14T14:22:30.624052Z","iopub.status.idle":"2022-02-14T14:22:30.631107Z","shell.execute_reply.started":"2022-02-14T14:22:30.624007Z","shell.execute_reply":"2022-02-14T14:22:30.630144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracker = Tracker(\n    distance_function=euclidean_distance, \n    distance_threshold=30,\n    hit_inertia_min=3,\n    hit_inertia_max=6,\n    initialization_delay=1,\n)\n\nmodel_yolov5_fold0 = load_model(list_yolov5_checkpoint[0], conf=list_yolov5_conf[0], iou=yolov5_iou)\nmodel_yolov5_fold1 = load_model(list_yolov5_checkpoint[1], conf=list_yolov5_conf[1], iou=yolov5_iou)\nmodel_yolov5_fold2 = load_model(list_yolov5_checkpoint[2], conf=list_yolov5_conf[2], iou=yolov5_iou)\n\nimage_paths = df[df.num_bbox>1].sample(100).image_path.tolist()\nframe_id = 0\nfor idx, path in enumerate(image_paths):\n    img = cv2.imread(path)[...,::-1]\n    if FDA_aug:\n        img = FDA_trans(image=img)['image']\n        \n    predict_box = predict_all_stage(tracker, img)\n\n    if len(predict_box)>0:\n        box = [list(map(int,box.split(' ')[1:])) for box in predict_box]\n    else:\n        box = []\n    display(show_img(img, box, bbox_format='coco'))\n    if idx>10:\n        break\n    frame_id += 1","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T14:22:30.632462Z","iopub.execute_input":"2022-02-14T14:22:30.634138Z","iopub.status.idle":"2022-02-14T14:22:51.792648Z","shell.execute_reply.started":"2022-02-14T14:22:30.634095Z","shell.execute_reply":"2022-02-14T14:22:51.791878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Init `Env`","metadata":{}},{"cell_type":"code","source":"import greatbarrierreef\nenv = greatbarrierreef.make_env()# initialize the environment\niter_test = env.iter_test()      # an iterator which loops over the test set and sample submission","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T14:22:51.794118Z","iopub.execute_input":"2022-02-14T14:22:51.794791Z","iopub.status.idle":"2022-02-14T14:22:51.825803Z","shell.execute_reply.started":"2022-02-14T14:22:51.794749Z","shell.execute_reply":"2022-02-14T14:22:51.824820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run Inference on **Test**","metadata":{}},{"cell_type":"code","source":"cd /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:22:51.827253Z","iopub.execute_input":"2022-02-14T14:22:51.827526Z","iopub.status.idle":"2022-02-14T14:22:51.834836Z","shell.execute_reply.started":"2022-02-14T14:22:51.827490Z","shell.execute_reply":"2022-02-14T14:22:51.833849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracker = Tracker(\n    distance_function=euclidean_distance, \n    distance_threshold=30,\n    hit_inertia_min=3,\n    hit_inertia_max=6,\n    initialization_delay=1,\n)\n\n# model = load_model(CKPT_PATH, conf=CONF, iou=IOU)\n# model_yolov5_fold0 = load_model(list_yolov5_checkpoint[0], conf=list_yolov5_conf[0], iou=yolov5_iou)\n# model_yolov5_fold1 = load_model(list_yolov5_checkpoint[1], conf=list_yolov5_conf[1], iou=yolov5_iou)\n# model_yolov5_fold2 = load_model(list_yolov5_checkpoint[2], conf=list_yolov5_conf[2], iou=yolov5_iou)\n\nframe_id =0\nfor idx, (img, pred_df) in enumerate(tqdm(iter_test)):\n    if FDA_aug:\n        img = FDA_trans(image=img)['image']\n    predictions = predict_all_stage(tracker, img)\n    prediction_str = ' '.join(predictions)\n    pred_df['annotations'] = prediction_str\n    env.predict(pred_df)\n    if frame_id < 3:\n        if len(predict_box)>0:\n            box = [list(map(int,box.split(' ')[1:])) for box in predictions]\n        else:\n            box = []\n        display(show_img(img, box, bbox_format='coco'))\n#     print('Prediction:', pred_df)\n    frame_id += 1","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T14:22:51.836723Z","iopub.execute_input":"2022-02-14T14:22:51.837024Z","iopub.status.idle":"2022-02-14T14:22:54.257112Z","shell.execute_reply.started":"2022-02-14T14:22:51.836987Z","shell.execute_reply":"2022-02-14T14:22:54.256424Z"},"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-14T14:22:54.258512Z","iopub.execute_input":"2022-02-14T14:22:54.258977Z","iopub.status.idle":"2022-02-14T14:22:54.274484Z","shell.execute_reply.started":"2022-02-14T14:22:54.258926Z","shell.execute_reply":"2022-02-14T14:22:54.273609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}