{"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":"code","source":"!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-14T17:12:34.729094Z","iopub.execute_input":"2022-02-14T17:12:34.729410Z","iopub.status.idle":"2022-02-14T17:13:32.494124Z","shell.execute_reply.started":"2022-02-14T17:12:34.729330Z","shell.execute_reply":"2022-02-14T17:13:32.493223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom tqdm.notebook import tqdm\n\ntqdm.pandas()\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport glob\nimport shutil\nimport sys\n\nsys.path.append(\"../input/tensorflow-great-barrier-reef\")\nimport torch\nfrom PIL import Image\nimport greatbarrierreef\nfrom bbox.utils import *\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-14T17:13:32.497412Z","iopub.execute_input":"2022-02-14T17:13:32.497900Z","iopub.status.idle":"2022-02-14T17:13:34.698153Z","shell.execute_reply.started":"2022-02-14T17:13:32.497854Z","shell.execute_reply":"2022-02-14T17:13:34.697425Z"},"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\"] = (\n    f\"/kaggle/input/tensorflow-great-barrier-reef/train_images/video_\"\n    + df.video_id.astype(str)\n    + \"/\"\n    + df.video_frame.astype(str)\n    + \".jpg\"\n)\ndf[\"annotations\"] = df[\"annotations\"].progress_apply(eval)\ndisplay(df.head(2))\n","metadata":{"execution":{"iopub.status.busy":"2022-02-14T17:13:34.699442Z","iopub.execute_input":"2022-02-14T17:13:34.699704Z","iopub.status.idle":"2022-02-14T17:13:35.136310Z","shell.execute_reply.started":"2022-02-14T17:13:34.699672Z","shell.execute_reply":"2022-02-14T17:13:35.135514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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}%\")\n","metadata":{"execution":{"iopub.status.busy":"2022-02-14T17:13:35.138521Z","iopub.execute_input":"2022-02-14T17:13:35.138783Z","iopub.status.idle":"2022-02-14T17:13:35.237922Z","shell.execute_reply.started":"2022-02-14T17:13:35.138748Z","shell.execute_reply":"2022-02-14T17:13:35.237170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_bbox(annots):\n    bboxes = [list(annot.values()) for annot in annots]\n    return bboxes\n\n\ndef get_imgsize(row):\n    row[\"width\"], row[\"height\"] = imagesize.get(row[\"image_path\"])\n    return row\n\n\nnp.random.seed(32)\ncolors = [\n    (np.random.randint(255), np.random.randint(255), np.random.randint(255))\n    for idx in range(1)\n]","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T17:13:35.239246Z","iopub.execute_input":"2022-02-14T17:13:35.239674Z","iopub.status.idle":"2022-02-14T17:13:35.247137Z","shell.execute_reply.started":"2022-02-14T17:13:35.239636Z","shell.execute_reply":"2022-02-14T17:13:35.246260Z"},"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-14T17:13:35.248714Z","iopub.execute_input":"2022-02-14T17:13:35.249041Z","iopub.status.idle":"2022-02-14T17:13:36.574545Z","shell.execute_reply.started":"2022-02-14T17:13:35.248998Z","shell.execute_reply":"2022-02-14T17:13:36.573567Z"},"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(\n        \"/kaggle/input/yolov5-lib-ds\",\n        \"custom\",\n        path=ckpt_path,\n        source=\"local\",\n        force_reload=True,\n    )  # 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-14T17:13:36.576451Z","iopub.execute_input":"2022-02-14T17:13:36.576736Z","iopub.status.idle":"2022-02-14T17:13:36.584162Z","shell.execute_reply.started":"2022-02-14T17:13:36.576700Z","shell.execute_reply":"2022-02-14T17:13:36.582238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\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\n\ndef show_img(img, bboxes, bbox_format=\"yolo\"):\n    names = [\"starfish\"] * len(bboxes)\n    labels = [0] * len(bboxes)\n    img = draw_bboxes(\n        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    )\n    return Image.fromarray(img).resize((800, 400))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T17:13:36.586615Z","iopub.execute_input":"2022-02-14T17:13:36.587215Z","iopub.status.idle":"2022-02-14T17:13:36.599244Z","shell.execute_reply.started":"2022-02-14T17:13:36.587178Z","shell.execute_reply":"2022-02-14T17:13:36.598277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CKPT_PATH = \"../input/yolov5s6/f2_sub2.pt\"\nCONF = 0.30\nIOU = 0.50\nIMG_SIZE = 6400\nAUGMENT = True","metadata":{"execution":{"iopub.status.busy":"2022-02-14T17:13:36.601640Z","iopub.execute_input":"2022-02-14T17:13:36.602221Z","iopub.status.idle":"2022-02-14T17:13:36.607615Z","shell.execute_reply.started":"2022-02-14T17:13:36.602184Z","shell.execute_reply":"2022-02-14T17:13:36.606910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"env = greatbarrierreef.make_env()\niter_test = env.iter_test()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T17:13:36.610428Z","iopub.execute_input":"2022-02-14T17:13:36.611387Z","iopub.status.idle":"2022-02-14T17:13:36.616020Z","shell.execute_reply.started":"2022-02-14T17:13:36.611351Z","shell.execute_reply":"2022-02-14T17:13:36.615321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-14T17:13:36.617283Z","iopub.execute_input":"2022-02-14T17:13:36.618453Z","iopub.status.idle":"2022-02-14T17:13:50.502430Z","shell.execute_reply.started":"2022-02-14T17:13:36.618414Z","shell.execute_reply":"2022-02-14T17:13:50.501643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv(\"submission.csv\")\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T17:13:50.504008Z","iopub.execute_input":"2022-02-14T17:13:50.505631Z","iopub.status.idle":"2022-02-14T17:13:50.518201Z","shell.execute_reply.started":"2022-02-14T17:13:50.505588Z","shell.execute_reply":"2022-02-14T17:13:50.517551Z"},"trusted":true},"execution_count":null,"outputs":[]}]}