{"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":"# 总结\n这个比赛使用了典型的目标检测算法YOLO，分为以下几个步骤：\n\n\n1、数据预处理，得到以下几种数据，目的时得到coco数据集样式的数据。\n\n2、分别用各种YOLO版本进行训练。\n\n3、测试及提交。","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-14T06:17:26.875941Z","iopub.execute_input":"2022-01-14T06:17:26.876232Z","iopub.status.idle":"2022-01-14T06:17:26.882495Z","shell.execute_reply.started":"2022-01-14T06:17:26.876203Z","shell.execute_reply":"2022-01-14T06:17:26.881165Z"}}},{"cell_type":"markdown","source":"# 1、数据预处理","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport ast\nimport os\nimport json\nimport pandas as pd\nimport torch\nimport importlib\nimport cv2 \n\nimport shutil\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nfrom sklearn.model_selection import GroupKFold\nfrom PIL import Image\nfrom string import Template\nfrom IPython.display import display\n","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:40:58.66532Z","iopub.execute_input":"2022-01-18T09:40:58.665635Z","iopub.status.idle":"2022-01-18T09:40:58.675906Z","shell.execute_reply.started":"2022-01-18T09:40:58.665601Z","shell.execute_reply":"2022-01-18T09:40:58.674817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = \"/kaggle/input/tensorflow-great-barrier-reef\"\nWORKING_PATH = \"/kaggle/working\"\nTRAIN_CSV_PATH = os.path.join(TRAIN_PATH, \"train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:40:58.679563Z","iopub.execute_input":"2022-01-18T09:40:58.680666Z","iopub.status.idle":"2022-01-18T09:40:58.687301Z","shell.execute_reply.started":"2022-01-18T09:40:58.680573Z","shell.execute_reply":"2022-01-18T09:40:58.686035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(TRAIN_CSV_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:40:58.689367Z","iopub.execute_input":"2022-01-18T09:40:58.689773Z","iopub.status.idle":"2022-01-18T09:40:58.746223Z","shell.execute_reply.started":"2022-01-18T09:40:58.689725Z","shell.execute_reply":"2022-01-18T09:40:58.745156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_bbox(annots):\n    bboxes = [list(annot.values()) for annot in annots]\n    return bboxes\n\ndef get_path(row):\n    row['image_path'] = f'{TRAIN_PATH}/train_images/video_{row.video_id}/{row.video_frame}.jpg'\n    return row","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:40:58.74805Z","iopub.execute_input":"2022-01-18T09:40:58.748511Z","iopub.status.idle":"2022-01-18T09:40:58.755473Z","shell.execute_reply.started":"2022-01-18T09:40:58.748462Z","shell.execute_reply":"2022-01-18T09:40:58.75415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Taken only annotated photos\n# num_bbox表示bbox地数量,计数方法是计算‘x’字符地数量\ndf[\"num_bbox\"] = df['annotations'].apply(lambda x: str.count(x, 'x'))\ndf_train = df[df[\"num_bbox\"]>0]\n#Annotations \n#progress_apply作用同apply,不过可以监视执行地进度\n#ast.literal_eval做string与list,tuple,dict之间的类型转换\ndf_train['annotations'] = df_train['annotations'].progress_apply(lambda x: ast.literal_eval(x))\ndf_train['bboxes'] = df_train.annotations.progress_apply(get_bbox)\n\n#Images resolution\ndf_train[\"width\"] = 1280\ndf_train[\"height\"] = 720\n\n#Path of images\ndf_train = df_train.progress_apply(get_path, axis=1)\ndf_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:40:58.758643Z","iopub.execute_input":"2022-01-18T09:40:58.759306Z","iopub.status.idle":"2022-01-18T09:41:04.291371Z","shell.execute_reply.started":"2022-01-18T09:40:58.75926Z","shell.execute_reply":"2022-01-18T09:41:04.290188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kf = GroupKFold(n_splits = 5) \ndf_train = df_train.reset_index(drop=True)\ndf_train['fold'] = -1\nfor fold, (train_idx, val_idx) in enumerate(kf.split(df_train, y = df_train.video_id.tolist(), groups=df_train.sequence)):\n    df_train.loc[val_idx, 'fold'] = fold\n\ndf_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:04.293779Z","iopub.execute_input":"2022-01-18T09:41:04.294133Z","iopub.status.idle":"2022-01-18T09:41:04.333466Z","shell.execute_reply.started":"2022-01-18T09:41:04.294066Z","shell.execute_reply":"2022-01-18T09:41:04.332471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dst_data_dir = os.path.join(WORKING_PATH, \"dataset\")\nif not os.path.isdir(dst_data_dir):\n    os.mkdir(dst_data_dir)\nimages_dir = os.path.join(dst_data_dir, \"images\")\nif not os.path.isdir(images_dir):\n    os.mkdir(images_dir)\ntrain_images_dir = os.path.join(dst_data_dir, \"train2017\")\nif not os.path.isdir(train_images_dir):\n    os.mkdir(train_images_dir)\nval_images_dir = os.path.join(dst_data_dir, \"val2017\")\nif not os.path.isdir(val_images_dir):\n    os.mkdir(val_images_dir)\nlabels_dir = os.path.join(dst_data_dir, \"labels\")\nif not os.path.isdir(labels_dir):\n    os.mkdir(labels_dir)\ntrain_lebels_dir = os.path.join(labels_dir, \"train\")\nif not os.path.isdir(train_lebels_dir):\n    os.mkdir(train_lebels_dir)\nval_lebels_dir = os.path.join(labels_dir, \"val\")\nif not os.path.isdir(val_lebels_dir):\n    os.mkdir(val_lebels_dir)\nannotations_dir = os.path.join(dst_data_dir, \"annotations\")\nif not os.path.isdir(annotations_dir):\n    os.mkdir(annotations_dir)","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:04.335357Z","iopub.execute_input":"2022-01-18T09:41:04.335592Z","iopub.status.idle":"2022-01-18T09:41:04.348365Z","shell.execute_reply.started":"2022-01-18T09:41:04.335549Z","shell.execute_reply":"2022-01-18T09:41:04.347176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def write_labels(dst_labels_path, bboxes):\n    fw = open(dst_labels_path, 'w')\n    for item in bboxes:\n        x = (item[0] + item[2] / 2.0) / 1280.0\n        y = (item[1] + item[3] / 2.0) / 720.0\n        w = item[2] / 1280.0\n        h = item[3] / 720.0\n        fw.write(\"0 %f %f %f %f\\n\" % (x, y, w, h))\n    fw.close()","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:04.350264Z","iopub.execute_input":"2022-01-18T09:41:04.351509Z","iopub.status.idle":"2022-01-18T09:41:04.361447Z","shell.execute_reply.started":"2022-01-18T09:41:04.351462Z","shell.execute_reply":"2022-01-18T09:41:04.360324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SELECTED_FOLD = 4\nfw_train = open(os.path.join(dst_data_dir, \"train.txt\"), 'w')\nfw_val = open(os.path.join(dst_data_dir, \"val.txt\"), 'w')\n\n#tqdm进度条\nfor i in tqdm(range(len(df_train))):\n    row = df_train.loc[i]\n    if row.fold != SELECTED_FOLD:\n        dst_image_path = os.path.join(train_images_dir, row.image_id + \".jpg\")\n        fw_train.write(dst_image_path + \"\\n\")\n        dst_labels_path = os.path.join(train_lebels_dir, row.image_id + \".txt\")\n    else:\n        dst_image_path = os.path.join(val_images_dir, row.image_id + \".jpg\")\n        fw_val.write(dst_image_path + \"\\n\")\n        dst_labels_path = os.path.join(val_lebels_dir, row.image_id + \".txt\")\n    shutil.copyfile(row.image_path, dst_image_path)\n    write_labels(dst_labels_path, row.bboxes)\nfw_train.close()\nfw_val.close()","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:04.3666Z","iopub.execute_input":"2022-01-18T09:41:04.36727Z","iopub.status.idle":"2022-01-18T09:41:21.131045Z","shell.execute_reply.started":"2022-01-18T09:41:04.36719Z","shell.execute_reply":"2022-01-18T09:41:21.128809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_annot_json(json_annotation, filename):\n    with open(filename, 'w') as f:\n        output_json = json.dumps(json_annotation)\n        f.write(output_json)","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.131882Z","iopub.status.idle":"2022-01-18T09:41:21.132274Z","shell.execute_reply.started":"2022-01-18T09:41:21.132065Z","shell.execute_reply":"2022-01-18T09:41:21.132089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotion_id = 0","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.133826Z","iopub.status.idle":"2022-01-18T09:41:21.134842Z","shell.execute_reply.started":"2022-01-18T09:41:21.134509Z","shell.execute_reply":"2022-01-18T09:41:21.13454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dataset2coco(df):\n    \n    global annotion_id\n    \n    annotations_json = {\n        \"info\": [],\n        \"licenses\": [],\n        \"categories\": [],\n        \"images\": [],\n        \"annotations\": []\n    }\n    \n    info = {\n        \"year\": \"2021\",\n        \"version\": \"1\",\n        \"description\": \"COTS dataset - COCO format\",\n        \"contributor\": \"\",\n        \"url\": \"https://kaggle.com\",\n        \"date_created\": \"2021-11-30T15:01:26+00:00\"\n    }\n    annotations_json[\"info\"].append(info)\n    \n    lic = {\n            \"id\": 1,\n            \"url\": \"\",\n            \"name\": \"Unknown\"\n        }\n    annotations_json[\"licenses\"].append(lic)\n\n    classes = {\"id\": 0, \"name\": \"starfish\", \"supercategory\": \"none\"}\n\n    annotations_json[\"categories\"].append(classes)\n\n    \n    for ann_row in df.itertuples():\n        images = {\n            \"id\": ann_row[0],\n            \"license\": 1,\n            \"file_name\": ann_row.image_id + '.jpg',\n            \"height\": ann_row.height,\n            \"width\": ann_row.width,\n            \"date_captured\": \"2021-11-30T15:01:26+00:00\"\n        }\n        \n        annotations_json[\"images\"].append(images)\n        \n        bbox_list = ann_row.bboxes\n        \n        for bbox in bbox_list:\n            b_width = bbox[2]\n            b_height = bbox[3]\n            \n            # some boxes in COTS are outside the image height and width\n            if (bbox[0] + bbox[2] > 1280):\n                #改b_width = bbox[0] - 1280 \n                b_width = 1280 -bbox[0]\n            if (bbox[1] + bbox[3] > 720):\n                b_height = 720 - bbox[1] \n                \n            image_annotations = {\n                \"id\": annotion_id,\n                \"image_id\": ann_row[0],\n                \"category_id\": 0,\n                \"bbox\": [bbox[0], bbox[1], b_width, b_height],\n                \"area\": bbox[2] * bbox[3],\n                \"segmentation\": [],\n                \"iscrowd\": 0\n            }\n            \n            annotion_id += 1\n            annotations_json[\"annotations\"].append(image_annotations)\n        \n        \n    print(f\"Dataset COTS annotation to COCO json format completed! Files: {len(df)}\")\n    return annotations_json","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.136755Z","iopub.status.idle":"2022-01-18T09:41:21.138548Z","shell.execute_reply.started":"2022-01-18T09:41:21.138182Z","shell.execute_reply":"2022-01-18T09:41:21.138214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert COTS dataset to JSON COCO\ntrain_annot_json = dataset2coco(df_train[df_train.fold != SELECTED_FOLD])\nval_annot_json = dataset2coco(df_train[df_train.fold == SELECTED_FOLD])\n\n# Save converted annotations\nsave_annot_json(train_annot_json, os.path.join(annotations_dir, \"train.json\"))\nsave_annot_json(val_annot_json, os.path.join(annotations_dir, \"valid.json\"))","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.140082Z","iopub.status.idle":"2022-01-18T09:41:21.141332Z","shell.execute_reply.started":"2022-01-18T09:41:21.141011Z","shell.execute_reply":"2022-01-18T09:41:21.141043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2、训练YOLO","metadata":{}},{"cell_type":"markdown","source":"训练yolox","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/Megvii-BaseDetection/YOLOX -q\n\n%cd YOLOX\n!pip install -U pip && pip install -r requirements.txt\n!pip install -v -e . ","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.142749Z","iopub.status.idle":"2022-01-18T09:41:21.143967Z","shell.execute_reply.started":"2022-01-18T09:41:21.143682Z","shell.execute_reply":"2022-01-18T09:41:21.143712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.14547Z","iopub.status.idle":"2022-01-18T09:41:21.14653Z","shell.execute_reply.started":"2022-01-18T09:41:21.146205Z","shell.execute_reply":"2022-01-18T09:41:21.146234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config_file_template = '''\n\n#!/usr/bin/env python3\n# -*- coding:utf-8 -*-\n# Copyright (c) Megvii, Inc. and its affiliates.\n\nimport os\n\nfrom yolox.exp import Exp as MyExp\n\n\nclass Exp(MyExp):\n    def __init__(self):\n        super(Exp, self).__init__()\n        self.depth = 1 #网络深度\n        self.width = 1 #网络高度\n        self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(\".\")[0]\n        \n        # Define yourself dataset path\n        self.data_dir = \"/kaggle/working/dataset\" #数据路径\n        self.train_ann = \"train.json\" #训练集annotation路径\n        self.val_ann = \"valid.json\" #验证机annotation路径\n\n        self.num_classes = 1 #类别数目\n\n        self.max_epoch = $max_epoch #最大epoch数量\n        self.data_num_workers = 2 #\n        self.eval_interval = 1\n        \n        self.mosaic_prob = 1.0\n        self.mixup_prob = 1.0\n        self.hsv_prob = 1.0\n        self.flip_prob = 0.5\n        self.no_aug_epochs = 2\n        \n        self.input_size = (800, 1280)\n        self.mosaic_scale = (0.5, 1.5)\n        self.random_size = (10, 20)\n        self.test_size = (800, 1280)\n'''\n\nPIPELINE_CONFIG_PATH='cots_config.py'\n\npipeline = Template(config_file_template).substitute(max_epoch = 10)\n\nwith open(PIPELINE_CONFIG_PATH, 'w') as f:\n    f.write(pipeline)","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.148336Z","iopub.status.idle":"2022-01-18T09:41:21.149739Z","shell.execute_reply.started":"2022-01-18T09:41:21.149388Z","shell.execute_reply":"2022-01-18T09:41:21.149419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/models/yx_l_003.pth ./","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:29.72287Z","iopub.execute_input":"2022-01-18T09:41:29.723409Z","iopub.status.idle":"2022-01-18T09:41:29.889967Z","shell.execute_reply.started":"2022-01-18T09:41:29.723375Z","shell.execute_reply":"2022-01-18T09:41:29.888856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coco_cls = '''\nCOCO_CLASSES = (\n  \"starfish\",\n)\n'''\nwith open('./yolox/data/datasets/coco_classes.py', 'w') as f:\n    f.write(coco_cls)\n\n# check if everything is ok    \n!more ./yolox/data/datasets/coco_classes.py","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.154202Z","iopub.status.idle":"2022-01-18T09:41:21.155289Z","shell.execute_reply.started":"2022-01-18T09:41:21.154877Z","shell.execute_reply":"2022-01-18T09:41:21.154911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sh = 'wget https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_l.pth'\nMODEL_FILE = 'yx_l_003.pth'\n\nwith open('script.sh', 'w') as file:\n    file.write(sh)\n\n!bash script.sh","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.157145Z","iopub.status.idle":"2022-01-18T09:41:21.158002Z","shell.execute_reply.started":"2022-01-18T09:41:21.157697Z","shell.execute_reply":"2022-01-18T09:41:21.157736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp ./tools/train.py ./","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.159796Z","iopub.status.idle":"2022-01-18T09:41:21.160868Z","shell.execute_reply.started":"2022-01-18T09:41:21.160566Z","shell.execute_reply":"2022-01-18T09:41:21.1606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py \\\n    -f cots_config.py \\\n    -d 1 \\\n    -b 8 \\\n    --fp16 \\\n    -o \\\n    -c {MODEL_FILE}","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.162535Z","iopub.status.idle":"2022-01-18T09:41:21.163352Z","shell.execute_reply.started":"2022-01-18T09:41:21.16303Z","shell.execute_reply":"2022-01-18T09:41:21.163061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cp ../../input/yolox-kaggle-fix-for-demo-inference/demo.py tools/demo.py","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.165041Z","iopub.status.idle":"2022-01-18T09:41:21.165973Z","shell.execute_reply.started":"2022-01-18T09:41:21.165602Z","shell.execute_reply":"2022-01-18T09:41:21.165648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nTEST_IMAGE_PATH = \"/kaggle/working/dataset/train2017/0-1000.jpg\"\nMODEL_PATH = \"./yx_l_003.pth\"\n\n!python tools/demo.py image \\\n    -f cots_config.py \\\n    -c {MODEL_PATH} \\\n    --path {TEST_IMAGE_PATH} \\\n    --conf 0.1 \\\n    --nms 0.45 \\\n    --tsize 960 \\\n    --save_result \\\n    --device gpu\n'''\n","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.167952Z","iopub.status.idle":"2022-01-18T09:41:21.168889Z","shell.execute_reply.started":"2022-01-18T09:41:21.168508Z","shell.execute_reply":"2022-01-18T09:41:21.168578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#OUTPUT_IMAGE_PATH = \"./YOLOX_outputs/cots_config/vis_res/0-1000.jpg\" \n#Image.open(OUTPUT_IMAGE_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.170546Z","iopub.status.idle":"2022-01-18T09:41:21.171393Z","shell.execute_reply.started":"2022-01-18T09:41:21.17106Z","shell.execute_reply":"2022-01-18T09:41:21.17112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yolox.utils import postprocess\nfrom yolox.data.data_augment import ValTransform\n\nCOCO_CLASSES = (\n  \"starfish\",\n)\n\n# get YOLOX experiment\ncurrent_exp = importlib.import_module('cots_config')\nexp = current_exp.Exp()\n\n# set inference parameters\ntest_size = (800, 1280)\nnum_classes = 1\nconfthre = 0.1\nnmsthre = 0.4\n\n\n# get YOLOX model\nmodel = exp.get_model()\nmodel.cuda()\nmodel.eval()\n\n# get custom trained checkpoint\nckpt_file = \"./YOLOX_outputs/cots_config/best_ckpt.pth\"\nckpt = torch.load(ckpt_file, map_location=\"cpu\")\nmodel.load_state_dict(ckpt[\"model\"])","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.17308Z","iopub.status.idle":"2022-01-18T09:41:21.173934Z","shell.execute_reply.started":"2022-01-18T09:41:21.173627Z","shell.execute_reply":"2022-01-18T09:41:21.17366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def yolox_inference(img, model, test_size): \n    bboxes = []\n    bbclasses = []\n    scores = []\n    \n    preproc = ValTransform(legacy = False)\n\n    tensor_img, _ = preproc(img, None, test_size)\n    tensor_img = torch.from_numpy(tensor_img).unsqueeze(0)\n    tensor_img = tensor_img.float()\n    tensor_img = tensor_img.cuda()\n\n    with torch.no_grad():\n        outputs = model(tensor_img)\n        outputs = postprocess(\n                    outputs, num_classes, confthre,\n                    nmsthre, class_agnostic=True\n                )\n\n    if outputs[0] is None:\n        return [], [], []\n    \n    outputs = outputs[0].cpu()\n    bboxes = outputs[:, 0:4]\n\n    bboxes /= min(test_size[0] / img.shape[0], test_size[1] / img.shape[1])\n    bbclasses = outputs[:, 6]\n    scores = outputs[:, 4] * outputs[:, 5]\n    \n    return bboxes, bbclasses, scores","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.175621Z","iopub.status.idle":"2022-01-18T09:41:21.176468Z","shell.execute_reply.started":"2022-01-18T09:41:21.176173Z","shell.execute_reply":"2022-01-18T09:41:21.176205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_yolox_predictions(img, bboxes, scores, bbclasses, confthre, classes_dict):\n    for i in range(len(bboxes)):\n            box = bboxes[i]\n            cls_id = int(bbclasses[i])\n            score = scores[i]\n            if score < confthre:\n                continue\n            x0 = int(box[0])\n            y0 = int(box[1])\n            x1 = int(box[2])\n            y1 = int(box[3])\n\n            cv2.rectangle(img, (x0, y0), (x1, y1), (0, 255, 0), 2)\n            cv2.putText(img, '{}:{:.1f}%'.format(classes_dict[cls_id], score * 100), (x0, y0 - 3), cv2.FONT_HERSHEY_PLAIN, 0.8, (0,255,0), thickness = 1)\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.178146Z","iopub.status.idle":"2022-01-18T09:41:21.179028Z","shell.execute_reply.started":"2022-01-18T09:41:21.178695Z","shell.execute_reply":"2022-01-18T09:41:21.178727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_IMAGE_PATH = \"/kaggle/working/dataset/val2017/0-4614.jpg\"\nimg = cv2.imread(TEST_IMAGE_PATH)\n\n# Get predictions\nbboxes, bbclasses, scores = yolox_inference(img, model, test_size)\n\n# Draw predictions\nout_image = draw_yolox_predictions(img, bboxes, scores, bbclasses, confthre, COCO_CLASSES)\n\n# Since we load image using OpenCV we have to convert it \nout_image = cv2.cvtColor(out_image, cv2.COLOR_BGR2RGB)\ndisplay(Image.fromarray(out_image))","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.180686Z","iopub.status.idle":"2022-01-18T09:41:21.181581Z","shell.execute_reply.started":"2022-01-18T09:41:21.181233Z","shell.execute_reply":"2022-01-18T09:41:21.181266Z"},"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-18T09:41:21.183272Z","iopub.status.idle":"2022-01-18T09:41:21.184126Z","shell.execute_reply.started":"2022-01-18T09:41:21.183794Z","shell.execute_reply":"2022-01-18T09:41:21.183827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_dict = {\n    'id': [],\n    'prediction_string': [],\n}\n\nfor (image_np, sample_prediction_df) in iter_test:\n \n    bboxes, bbclasses, scores = yolox_inference(image_np, model, test_size)\n    \n    predictions = []\n    for i in range(len(bboxes)):\n        box = bboxes[i]\n        cls_id = int(bbclasses[i])\n        score = scores[i]\n        if score < confthre:\n            continue\n        x_min = int(box[0])\n        y_min = int(box[1])\n        x_max = int(box[2])\n        y_max = int(box[3])\n        \n        bbox_width = x_max - x_min\n        bbox_height = y_max - y_min\n        \n        predictions.append('{:.2f} {} {} {} {}'.format(score, x_min, y_min, bbox_width, bbox_height))\n    \n    prediction_str = ' '.join(predictions)\n    sample_prediction_df['annotations'] = prediction_str\n    env.predict(sample_prediction_df)\n\n    print('Prediction:', prediction_str)","metadata":{"execution":{"iopub.status.busy":"2022-01-18T09:41:21.185849Z","iopub.status.idle":"2022-01-18T09:41:21.186735Z","shell.execute_reply.started":"2022-01-18T09:41:21.186417Z","shell.execute_reply":"2022-01-18T09:41:21.18645Z"},"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-18T09:41:21.188468Z","iopub.status.idle":"2022-01-18T09:41:21.189347Z","shell.execute_reply.started":"2022-01-18T09:41:21.189002Z","shell.execute_reply":"2022-01-18T09:41:21.189036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}