{"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":"# 🛠 Instalowanie bibliotek","metadata":{}},{"cell_type":"code","source":"# bbox-utility, check https://github.com/awsaf49/bbox for source code\n!cp /kaggle/input/loguru-lib-ds/loguru-0.5.3-py3-none-any.whl /kaggle/working/loguru-0.5.3-py3-none-any.whl\n!cp -r /kaggle/input/bbox-lib-ds /kaggle/working/bbox-lib-ds\n!pip install -q /kaggle/working/loguru-0.5.3-py3-none-any.whl\n!pip install -q /kaggle/working/bbox-lib-ds","metadata":{"execution":{"iopub.status.busy":"2022-02-17T00:03:15.612446Z","iopub.execute_input":"2022-02-17T00:03:15.612807Z","iopub.status.idle":"2022-02-17T00:03:33.213782Z","shell.execute_reply.started":"2022-02-17T00:03:15.612760Z","shell.execute_reply":"2022-02-17T00:03:33.212846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 📚 Importowanie Bibliotek","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":{"execution":{"iopub.status.busy":"2022-02-17T00:03:33.218511Z","iopub.execute_input":"2022-02-17T00:03:33.221057Z","iopub.status.idle":"2022-02-17T00:03:33.234526Z","shell.execute_reply.started":"2022-02-17T00:03:33.221015Z","shell.execute_reply":"2022-02-17T00:03:33.233827Z"},"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-17T00:03:33.238504Z","iopub.execute_input":"2022-02-17T00:03:33.242085Z","iopub.status.idle":"2022-02-17T00:03:34.041518Z","shell.execute_reply.started":"2022-02-17T00:03:33.242039Z","shell.execute_reply":"2022-02-17T00:03:34.040867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Liczba Bounding Boxów","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-17T00:03:34.045623Z","iopub.execute_input":"2022-02-17T00:03:34.047693Z","iopub.status.idle":"2022-02-17T00:03:34.177676Z","shell.execute_reply.started":"2022-02-17T00:03:34.047639Z","shell.execute_reply":"2022-02-17T00:03:34.176946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔨 Funkcje pomocnicze","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":{"execution":{"iopub.status.busy":"2022-02-17T00:03:34.180081Z","iopub.execute_input":"2022-02-17T00:03:34.180871Z","iopub.status.idle":"2022-02-17T00:03:34.189640Z","shell.execute_reply.started":"2022-02-17T00:03:34.180831Z","shell.execute_reply":"2022-02-17T00:03:34.188911Z"},"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":{"execution":{"iopub.status.busy":"2022-02-17T00:03:34.190901Z","iopub.execute_input":"2022-02-17T00:03:34.191395Z","iopub.status.idle":"2022-02-17T00:03:35.605473Z","shell.execute_reply.started":"2022-02-17T00:03:34.191356Z","shell.execute_reply":"2022-02-17T00:03:35.604500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Funkcja ładująca model","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2022-02-17T00:03:35.607178Z","iopub.execute_input":"2022-02-17T00:03:35.607457Z","iopub.status.idle":"2022-02-17T00:03:35.613837Z","shell.execute_reply.started":"2022-02-17T00:03:35.607421Z","shell.execute_reply":"2022-02-17T00:03:35.613176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🔭 Inference","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":{"execution":{"iopub.status.busy":"2022-02-17T00:03:35.615223Z","iopub.execute_input":"2022-02-17T00:03:35.615947Z","iopub.status.idle":"2022-02-17T00:03:35.628452Z","shell.execute_reply.started":"2022-02-17T00:03:35.615911Z","shell.execute_reply":"2022-02-17T00:03:35.627687Z"},"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-17T00:03:35.631420Z","iopub.execute_input":"2022-02-17T00:03:35.631613Z","iopub.status.idle":"2022-02-17T00:03:35.640599Z","shell.execute_reply.started":"2022-02-17T00:03:35.631587Z","shell.execute_reply":"2022-02-17T00:03:35.639941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ładowanie modelu","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2022-02-17T00:03:35.641995Z","iopub.execute_input":"2022-02-17T00:03:35.642492Z","iopub.status.idle":"2022-02-17T00:03:40.291877Z","shell.execute_reply.started":"2022-02-17T00:03:35.642455Z","shell.execute_reply":"2022-02-17T00:03:40.291026Z"},"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":{"execution":{"iopub.status.busy":"2022-02-17T00:03:40.293148Z","iopub.execute_input":"2022-02-17T00:03:40.293508Z","iopub.status.idle":"2022-02-17T00:03:40.469800Z","shell.execute_reply.started":"2022-02-17T00:03:40.293474Z","shell.execute_reply":"2022-02-17T00:03:40.468592Z"},"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-17T00:03:40.471077Z","iopub.status.idle":"2022-02-17T00:03:40.471564Z","shell.execute_reply.started":"2022-02-17T00:03:40.471323Z","shell.execute_reply":"2022-02-17T00:03:40.471349Z"},"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":{"execution":{"iopub.status.busy":"2022-02-17T00:03:40.472993Z","iopub.status.idle":"2022-02-17T00:03:40.473484Z","shell.execute_reply.started":"2022-02-17T00:03:40.473241Z","shell.execute_reply":"2022-02-17T00:03:40.473267Z"},"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-17T00:03:40.475124Z","iopub.status.idle":"2022-02-17T00:03:40.475544Z","shell.execute_reply.started":"2022-02-17T00:03:40.475321Z","shell.execute_reply":"2022-02-17T00:03:40.475344Z"},"trusted":true},"execution_count":null,"outputs":[]}]}