{"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":"[](http://)","metadata":{}},{"cell_type":"markdown","source":"# Sadly, I didn't adopt this method. I share it as a reference for everyone's evaluation. If you think it is useful, please vote！You just need good parameters!","metadata":{}},{"cell_type":"markdown","source":"# 🛠 Install Libraries","metadata":{}},{"cell_type":"code","source":"# bbox-utility, check https://github.com/awsaf49/bbox for source code\n!pip install -q /kaggle/input/loguru-lib-ds/loguru-0.5.3-py3-none-any.whl\n!pip install -q /kaggle/input/bbox-lib-ds","metadata":{"_kg_hide-output":true,"_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-02-05T11:34:49.640385Z","iopub.execute_input":"2022-02-05T11:34:49.641104Z","iopub.status.idle":"2022-02-05T11:35:50.14462Z","shell.execute_reply.started":"2022-02-05T11:34:49.640998Z","shell.execute_reply":"2022-02-05T11:35:50.143518Z"},"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-05T11:35:50.146653Z","iopub.execute_input":"2022-02-05T11:35:50.146938Z","iopub.status.idle":"2022-02-05T11:35:51.586199Z","shell.execute_reply.started":"2022-02-05T11:35:50.146905Z","shell.execute_reply":"2022-02-05T11:35:51.585399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Data\ndf = pd.read_csv(f'/kaggle/input/tensorflow-great-barrier-reef/train.csv')\ndf['image_path'] = f'/kaggle/input/tensorflow-great-barrier-reef/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))","metadata":{"execution":{"iopub.status.busy":"2022-02-05T11:35:51.588236Z","iopub.execute_input":"2022-02-05T11:35:51.588788Z","iopub.status.idle":"2022-02-05T11:35:52.091604Z","shell.execute_reply.started":"2022-02-05T11:35:51.588738Z","shell.execute_reply":"2022-02-05T11:35:52.090729Z"},"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-05T11:35:52.094024Z","iopub.execute_input":"2022-02-05T11:35:52.094344Z","iopub.status.idle":"2022-02-05T11:35:52.1997Z","shell.execute_reply.started":"2022-02-05T11:35:52.094303Z","shell.execute_reply":"2022-02-05T11:35:52.198741Z"},"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\nfrom bbox.utils import coco2yolo, coco2voc, voc2yolo, voc2coco\nfrom bbox.utils import draw_bboxes, load_image\nfrom bbox.utils import clip_bbox, str2annot, annot2str\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-05T11:35:52.200858Z","iopub.execute_input":"2022-02-05T11:35:52.201079Z","iopub.status.idle":"2022-02-05T11:35:52.99215Z","shell.execute_reply.started":"2022-02-05T11:35:52.201053Z","shell.execute_reply":"2022-02-05T11:35:52.991304Z"},"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-05T11:35:52.993508Z","iopub.execute_input":"2022-02-05T11:35:52.993735Z","iopub.status.idle":"2022-02-05T11:35:54.520898Z","shell.execute_reply.started":"2022-02-05T11:35:52.993707Z","shell.execute_reply":"2022-02-05T11:35:54.519582Z"},"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-05T11:35:54.523166Z","iopub.execute_input":"2022-02-05T11:35:54.523507Z","iopub.status.idle":"2022-02-05T11:35:54.530122Z","shell.execute_reply.started":"2022-02-05T11:35:54.523473Z","shell.execute_reply":"2022-02-05T11:35:54.529179Z"},"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-05T11:35:54.531777Z","iopub.execute_input":"2022-02-05T11:35:54.532069Z","iopub.status.idle":"2022-02-05T11:35:54.547123Z","shell.execute_reply.started":"2022-02-05T11:35:54.532029Z","shell.execute_reply":"2022-02-05T11:35:54.546373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run Inference on **Train**","metadata":{}},{"cell_type":"code","source":"CKPT_PATH = '../input/tensorflow-f2-sub2/f2_sub2.pt'\nCONF = 0.50\nIOU = 0.50\nIMG_SIZE = 5720\nAUGMENT = True","metadata":{"execution":{"iopub.status.busy":"2022-02-05T11:35:54.548417Z","iopub.execute_input":"2022-02-05T11:35:54.548741Z","iopub.status.idle":"2022-02-05T11:35:54.559301Z","shell.execute_reply.started":"2022-02-05T11:35:54.548699Z","shell.execute_reply":"2022-02-05T11:35:54.558674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model(CKPT_PATH, conf=CONF, iou=IOU)\nimage_paths = df[df.num_bbox>1].sample(100).image_path.tolist()\nfor idx, path in enumerate(image_paths):\n    img = cv2.imread(path)[...,::-1]\n    bboxes, confis = predict(model, img, size=IMG_SIZE, augment=AUGMENT)\n    display(show_img(img, bboxes, bbox_format='coco'))\n    if idx>5:\n        break","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-05T11:35:54.578293Z","iopub.execute_input":"2022-02-05T11:35:54.578869Z","iopub.status.idle":"2022-02-05T11:37:06.200793Z","shell.execute_reply.started":"2022-02-05T11:35:54.578828Z","shell.execute_reply":"2022-02-05T11:37:06.199969Z"},"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-05T11:37:06.202469Z","iopub.execute_input":"2022-02-05T11:37:06.202922Z","iopub.status.idle":"2022-02-05T11:37:06.227815Z","shell.execute_reply.started":"2022-02-05T11:37:06.202873Z","shell.execute_reply":"2022-02-05T11:37:06.226828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"iter_test = env.iter_test()      # an iterator which loops over the test set and sample submission","metadata":{"execution":{"iopub.status.busy":"2022-02-05T11:37:06.229435Z","iopub.execute_input":"2022-02-05T11:37:06.229799Z","iopub.status.idle":"2022-02-05T11:37:06.234707Z","shell.execute_reply.started":"2022-02-05T11:37:06.22975Z","shell.execute_reply":"2022-02-05T11:37:06.233852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run Inference on **Test**","metadata":{}},{"cell_type":"code","source":"model = load_model(CKPT_PATH, conf=CONF, iou=IOU)\nfor 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'))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-05T11:37:06.235765Z","iopub.execute_input":"2022-02-05T11:37:06.235988Z","iopub.status.idle":"2022-02-05T11:37:35.933822Z","shell.execute_reply.started":"2022-02-05T11:37:06.23596Z","shell.execute_reply":"2022-02-05T11:37:35.932789Z"},"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-05T11:37:35.935261Z","iopub.execute_input":"2022-02-05T11:37:35.935519Z","iopub.status.idle":"2022-02-05T11:37:35.948146Z","shell.execute_reply.started":"2022-02-05T11:37:35.935488Z","shell.execute_reply":"2022-02-05T11:37:35.947296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Please Upvote if you find this Helpful","metadata":{}}]}