{"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":"# Import modules","metadata":{}},{"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 matplotlib.pyplot as plt\nimport cv2\nimport json\nfrom PIL import Image\nimport shutil\nfrom tqdm import tqdm\nimport torch\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-04T02:55:35.564378Z","iopub.execute_input":"2022-01-04T02:55:35.565123Z","iopub.status.idle":"2022-01-04T02:55:37.169261Z","shell.execute_reply.started":"2022-01-04T02:55:35.565029Z","shell.execute_reply":"2022-01-04T02:55:37.168550Z"},"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-01-04T02:55:37.170914Z","iopub.execute_input":"2022-01-04T02:55:37.171143Z","iopub.status.idle":"2022-01-04T02:55:38.505894Z","shell.execute_reply.started":"2022-01-04T02:55:37.171112Z","shell.execute_reply":"2022-01-04T02:55:38.504911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Initialise environment","metadata":{}},{"cell_type":"code","source":"# for submission format\nimport sys\nsys.path.append('../input/tensorflow-great-barrier-reef')","metadata":{"execution":{"iopub.status.busy":"2022-01-04T02:55:38.509417Z","iopub.execute_input":"2022-01-04T02:55:38.509643Z","iopub.status.idle":"2022-01-04T02:55:38.516389Z","shell.execute_reply.started":"2022-01-04T02:55:38.509616Z","shell.execute_reply":"2022-01-04T02:55:38.515534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\n\nenv = greatbarrierreef.make_env()   # initialize the environment\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2022-01-04T02:55:38.517991Z","iopub.execute_input":"2022-01-04T02:55:38.518577Z","iopub.status.idle":"2022-01-04T02:55:38.548706Z","shell.execute_reply.started":"2022-01-04T02:55:38.518539Z","shell.execute_reply":"2022-01-04T02:55:38.547997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Model","metadata":{}},{"cell_type":"code","source":"model = torch.hub.load('/kaggle/input/yolov5-lib-ds', 'custom', path='/kaggle/input/weights/best.pt', source='local', force_reload=True)  # or yolov5m, yolov5l, yolov5x, custom","metadata":{"execution":{"iopub.status.busy":"2022-01-04T02:55:38.550907Z","iopub.execute_input":"2022-01-04T02:55:38.551563Z","iopub.status.idle":"2022-01-04T02:55:44.376607Z","shell.execute_reply.started":"2022-01-04T02:55:38.551525Z","shell.execute_reply":"2022-01-04T02:55:44.375851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission environment\n\nThis section contains code to actually submit for the competition.","metadata":{}},{"cell_type":"markdown","source":"## Submit predictions","metadata":{}},{"cell_type":"code","source":"# initialise variables\nidc = 0\nsubmission_dict = {\n    'index': [],\n    'annotations': [],\n}\n\n# submit predictions\nfor (image, sample_prediction_df) in iter_test:\n    results = model(image)\n    predictions = []\n    prediction_str = ''\n    \n    submission_dict['index'].append(idc)\n    for i in results.xyxy[0].tolist():\n        x1, y1, x2, y2, _, _ = map(int, i)\n        _, _, _, _, c,_ = i\n        # if c < 0.2:\n        #     continue\n        w = x2 - x1\n        h = y2 - y1\n        predictions.append('{:.2f} {} {} {} {}'.format(c, x1, y1, w, h))\n    \n    prediction_str = ' '.join(predictions)\n    submission_dict['annotations'].append(prediction_str)\n    # sample_prediction_df['annotations'] = '0 0 0 0 0'\n    sample_prediction_df['annotations'] = ''\n    # sample_prediction_df['annotations'] = prediction_str\n    \n    for key,value in sample_prediction_df.items():\n        print(key, ':', value)\n    \n    env.predict(sample_prediction_df)\n    idc+=1","metadata":{"execution":{"iopub.status.busy":"2022-01-04T02:55:44.378184Z","iopub.execute_input":"2022-01-04T02:55:44.378718Z","iopub.status.idle":"2022-01-04T02:55:49.507920Z","shell.execute_reply.started":"2022-01-04T02:55:44.378677Z","shell.execute_reply":"2022-01-04T02:55:49.507205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_prediction_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-04T02:55:49.509408Z","iopub.execute_input":"2022-01-04T02:55:49.509909Z","iopub.status.idle":"2022-01-04T02:55:49.523939Z","shell.execute_reply.started":"2022-01-04T02:55:49.509870Z","shell.execute_reply":"2022-01-04T02:55:49.523007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Submission","metadata":{}},{"cell_type":"code","source":"df= pd.DataFrame(submission_dict)\ndf.to_csv(\"submission.csv\", index = False, na_rep = '0')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-04T02:55:49.525123Z","iopub.execute_input":"2022-01-04T02:55:49.525602Z","iopub.status.idle":"2022-01-04T02:55:49.537823Z","shell.execute_reply.started":"2022-01-04T02:55:49.525565Z","shell.execute_reply":"2022-01-04T02:55:49.537023Z"},"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-01-04T02:55:49.539116Z","iopub.execute_input":"2022-01-04T02:55:49.539753Z","iopub.status.idle":"2022-01-04T02:55:49.552568Z","shell.execute_reply.started":"2022-01-04T02:55:49.539718Z","shell.execute_reply":"2022-01-04T02:55:49.551729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Development\nThis section contains code for testing and development.","metadata":{}},{"cell_type":"markdown","source":"## Initialise variables and small test set","metadata":{}},{"cell_type":"code","source":"idc = 0\nsubmission_dict = {\n    'index': [],\n    'annotations': [],\n}\n\n# two images\nimage = (\"/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/0.jpg\",\"/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/63.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-01-04T02:55:49.553608Z","iopub.execute_input":"2022-01-04T02:55:49.554136Z","iopub.status.idle":"2022-01-04T02:55:49.558736Z","shell.execute_reply.started":"2022-01-04T02:55:49.554100Z","shell.execute_reply":"2022-01-04T02:55:49.557969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make predictions\nSet `dev = True` to overwrite previous `submission.csv` file.","metadata":{}},{"cell_type":"code","source":"dev = False\n# dev = True\n\nif dev:\n    for path in image:\n        results = model(path)\n        predictions = []\n        prediction_str = ''\n        submission_dict['index'].append(idc)\n        print(len(results.xyxy[0].tolist()))\n        print(results.xyxy[0].tolist())\n\n        # append all results together for multiple COTS\n        for i in results.xyxy[0].tolist():\n            x1, y1, x2, y2, _, _ = map(int, i)\n            _, _, _, _, c,_ = i\n            # if c < 0.2:\n                # continue\n            w = x2 - x1\n            h = y2 - y1\n            predictions.append('{:.2f} {:d} {:d} {:d} {:d}'.format(c, x1, y1, w, h))\n\n            prediction_str = ' '.join(predictions)\n        submission_dict['annotations'].append(prediction_str)\n        idc+=1","metadata":{"execution":{"iopub.status.busy":"2022-01-04T02:55:49.559981Z","iopub.execute_input":"2022-01-04T02:55:49.560935Z","iopub.status.idle":"2022-01-04T02:55:49.570006Z","shell.execute_reply.started":"2022-01-04T02:55:49.560901Z","shell.execute_reply":"2022-01-04T02:55:49.569193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission_dict)\n\ndf= pd.DataFrame(submission_dict)\ndf","metadata":{"execution":{"iopub.status.busy":"2022-01-04T02:55:49.572736Z","iopub.execute_input":"2022-01-04T02:55:49.573256Z","iopub.status.idle":"2022-01-04T02:55:49.586583Z","shell.execute_reply.started":"2022-01-04T02:55:49.573228Z","shell.execute_reply":"2022-01-04T02:55:49.585222Z"},"trusted":true},"execution_count":null,"outputs":[]}]}