{"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 time\nstart = time.time()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-13T07:05:35.974509Z","iopub.execute_input":"2022-02-13T07:05:35.974934Z","iopub.status.idle":"2022-02-13T07:05:36.001126Z","shell.execute_reply.started":"2022-02-13T07:05:35.974833Z","shell.execute_reply":"2022-02-13T07:05:36.000419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport torch\nfrom IPython.core.interactiveshell import InteractiveShell\n\nInteractiveShell.ast_node_interactivity = \"all\"\nimport ast\nimport sys\nfrom tqdm import tqdm\n","metadata":{"execution":{"iopub.status.busy":"2022-02-13T07:05:36.002985Z","iopub.execute_input":"2022-02-13T07:05:36.003272Z","iopub.status.idle":"2022-02-13T07:05:37.331373Z","shell.execute_reply.started":"2022-02-13T07:05:36.003235Z","shell.execute_reply":"2022-02-13T07:05:37.330675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append('../input/yolov5reef/yolov5')\nfrom custom_utils import plot_bboxes, get_ensemble, ensemble_predict\n!mkdir -p /root/.config/Ultralytics\n!cp -r /kaggle/input/yolov5reef/Arial.ttf /root/.config/Ultralytics/Arial.ttf","metadata":{"execution":{"iopub.status.busy":"2022-02-13T07:05:37.332899Z","iopub.execute_input":"2022-02-13T07:05:37.333143Z","iopub.status.idle":"2022-02-13T07:05:39.265499Z","shell.execute_reply.started":"2022-02-13T07:05:37.333109Z","shell.execute_reply":"2022-02-13T07:05:39.264529Z"},"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-13T07:05:39.269879Z","iopub.execute_input":"2022-02-13T07:05:39.270104Z","iopub.status.idle":"2022-02-13T07:05:39.293487Z","shell.execute_reply.started":"2022-02-13T07:05:39.270075Z","shell.execute_reply":"2022-02-13T07:05:39.292775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ensemble & tracking","metadata":{}},{"cell_type":"code","source":"from IPython.display import clear_output\n!pip install ../input/tracking/filterpy-1.4.5-py3-none-any.whl\n!pip install ../input/tracking/commonmark-0.9.1-py2.py3-none-any.whl\n!pip install ../input/tracking/rich-9.13.0-py3-none-any.whl\n!pip install ../input/tracking/norfair-0.3.1-py3-none-any.whl\nclear_output()\n\nfrom norfair import Detection, Tracker\n","metadata":{"execution":{"iopub.status.busy":"2022-02-13T07:05:39.901308Z","iopub.execute_input":"2022-02-13T07:05:39.901789Z","iopub.status.idle":"2022-02-13T07:07:32.639433Z","shell.execute_reply.started":"2022-02-13T07:05:39.901753Z","shell.execute_reply":"2022-02-13T07:07:32.638622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ensemble_to_norfair(prediction, frame_id):\n    result = []\n    for xc, yc, w, h, score in prediction:\n        result.append(Detection(points=np.array([int(xc), int(yc)]), scores=np.array([score]), data=np.array([int(w), int(h), frame_id])))\n    return result\n\ndef euclidean_distance(detection, tracked_object):\n    return np.linalg.norm(detection.points - tracked_object.estimate)\n\ntest = pd.read_csv('../input/tensorflow-great-barrier-reef/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-13T07:05:39.901308Z","iopub.execute_input":"2022-02-13T07:05:39.901789Z","iopub.status.idle":"2022-02-13T07:07:32.639433Z","shell.execute_reply.started":"2022-02-13T07:05:39.901753Z","shell.execute_reply":"2022-02-13T07:07:32.638622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODELS =[\n    ['../input/yolov5weights3/yolov5l6_val2_best.pt', 2880*2, 0.1, False],\n    ['../input/reefyolov5fulltrainweights/yolov5x6_2400_seq_id_rand_0.pt',2400*1.5, 0.1, False],\n    [ '../input/yolov5seqweights0/yolov5l6_seq_3008_22_f2_best.pt', 3008*1.5, 0.1, False],\n    ['../input/reefyolov5fulltrainweights/epoch6.pt', 3008*1.5, 0.1, False],\n    ['../input/reefyolov5fulltrainweights/yolov5l6_2880_seq_id_6_best.pt', 2880,0.1, False]\n]\n\nmodels, MODEL_PATHS, IMG_SIZES, CONF_THRESHS = get_ensemble(MODELS, 0.4)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T07:07:32.641075Z","iopub.execute_input":"2022-02-13T07:07:32.641335Z","iopub.status.idle":"2022-02-13T07:07:59.202313Z","shell.execute_reply.started":"2022-02-13T07:07:32.641297Z","shell.execute_reply":"2022-02-13T07:07:59.20152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# test","metadata":{}},{"cell_type":"code","source":"start_pred = time.time()\nfinal_thres = 0.37\nwbf_skip_box_thr = 0.1\nwbf_iou_thres = 0.35\nconf_type = \"max\"\nAUGMENT = [x[3] for x in MODELS]\nTRACK_THRES = 0.35\nhit_inertia_min = 3\nhit_inertia_max = 10\ninitialization_delay = 3\ndistance_threshold = 35\n\ntracker = Tracker(\n    distance_function=euclidean_distance, \n    distance_threshold=distance_threshold,\n    hit_inertia_min=hit_inertia_min,\n    hit_inertia_max=hit_inertia_max,\n    initialization_delay=initialization_delay\n)\nseq_init = test.loc[0, 'sequence']\nfor n, (pixel_array, sample_prediction_df) in enumerate(tqdm(iter_test)):\n    one_start = time.time()\n    seq = test.loc[n, 'sequence']\n    if seq!=seq_init:\n        tracker = Tracker(\n            distance_function=euclidean_distance, \n            distance_threshold=distance_threshold,\n            hit_inertia_min=hit_inertia_min,\n            hit_inertia_max=hit_inertia_max,\n            initialization_delay=initialization_delay\n            )\n        seq_init = seq\n    final_bboxes, tracks = ensemble_predict(\n        models = models,\n        img_sizes = IMG_SIZES,\n        img = pixel_array,\n        augment = AUGMENT,\n        skip_box_thr = wbf_skip_box_thr,\n        iou_thres = wbf_iou_thres,\n        final_thres = final_thres,\n        conf_type = conf_type,\n        mod_weights=None,\n        WBF = True\n    )\n    tracked_objects = tracker.update(detections=ensemble_to_norfair(tracks, n))\n    for obj in tracked_objects:\n        bbox_width, bbox_height, last_detected_frame_id = obj.last_detection.data\n        if last_detected_frame_id == n or (n - last_detected_frame_id)==5:\n            continue\n        else:\n            score = obj.last_detection.scores[0]\n            if score>=TRACK_THRES:\n                xc, yc = obj.estimate[0]\n                x_min, y_min = int(round(xc - bbox_width / 2)), int(round(yc - bbox_height / 2))\n                score *= 0.5\n                new_bbox_width = bbox_width if (x_min + bbox_width)<=1280 else (1280-x_min)\n                new_bbox_height = bbox_height if (y_min + bbox_height)<=720 else (720-y_min)\n                if x_min < 0:\n                    new_bbox_width = new_bbox_width + x_min\n                    x_min = 0\n                if y_min < 0:\n                    new_bbox_height = new_bbox_height + y_min\n                    y_min = 0\n                if bbox_width*bbox_height >= 280 and new_bbox_height >=np.max([bbox_height*0.3, 18]) and new_bbox_width>=np.max([bbox_width*0.3,18]):\n                    final_bboxes.append('{:.3f} {} {} {} {}'.format(score, x_min, y_min, new_bbox_width, new_bbox_height))\n    sample_prediction_df[\"annotations\"] = ' '.join(final_bboxes)\n    env.predict(sample_prediction_df)\n    time.time() - one_start\nsubmission = pd.read_csv('/kaggle/working/submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-04T12:33:38.238966Z","iopub.execute_input":"2022-02-04T12:33:38.239771Z","iopub.status.idle":"2022-02-04T12:33:38.247469Z","shell.execute_reply.started":"2022-02-04T12:33:38.239723Z","shell.execute_reply":"2022-02-04T12:33:38.246582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}