{"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 numpy as np\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nimport pandas as pd\nimport os\nimport cv2\nimport sys\nsys.path.append('../input/tensorflow-great-barrier-reef')\nimport torch\n!pip install ../input/ensemble-boxes/ensemble_boxes-1.0.7-py3-none-any.whl  --no-deps\nflips=[False,False,False,False]\nCONF      = 0.2\nIOU       = 0.50\nfrom ensemble_boxes import *\nROOT_DIR  = '/kaggle/input/tensorflow-great-barrier-reef/'\n\nsizes=[4544,6400]\nflips=[False,False,False,False]\nCONF      = 0.2\nIOU       = 0.50\nAUGMENT   = False\nMIN_SZ    = 4","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-14T12:15:57.249681Z","iopub.execute_input":"2022-02-14T12:15:57.249998Z","iopub.status.idle":"2022-02-14T12:16:21.716914Z","shell.execute_reply.started":"2022-02-14T12:15:57.249924Z","shell.execute_reply":"2022-02-14T12:16:21.716132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import train Data\ndf = pd.read_csv(f'{ROOT_DIR}/train.csv')\ndf['image_path'] = f'{ROOT_DIR}/train_images/video_'+df.video_id.astype(str)+'/'+df.video_frame.astype(str)+'.jpg'\ndf['annotations'] = df['annotations'].progress_apply(eval)\ndisplay(df.head(2))\ndf['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-14T12:16:21.719172Z","iopub.execute_input":"2022-02-14T12:16:21.719471Z","iopub.status.idle":"2022-02-14T12:16:22.340403Z","shell.execute_reply.started":"2022-02-14T12:16:21.71943Z","shell.execute_reply":"2022-02-14T12:16:22.339545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"execution":{"iopub.status.busy":"2022-02-14T12:16:22.341705Z","iopub.execute_input":"2022-02-14T12:16:22.342037Z","iopub.status.idle":"2022-02-14T12:16:22.348395Z","shell.execute_reply.started":"2022-02-14T12:16:22.341998Z","shell.execute_reply":"2022-02-14T12:16:22.347635Z"},"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/\n!mkdir -p /root/mylibs/\n!cp -r '../input/yolov5-lib-ds/models' /root/mylibs/\nsys.path.insert(0,'/root/mylibs/')\nsys.path.append(\"../input/yolov5-lib-ds/\")\nfrom models.experimental import attempt_load","metadata":{"execution":{"iopub.status.busy":"2022-02-14T12:16:22.34983Z","iopub.execute_input":"2022-02-14T12:16:22.35019Z","iopub.status.idle":"2022-02-14T12:16:26.305378Z","shell.execute_reply.started":"2022-02-14T12:16:22.350154Z","shell.execute_reply":"2022-02-14T12:16:26.304579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m1 = torch.hub.load('../input/yolov5-lib-ds', \n                       'custom', \n                       path='../input/yolov5s6/f2_sub2.pt',\n                       source='local',\n                       force_reload=True)  # local repo\nm1.conf = 0.1","metadata":{"execution":{"iopub.status.busy":"2022-02-14T12:16:26.307922Z","iopub.execute_input":"2022-02-14T12:16:26.308181Z","iopub.status.idle":"2022-02-14T12:16:30.978887Z","shell.execute_reply.started":"2022-02-14T12:16:26.308141Z","shell.execute_reply":"2022-02-14T12:16:30.978157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nm2 = torch.hub.load('../input/yolov5-lib-ds', \n                       'custom', \n                       path='../input/yolov5s6/yolov5s6_5.pt',\n                       source='local',\n                       force_reload=True)  # local repo\nm2.conf = 0.1","metadata":{"execution":{"iopub.status.busy":"2022-02-14T12:16:30.98069Z","iopub.execute_input":"2022-02-14T12:16:30.980958Z","iopub.status.idle":"2022-02-14T12:16:31.735188Z","shell.execute_reply.started":"2022-02-14T12:16:30.980918Z","shell.execute_reply":"2022-02-14T12:16:31.734443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\nenv = greatbarrierreef.make_env()# initialize the environment\niter_test = env.iter_test()  ","metadata":{"execution":{"iopub.status.busy":"2022-02-14T12:16:31.73675Z","iopub.execute_input":"2022-02-14T12:16:31.737322Z","iopub.status.idle":"2022-02-14T12:16:31.758729Z","shell.execute_reply.started":"2022-02-14T12:16:31.737273Z","shell.execute_reply":"2022-02-14T12:16:31.758026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Methods","metadata":{}},{"cell_type":"code","source":"def get_bboxs_yolo(results, IOU=0.25):\n    anno = ''\n    pds = results.pandas().xyxy[0]\n    is_empty = pds.shape[0]\n    if is_empty == 0:\n        return ''\n    for idx, row in pds.iterrows():\n        if row.confidence > IOU:\n            anno += f'{row.confidence} {int(row.xmin)} {int(row.ymin)} {int(row.xmax-row.xmin)} {int(row.ymax-row.ymin)} '\n    return anno.strip(' ')        ","metadata":{"execution":{"iopub.status.busy":"2022-02-14T12:16:31.76002Z","iopub.execute_input":"2022-02-14T12:16:31.760311Z","iopub.status.idle":"2022-02-14T12:16:31.766201Z","shell.execute_reply.started":"2022-02-14T12:16:31.760266Z","shell.execute_reply":"2022-02-14T12:16:31.76539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def box_scale(xmin, ymin, xmax, ymax, rate):\n    x_nmin = xmin+(xmax-xmin)*rate\n    y_nmin = ymin+(ymax-ymin)*rate\n    x_nmax = xmax-(xmax-xmin)*rate\n    y_nmax = ymax-(ymax-ymin)*rate\n    return x_nmin,y_nmin,x_nmax,y_nmax","metadata":{"execution":{"iopub.status.busy":"2022-02-14T12:16:31.767689Z","iopub.execute_input":"2022-02-14T12:16:31.768312Z","iopub.status.idle":"2022-02-14T12:16:31.774988Z","shell.execute_reply.started":"2022-02-14T12:16:31.768267Z","shell.execute_reply":"2022-02-14T12:16:31.774132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def wbf_one_img_result(result_yolo1, result_yolo2, IOU_WBF=0.2):\n    boxes_list = [[],[]]\n    scores_list = [[], []]\n    labels_list = [[], []]\n    weights = [1, 1]\n    iou_thr = 0.50\n    skip_box_thr = 0.3\n    for idx1, row1 in result_yolo1.pandas().xyxy[0].iterrows():\n        if row1.confidence > 0.1:\n            boxes_list[0].append([row1.xmin/1280, row1.ymin/720, row1.xmax/1280, row1.ymax/720])\n            scores_list[0].append(row1.confidence)\n            labels_list[0].append(0)\n    for idx2, row2 in result_yolo2.pandas().xyxy[0].iterrows():\n        if row2.confidence > 0.1:\n            boxes_list[1].append([row2.xmin/1280, row2.ymin/720, row2.xmax/1280, row2.ymax/720])\n            scores_list[1].append(row2.confidence)\n            labels_list[1].append(0)\n    \n    boxes, scores, labels = weighted_boxes_fusion(boxes_list, scores_list, labels_list, weights=weights, iou_thr=iou_thr, skip_box_thr=skip_box_thr)\n    \n    length = len(scores)\n    res = ''\n    for idx in range(length):\n        conf = scores[idx]\n        xmin, ymin, xmax, ymax = boxes[idx]\n        xnmin,ynmin,xnmax,ynmax = box_scale(xmin, ymin, xmax, ymax, 0.045)\n        if conf > 0.3:\n            res += f'{conf} {xnmin*1280} {ynmin*720} {(xnmax-xnmin)*1280} {(ynmax-ynmin)*720} '\n    return res.strip(' ')","metadata":{"execution":{"iopub.status.busy":"2022-02-14T12:16:31.776555Z","iopub.execute_input":"2022-02-14T12:16:31.776886Z","iopub.status.idle":"2022-02-14T12:16:31.790715Z","shell.execute_reply.started":"2022-02-14T12:16:31.776852Z","shell.execute_reply":"2022-02-14T12:16:31.789943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test","metadata":{}},{"cell_type":"code","source":"for (pixel_array, df_pred) in iter_test:  # iterate through all test set images\n    \n    result_yolo1 = m1(pixel_array, size=9000, augment=True)\n    result_yolo2 = m2(pixel_array, size=9000, augment=True)\n    df_pred['annotations'] = wbf_one_img_result(result_yolo1, result_yolo2, IOU_WBF=0.2)\n    env.predict(df_pred)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T12:16:31.792079Z","iopub.execute_input":"2022-02-14T12:16:31.792393Z","iopub.status.idle":"2022-02-14T12:16:43.587749Z","shell.execute_reply.started":"2022-02-14T12:16:31.792331Z","shell.execute_reply":"2022-02-14T12:16:43.587064Z"},"trusted":true},"execution_count":null,"outputs":[]}]}