{"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":"# 🛠 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-14T19:59:38.17223Z","iopub.execute_input":"2022-02-14T19:59:38.172935Z","iopub.status.idle":"2022-02-14T20:00:35.557589Z","shell.execute_reply.started":"2022-02-14T19:59:38.172813Z","shell.execute_reply":"2022-02-14T20:00:35.556763Z"},"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-14T20:00:35.559622Z","iopub.execute_input":"2022-02-14T20:00:35.559913Z","iopub.status.idle":"2022-02-14T20:00:37.304075Z","shell.execute_reply.started":"2022-02-14T20:00:35.559861Z","shell.execute_reply":"2022-02-14T20:00:37.303253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Configuration","metadata":{}},{"cell_type":"code","source":"ROOT_DIR  = '/kaggle/input/tensorflow-great-barrier-reef/'\nCKPT_PATH = '/kaggle/input/1st-train/yolov5s6_trial5_img2000_epoch25_batch8.pt'\nIMG_SIZE  = 4000\nCONF      = 0.28\nIOU       = 0.30\nAUGMENT   = True","metadata":{"execution":{"iopub.status.busy":"2022-02-14T20:00:37.305584Z","iopub.execute_input":"2022-02-14T20:00:37.305853Z","iopub.status.idle":"2022-02-14T20:00:37.321829Z","shell.execute_reply.started":"2022-02-14T20:00:37.305817Z","shell.execute_reply":"2022-02-14T20:00:37.317963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/tensorflow-great-barrier-reef/train.csv')\ntrain['image_path'] = f'{ROOT_DIR}/train_images/video_'+train.video_id.astype(str)+'/'+train.video_frame.astype(str)+'.jpg'\ntrain['annotations'] = train['annotations'].apply(eval)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T20:00:37.329274Z","iopub.execute_input":"2022-02-14T20:00:37.331102Z","iopub.status.idle":"2022-02-14T20:00:37.868578Z","shell.execute_reply.started":"2022-02-14T20:00:37.330939Z","shell.execute_reply":"2022-02-14T20:00:37.867962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Number of BBoxes","metadata":{}},{"cell_type":"code","source":"train['num_bbox'] = train['annotations'].progress_apply(lambda x: len(x))\ndata = (train.num_bbox>0).value_counts()/len(train)*100\nprint(f\"No BBox: {data[0]:0.2f}% | With BBox: {data[1]:0.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2022-02-14T20:00:37.872197Z","iopub.execute_input":"2022-02-14T20:00:37.874144Z","iopub.status.idle":"2022-02-14T20:00:38.045828Z","shell.execute_reply.started":"2022-02-14T20:00:37.874104Z","shell.execute_reply":"2022-02-14T20:00:38.045201Z"},"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-14T20:00:38.049544Z","iopub.execute_input":"2022-02-14T20:00:38.051433Z","iopub.status.idle":"2022-02-14T20:00:38.681911Z","shell.execute_reply.started":"2022-02-14T20:00:38.051394Z","shell.execute_reply":"2022-02-14T20:00:38.681209Z"},"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-14T20:00:38.683037Z","iopub.execute_input":"2022-02-14T20:00:38.683278Z","iopub.status.idle":"2022-02-14T20:00:38.692177Z","shell.execute_reply.started":"2022-02-14T20:00:38.683246Z","shell.execute_reply":"2022-02-14T20:00:38.691456Z"},"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    \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((600, 300))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T20:00:38.693728Z","iopub.execute_input":"2022-02-14T20:00:38.694122Z","iopub.status.idle":"2022-02-14T20:00:38.704962Z","shell.execute_reply.started":"2022-02-14T20:00:38.694085Z","shell.execute_reply":"2022-02-14T20:00:38.704236Z"},"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-14T20:00:38.706336Z","iopub.execute_input":"2022-02-14T20:00:38.706605Z","iopub.status.idle":"2022-02-14T20:00:40.044866Z","shell.execute_reply.started":"2022-02-14T20:00:38.706569Z","shell.execute_reply":"2022-02-14T20:00:40.043892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔭 Inference","metadata":{}},{"cell_type":"markdown","source":"## Run Inference on **Train**","metadata":{}},{"cell_type":"code","source":"model = load_model(CKPT_PATH, conf=CONF, iou=IOU)\ntmp_df = train[train.num_bbox>1].sample(20)\nimage_path = tmp_df.image_path.tolist()\nsample_anno = tmp_df.annotations.tolist()\nfor idx in range(5):\n    img = cv2.imread(image_path[idx])[...,::-1]\n    sample_bbox = [[i['x'],i['y'],i['width'],i['height']] for i in sample_anno[idx]]\n    bboxes, confis = predict(model, img, size=IMG_SIZE, augment=AUGMENT)\n    print('Ground Truth')\n    display(show_img(img, sample_bbox, bbox_format='coco'))\n    print('Prediction')\n    display(show_img(img, bboxes, bbox_format='coco'))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-14T20:00:40.048191Z","iopub.execute_input":"2022-02-14T20:00:40.04855Z","iopub.status.idle":"2022-02-14T20:00:51.941872Z","shell.execute_reply.started":"2022-02-14T20:00:40.048456Z","shell.execute_reply":"2022-02-14T20:00:51.940977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run Inference on **Test**","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-14T20:00:51.943773Z","iopub.execute_input":"2022-02-14T20:00:51.944072Z","iopub.status.idle":"2022-02-14T20:00:51.97207Z","shell.execute_reply.started":"2022-02-14T20:00:51.944037Z","shell.execute_reply":"2022-02-14T20:00:51.971396Z"},"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-14T20:00:51.9736Z","iopub.execute_input":"2022-02-14T20:00:51.973869Z","iopub.status.idle":"2022-02-14T20:00:53.217569Z","shell.execute_reply.started":"2022-02-14T20:00:51.973834Z","shell.execute_reply":"2022-02-14T20:00:53.216955Z"},"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-14T20:00:53.218822Z","iopub.execute_input":"2022-02-14T20:00:53.219478Z","iopub.status.idle":"2022-02-14T20:00:53.233381Z","shell.execute_reply.started":"2022-02-14T20:00:53.219437Z","shell.execute_reply":"2022-02-14T20:00:53.232711Z"},"trusted":true},"execution_count":null,"outputs":[]}]}