{"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":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport greatbarrierreef\nimport sys\nimport cv2 as cv\nimport os\nfrom tqdm.notebook import tqdm\ntqdm.pandas()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-03T04:07:57.962728Z","iopub.execute_input":"2022-02-03T04:07:57.963032Z","iopub.status.idle":"2022-02-03T04:07:57.969048Z","shell.execute_reply.started":"2022-02-03T04:07:57.962989Z","shell.execute_reply":"2022-02-03T04:07:57.967795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model(conf=0.25, iou=0.50):\n    net = cv.dnn.readNet(f'/kaggle/input/yolov4608/yolo-v4-custom.cfg',\n                            f'/kaggle/input/yolov4608/yolov4-custom_best.weights')\n    net = cv.dnn_DetectionModel(net)\n    net.setInputParams(size=(608, 608), scale=1/255, swapRB=True)\n    return net","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:08:12.352018Z","iopub.execute_input":"2022-02-03T04:08:12.352312Z","iopub.status.idle":"2022-02-03T04:08:12.357498Z","shell.execute_reply.started":"2022-02-03T04:08:12.352279Z","shell.execute_reply":"2022-02-03T04:08:12.356603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confThreshold = 0.25\nconfthre = 0.25\nIMG_SIZE=416\nwith open('/kaggle/input/yolov4608/obj.names', 'rt') as f:\n    names = f.read().rstrip('\\n').split('\\n')\n\ndef predict(net, img, size=IMG_SIZE):\n    confs = []\n    bboxes = []\n    height, width = img.shape[:2]\n    bbclasses, scores, bboxes = net.detect(img, confThreshold=confThreshold, nmsThreshold=0.4)\n   \n    if len(bboxes):\n        confs=[]\n        for i in scores:\n            confs.append('{:.2f}'.format(i))\n        score=np.array(confs,dtype=float)  \n        return bboxes, score\n    else:\n        return [],[]\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","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:08:16.342786Z","iopub.execute_input":"2022-02-03T04:08:16.343337Z","iopub.status.idle":"2022-02-03T04:08:16.358406Z","shell.execute_reply.started":"2022-02-03T04:08:16.343301Z","shell.execute_reply":"2022-02-03T04:08:16.357724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import Image\n# from numpy import asarray\n  \n  \n\n# img = Image.open('/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/1010.jpg')\n  \n# \\\n# numpydata = asarray(img)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:11:52.580395Z","iopub.execute_input":"2022-02-03T04:11:52.581038Z","iopub.status.idle":"2022-02-03T04:11:52.654718Z","shell.execute_reply.started":"2022-02-03T04:11:52.580997Z","shell.execute_reply":"2022-02-03T04:11:52.653943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# net = load_model(conf=0.25, iou=0.40)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:08:25.018068Z","iopub.execute_input":"2022-02-03T04:08:25.018353Z","iopub.status.idle":"2022-02-03T04:08:29.030710Z","shell.execute_reply.started":"2022-02-03T04:08:25.018324Z","shell.execute_reply":"2022-02-03T04:08:29.029878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import Image\n# from numpy import asarray\n  \n  \n\n# img = Image.open('/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/1915.jpg')\n  \n\n# numpydata = asarray(img)\n# bboxes, confs  = predict(net, numpydata, size=IMG_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:13:05.296748Z","iopub.execute_input":"2022-02-03T04:13:05.297485Z","iopub.status.idle":"2022-02-03T04:13:06.889935Z","shell.execute_reply.started":"2022-02-03T04:13:05.297446Z","shell.execute_reply":"2022-02-03T04:13:06.888989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(bboxes, confs)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:13:08.514386Z","iopub.execute_input":"2022-02-03T04:13:08.514981Z","iopub.status.idle":"2022-02-03T04:13:08.520554Z","shell.execute_reply.started":"2022-02-03T04:13:08.514943Z","shell.execute_reply":"2022-02-03T04:13:08.519256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"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-01-30T15:41:57.350847Z","iopub.execute_input":"2022-01-30T15:41:57.351659Z","iopub.status.idle":"2022-01-30T15:41:57.356792Z","shell.execute_reply.started":"2022-01-30T15:41:57.351612Z","shell.execute_reply":"2022-01-30T15:41:57.355501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net = load_model(conf=0.25, iou=0.40)\nfor idx, (img, pred_df) in enumerate(tqdm(iter_test)):\n    \n    bboxes, confs  = predict(net, img, size=IMG_SIZE)\n    annot = format_prediction(bboxes, confs)\n    pred_df['annotations'] = annot\n    env.predict(pred_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-30T15:42:01.91415Z","iopub.execute_input":"2022-01-30T15:42:01.914608Z","iopub.status.idle":"2022-01-30T15:42:05.106092Z","shell.execute_reply.started":"2022-01-30T15:42:01.914559Z","shell.execute_reply":"2022-01-30T15:42:05.105052Z"},"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-30T15:42:16.79003Z","iopub.execute_input":"2022-01-30T15:42:16.7903Z","iopub.status.idle":"2022-01-30T15:42:16.808018Z","shell.execute_reply.started":"2022-01-30T15:42:16.790272Z","shell.execute_reply":"2022-01-30T15:42:16.807411Z"},"trusted":true},"execution_count":null,"outputs":[]}]}