{"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\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nimport pandas as pd\nimport os\nimport cv2\nimport sys\nsys.path.append('../input/tensorflow-great-barrier-reef')\nimport torch","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-09T14:36:44.077220Z","iopub.execute_input":"2022-02-09T14:36:44.077513Z","iopub.status.idle":"2022-02-09T14:36:44.083525Z","shell.execute_reply.started":"2022-02-09T14:36:44.077483Z","shell.execute_reply":"2022-02-09T14:36:44.082580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR  = '/kaggle/input/tensorflow-great-barrier-reef/'\nMODEL_NAMES = ['../input/cotsmodels/yolo5m6_3000_2.pt',\n               '../input/yolov5s6/f2_sub2.pt',\n              '../input/reystarmodels/yolo5m6_3000_all_1.pt']\nsizes=[4544,6400,3456]\nflips=[False,False,False,False]\nCONF      = 0.1\nIOU       = 0.50\nAUGMENT   = False\nMIN_SZ    = 4","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:36:44.085199Z","iopub.execute_input":"2022-02-09T14:36:44.086022Z","iopub.status.idle":"2022-02-09T14:36:44.094872Z","shell.execute_reply.started":"2022-02-09T14:36:44.085860Z","shell.execute_reply":"2022-02-09T14:36:44.093384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Data\ndf = pd.read_csv(f'{ROOT_DIR}/train.csv')\ndf['image_path'] = f'{ROOT_DIR}/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))\ndf['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-09T14:36:44.097064Z","iopub.execute_input":"2022-02-09T14:36:44.097591Z","iopub.status.idle":"2022-02-09T14:36:44.580447Z","shell.execute_reply.started":"2022-02-09T14:36:44.097427Z","shell.execute_reply":"2022-02-09T14:36:44.579653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /root/mylibs/\n!cp -r '../input/yolov5detect/detection' /root/mylibs/\nsys.path.insert(0,'/root/mylibs/')\n!ls /root/mylibs/detection\nfrom detection.detection_predict import get_img_predict_multi\nfrom detection.yolov5.models.experimental import attempt_load","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:36:44.581828Z","iopub.execute_input":"2022-02-09T14:36:44.582238Z","iopub.status.idle":"2022-02-09T14:36:46.689052Z","shell.execute_reply.started":"2022-02-09T14:36:44.582202Z","shell.execute_reply":"2022-02-09T14:36:46.688268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef format_prediction_str(bboxes):\n    annot = ''\n    if len(bboxes)>0:\n        for bb in bboxes:\n            annot += f'{bb[4]:.2f} {int(bb[0])} {int(bb[1])} {int(bb[2])} {int(bb[3])}'\n            annot +=' '\n        annot = annot.strip(' ')\n    return annot\n\ndef format_prediction(bboxes):\n    pred_strings = []\n    if len(bboxes)>0:\n        for bb in bboxes:\n            pred_strings.append(f'{bb[4]:.2f} {int(bb[0])} {int(bb[1])} {int(bb[2])} {int(bb[3])}')\n    return ' '.join(pred_strings)\n\ndef check_result(bboxes,shape):\n    if len(bboxes)==0:\n        return []\n    bboxes=np.array(bboxes)\n    bboxes=bboxes[bboxes[:,4]>CONF]\n    bboxes[bboxes<0]=0\n    bboxes=bboxes[(bboxes[:,0]+bboxes[:,2]) < shape[1]]\n    bboxes=bboxes[(bboxes[:,1]+bboxes[:,3]) < shape[0]]\n    bboxes=bboxes[bboxes[:,2]>MIN_SZ]\n    bboxes=bboxes[bboxes[:,3]>MIN_SZ]\n    return bboxes","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:36:46.691702Z","iopub.execute_input":"2022-02-09T14:36:46.692232Z","iopub.status.idle":"2022-02-09T14:36:46.701483Z","shell.execute_reply.started":"2022-02-09T14:36:46.692192Z","shell.execute_reply":"2022-02-09T14:36:46.700611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Run code","metadata":{}},{"cell_type":"code","source":"device='cuda:0'\n   \nmodels=[]\nfor model_name in MODEL_NAMES:\n    detect_model = attempt_load(model_name, map_location=device)\n    detect_model.eval()\n    models.append(detect_model.half())\n    # sizes.append(int(model_name.split('.')[0].split('_')[-1]))","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:36:46.702927Z","iopub.execute_input":"2022-02-09T14:36:46.703432Z","iopub.status.idle":"2022-02-09T14:36:48.766181Z","shell.execute_reply.started":"2022-02-09T14:36:46.703397Z","shell.execute_reply":"2022-02-09T14:36:48.765330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_paths = df[df.num_bbox>1].sample(100).image_path.tolist()\nfor idx, path in enumerate(tqdm(image_paths)):\n    img = cv2.imread(path)[...,::-1]\n    bb_pred = get_img_predict_multi(models, img,sizes,flips)\n    bb_pred = check_result(bb_pred,img.shape)\n    annot   = format_prediction(bb_pred)\n    print(idx,'---------',annot)\n    if idx>5:\n        break","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:36:48.769525Z","iopub.execute_input":"2022-02-09T14:36:48.769731Z","iopub.status.idle":"2022-02-09T14:37:03.091495Z","shell.execute_reply.started":"2022-02-09T14:36:48.769700Z","shell.execute_reply":"2022-02-09T14:37:03.090798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Run Test","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-02-09T14:37:03.093272Z","iopub.execute_input":"2022-02-09T14:37:03.093764Z","iopub.status.idle":"2022-02-09T14:37:03.119832Z","shell.execute_reply.started":"2022-02-09T14:37:03.093708Z","shell.execute_reply":"2022-02-09T14:37:03.118662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, (img, pred_df) in tqdm(enumerate(iter_test)):\n    #img=img[...,::-1]\n    try:\n        bb_pred = get_img_predict_multi(models,img, sizes, flips)\n        bb_pred = check_result(bb_pred,img.shape)\n        annot = format_prediction(bb_pred)\n    except:\n        annot = ''\n    pred_df['annotations'] = annot\n    env.predict(pred_df)\n    if idx<3:\n        print(annot)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T14:37:03.121087Z","iopub.status.idle":"2022-02-09T14:37:03.121503Z","shell.execute_reply.started":"2022-02-09T14:37:03.121278Z","shell.execute_reply":"2022-02-09T14:37:03.121300Z"},"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-02-09T14:37:03.122979Z","iopub.status.idle":"2022-02-09T14:37:03.124074Z","shell.execute_reply.started":"2022-02-09T14:37:03.123808Z","shell.execute_reply":"2022-02-09T14:37:03.123834Z"},"trusted":true},"execution_count":null,"outputs":[]}]}