{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch\nfrom tqdm import tqdm\nimport sys\n\nsys.path.append('/kaggle/input/tensorflow-great-barrier-reef')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-14T04:34:33.950401Z","iopub.execute_input":"2022-02-14T04:34:33.950737Z","iopub.status.idle":"2022-02-14T04:34:35.314213Z","shell.execute_reply.started":"2022-02-14T04:34:33.950663Z","shell.execute_reply":"2022-02-14T04:34:35.313501Z"},"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-14T04:34:35.315692Z","iopub.execute_input":"2022-02-14T04:34:35.315934Z","iopub.status.idle":"2022-02-14T04:34:36.662288Z","shell.execute_reply.started":"2022-02-14T04:34:35.315902Z","shell.execute_reply":"2022-02-14T04:34:36.661226Z"},"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\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-14T04:34:36.664450Z","iopub.execute_input":"2022-02-14T04:34:36.665005Z","iopub.status.idle":"2022-02-14T04:35:52.505956Z","shell.execute_reply.started":"2022-02-14T04:34:36.664966Z","shell.execute_reply":"2022-02-14T04:35:52.505134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2022-02-14T04:35:52.509922Z","iopub.execute_input":"2022-02-14T04:35:52.510139Z","iopub.status.idle":"2022-02-14T04:35:52.516702Z","shell.execute_reply.started":"2022-02-14T04:35:52.510112Z","shell.execute_reply":"2022-02-14T04:35:52.515988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2022-02-14T04:35:52.517961Z","iopub.execute_input":"2022-02-14T04:35:52.518201Z","iopub.status.idle":"2022-02-14T04:35:52.555665Z","shell.execute_reply.started":"2022-02-14T04:35:52.518168Z","shell.execute_reply":"2022-02-14T04:35:52.555027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torch.hub.load('/kaggle/input/yolov5-lib-ds', \n                       'custom', \n                       path='/kaggle/input/yolov5s6/best(2).pt',\n                       source='local',\n                       force_reload=True)  # local repo\nmodel.conf = 0.01\n# model.iou = 0.6","metadata":{"execution":{"iopub.status.busy":"2022-02-14T04:35:52.556735Z","iopub.execute_input":"2022-02-14T04:35:52.556988Z","iopub.status.idle":"2022-02-14T04:35:58.881106Z","shell.execute_reply.started":"2022-02-14T04:35:52.556954Z","shell.execute_reply":"2022-02-14T04:35:58.880343Z"},"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        ","metadata":{"execution":{"iopub.status.busy":"2022-02-14T04:35:58.883029Z","iopub.execute_input":"2022-02-14T04:35:58.883322Z","iopub.status.idle":"2022-02-14T04:35:58.952142Z","shell.execute_reply.started":"2022-02-14T04:35:58.883283Z","shell.execute_reply":"2022-02-14T04:35:58.951401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_dict = {\n    'id': [],\n    'prediction_string': [],\n}\n\n\n#######################################################\n#                      Tracking                       #\n#######################################################\n\n# Tracker will update tracks based on detections from current frame\n# Matching based on euclidean distance between bbox centers of detections \n# from current frame and tracked_objects based on previous frames\n# You can check it's parameters in norfair docs\n# https://github.com/tryolabs/norfair/blob/master/docs/README.md\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)\n\n# Save frame_id into detection to know which tracks have no detections on current frame\nframe_id = 0\n#######################################################\n\n\nfor (img, pred_df) in tqdm(iter_test):\n#     print(img)\n#     print('histart')\n    r = model(img, size=6000, augment=True)\n    \n#     bboxes, bbclasses, scores = yolox_inference(image_np[:,:,::-1], model, test_size)\n    \n    predictions = []\n    detects = []\n    if r.pandas().xyxy[0].shape[0] > 0:\n        for idx, row in r.pandas().xyxy[0].iterrows():\n            if row.confidence > 0.15:\n                detects.append([int(row.xmin), int(row.ymin), int(row.xmax), int(row.ymax), row.confidence])\n                predictions.append('{:.2f} {} {} {} {}'.format(row.confidence, int(row.xmin), int(row.ymin), int(row.xmax-row.xmin), int(row.ymax-row.ymin)))\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#         x_min = int(box[0])\n#         y_min = int(box[1])\n#         x_max = int(box[2])\n#         y_max = int(box[3])\n#         detects.append([x_min, y_min, x_max, y_max, score])\n        \n#         bbox_width = x_max - x_min\n#         bbox_height = y_max - y_min\n        \n#         predictions.append('{:.2f} {} {} {} {}'.format(score, x_min, y_min, bbox_width, bbox_height))\n    \n    #######################################################\n    #                      Tracking                       #\n    #######################################################\n    \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            \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    \n    prediction_str = ' '.join(predictions)\n    pred_df['annotations'] = prediction_str\n    env.predict(pred_df)\n    \n#     print('hiend')\n\n#     print('Prediction:', prediction_str)\n    frame_id += 1","metadata":{"execution":{"iopub.status.busy":"2022-02-14T04:35:58.953326Z","iopub.execute_input":"2022-02-14T04:35:58.954059Z","iopub.status.idle":"2022-02-14T04:36:05.472721Z","shell.execute_reply.started":"2022-02-14T04:35:58.954020Z","shell.execute_reply":"2022-02-14T04:36:05.471870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('submission.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2022-02-14T04:36:05.474253Z","iopub.execute_input":"2022-02-14T04:36:05.474518Z","iopub.status.idle":"2022-02-14T04:36:05.492214Z","shell.execute_reply.started":"2022-02-14T04:36:05.474481Z","shell.execute_reply":"2022-02-14T04:36:05.491523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}