{"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":"# Barrier Reef Detectron2 [Inference]","metadata":{"papermill":{"duration":0.016273,"end_time":"2021-11-12T09:12:53.973309","exception":false,"start_time":"2021-11-12T09:12:53.957036","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### Hi kagglers, This is `inference` notebook using `Detectron2`.\n[Barrier Reef Detectron2 [training]](https://www.kaggle.com/ammarnassanalhajali/barrier-reef-detectron2-training) \n### Please if this kernel is useful, <font color='red'>please upvote !!</font>","metadata":{"papermill":{"duration":0.014102,"end_time":"2021-11-12T09:12:54.001019","exception":false,"start_time":"2021-11-12T09:12:53.986917","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Other notebooks in this competition \n- [Barrier Reef YOLOv5 [Training]](https://www.kaggle.com/ammarnassanalhajali/barrier-reef-yolov5-training)\n- [Barrier Reef YOLOv5 [Inference]](https://www.kaggle.com/ammarnassanalhajali/barrier-reef-yolov5-inference)","metadata":{"papermill":{"duration":0.012887,"end_time":"2021-11-12T09:12:54.028158","exception":false,"start_time":"2021-11-12T09:12:54.015271","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# Detectron2 \nDetectron2 is Facebook AI Research's next generation software system that implements state-of-the-art object detection algorithms. It is a ground-up rewrite of the previous version, Detectron, and it originates from maskrcnn-benchmark\n\n","metadata":{"papermill":{"duration":0.011944,"end_time":"2021-11-12T09:12:54.05242","exception":false,"start_time":"2021-11-12T09:12:54.040476","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Install Detectron2 offline","metadata":{"papermill":{"duration":0.011486,"end_time":"2021-11-12T09:12:54.07535","exception":false,"start_time":"2021-11-12T09:12:54.063864","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!pip install ../input/detectron-05/whls/pycocotools-2.0.2/dist/pycocotools-2.0.2.tar --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/fvcore-0.1.5.post20211019/fvcore-0.1.5.post20211019 --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/antlr4-python3-runtime-4.8/antlr4-python3-runtime-4.8 --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/detectron2-0.5/detectron2 --no-index --find-links ../input/detectron-05/whls ","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"papermill":{"duration":200.225352,"end_time":"2021-11-12T09:16:14.312085","exception":false,"start_time":"2021-11-12T09:12:54.086733","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-12T17:09:41.335629Z","iopub.execute_input":"2022-01-12T17:09:41.336479Z","iopub.status.idle":"2022-01-12T17:12:59.594318Z","shell.execute_reply.started":"2022-01-12T17:09:41.336343Z","shell.execute_reply":"2022-01-12T17:12:59.593509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# importing libraries","metadata":{"papermill":{"duration":0.06065,"end_time":"2021-11-12T09:16:14.418191","exception":false,"start_time":"2021-11-12T09:16:14.357541","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from PIL import Image\nimport cv2\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nimport torch\nimport detectron2\nfrom detectron2 import model_zoo\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2.config import get_cfg\nfrom fastcore.all import *\ndetectron2.__version__","metadata":{"papermill":{"duration":1.330639,"end_time":"2021-11-12T09:16:15.797851","exception":false,"start_time":"2021-11-12T09:16:14.467212","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-12T17:12:59.597994Z","iopub.execute_input":"2022-01-12T17:12:59.598206Z","iopub.status.idle":"2022-01-12T17:13:00.787113Z","shell.execute_reply.started":"2022-01-12T17:12:59.59818Z","shell.execute_reply":"2022-01-12T17:13:00.786425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Functions","metadata":{}},{"cell_type":"code","source":"import 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\nimport ast\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))\n\ndef plot_one_box(x, img, color=None, label=None, line_thickness=None):\n    # Plots one bounding box on image img\n    tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness\n    color = color or [random.randint(0, 255) for _ in range(3)]\n    c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))\n    cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)\n    if label:\n        tf = max(tl - 1, 1)  # font thickness\n        t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]\n        c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3\n        cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled\n        cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)\n\ndef draw_bboxes(img, bboxes, classes, class_ids, colors = None, show_classes = None, bbox_format = 'yolo', class_name = False, line_thickness = 2):  \n     \n    image = img.copy()\n    show_classes = classes if show_classes is None else show_classes\n    colors = (0, 255 ,0) if colors is None else colors\n    \n    if bbox_format == 'yolo':\n        \n        for idx in range(len(bboxes)):  \n            \n            bbox  = bboxes[idx]\n            cls   = classes[idx]\n            cls_id = class_ids[idx]\n            color = colors[cls_id] if type(colors) is list else colors\n            \n            if cls in show_classes:\n            \n                x1 = round(float(bbox[0])*image.shape[1])\n                y1 = round(float(bbox[1])*image.shape[0])\n                w  = round(float(bbox[2])*image.shape[1]/2) #w/2 \n                h  = round(float(bbox[3])*image.shape[0]/2)\n\n                voc_bbox = (x1-w, y1-h, x1+w, y1+h)\n                plot_one_box(voc_bbox, \n                             image,\n                             color = color,\n                             label = cls if class_name else str(get_label(cls)),\n                             line_thickness = line_thickness)\n            \n    elif bbox_format == 'coco':\n        \n        for idx in range(len(bboxes)):  \n            \n            bbox  = bboxes[idx]\n            cls   = classes[idx]\n            cls_id = class_ids[idx]\n            color = colors[cls_id] if type(colors) is list else colors\n            \n            if cls in show_classes:            \n                x1 = int(round(bbox[0]))\n                y1 = int(round(bbox[1]))\n                w  = int(round(bbox[2]))\n                h  = int(round(bbox[3]))\n\n                voc_bbox = (x1, y1, x1+w, y1+h)\n                plot_one_box(voc_bbox, \n                             image,\n                             color = color,\n                             label = cls if class_name else str(cls_id),\n                             line_thickness = line_thickness)\n\n    elif bbox_format == 'voc_pascal':\n        \n        for idx in range(len(bboxes)):  \n            \n            bbox  = bboxes[idx]\n            cls   = classes[idx]\n            cls_id = class_ids[idx]\n            color = colors[cls_id] if type(colors) is list else colors\n            \n            if cls in show_classes: \n                x1 = int(round(bbox[0]))\n                y1 = int(round(bbox[1]))\n                x2 = int(round(bbox[2]))\n                y2 = int(round(bbox[3]))\n                voc_bbox = (x1, y1, x2, y2)\n                plot_one_box(voc_bbox, \n                             image,\n                             color = color,\n                             label = cls if class_name else str(cls_id),\n                             line_thickness = line_thickness)\n    else:\n        raise ValueError('wrong bbox format')\n\n    return image\n\ncolors=(255,0,0)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-12T17:13:00.788526Z","iopub.execute_input":"2022-01-12T17:13:00.78879Z","iopub.status.idle":"2022-01-12T17:13:00.81535Z","shell.execute_reply.started":"2022-01-12T17:13:00.788755Z","shell.execute_reply":"2022-01-12T17:13:00.814578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Model","metadata":{"papermill":{"duration":0.028495,"end_time":"2021-11-12T09:16:16.052145","exception":false,"start_time":"2021-11-12T09:16:16.02365","status":"completed"},"tags":[]}},{"cell_type":"code","source":"cfg = get_cfg()\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\"))\ncfg.MODEL.WEIGHTS = \"../input/brdetectron2train2/A_Fold1_v2_R_50_F2S0.354_AP15.415_best.pth\" \ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 1 \ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.40\npredictor = DefaultPredictor(cfg)","metadata":{"papermill":{"duration":8.38867,"end_time":"2021-11-12T09:16:24.469703","exception":false,"start_time":"2021-11-12T09:16:16.081033","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-12T17:13:00.818Z","iopub.execute_input":"2022-01-12T17:13:00.818975Z","iopub.status.idle":"2022-01-12T17:13:07.681334Z","shell.execute_reply.started":"2022-01-12T17:13:00.818937Z","shell.execute_reply":"2022-01-12T17:13:07.68059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def format_prediction(bboxes, confs):\n    annot = ''\n    if len(bboxes)>0:\n        for idx in range(len(bboxes)):\n            xmin, ymin, xmax, ymax = bboxes[idx]\n            w=xmax-xmin\n            h=ymax-ymin\n            conf= confs[idx]\n            if conf >0.5:\n                annot += f'{conf} {xmin} {ymin} {w} {h}'\n                annot +=' '\n        annot = annot.strip(' ')\n    return annot\n","metadata":{"execution":{"iopub.status.busy":"2022-01-12T17:13:07.682617Z","iopub.execute_input":"2022-01-12T17:13:07.682873Z","iopub.status.idle":"2022-01-12T17:13:07.688594Z","shell.execute_reply.started":"2022-01-12T17:13:07.68284Z","shell.execute_reply":"2022-01-12T17:13:07.68786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference on a image from the validation set","metadata":{}},{"cell_type":"code","source":"img3=cv2.imread(\"../input/tensorflow-great-barrier-reef/train_images/video_2/5745.jpg\")\noutputs = predictor(img3)\npred_boxes = outputs['instances'].pred_boxes.tensor\npred_scores =outputs['instances'].scores\npred_boxes = pred_boxes.cpu().numpy()\npred_scores = pred_scores.cpu().numpy()\ndisplay(show_img(img3[:, :, ::-1], pred_boxes, bbox_format='voc_pascal'))","metadata":{"execution":{"iopub.status.busy":"2022-01-12T17:13:07.68992Z","iopub.execute_input":"2022-01-12T17:13:07.690389Z","iopub.status.idle":"2022-01-12T17:13:13.644005Z","shell.execute_reply.started":"2022-01-12T17:13:07.690352Z","shell.execute_reply":"2022-01-12T17:13:13.643375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# inference","metadata":{}},{"cell_type":"code","source":"import greatbarrierreef\nenv = greatbarrierreef.make_env()# initialize the environment\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T17:13:13.644924Z","iopub.execute_input":"2022-01-12T17:13:13.645154Z","iopub.status.idle":"2022-01-12T17:13:13.670936Z","shell.execute_reply.started":"2022-01-12T17:13:13.645123Z","shell.execute_reply":"2022-01-12T17:13:13.670216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, (img, pred_df) in enumerate(tqdm(iter_test)):\n    outputs = predictor(img[:,:,::-1])\n    pred_boxes = outputs['instances'].pred_boxes.tensor\n    pred_scores =outputs['instances'].scores\n    pred_boxes = pred_boxes.cpu().numpy()\n    pred_scores = pred_scores.cpu().numpy()\n    annot = format_prediction(pred_boxes,pred_scores)\n    pred_df['annotations'] = annot\n    env.predict(pred_df)\n    if idx<3:\n        display(show_img(img, pred_boxes, bbox_format='voc_pascal'))","metadata":{"execution":{"iopub.status.busy":"2022-01-12T17:13:13.672079Z","iopub.execute_input":"2022-01-12T17:13:13.67249Z","iopub.status.idle":"2022-01-12T17:13:14.905148Z","shell.execute_reply.started":"2022-01-12T17:13:13.672449Z","shell.execute_reply":"2022-01-12T17:13:14.904377Z"},"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-01-12T17:13:14.906429Z","iopub.execute_input":"2022-01-12T17:13:14.906935Z","iopub.status.idle":"2022-01-12T17:13:14.92244Z","shell.execute_reply.started":"2022-01-12T17:13:14.90688Z","shell.execute_reply":"2022-01-12T17:13:14.921679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"from detectron2.utils.visualizer import ColorMode\nfrom detectron2.utils.visualizer import Visualizer\n\nv = Visualizer(img3,scale=0.8)\nout = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\nfig, ax = plt.subplots(figsize =(20,50))\nax.imshow(out.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2021-12-02T15:39:32.660565Z","iopub.execute_input":"2021-12-02T15:39:32.660837Z","iopub.status.idle":"2021-12-02T15:39:33.621388Z","shell.execute_reply.started":"2021-12-02T15:39:32.660792Z","shell.execute_reply":"2021-12-02T15:39:33.620749Z"}}},{"cell_type":"markdown","source":"# References\n* https://www.kaggle.com/slawekbiel/positive-score-with-detectron-3-3-inference","metadata":{"papermill":{"duration":0.047725,"end_time":"2021-11-12T09:16:33.199963","exception":false,"start_time":"2021-11-12T09:16:33.152238","status":"completed"},"tags":[]}}]}