{"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":"<div class=\"alert alert-success\" role=\"alert\">\nThis work consists of two parts:     \n    <ul>\n        <li> PART 1 - UTILIZE YOLOv5 MODEL!!</li>\n        <li> PART 2 - LARGER RESOLUTION INFERENCE!!</a></li>\n    </ul>\n    \n</div>\n\n<div class=\"alert alert-warning\">\n<strong>Feel free to use it and enjoy!\n    I really appreciate if you upvote this notebook. Thank you! </strong>\n</div>\n","metadata":{}},{"cell_type":"markdown","source":"# [Tensorflow - Help Protect the Great Barrier Reef](https://www.kaggle.com/c/tensorflow-great-barrier-reef)\n> Detect crown-of-thorns starfish in underwater image data\n\n<img src=\"https://storage.googleapis.com/kaggle-competitions/kaggle/31703/logos/header.png?t=2021-10-29-00-30-04\">","metadata":{}},{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-02T05:42:35.342265Z","iopub.execute_input":"2022-02-02T05:42:35.342780Z","iopub.status.idle":"2022-02-02T05:42:37.187867Z","shell.execute_reply.started":"2022-02-02T05:42:35.342696Z","shell.execute_reply":"2022-02-02T05:42:37.186858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📌 Key-Points\n* One have to submit prediction using the provided **python time-series API**, which makes this competition different from previous Object Detection Competitions.\n* Each prediction row needs to include all bounding boxes for the image. Submission is format seems also **COCO** which means `[x_min, y_min, width, height]`\n* Copmetition metric `F2` tolerates some false positives(FP) in order to ensure very few starfish are missed. Which means tackling **false negatives(FN)** is more important than false positives(FP). \n$$F2 = 5 \\cdot \\frac{precision \\cdot recall}{4\\cdot precision + recall}$$","metadata":{}},{"cell_type":"markdown","source":"## Please Upvote if you find this Helpful","metadata":{}},{"cell_type":"markdown","source":"# 📖 Meta Data\n* `train_images/` - Folder containing training set photos of the form `video_{video_id}/{video_frame}.jpg`.\n\n* `[train/test].csv` - Metadata for the images. As with other test files, most of the test metadata data is only available to your notebook upon submission. Just the first few rows available for download.\n\n* `video_id` - ID number of the video the image was part of. The video ids are not meaningfully ordered.\n* `video_frame` - The frame number of the image within the video. Expect to see occasional gaps in the frame number from when the diver surfaced.\n* `sequence` - ID of a gap-free subset of a given video. The sequence ids are not meaningfully ordered.\n* `sequence_frame` - The frame number within a given sequence.\n* `image_id` - ID code for the image, in the format `{video_id}-{video_frame}`\n* `annotations` - The bounding boxes of any starfish detections in a string format that can be evaluated directly with Python. Does not use the same format as the predictions you will submit. Not available in test.csv. A bounding box is described by the pixel coordinate `(x_min, y_min)` of its lower left corner within the image together with its `width` and `height` in pixels --> (COCO format).","metadata":{}},{"cell_type":"code","source":"ROOT_DIR  = '/kaggle/input/tensorflow-great-barrier-reef/'\nweights_path = '../input/yolov5-1920-4/'   # ../input/yolov5-1920-4/best.pt\nCKPT_PATH = weights_path + '/best.pt'\nIMG_SIZE  = 10000#int(2000*3)  # \nCONF      = 0.275     # 1920*3 + conf-0.3\nIOU       = 0.2\nAUGMENT   = True     # TTA will run for an hour, will gain improvement\nTRACKING  = False\nFDA_aug   = False","metadata":{"execution":{"iopub.status.busy":"2022-02-02T05:42:37.189794Z","iopub.execute_input":"2022-02-02T05:42:37.190383Z","iopub.status.idle":"2022-02-02T05:42:37.196559Z","shell.execute_reply.started":"2022-02-02T05:42:37.190344Z","shell.execute_reply":"2022-02-02T05:42:37.195410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Tracker","metadata":{}},{"cell_type":"code","source":"# if TRACKING:\n# 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%cd /kaggle/input/norfair031py3/\n!pip install norfair-0.3.1-py3-none-any.whl -f ./ --no-index\n%cd /kaggle/working/\n\nfrom norfair import Detection, Tracker\n\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\ntracker = 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)\nframe_id = 0\n","metadata":{"execution":{"iopub.status.busy":"2022-02-02T05:42:37.200580Z","iopub.execute_input":"2022-02-02T05:42:37.201389Z","iopub.status.idle":"2022-02-02T05:43:52.692679Z","shell.execute_reply.started":"2022-02-02T05:42:37.201335Z","shell.execute_reply":"2022-02-02T05:43:52.691798Z"},"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-02T05:43:52.694048Z","iopub.execute_input":"2022-02-02T05:43:52.694295Z","iopub.status.idle":"2022-02-02T05:43:52.698937Z","shell.execute_reply.started":"2022-02-02T05:43:52.694258Z","shell.execute_reply":"2022-02-02T05:43:52.698250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Train Data\n# df = pd.read_csv(f'{ROOT_DIR}/train.csv')\n# df = df.progress_apply(get_path, axis=1)\n# df['annotations'] = df['annotations'].progress_apply(lambda x: ast.literal_eval(x))\n# display(df.head(2))","metadata":{"execution":{"iopub.status.busy":"2022-02-02T05:44:42.324373Z","iopub.execute_input":"2022-02-02T05:44:42.324643Z","iopub.status.idle":"2022-02-02T05:44:42.328363Z","shell.execute_reply.started":"2022-02-02T05:44:42.324613Z","shell.execute_reply":"2022-02-02T05:44:42.327489Z"},"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))\n# data = (df.num_bbox>0).value_counts()/len(df)*100\n# print(f\"No BBox: {data[0]:0.2f}% | With BBox: {data[1]:0.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2022-02-02T05:44:43.758750Z","iopub.execute_input":"2022-02-02T05:44:43.759338Z","iopub.status.idle":"2022-02-02T05:44:43.763101Z","shell.execute_reply.started":"2022-02-02T05:44:43.759301Z","shell.execute_reply":"2022-02-02T05:44:43.762255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔨 Helper","metadata":{}},{"cell_type":"code","source":"# check https://github.com/awsaf49/bbox for source code of following utility functions\n# from bbox.utils import coco2yolo, coco2voc, voc2yolo\n# from bbox.utils import draw_bboxes, load_image\n# from bbox.utils import clip_bbox, str2annot, annot2str\n\n# def get_bbox(annots):\n#     bboxes = [list(annot.values()) for annot in annots]\n#     return bboxes\n\n# def get_imgsize(row):\n#     row['width'], row['height'] = imagesize.get(row['image_path'])\n#     return row\n\n# np.random.seed(32)\n# colors = [(np.random.randint(255), np.random.randint(255), np.random.randint(255))\\\n#           for idx in range(1)]","metadata":{"execution":{"iopub.status.busy":"2022-02-02T05:44:44.847633Z","iopub.execute_input":"2022-02-02T05:44:44.848354Z","iopub.status.idle":"2022-02-02T05:44:44.852710Z","shell.execute_reply.started":"2022-02-02T05:44:44.848312Z","shell.execute_reply":"2022-02-02T05:44:44.852025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-02T05:44:45.486926Z","iopub.execute_input":"2022-02-02T05:44:45.487485Z","iopub.status.idle":"2022-02-02T05:44:45.527214Z","shell.execute_reply.started":"2022-02-02T05:44:45.487448Z","shell.execute_reply":"2022-02-02T05:44:45.526239Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-02T05:44:45.901341Z","iopub.execute_input":"2022-02-02T05:44:45.901935Z","iopub.status.idle":"2022-02-02T05:44:47.251482Z","shell.execute_reply.started":"2022-02-02T05:44:45.901901Z","shell.execute_reply":"2022-02-02T05:44:47.250493Z"},"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('../input/d/awsaf49/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-02T05:44:47.253622Z","iopub.execute_input":"2022-02-02T05:44:47.254072Z","iopub.status.idle":"2022-02-02T05:44:47.260491Z","shell.execute_reply.started":"2022-02-02T05:44:47.254034Z","shell.execute_reply":"2022-02-02T05:44:47.259708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔭 Inference","metadata":{}},{"cell_type":"markdown","source":"## Helper","metadata":{}},{"cell_type":"code","source":"def predict(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-02T05:44:48.416075Z","iopub.execute_input":"2022-02-02T05:44:48.416931Z","iopub.status.idle":"2022-02-02T05:44:48.427056Z","shell.execute_reply.started":"2022-02-02T05:44:48.416858Z","shell.execute_reply":"2022-02-02T05:44:48.426233Z"},"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-02T05:44:48.797739Z","iopub.execute_input":"2022-02-02T05:44:48.798337Z","iopub.status.idle":"2022-02-02T05:44:48.807476Z","shell.execute_reply.started":"2022-02-02T05:44:48.798303Z","shell.execute_reply":"2022-02-02T05:44:48.806534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_bbox(anno):\n    for idx, a in enumerate(anno):\n        try:\n            anno[idx] = [a['x'], a['y'], a['width'], a['height']]\n        except:\n            break\n    return anno","metadata":{"execution":{"iopub.status.busy":"2022-02-02T05:44:49.146573Z","iopub.execute_input":"2022-02-02T05:44:49.147148Z","iopub.status.idle":"2022-02-02T05:44:49.157919Z","shell.execute_reply.started":"2022-02-02T05:44:49.147099Z","shell.execute_reply":"2022-02-02T05:44:49.156508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run Inference on **Train**","metadata":{}},{"cell_type":"code","source":"!cp ../input/yolov5-font/Arial.ttf /root/.config/Ultralytics/","metadata":{"execution":{"iopub.status.busy":"2022-02-02T05:44:49.886345Z","iopub.execute_input":"2022-02-02T05:44:49.886651Z","iopub.status.idle":"2022-02-02T05:44:50.564021Z","shell.execute_reply.started":"2022-02-02T05:44:49.886618Z","shell.execute_reply":"2022-02-02T05:44:50.563061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(\"Use TRACKING...\")    \n# 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# image_paths = df[df.num_bbox>1].sample(100)\n# frame_id = 0\n# for idx, path in enumerate(image_paths.image_path.tolist()):\n#     img = cv2.imread(path)[...,::-1]\n#     if FDA_aug:\n#         img = FDA_trans(image=img)['image']\n#     bboxes, confis = predict(model, img, size=IMG_SIZE, augment=AUGMENT)\n#     predict_box = tracking_function(tracker, frame_id, bboxes, confis)\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#     display(show_img(img, bboxes, bbox_format='coco'))  # Predict\n#     display(show_img(img, extract_bbox(image_paths.iloc[idx].annotations), bbox_format='coco'))\n#     if idx>3:\n#         break\n#     frame_id += 1\n    \n# # print(\"Not Use TRACKING...\")    \n# # model = load_model(CKPT_PATH, conf=CONF, iou=IOU)\n\n# # # d = df[df.num_bbox>1].sample(100)\n\n# # for idx, path in enumerate(image_paths.image_path.tolist()):\n# #     img = cv2.imread(path)[...,::-1]\n# #     bboxes, confis = predict(model, img, size=IMG_SIZE, augment=AUGMENT)\n# # #     print(bboxes, extract_bbox(d.iloc[idx].annotations), confis)\n\n    \n# #     display(show_img(img, extract_bbox(image_paths.iloc[idx].annotations), bbox_format='coco'))\n\n# #     if idx>3:\n# #         break","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-02T05:44:50.566242Z","iopub.execute_input":"2022-02-02T05:44:50.566558Z","iopub.status.idle":"2022-02-02T05:44:50.572376Z","shell.execute_reply.started":"2022-02-02T05:44:50.566514Z","shell.execute_reply":"2022-02-02T05:44:50.571671Z"},"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-02T05:44:51.015348Z","iopub.execute_input":"2022-02-02T05:44:51.017011Z","iopub.status.idle":"2022-02-02T05:44:51.046559Z","shell.execute_reply.started":"2022-02-02T05:44:51.016958Z","shell.execute_reply":"2022-02-02T05:44:51.045861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run Inference on **Test**","metadata":{}},{"cell_type":"code","source":"!ls /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2022-02-02T05:44:52.078460Z","iopub.execute_input":"2022-02-02T05:44:52.079072Z","iopub.status.idle":"2022-02-02T05:44:52.763055Z","shell.execute_reply.started":"2022-02-02T05:44:52.079035Z","shell.execute_reply":"2022-02-02T05:44:52.762190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if TRACKING:\n    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\n    frame_id =0\n    for idx, (img, pred_df) in enumerate(tqdm(iter_test)):\n        if FDA_aug:\n            img = FDA_trans(image=img)['image']\n        bboxes, confs  = predict(model, img, size=IMG_SIZE, augment=AUGMENT)\n\n        predictions = tracking_function(tracker, frame_id, bboxes, confs)\n\n        prediction_str = ' '.join(predictions)\n        pred_df['annotations'] = prediction_str\n        env.predict(pred_df)\n        if frame_id < 3:\n            if len(predictions)>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\n\nelse:\n    model = load_model(CKPT_PATH, conf=CONF, iou=IOU)\n    for idx, (img, pred_df) in enumerate(tqdm(iter_test)):\n        bboxes, confs  = predict(model, img, size=IMG_SIZE, augment=AUGMENT)\n        annot          = format_prediction(bboxes, confs)\n        pred_df['annotations'] = annot\n        env.predict(pred_df)\n        if idx<3:\n            display(show_img(img, bboxes, bbox_format='coco'))\n    ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-02T05:44:52.876979Z","iopub.execute_input":"2022-02-02T05:44:52.877870Z","iopub.status.idle":"2022-02-02T05:45:17.214278Z","shell.execute_reply.started":"2022-02-02T05:44:52.877806Z","shell.execute_reply":"2022-02-02T05:45:17.213606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 👀 Check Submission","metadata":{}},{"cell_type":"code","source":"sub_df = pd.read_csv('submission.csv')\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-02T05:45:17.216333Z","iopub.execute_input":"2022-02-02T05:45:17.216845Z","iopub.status.idle":"2022-02-02T05:45:17.231645Z","shell.execute_reply.started":"2022-02-02T05:45:17.216786Z","shell.execute_reply":"2022-02-02T05:45:17.230867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Please Upvote if you find this Helpful","metadata":{}},{"cell_type":"markdown","source":"<img src=\"https://www.pngall.com/wp-content/uploads/2018/04/Under-Construction-PNG-File.png\">","metadata":{}}]}