{"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":"!mkdir -p /root/.config/Ultralytics\n!cp /kaggle/input/yolov5-font/Arial.ttf /root/.config/Ultralytics/","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:18:42.386749Z","iopub.execute_input":"2022-02-06T01:18:42.387079Z","iopub.status.idle":"2022-02-06T01:18:43.749435Z","shell.execute_reply.started":"2022-02-06T01:18:42.387007Z","shell.execute_reply":"2022-02-06T01:18:43.748465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/yolox-wheel-only/loguru-0.5.3-py3-none-any.whl\n!pip install ../input/yolox-wheel-only/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/yolox-wheel-only/tabulate-0.8.9-py3-none-any.whl\n!pip install ../input/yolox-wheel-only/thop-0.0.31.post2005241907-py3-none-any.whl\n!pip install ../input/yolox-wheel-only/yolox-0.1.0-cp37-cp37m-linux_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:18:43.751529Z","iopub.execute_input":"2022-02-06T01:18:43.751763Z","iopub.status.idle":"2022-02-06T01:21:01.371215Z","shell.execute_reply.started":"2022-02-06T01:18:43.751735Z","shell.execute_reply":"2022-02-06T01:21:01.370226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/mmdet-wheel-1317/terminaltables/dist/terminaltables-3.1.0-py3-none-any.whl\n!pip install ../input/mmdet-wheel-1317/addict-2.4.0-py3-none-any.whl\n!pip install ../input/mmdet-wheel-1317/yapf-0.31.0-py2.py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:21:01.374666Z","iopub.execute_input":"2022-02-06T01:21:01.374896Z","iopub.status.idle":"2022-02-06T01:22:24.083542Z","shell.execute_reply.started":"2022-02-06T01:21:01.374866Z","shell.execute_reply":"2022-02-06T01:22:24.082712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"!pip install ../input/mmdet-only-wheel/mmdetection-master/dist/mmdet-2.18.0-py3-none-any.whl","metadata":{}},{"cell_type":"code","source":"!pip install ../input/mmdet-wheel-1317/mmcv-full-1.3.17/dist/mmcv_full-1.3.17-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/mmdet-only-wheel/mmdetection-master/pycocotools-2.0.2/dist/pycocotools-2.0.2-cp37-cp37m-linux_x86_64.whl\n","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:22:24.08735Z","iopub.execute_input":"2022-02-06T01:22:24.087568Z","iopub.status.idle":"2022-02-06T01:23:21.247104Z","shell.execute_reply.started":"2022-02-06T01:22:24.087538Z","shell.execute_reply":"2022-02-06T01:23:21.246067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/mmdet-convnext-only-wheel/mmdetection-ConvNeXt/dist/mmdet-2.20.0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:23:21.24859Z","iopub.execute_input":"2022-02-06T01:23:21.248874Z","iopub.status.idle":"2022-02-06T01:23:49.811996Z","shell.execute_reply.started":"2022-02-06T01:23:21.248828Z","shell.execute_reply":"2022-02-06T01:23:49.811184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\nimport timm","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:23:49.815396Z","iopub.execute_input":"2022-02-06T01:23:49.815613Z","iopub.status.idle":"2022-02-06T01:23:56.930387Z","shell.execute_reply.started":"2022-02-06T01:23:49.815585Z","shell.execute_reply":"2022-02-06T01:23:56.929485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/ensemble-boxes/ensemble_boxes-1.0.7-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:23:56.933065Z","iopub.execute_input":"2022-02-06T01:23:56.933492Z","iopub.status.idle":"2022-02-06T01:24:24.369545Z","shell.execute_reply.started":"2022-02-06T01:23:56.933451Z","shell.execute_reply":"2022-02-06T01:24:24.368709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ensemble_boxes import *","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:24.371167Z","iopub.execute_input":"2022-02-06T01:24:24.371447Z","iopub.status.idle":"2022-02-06T01:24:24.933879Z","shell.execute_reply.started":"2022-02-06T01:24:24.371407Z","shell.execute_reply":"2022-02-06T01:24:24.932976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"%cd mmdetection-master","metadata":{}},{"cell_type":"code","source":"from tqdm.notebook import tqdm\nimport pandas as pd\nimport glob\nimport numpy as np\nimport random\nimport os, shutil\nimport json, cv2","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:24.935079Z","iopub.execute_input":"2022-02-06T01:24:24.937359Z","iopub.status.idle":"2022-02-06T01:24:25.106534Z","shell.execute_reply.started":"2022-02-06T01:24:24.93732Z","shell.execute_reply":"2022-02-06T01:24:25.10584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check pytorch installation: \nimport torch, torchvision\nprint(torch.__version__, torch.cuda.is_available())","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:25.109943Z","iopub.execute_input":"2022-02-06T01:24:25.110212Z","iopub.status.idle":"2022-02-06T01:24:25.158604Z","shell.execute_reply.started":"2022-02-06T01:24:25.110176Z","shell.execute_reply":"2022-02-06T01:24:25.157449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport random\nimport os\ndef fix_seed(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n\nSEED = 42\nfix_seed(SEED)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:25.160314Z","iopub.execute_input":"2022-02-06T01:24:25.160851Z","iopub.status.idle":"2022-02-06T01:24:25.170729Z","shell.execute_reply.started":"2022-02-06T01:24:25.160802Z","shell.execute_reply":"2022-02-06T01:24:25.169971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## helper","metadata":{}},{"cell_type":"code","source":"def 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-06T01:24:25.172144Z","iopub.execute_input":"2022-02-06T01:24:25.172596Z","iopub.status.idle":"2022-02-06T01:24:25.180259Z","shell.execute_reply.started":"2022-02-06T01:24:25.17256Z","shell.execute_reply":"2022-02-06T01:24:25.179523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### yolov5","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm\nimport json\nimport cv2\nimport torch\nimport numpy as np\n\nclass Yolov5Predictor():\n    def __init__(self, hub_path, weight_path, test_size, test_conf, augment=False):\n\n        self.model = torch.hub.load(hub_path, 'custom', path=weight_path,source='local',force_reload=True)\n        self.model.conf = 0.01\n        self.test_size = test_size\n        self.test_conf = test_conf\n        self.augment=augment\n    def predict(self, img):\n        \"\"\"\n        def pandas(self):\n            # return detections as pandas DataFrames, i.e. print(results.pandas().xyxy[0])\n            new = copy(self)  # return copy\n            ca = 'xmin', 'ymin', 'xmax', 'ymax', 'confidence', 'class', 'name'  # xyxy columns\n            cb = 'xcenter', 'ycenter', 'width', 'height', 'confidence', 'class', 'name'  # xywh columns\n            for k, c in zip(['xyxy', 'xyxyn', 'xywh', 'xywhn'], [ca, ca, cb, cb]):\n                a = [[x[:5] + [int(x[5]), self.names[int(x[5])]] for x in x.tolist()] for x in getattr(self, k)]  # update\n                setattr(new, k, [pd.DataFrame(x, columns=c) for x in a])\n            return new\n        \"\"\"\n\n        r = self.model(img, size=self.test_size,augment=self.augment)\n        \n\n        if r.pandas().xyxy[0].shape[0] == 0:\n            bboxes, confs = [], []\n            return bboxes, confs\n\n        df = r.pandas().xyxy[0]\n        bbox_cols = ['xmin','ymin','xmax','ymax']\n        bboxes = df[bbox_cols].values\n        confs = df['confidence'].values\n\n        idxs = (confs >= self.test_conf)\n        bboxes = bboxes[idxs, :]\n        confs = confs[idxs]\n\n        return bboxes, confs\n    def xyxy_to_xywh(self, bboxes):\n        if len(bboxes) == 0:\n            # bboxes = []\n            return bboxes\n            \n        bboxes = bboxes.astype(int)\n        left = bboxes[:,0]\n        top = bboxes[:,1]\n        right = bboxes[:, 2]\n        bottom = bboxes[:, 3]\n\n        top = np.clip(top, 0, 720)\n        left = np.clip(left, 0, 1280)\n        bottom = np.clip(bottom, 0, 720)\n        right = np.clip(right, 0, 1280)\n        height = bottom - top\n        width = right - left\n        \n        bboxes = np.vstack([left,top,width,height]).T\n        \n        return bboxes\n        \n    def get_annot(self, img_rgb):\n        bboxes, confs = self.predict(img_rgb)\n        bboxes = self.xyxy_to_xywh(bboxes)\n        annot = format_prediction(bboxes, confs)\n        return annot","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:25.181542Z","iopub.execute_input":"2022-02-06T01:24:25.181903Z","iopub.status.idle":"2022-02-06T01:24:25.196086Z","shell.execute_reply.started":"2022-02-06T01:24:25.181867Z","shell.execute_reply":"2022-02-06T01:24:25.195168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## yolox","metadata":{}},{"cell_type":"code","source":"import cv2\nfrom yolox.exp import get_exp\nimport importlib\nimport torch\nimport numpy as np\nimport torch\n\nfrom yolox.data.data_augment import ValTransform\nfrom yolox.data.datasets import COCO_CLASSES\nfrom yolox.exp import get_exp\nfrom yolox.utils import fuse_model, get_model_info, postprocess, vis\n\n\nclass YOLOXPredictor:\n    def __init__(self, exp_name, ckpt_path, test_size=None):\n        self.test_size = test_size\n        self.exp = self.get_exp(exp_name)\n        self.model = self.get_model(ckpt_path)\n\n    def get_exp(self, exp_name):\n        # get YOLOX experiment\n        current_exp = importlib.import_module(exp_name)\n        exp = current_exp.Exp()\n        \n        # 追加\n        if self.test_size is not None:\n            exp.test_size = self.test_size\n\n        print(exp.test_size)\n        return exp\n\n    def get_model(self, ckpt_file):\n        # get YOLOX model\n        model = self.exp.get_model()\n        model.cuda()\n        model.half()\n        model.eval()\n\n        # get custom trained checkpoint\n        ckpt = torch.load(ckpt_file, map_location=\"cpu\")\n        model.load_state_dict(ckpt[\"model\"])\n\n        return model\n\n    def get_outputs(self, img):\n        \"\"\"\n        img: bgr\n        \"\"\"\n\n        preproc = ValTransform(legacy=False)\n\n        tensor_img, _ = preproc(img, None, self.exp.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        tensor_img = tensor_img.half()\n\n        with torch.no_grad():\n            outputs = self.model(tensor_img)\n            outputs = postprocess(\n                outputs, self.exp.num_classes, self.exp.test_conf,\n                self.exp.nmsthre, class_agnostic=True\n            )\n\n        return outputs\n\n    def outputs_to_xyxy_confs(self, img, outputs):\n        # 追加\n        outputs = outputs[0]\n        if outputs is None:\n            bboxes, confs = [], []\n            return bboxes, confs\n\n        outputs = outputs.cpu().numpy()\n\n        bboxes = outputs[:, 0:4]\n        # yoloxのinferenceのコードから\n        ratio = min(self.exp.test_size[0] / img.shape[0],\n                    self.exp.test_size[1] / img.shape[1])\n\n        bboxes /= ratio\n        confs = outputs[:, 4] * outputs[:, 5]\n\n        return bboxes, confs\n\n    def xyxy_to_xywh(self, bboxes):\n        if len(bboxes) == 0:\n            return bboxes\n\n        bboxes = bboxes.astype(int)\n        left = bboxes[:, 0]\n        top = bboxes[:, 1]\n        right = bboxes[:, 2]\n        bottom = bboxes[:, 3]\n\n        top = np.clip(top, 0, 720)\n        left = np.clip(left, 0, 1280)\n        bottom = np.clip(bottom, 0, 720)\n        right = np.clip(right, 0, 1280)\n        height = bottom - top\n        width = right - left\n\n        bboxes = np.vstack([left, top, width, height]).T\n\n        return bboxes\n\n    def predict(self, img):\n        \"\"\"\n        input: img rgb\n        output: xyxy, confs\n        \"\"\"\n        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n        outputs = self.get_outputs(img)\n        bboxes, confs = self.outputs_to_xyxy_confs(img, outputs)\n\n        return bboxes, confs\n\n    def get_annot(self, img_rgb):\n        bboxes, confs = self.predict(img_rgb)\n        bboxes = self.xyxy_to_xywh(bboxes)\n        annot = format_prediction(bboxes, confs)\n        return annot","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:27:29.695387Z","iopub.execute_input":"2022-02-06T01:27:29.695705Z","iopub.status.idle":"2022-02-06T01:27:29.717286Z","shell.execute_reply.started":"2022-02-06T01:27:29.695666Z","shell.execute_reply":"2022-02-06T01:27:29.716425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## mmdet","metadata":{}},{"cell_type":"code","source":"import json, cv2\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport pandas as pd\nfrom mmdet.apis import init_detector, inference_detector\nimport os, shutil,gc\nimport subprocess\nfrom mmcv import Config\n\nclass MMDetPredictor:\n    def __init__(self, cfg_path, ckpt_path):\n\n        # config_file = f'configs/reef/{exp_name}.py'\n\n        print(f'Config:\\\\n{Config.fromfile(cfg_path).pretty_text}')\n        \n        self.model = init_detector(cfg_path, ckpt_path, device='cuda:0')\n\n    def predict(self, img):\n\n        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n        pred = inference_detector(self.model, img)\n        \n        bboxes_confs = pred[0]\n        bboxes = bboxes_confs[:, :4]\n        confs = bboxes_confs[:, 4]\n\n        return bboxes, confs\n\n    def xyxy_to_xywh(self, bboxes):\n        if len(bboxes) == 0:\n            return bboxes\n            \n        bboxes = bboxes.astype(int)\n        left = bboxes[:,0]\n        top = bboxes[:,1]\n        right = bboxes[:, 2]\n        bottom = bboxes[:, 3]\n\n        top = np.clip(top, 0, 720)\n        left = np.clip(left, 0, 1280)\n        bottom = np.clip(bottom, 0, 720)\n        right = np.clip(right, 0, 1280)\n        height = bottom - top\n        width = right - left\n        \n        bboxes = np.vstack([left,top,width,height]).T\n        \n        return bboxes\n        \n    def get_annot(self, img_rgb):\n        bboxes, confs = self.predict(img_rgb)\n        bboxes = self.xyxy_to_xywh(bboxes)\n        annot = format_prediction(bboxes, confs)\n        return annot","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:25.251985Z","iopub.execute_input":"2022-02-06T01:24:25.252239Z","iopub.status.idle":"2022-02-06T01:24:48.840814Z","shell.execute_reply.started":"2022-02-06T01:24:25.252205Z","shell.execute_reply":"2022-02-06T01:24:48.840035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ensemble","metadata":{}},{"cell_type":"code","source":"\n\nclass Ensemble:\n    def __init__(self, ens_method, models=None, iou_thr=0.55, skip_box_thr=0.001, test_conf=0.4, conf_pre=0.3, weights=None, conf_type='avg') -> None:\n        self.ens_method = ens_method\n        self.models = models\n        self.iou_thr = iou_thr\n        self.skip_box_thr = skip_box_thr\n        self.test_conf = test_conf\n        self.conf_pre = conf_pre\n        self.weights = weights\n        self.conf_type = conf_type\n\n        # normalize用に元の画像サイズ\n        self.width = 1280\n        self.height = 720\n\n    def conf_prefilter(self, bboxes_list, confs_list):\n        bboxes_list_new, confs_list_new = [], []\n\n        for bboxes, confs in zip(bboxes_list, confs_list):\n            if len(confs) != 0:\n                idxs = (confs >= self.conf_pre)\n                bboxes = bboxes[idxs, :]\n                confs = confs[idxs]\n\n            bboxes_list_new.append(bboxes)\n            confs_list_new.append(confs)\n\n        return bboxes_list_new, confs_list_new\n\n    def norm_bbox(self, bboxes):\n        if len(bboxes) == 0:\n            return bboxes\n\n        # floatにする必要がある\n        bboxes = bboxes.astype(float)\n\n        bboxes[:, [0, 2]] = np.clip(bboxes[:, [0, 2]], 0, self.width)\n        bboxes[:, [1, 3]] = np.clip(bboxes[:, [1, 3]], 0, self.height)\n\n        bboxes[:, [0, 2]] = bboxes[:, [0, 2]] / self.width\n        bboxes[:, [1, 3]] = bboxes[:, [1, 3]] / self.height\n        return bboxes\n\n    def xyxy_to_xywh(self, bboxes):\n        if len(bboxes) == 0:\n            return bboxes\n\n        bboxes = bboxes.astype(int)\n        left = bboxes[:, 0]\n        top = bboxes[:, 1]\n        right = bboxes[:, 2]\n        bottom = bboxes[:, 3]\n\n        top = np.clip(top, 0, 720)\n        left = np.clip(left, 0, 1280)\n        bottom = np.clip(bottom, 0, 720)\n        right = np.clip(right, 0, 1280)\n        height = bottom - top\n        width = right - left\n\n        bboxes = np.vstack([left, top, width, height]).T\n\n        return bboxes\n\n    def preprocess(self, bboxes_list, confs_list):\n        bboxes_list, confs_list = self.conf_prefilter(bboxes_list, confs_list)\n\n        # normalize\n        bboxes_list = [self.norm_bbox(bboxes) for bboxes in bboxes_list]\n        labels_list = [np.zeros(len(confs)).tolist()\n                       for confs in confs_list]\n\n        return bboxes_list, confs_list, labels_list\n\n    def postprocess(self, bboxes, confs):\n        idxs = (confs >= self.test_conf)\n        bboxes = bboxes[idxs, :]\n        confs = confs[idxs]\n\n        return bboxes, confs\n\n    def get_bboxes_confs_list(self, img):\n        \"\"\"\n        input: img rgb\n\n        model.predict()はimg rgbを入力、xyxyのbboxを出力\n        \"\"\"\n        bboxes_list = []\n        confs_list = []\n        for model in self.models:\n            bboxes, confs = model.predict(img)\n            bboxes_list.append(bboxes)\n            confs_list.append(confs)\n\n        return bboxes_list, confs_list\n\n    def ensemble(self, bboxes_list, confs_list):\n        \"\"\"\n        inputs: xyxy, confs\n        outputs: xyxy, confs\n        \"\"\"\n\n        bboxes_list, confs_list, labels_list = self.preprocess(\n            bboxes_list, confs_list)\n\n        if self.ens_method == 'wbf':\n            bboxes, confs, _ = weighted_boxes_fusion(bboxes_list, confs_list, labels_list,\n                                                     weights=self.weights, iou_thr=self.iou_thr,\n                                                     skip_box_thr=self.skip_box_thr, conf_type=self.conf_type)\n        elif self.ens_method == 'nms':\n            # 空のboxを消す\n            bboxes_list = [bboxes for bboxes in bboxes_list if len(bboxes) > 0]\n            confs_list = [confs for confs in confs_list if len(confs) > 0]\n            labels_list = [labels for labels in labels_list if len(labels) > 0]\n\n            if len(confs_list) == 0:\n                bboxes, confs = np.empty((0, 4)), np.empty(0)\n            else:\n                bboxes, confs, _ = nms(bboxes_list, confs_list, labels_list,\n                                       weights=self.weights, iou_thr=self.iou_thr)\n\n        bboxes[:, [0, 2]] *= self.width\n        bboxes[:, [1, 3]] *= self.height\n        bboxes = bboxes.astype(int)\n\n        bboxes, confs = self.postprocess(bboxes, confs)\n\n        return bboxes, confs\n\n    def predict(self, img):\n        \"\"\"\n        input: img rgb\n        output: xyxy, confs\n        \"\"\"\n        bboxes_list, confs_list = self.get_bboxes_confs_list(img)\n        bboxes, confs = self.ensemble(bboxes_list, confs_list)\n\n        return bboxes, confs\n\n    def get_annot(self, img_rgb):\n        bboxes, confs = self.predict(img_rgb)\n        bboxes = self.xyxy_to_xywh(bboxes)\n        annot = format_prediction(bboxes, confs)\n        return annot","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:48.842348Z","iopub.execute_input":"2022-02-06T01:24:48.844027Z","iopub.status.idle":"2022-02-06T01:24:48.872244Z","shell.execute_reply.started":"2022-02-06T01:24:48.843985Z","shell.execute_reply":"2022-02-06T01:24:48.871497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## heavy model","metadata":{}},{"cell_type":"code","source":"models_all = []","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:48.873932Z","iopub.execute_input":"2022-02-06T01:24:48.874186Z","iopub.status.idle":"2022-02-06T01:24:48.886748Z","shell.execute_reply.started":"2022-02-06T01:24:48.874153Z","shell.execute_reply":"2022-02-06T01:24:48.886055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## yolov5","metadata":{}},{"cell_type":"code","source":"models = []","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:48.888334Z","iopub.execute_input":"2022-02-06T01:24:48.888995Z","iopub.status.idle":"2022-02-06T01:24:48.894819Z","shell.execute_reply.started":"2022-02-06T01:24:48.888956Z","shell.execute_reply":"2022-02-06T01:24:48.894152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## yolox","metadata":{}},{"cell_type":"code","source":"%%writefile yolox_1.py\n\n\nimport os\n\n\nimport torch\nimport torch.distributed as dist\nimport torch.nn as nn\n\nfrom yolox.exp import Exp as MyExp\n\ndata_dir = '/content/tensorflow-great-barrier-reef/'\n\nclass Exp(MyExp):\n    def __init__(self):\n        super(Exp, self).__init__()\n        # model config\n        self.num_classes = 1\n        self.seed = 42\n\n        # s\n        \n        self.depth = 0.33\n        self.width = 0.50\n        \n        # x\n        \"\"\"\n        self.depth = 1.33\n        self.width = 1.25\n        \"\"\"\n\n        # l\n        \"\"\"\n        self.depth = 1.0\n        self.width = 1.0\n        \"\"\"\n\n        self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(\".\")[0]\n        # self.input_size = (704, 1280) # # (height, width)\n        self.input_size =  (2048, 3584)\n        # self.random_size =  (2560 // 32, 3584 // 32)\n        self.random_size =  (1440 // 32, 2048 // 32)\n        # self.random_size =  (1440 // 32, 2592 // 32)\n        # self.multiscale_range = 5 # 5\n        self.print_interval = 50\n\n        # self.no_aug = self.start_epoch >= self.max_epoch - self.exp.no_aug_epochs\n        self.no_aug_epochs = 1\n        self.warmup_epochs = 1\n\n        # Define yourself dataset path\n        self.data_dir = data_dir\n        self.name = 'train_images'\n        self.train_ann = \"train.json\"\n        self.val_ann = \"val.json\"\n\n        self.max_epoch = 12\n        self.data_num_workers = 8\n        self.eval_interval = 1\n        self.basic_lr_per_img = 0.01 / 64\n\n        # --------------- transform config ----------------- #\n        self.mosaic_prob = 1.0 # 1.0\n        self.mixup_prob = 1.0 # 1.0\n        self.hsv_prob = 1.0\n        self.flip_prob = 0.5\n        self.degrees = 10.0\n        self.translate = 0.1\n        self.mosaic_scale = (0.5, 1.5) # (0.1, 2) 変更しろ\n        self.mixup_scale = (0.5, 1.5) # 変更しろ\n        self.shear = 2.0\n        self.enable_mixup = True\n\n        # -----------------  testing config ------------------ #\n        # self.test_size = (704, 1280)\n        self.test_size = (2048, 3584)\n        self.test_conf = 0.05\n        self.nmsthre = 0.65\n\n    def get_data_loader(\n        self, batch_size, is_distributed, no_aug=False, cache_img=False\n    ):\n        from yolox.data import (\n            COCODataset,\n            TrainTransform,\n            YoloBatchSampler,\n            DataLoader,\n            InfiniteSampler,\n            MosaicDetection,\n            worker_init_reset_seed,\n        )\n        from yolox.utils import (\n            wait_for_the_master,\n            get_local_rank,\n        )\n\n        local_rank = get_local_rank()\n\n        with wait_for_the_master(local_rank):\n            dataset = COCODataset(\n                data_dir=self.data_dir,\n                name = self.name,\n                json_file=self.train_ann,\n                img_size=self.input_size,\n                preproc=TrainTransform(\n                    max_labels=50,\n                    flip_prob=self.flip_prob,\n                    hsv_prob=self.hsv_prob),\n                cache=cache_img,\n            )\n\n        dataset = MosaicDetection(\n            dataset,\n            mosaic=not no_aug,\n            img_size=self.input_size,\n            preproc=TrainTransform(\n                max_labels=120,\n                flip_prob=self.flip_prob,\n                hsv_prob=self.hsv_prob),\n            degrees=self.degrees,\n            translate=self.translate,\n            mosaic_scale=self.mosaic_scale,\n            mixup_scale=self.mixup_scale,\n            shear=self.shear,\n            enable_mixup=self.enable_mixup,\n            mosaic_prob=self.mosaic_prob,\n            mixup_prob=self.mixup_prob,\n        )\n\n        self.dataset = dataset\n\n        if is_distributed:\n            batch_size = batch_size // dist.get_world_size()\n\n        sampler = InfiniteSampler(len(self.dataset), seed=self.seed if self.seed else 0)\n\n        batch_sampler = YoloBatchSampler(\n            sampler=sampler,\n            batch_size=batch_size,\n            drop_last=False,\n            mosaic=not no_aug,\n        )\n\n        dataloader_kwargs = {\"num_workers\": self.data_num_workers, \"pin_memory\": True}\n        dataloader_kwargs[\"batch_sampler\"] = batch_sampler\n\n        # Make sure each process has different random seed, especially for 'fork' method.\n        # Check https://github.com/pytorch/pytorch/issues/63311 for more details.\n        dataloader_kwargs[\"worker_init_fn\"] = worker_init_reset_seed\n\n        train_loader = DataLoader(self.dataset, **dataloader_kwargs)\n\n        return train_loader\n\n\n    def get_eval_loader(self, batch_size, is_distributed, testdev=False, legacy=False):\n        from yolox.data import COCODataset, ValTransform\n\n        valdataset = COCODataset(\n            data_dir=self.data_dir,\n            json_file=self.val_ann if not testdev else \"image_info_test-dev2017.json\",\n            name=self.name,\n            img_size=self.test_size,\n            preproc=ValTransform(legacy=legacy),\n        )\n\n        if is_distributed:\n            batch_size = batch_size // dist.get_world_size()\n            sampler = torch.utils.data.distributed.DistributedSampler(\n                valdataset, shuffle=False\n            )\n        else:\n            sampler = torch.utils.data.SequentialSampler(valdataset)\n\n        dataloader_kwargs = {\n            \"num_workers\": self.data_num_workers,\n            \"pin_memory\": True,\n            \"sampler\": sampler,\n        }\n        dataloader_kwargs[\"batch_size\"] = batch_size\n        val_loader = torch.utils.data.DataLoader(valdataset, **dataloader_kwargs)\n\n        return val_loader\n","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:48.896632Z","iopub.execute_input":"2022-02-06T01:24:48.897199Z","iopub.status.idle":"2022-02-06T01:24:48.907545Z","shell.execute_reply.started":"2022-02-06T01:24:48.89716Z","shell.execute_reply":"2022-02-06T01:24:48.906709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"test_sizes = [(1440, 2560), (1728, 3072), (2048, 3584)]\n\ntest_sizes = [(2048, 3584), (3008, 5376), (4032, 7168)]\n\nfor test_size in test_sizes:\n    models.append(YOLOXPredictor(exp_name, ckpt_path, test_size))\n\ntest_conf = 0.05\nconf_pre = 0.05\n\nens_method = 'wbf'\nmodel = Ensemble(ens_method, models=models, test_conf=test_conf, conf_pre=conf_pre)","metadata":{}},{"cell_type":"code","source":"exp_name = 'yolox_1'\n# ckpt_path = '../input/yolox-split-videoid-2/yolox_l_1024_1792/results/latest_ckpt.pth'\n# ckpt_path = '../input/yolox-del-empty-2/yolox_s_batch2_multiscale/results/latest_ckpt.pth'\n# ckpt_path = '../input/yolox-del-empty-all/yolox_s_batch2_all/results/latest_ckpt.pth'\n# ckpt_path = '../input/yolox-del-empty-a100/yolox_s_2560_3584_batch2/results/latest_ckpt.pth'\n# ckpt_path = '../input/yolox-del-empty-a100/yolox_s_2560_3584_batch2_12epoch/results/latest_ckpt.pth'\n# ckpt_path = '../input/yolox-del-empty-a100-3/yolox_x_2560_3584_epoch12_mosaic05/results/latest_ckpt.pth'\n# ckpt_path = '../input/yolox-del-empty-a100-3/yolox_s_2560_3584_mosaic_scale_05/results/latest_ckpt.pth'\nckpt_path = '../input/yolox-del-empty-a100-all/yolox_s_2560_3584_mosaic_scale_01_all/results/latest_ckpt.pth'\n\ntest_sizes = [(1440, 2560), (1728, 3072), (2048, 3584), (2304, 4096)]\n\nfor test_size in test_sizes:\n    models.append(YOLOXPredictor(exp_name, ckpt_path, test_size))\n\ntest_conf = 0.05\nconf_pre = 0.05\n\nens_method = 'wbf'\nmodel = Ensemble(ens_method, models=models, test_conf=test_conf, conf_pre=conf_pre)\n\nmodels_all.append(model)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:48.909499Z","iopub.execute_input":"2022-02-06T01:24:48.910141Z","iopub.status.idle":"2022-02-06T01:24:55.589949Z","shell.execute_reply.started":"2022-02-06T01:24:48.910114Z","shell.execute_reply":"2022-02-06T01:24:55.589121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## mmdet","metadata":{}},{"cell_type":"code","source":"%%writefile /kaggle/working/convnext_base.py\n\n\ndataset_type = 'CocoDataset'\nclasses = ('starfish',)\n\n# optimizer\noptimizer = dict(type='AdamW', lr=0.0001, weight_decay=0.0001)\n# optimizer = dict(type='SGD', lr=0.0025, momentum=0.9, weight_decay=0.0001)\n# optimizer = dict(type='SGD', lr=0.00025, momentum=0.9, weight_decay=0.0001)\n\nfp16 = dict(loss_scale=512.)\n\nnorm_cfg = dict(type='BN', requires_grad=True)\n\nimg_norm_cfg = dict(\n    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)\n\nload_from = 'https://dl.fbaipublicfiles.com/convnext/coco/cascade_mask_rcnn_convnext_base_22k_3x.pth'\n# load_from = 'https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'\n# load_from = 'https://dl.fbaipublicfiles.com/convnext/coco/cascade_mask_rcnn_convnext_small_1k_3x.pth'\n# load_from = 'https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'\n# load_from = 'https://dl.fbaipublicfiles.com/convnext/coco/cascade_mask_rcnn_convnext_large_22k_3x.pth'\n# load_from = 'https://dl.fbaipublicfiles.com/convnext/coco/cascade_mask_rcnn_convnext_base_22k_3x.pth'\n# load_from = 'https://dl.fbaipublicfiles.com/convnext/coco/cascade_mask_rcnn_convnext_small_1k_3x.pth'\n# load_from = 'https://dl.fbaipublicfiles.com/convnext/coco/cascade_mask_rcnn_convnext_tiny_1k_3x.pth'\n# load_from = 'https://dl.fbaipublicfiles.com/convnext/coco/mask_rcnn_convnext_tiny_1k_3x.pth'\n# load_from = 'https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'\nsamples_per_gpu=2\n\ntest_img_scale = [(2816, 2816)]\n# test_img_scale = [(2816, 2816), (4224, 4224)]\n\n\nbase_size = 2432\ncount = 3 * 4\ntrain_img_scale=[(base_size + 32 * i, base_size + 32 * i) for i in range(-count, count+1)]\n# train_img_scale = (3072, 3072)\n\nnum_classes = 1\n\nscore_thr = 0.1\n\ndel_bg = True\nval_id = 2\n# train_ann_file=f'/content/drive/MyDrive/kaggledata/Great_Barrier_Reef/datasets/coco_dataset/train_all_del_empty_{del_bg}/train.json'\ntrain_ann_file=f'/content/drive/MyDrive/kaggledata/Great_Barrier_Reef/datasets/coco_dataset/skip_1/val_{val_id}/del_bg_{del_bg}/train.json',\n\nmodel = dict(\n    type='CascadeRCNN',\n    pretrained=None,\n    backbone=dict(\n        type='ConvNeXt',\n        in_chans=3,\n        depths=[3, 3, 27, 3], \n        dims=[128, 256, 512, 1024], \n        drop_path_rate=0.6,\n        layer_scale_init_value=1.0,\n        out_indices=[0, 1, 2, 3],\n    ),\n    neck=dict(\n        type='FPN',\n        in_channels=[128, 256, 512, 1024],\n        out_channels=256,\n        num_outs=5),\n    rpn_head=dict(\n        type='RPNHead',\n        in_channels=256,\n        feat_channels=256,\n        anchor_generator=dict(\n            type='AnchorGenerator',\n            scales=[8],\n            ratios=[0.5, 1.0, 2.0],\n            strides=[4, 8, 16, 32, 64]),\n        bbox_coder=dict(\n            type='DeltaXYWHBBoxCoder',\n            target_means=[0.0, 0.0, 0.0, 0.0],\n            target_stds=[1.0, 1.0, 1.0, 1.0]),\n        loss_cls=dict(\n            type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0),\n        loss_bbox=dict(\n            type='SmoothL1Loss', beta=0.1111111111111111, loss_weight=1.0)),\n    roi_head=dict(\n        type='CascadeRoIHead',\n        num_stages=3,\n        stage_loss_weights=[1, 0.5, 0.25],\n        bbox_roi_extractor=dict(\n            type='SingleRoIExtractor',\n            roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0),\n            out_channels=256,\n            featmap_strides=[4, 8, 16, 32]),\n        bbox_head=[\n            dict(\n                type='ConvFCBBoxHead',\n                num_shared_convs=4,\n                num_shared_fcs=1,\n                in_channels=256,\n                conv_out_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=num_classes,\n                bbox_coder=dict(\n                    type='DeltaXYWHBBoxCoder',\n                    target_means=[0.0, 0.0, 0.0, 0.0],\n                    target_stds=[0.1, 0.1, 0.2, 0.2]),\n                reg_class_agnostic=False,\n                reg_decoded_bbox=True,\n                norm_cfg=norm_cfg,\n                loss_cls=dict(\n                    type='CrossEntropyLoss',\n                    use_sigmoid=False,\n                    loss_weight=1.0),\n                loss_bbox=dict(type='GIoULoss', loss_weight=10.0)),\n            dict(\n                type='ConvFCBBoxHead',\n                num_shared_convs=4,\n                num_shared_fcs=1,\n                in_channels=256,\n                conv_out_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=num_classes,\n                bbox_coder=dict(\n                    type='DeltaXYWHBBoxCoder',\n                    target_means=[0.0, 0.0, 0.0, 0.0],\n                    target_stds=[0.05, 0.05, 0.1, 0.1]),\n                reg_class_agnostic=False,\n                reg_decoded_bbox=True,\n                norm_cfg=norm_cfg,\n                loss_cls=dict(\n                    type='CrossEntropyLoss',\n                    use_sigmoid=False,\n                    loss_weight=1.0),\n                loss_bbox=dict(type='GIoULoss', loss_weight=10.0)),\n            dict(\n                type='ConvFCBBoxHead',\n                num_shared_convs=4,\n                num_shared_fcs=1,\n                in_channels=256,\n                conv_out_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=num_classes,\n                bbox_coder=dict(\n                    type='DeltaXYWHBBoxCoder',\n                    target_means=[0.0, 0.0, 0.0, 0.0],\n                    target_stds=[0.033, 0.033, 0.067, 0.067]),\n                reg_class_agnostic=False,\n                reg_decoded_bbox=True,\n                norm_cfg=norm_cfg,\n                loss_cls=dict(\n                    type='CrossEntropyLoss',\n                    use_sigmoid=False,\n                    loss_weight=1.0),\n                loss_bbox=dict(type='GIoULoss', loss_weight=10.0))\n        ]),\n    train_cfg=dict(\n        rpn=dict(\n            assigner=dict(\n                type='MaxIoUAssigner',\n                pos_iou_thr=0.7,\n                neg_iou_thr=0.3,\n                min_pos_iou=0.3,\n                match_low_quality=True,\n                ignore_iof_thr=-1),\n            sampler=dict(\n                type='RandomSampler',\n                num=256,\n                pos_fraction=0.5,\n                neg_pos_ub=-1,\n                add_gt_as_proposals=False),\n            allowed_border=0,\n            pos_weight=-1,\n            debug=False),\n        rpn_proposal=dict(\n            nms_across_levels=False,\n            nms_pre=2000,\n            nms_post=2000,\n            max_per_img=2000,\n            nms=dict(type='nms', iou_threshold=0.7),\n            min_bbox_size=0),\n        rcnn=[\n            dict(\n                assigner=dict(\n                    type='MaxIoUAssigner',\n                    pos_iou_thr=0.5,\n                    neg_iou_thr=0.5,\n                    min_pos_iou=0.5,\n                    match_low_quality=False,\n                    ignore_iof_thr=-1),\n                sampler=dict(\n                    type='RandomSampler',\n                    num=512,\n                    pos_fraction=0.25,\n                    neg_pos_ub=-1,\n                    add_gt_as_proposals=True),\n                mask_size=28,\n                pos_weight=-1,\n                debug=False),\n            dict(\n                assigner=dict(\n                    type='MaxIoUAssigner',\n                    pos_iou_thr=0.6,\n                    neg_iou_thr=0.6,\n                    min_pos_iou=0.6,\n                    match_low_quality=False,\n                    ignore_iof_thr=-1),\n                sampler=dict(\n                    type='RandomSampler',\n                    num=512,\n                    pos_fraction=0.25,\n                    neg_pos_ub=-1,\n                    add_gt_as_proposals=True),\n                mask_size=28,\n                pos_weight=-1,\n                debug=False),\n            dict(\n                assigner=dict(\n                    type='MaxIoUAssigner',\n                    pos_iou_thr=0.7,\n                    neg_iou_thr=0.7,\n                    min_pos_iou=0.7,\n                    match_low_quality=False,\n                    ignore_iof_thr=-1),\n                sampler=dict(\n                    type='RandomSampler',\n                    num=512,\n                    pos_fraction=0.25,\n                    neg_pos_ub=-1,\n                    add_gt_as_proposals=True),\n                mask_size=28,\n                pos_weight=-1,\n                debug=False)\n        ]),\n    test_cfg=dict(\n        rpn=dict(\n            nms_across_levels=False,\n            nms_pre=1000,\n            nms_post=1000,\n            max_per_img=1000,\n            nms=dict(type='nms', iou_threshold=0.7),\n            min_bbox_size=0),\n        rcnn=dict(\n            score_thr=score_thr,\n            nms=dict(type='nms', iou_threshold=0.3),\n            max_per_img=100,\n            mask_thr_binary=0.5)))\n\nalbu_train_transforms = [\n    dict(\n        type=\"OneOf\",\n        transforms=[\n            dict(type=\"RandomGamma\"),\n            dict(type=\"CLAHE\"),\n        ],\n        p=0.5,\n    ),\n    dict(type='RandomBrightnessContrast', p=0.5),\n    dict(type='ShiftScaleRotate',\n                 shift_limit=0.10,\n                 scale_limit=0,\n                 rotate_limit=0,\n                 p=0.5),\n    dict(\n        type=\"OneOf\", # 6\n        transforms=[\n            dict(type=\"Blur\"),\n            dict(type=\"MotionBlur\"),\n            dict(type=\"GaussNoise\"),\n        ],\n        p=0.4,\n    ), \n]\n\n\ntrain_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(type='LoadAnnotations', with_bbox=True),\n    dict(\n        type='Resize',\n        img_scale=train_img_scale, # [(1024, 1024),(1536, 1536)]\n        multiscale_mode=\"value\",\n        keep_ratio=True),\n    dict(\n        type='Albu',\n        transforms=albu_train_transforms,\n        bbox_params=dict(\n            type='BboxParams',\n            format='pascal_voc',\n            label_fields=['gt_labels'],\n            min_visibility=0.0,\n            filter_lost_elements=True),\n        keymap={\n            'img': 'image',\n            'gt_bboxes': 'bboxes'\n        },\n        update_pad_shape=False,\n        skip_img_without_anno=False),\n    dict(type='RandomFlip', flip_ratio=0.5,direction=['horizontal', \"vertical\", \"diagonal\"]), # ['horizontal', 'vertical']\n    dict(type='Normalize', **img_norm_cfg),\n    dict(type='Pad', size_divisor=32),\n    dict(type='DefaultFormatBundle'),\n    dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']),\n]\ntest_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(\n        type='MultiScaleFlipAug',\n        img_scale=test_img_scale, # [(1024, 1024),(1280, 1280),(1536, 1536)]\n        flip=False,\n        transforms=[\n            dict(type='Resize', keep_ratio=True),\n            dict(type='RandomFlip',direction=['horizontal']), #direction=['horizontal', 'vertical'] \n            dict(type='Normalize', **img_norm_cfg),\n            dict(type='Pad', size_divisor=32),\n            dict(type='ImageToTensor', keys=['img']),\n            dict(type='Collect', keys=['img']),\n        ])\n]\n\n\n\ndata = dict(\n    samples_per_gpu=samples_per_gpu,\n    workers_per_gpu=2,\n    train=dict(\n        type=dataset_type,\n        filter_empty_gt=False,\n        ann_file=train_ann_file,\n        img_prefix='/content/tensorflow-great-barrier-reef/train_images/',\n        classes=classes,\n        pipeline=train_pipeline),\n    val=dict(\n        type=dataset_type,\n        filter_empty_gt=False,\n        ann_file=f'/content/drive/MyDrive/kaggledata/Great_Barrier_Reef/datasets/coco_dataset/skip_1/val_{val_id}/del_bg_{del_bg}/val.json',\n        img_prefix='/content/tensorflow-great-barrier-reef/train_images/',\n        classes=classes,\n        pipeline=test_pipeline),\n    test=dict(\n        type=dataset_type,\n        filter_empty_gt=False,\n        ann_file=f'/content/drive/MyDrive/kaggledata/Great_Barrier_Reef/datasets/coco_dataset/skip_1/val_{val_id}/del_bg_{del_bg}/val.json',\n        img_prefix='/content/tensorflow-great-barrier-reef/train_images/',\n        classes=classes,\n        pipeline=test_pipeline),\n)\n\n\nevaluation = dict(interval=1, metric='bbox', save_best='bbox_mAP')\n\noptimizer_config = dict(grad_clip=None)\n# learning policy\nlr_config = dict(\n    policy='step',\n    warmup='linear',\n    warmup_iters=500 // samples_per_gpu,\n    warmup_ratio=0.001,\n    step=[2, 3])\nmax_epochs = 4\n\ncustom_hooks = [dict(type='NumClassCheckHook')]\n\n\nrunner = dict(type='EpochBasedRunner', max_epochs=max_epochs) \ncheckpoint_config = dict(interval=1)\n# custom_hooks = [dict(type='NumClassCheckHook')]\n# yapf:disable\nlog_config = dict(\n    interval=10,\n    hooks=[\n        dict(type='TextLoggerHook'),\n        # dict(type='TensorboardLoggerHook')\n    ])\n# yapf:enable\ndist_params = dict(backend='nccl')\nlog_level = 'INFO'\n\n\nresume_from = None\n\nworkflow = [('train', 1)]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cfg_path = '/kaggle/working/mmdet_cfg.py'\n\n\n# ckpt_path = '../input/convnext-mmdet-ori-a100/frcnn_r50_all/results/latest.pth'\n\n# cfg_path = '/kaggle/working/convnext_small.py'\n# ckpt_path = '../input/convnext-mmdet-ori-a100/convnext_small_batch2_multi32/results/latest.pth'\n# ckpt_path = '../input/convnext-mmdet-ori-a100/convnext_small_batch2_multi32_all/results/latest.pth'\n\ncfg_path = '/kaggle/working/convnext_base.py'\n# ckpt_path = '../input/convnext-mmdet-ori-a100-2/convnext_base_batch2_multi32/results/latest.pth'\nckpt_path = '../input/convnext-mmdet-ori-a100-2/convnext_base_batch2_multi32_all/results/latest.pth'\n\n# cfg_path = '/kaggle/working/convnext_xlarge.py'\n# ckpt_path = '../input/convnext-mmdet-ori-a100-2/convnext_xlarge_batch2_multi32/results/latest.pth'\n# ckpt_path = '../input/convnext-mmdet-ori-a100-2/convnext_xlarge_batch2_multi32_all/results/latest.pth'\n\nmodel = MMDetPredictor(cfg_path, ckpt_path)\nmodels_all.append(model)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ensemble","metadata":{}},{"cell_type":"code","source":"test_conf = 0.2\nconf_pre = 0.05\n\nens_method = 'wbf'\nmodel = Ensemble(ens_method, models=models_all, test_conf=test_conf, conf_pre=conf_pre)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## yolox","metadata":{}},{"cell_type":"markdown","source":"## all","metadata":{}},{"cell_type":"markdown","source":"## 高速化用のモデル","metadata":{}},{"cell_type":"markdown","source":"## yolov5","metadata":{}},{"cell_type":"markdown","source":"## yolox","metadata":{}},{"cell_type":"markdown","source":"## ensemble","metadata":{}},{"cell_type":"markdown","source":"## main","metadata":{}},{"cell_type":"code","source":"import greatbarrierreef\nenv = greatbarrierreef.make_env()\niter_test = env.iter_test() ","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:55.85836Z","iopub.status.idle":"2022-02-06T01:24:55.858817Z","shell.execute_reply.started":"2022-02-06T01:24:55.858544Z","shell.execute_reply":"2022-02-06T01:24:55.858567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nfor (img_rgb, pred_df) in iter_test:  # iterate through all test set images\n    # 空でなければ重めのモデルで予測\n    \"\"\"\n    annot = light_model.get_annot(img_rgb)\n    if annot == \"\":\n        pred_df['annotations'] = annot\n        env.predict(pred_df)\n        continue\n    \"\"\"\n    \n    annot = model.get_annot(img_rgb)\n    pred_df['annotations'] = annot\n    env.predict(pred_df)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:55.861261Z","iopub.status.idle":"2022-02-06T01:24:55.861964Z","shell.execute_reply.started":"2022-02-06T01:24:55.861723Z","shell.execute_reply":"2022-02-06T01:24:55.861748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, cv2\ntrain_dir = '../input/tensorflow-great-barrier-reef/train_images/video_2/'\nimg_files = os.listdir(train_dir)\nfor i, img_file in enumerate(img_files):\n    img_path = train_dir+img_file\n    img_rgb = cv2.imread(img_path)\n    img_rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB)\n    \n    \"\"\"\n    annot = light_model.get_annot(img_rgb)\n    if annot == \"\":\n        print('skip')\n    else:\n        print('light')\n        print(annot)\n    \"\"\"\n        \n    print('heavy')\n    annot = model.get_annot(img_rgb)\n    print(annot)\n    \n    if i == 20:\n        break","metadata":{"execution":{"iopub.status.busy":"2022-02-06T01:24:55.863091Z","iopub.status.idle":"2022-02-06T01:24:55.863717Z","shell.execute_reply.started":"2022-02-06T01:24:55.863453Z","shell.execute_reply":"2022-02-06T01:24:55.863478Z"},"trusted":true},"execution_count":null,"outputs":[]}]}