{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":31703,"databundleVersionId":2871752,"sourceType":"competition"},{"sourceId":2869358,"sourceType":"datasetVersion","datasetId":1757219}],"dockerImageVersionId":30146,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\nfrom shutil import copyfile\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\nTRAIN_PATH = '/kaggle/input/tensorflow-great-barrier-reef'","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:31:51.600817Z","iopub.execute_input":"2023-12-24T23:31:51.601112Z","iopub.status.idle":"2023-12-24T23:31:51.609420Z","shell.execute_reply.started":"2023-12-24T23:31:51.601076Z","shell.execute_reply":"2023-12-24T23:31:51.608520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check Torch and CUDA version\nprint(f\"Torch: {torch.__version__}\")\n!nvcc --version","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:31:55.121789Z","iopub.execute_input":"2023-12-24T23:31:55.122493Z","iopub.status.idle":"2023-12-24T23:31:56.083864Z","shell.execute_reply.started":"2023-12-24T23:31:55.122447Z","shell.execute_reply":"2023-12-24T23:31:56.082970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 . \n!pip install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-12-24T23:31:56.085964Z","iopub.execute_input":"2023-12-24T23:31:56.086628Z","iopub.status.idle":"2023-12-24T23:33:20.113069Z","shell.execute_reply.started":"2023-12-24T23:31:56.086580Z","shell.execute_reply":"2023-12-24T23:33:20.112231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install protobuf==3.20.","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:33:20.114591Z","iopub.execute_input":"2023-12-24T23:33:20.114858Z","iopub.status.idle":"2023-12-24T23:33:30.899243Z","shell.execute_reply.started":"2023-12-24T23:33:20.114825Z","shell.execute_reply":"2023-12-24T23:33:30.898061Z"},"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":"2023-12-24T23:33:43.492017Z","iopub.execute_input":"2023-12-24T23:33:43.492852Z","iopub.status.idle":"2023-12-24T23:33:43.498053Z","shell.execute_reply.started":"2023-12-24T23:33:43.492811Z","shell.execute_reply":"2023-12-24T23:33:43.497313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:33:43.694812Z","iopub.execute_input":"2023-12-24T23:33:43.695445Z","iopub.status.idle":"2023-12-24T23:33:43.765481Z","shell.execute_reply.started":"2023-12-24T23:33:43.695394Z","shell.execute_reply":"2023-12-24T23:33:43.764700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Taken only annotated photos\ndf[\"num_bbox\"] = df['annotations'].apply(lambda x: str.count(x, 'x'))\ndf_train = df[df[\"num_bbox\"]>0]\n\n#Annotations \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)","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:33:43.844375Z","iopub.execute_input":"2023-12-24T23:33:43.844716Z","iopub.status.idle":"2023-12-24T23:33:47.900947Z","shell.execute_reply.started":"2023-12-24T23:33:43.844683Z","shell.execute_reply":"2023-12-24T23:33:47.900184Z"},"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":"2023-12-24T23:33:47.902340Z","iopub.execute_input":"2023-12-24T23:33:47.902578Z","iopub.status.idle":"2023-12-24T23:33:47.935045Z","shell.execute_reply.started":"2023-12-24T23:33:47.902549Z","shell.execute_reply":"2023-12-24T23:33:47.934313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HOME_DIR = '/kaggle/working/' \nDATASET_PATH = 'dataset/images'\n\n!mkdir {HOME_DIR}dataset\n!mkdir {HOME_DIR}{DATASET_PATH}\n!mkdir {HOME_DIR}{DATASET_PATH}/train2017\n!mkdir {HOME_DIR}{DATASET_PATH}/val2017\n!mkdir {HOME_DIR}{DATASET_PATH}/annotations","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:33:47.936336Z","iopub.execute_input":"2023-12-24T23:33:47.936637Z","iopub.status.idle":"2023-12-24T23:33:52.774968Z","shell.execute_reply.started":"2023-12-24T23:33:47.936601Z","shell.execute_reply":"2023-12-24T23:33:52.773910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SELECTED_FOLD = 4\n\nfor i in tqdm(range(len(df_train))):\n    row = df_train.loc[i]\n    if row.fold != SELECTED_FOLD:\n        copyfile(f'{row.image_path}', f'{HOME_DIR}{DATASET_PATH}/train2017/{row.image_id}.jpg')\n    else:\n        copyfile(f'{row.image_path}', f'{HOME_DIR}{DATASET_PATH}/val2017/{row.image_id}.jpg') ","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:33:52.777132Z","iopub.execute_input":"2023-12-24T23:33:52.777413Z","iopub.status.idle":"2023-12-24T23:34:43.561497Z","shell.execute_reply.started":"2023-12-24T23:33:52.777366Z","shell.execute_reply":"2023-12-24T23:34:43.560675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Number of training files: {len(os.listdir(f\"{HOME_DIR}{DATASET_PATH}/train2017/\"))}')\nprint(f'Number of validation files: {len(os.listdir(f\"{HOME_DIR}{DATASET_PATH}/val2017/\"))}')","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:34:43.562669Z","iopub.execute_input":"2023-12-24T23:34:43.562899Z","iopub.status.idle":"2023-12-24T23:34:43.571366Z","shell.execute_reply.started":"2023-12-24T23:34:43.562870Z","shell.execute_reply":"2023-12-24T23:34:43.570640Z"},"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":"2023-12-24T23:34:43.572590Z","iopub.execute_input":"2023-12-24T23:34:43.573247Z","iopub.status.idle":"2023-12-24T23:34:43.579687Z","shell.execute_reply.started":"2023-12-24T23:34:43.573208Z","shell.execute_reply":"2023-12-24T23:34:43.578881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotion_id = 0","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:34:43.580793Z","iopub.execute_input":"2023-12-24T23:34:43.581055Z","iopub.status.idle":"2023-12-24T23:34:43.589970Z","shell.execute_reply.started":"2023-12-24T23:34:43.581018Z","shell.execute_reply":"2023-12-24T23:34:43.589231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dataset2coco(df, dest_path):\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            \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            if (bbox[1] + bbox[3] > 720):\n                b_height = bbox[1] - 720 \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":"2023-12-24T23:34:43.590980Z","iopub.execute_input":"2023-12-24T23:34:43.591230Z","iopub.status.idle":"2023-12-24T23:34:43.606294Z","shell.execute_reply.started":"2023-12-24T23:34:43.591173Z","shell.execute_reply":"2023-12-24T23:34:43.605432Z"},"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], f\"{HOME_DIR}{DATASET_PATH}/train2017/\")\nval_annot_json = dataset2coco(df_train[df_train.fold == SELECTED_FOLD], f\"{HOME_DIR}{DATASET_PATH}/val2017/\")\n\n# Save converted annotations\nsave_annot_json(train_annot_json, f\"{HOME_DIR}{DATASET_PATH}/annotations/train.json\")\nsave_annot_json(val_annot_json, f\"{HOME_DIR}{DATASET_PATH}/annotations/valid.json\")","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:34:43.607317Z","iopub.execute_input":"2023-12-24T23:34:43.607569Z","iopub.status.idle":"2023-12-24T23:34:43.889890Z","shell.execute_reply.started":"2023-12-24T23:34:43.607540Z","shell.execute_reply":"2023-12-24T23:34:43.888973Z"},"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 = 0.33\n        self.width = 0.50\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/images\"\n        self.train_ann = \"train.json\"\n        self.val_ann = \"valid.json\"\n\n        self.num_classes = 1\n\n        self.max_epoch = $max_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 = (960, 960)\n        self.mosaic_scale = (0.5, 1.5)\n        self.random_size = (10, 20)\n        self.test_size = (960, 960)\n'''","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:34:43.892549Z","iopub.execute_input":"2023-12-24T23:34:43.892838Z","iopub.status.idle":"2023-12-24T23:34:43.898408Z","shell.execute_reply.started":"2023-12-24T23:34:43.892797Z","shell.execute_reply":"2023-12-24T23:34:43.897404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PIPELINE_CONFIG_PATH='cots_config.py'\n\npipeline = Template(config_file_template).substitute(max_epoch = 20)\n\nwith open(PIPELINE_CONFIG_PATH, 'w') as f:\n    f.write(pipeline)","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:34:43.899433Z","iopub.execute_input":"2023-12-24T23:34:43.899707Z","iopub.status.idle":"2023-12-24T23:34:44.289442Z","shell.execute_reply.started":"2023-12-24T23:34:43.899669Z","shell.execute_reply":"2023-12-24T23:34:44.288640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ./yolox/data/datasets/voc_classes.py\n\nvoc_cls = '''\nVOC_CLASSES = (\n  \"starfish\",\n)\n'''\nwith open('./yolox/data/datasets/voc_classes.py', 'w') as f:\n    f.write(voc_cls)\n\n# ./yolox/data/datasets/coco_classes.py\n\ncoco_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":"2023-12-24T23:34:44.290790Z","iopub.execute_input":"2023-12-24T23:34:44.291505Z","iopub.status.idle":"2023-12-24T23:34:45.263647Z","shell.execute_reply.started":"2023-12-24T23:34:44.291464Z","shell.execute_reply":"2023-12-24T23:34:45.262637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sh = 'wget https://github.com/Megvii-BaseDetection/storage/releases/download/0.0.1/yolox_s.pth'\nMODEL_FILE = 'yolox_s.pth'\n\n\nwith open('script.sh', 'w') as file:\n  file.write(sh)\n\n!bash script.sh","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:34:45.265308Z","iopub.execute_input":"2023-12-24T23:34:45.265593Z","iopub.status.idle":"2023-12-24T23:34:47.033483Z","shell.execute_reply.started":"2023-12-24T23:34:45.265560Z","shell.execute_reply":"2023-12-24T23:34:47.032667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp ./tools/train.py ./","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:34:47.035130Z","iopub.execute_input":"2023-12-24T23:34:47.035369Z","iopub.status.idle":"2023-12-24T23:34:47.998419Z","shell.execute_reply.started":"2023-12-24T23:34:47.035341Z","shell.execute_reply":"2023-12-24T23:34:47.997471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py \\\n    -f cots_config.py \\\n    -d 1 \\\n    -b 32 \\\n    --fp16 \\\n    -o \\\n    -c {MODEL_FILE}   # Remember to chenge this line if you take different model eg. yolo_nano.pth, yolox_s.pth or yolox_m.pth","metadata":{"execution":{"iopub.status.busy":"2023-12-24T23:34:47.999952Z","iopub.execute_input":"2023-12-24T23:34:48.000235Z","iopub.status.idle":"2023-12-25T01:35:57.265881Z","shell.execute_reply.started":"2023-12-24T23:34:48.000195Z","shell.execute_reply":"2023-12-25T01:35:57.265047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# I have to fix demo.py file because it:\n# - raises error in Kaggle (cvWaitKey does not work) \n# - saves result files in time named directory eg. /2021_11_29_22_51_08/ which is difficult then to automatically show results\n\n%cp ../../input/yolox-kaggle-fix-for-demo-inference/demo.py tools/demo.py","metadata":{"execution":{"iopub.status.busy":"2023-12-25T01:35:57.268438Z","iopub.execute_input":"2023-12-25T01:35:57.268717Z","iopub.status.idle":"2023-12-25T01:35:58.258777Z","shell.execute_reply.started":"2023-12-25T01:35:57.268685Z","shell.execute_reply":"2023-12-25T01:35:58.257549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_IMAGE_PATH = \"/kaggle/working/dataset/images/val2017/0-4610.jpg\"\nMODEL_PATH = \"./YOLOX_outputs/cots_config/best_ckpt.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","metadata":{"execution":{"iopub.status.busy":"2023-12-25T01:37:53.189764Z","iopub.execute_input":"2023-12-25T01:37:53.190049Z","iopub.status.idle":"2023-12-25T01:37:59.117839Z","shell.execute_reply.started":"2023-12-25T01:37:53.190018Z","shell.execute_reply":"2023-12-25T01:37:59.116770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/working/YOLOX/YOLOX_outputs/cots_config","metadata":{"execution":{"iopub.status.busy":"2023-12-25T01:37:59.119854Z","iopub.execute_input":"2023-12-25T01:37:59.120080Z","iopub.status.idle":"2023-12-25T01:38:00.082211Z","shell.execute_reply.started":"2023-12-25T01:37:59.120051Z","shell.execute_reply":"2023-12-25T01:38:00.081440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OUTPUT_IMAGE_PATH = \"./YOLOX_outputs/cots_config/vis_res/0-4610.jpg\" \nImage.open(OUTPUT_IMAGE_PATH)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T01:38:00.083796Z","iopub.execute_input":"2023-12-25T01:38:00.084067Z","iopub.status.idle":"2023-12-25T01:38:00.433501Z","shell.execute_reply.started":"2023-12-25T01:38:00.084034Z","shell.execute_reply":"2023-12-25T01:38:00.432679Z"},"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 = (960, 960)\nnum_classes = 1\nconfthre = 0.1\nnmsthre = 0.45\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":"2023-12-25T01:38:24.036255Z","iopub.execute_input":"2023-12-25T01:38:24.037083Z","iopub.status.idle":"2023-12-25T01:38:26.255317Z","shell.execute_reply.started":"2023-12-25T01:38:24.037039Z","shell.execute_reply":"2023-12-25T01:38:26.254560Z"},"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":"2023-12-25T01:38:27.320521Z","iopub.execute_input":"2023-12-25T01:38:27.321304Z","iopub.status.idle":"2023-12-25T01:38:27.330442Z","shell.execute_reply.started":"2023-12-25T01:38:27.321271Z","shell.execute_reply":"2023-12-25T01:38:27.329679Z"},"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":"2023-12-25T01:38:29.826281Z","iopub.execute_input":"2023-12-25T01:38:29.827040Z","iopub.status.idle":"2023-12-25T01:38:29.835186Z","shell.execute_reply.started":"2023-12-25T01:38:29.826998Z","shell.execute_reply":"2023-12-25T01:38:29.834437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_IMAGE_PATH = \"/kaggle/working/dataset/images/val2017/0-4610.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":"2023-12-25T01:39:17.280540Z","iopub.execute_input":"2023-12-25T01:39:17.281227Z","iopub.status.idle":"2023-12-25T01:39:17.657094Z","shell.execute_reply.started":"2023-12-25T01:39:17.281187Z","shell.execute_reply":"2023-12-25T01:39:17.656008Z"},"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":"2023-12-24T23:13:42.218212Z","iopub.status.idle":"2023-12-24T23:13:42.218534Z","shell.execute_reply.started":"2023-12-24T23:13:42.218364Z","shell.execute_reply":"2023-12-24T23:13:42.218386Z"},"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":"2023-12-24T23:13:42.219963Z","iopub.status.idle":"2023-12-24T23:13:42.220258Z","shell.execute_reply.started":"2023-12-24T23:13:42.220103Z","shell.execute_reply":"2023-12-24T23:13:42.220118Z"},"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":"2023-12-24T23:13:42.221252Z","iopub.status.idle":"2023-12-24T23:13:42.221556Z","shell.execute_reply.started":"2023-12-24T23:13:42.221395Z","shell.execute_reply":"2023-12-24T23:13:42.221410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}