{"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":"!pip install mmcv-full==1.3.8 -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.html\n!rm -rf mmdetection\n!git clone https://github.com/open-mmlab/mmdetection.git\n!cd mmdetection && pip install -e .","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:54:11.02872Z","iopub.execute_input":"2021-07-27T11:54:11.029233Z","iopub.status.idle":"2021-07-27T11:54:53.498128Z","shell.execute_reply.started":"2021-07-27T11:54:11.029199Z","shell.execute_reply":"2021-07-27T11:54:53.497175Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.insert(0, \"./mmdetection\")\n\nimport mmdet\nimport mmdet.core\nfrom mmdet.apis import init_detector, inference_detector\nimport mmcv","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:54:53.499927Z","iopub.execute_input":"2021-07-27T11:54:53.500282Z","iopub.status.idle":"2021-07-27T11:55:09.466165Z","shell.execute_reply.started":"2021-07-27T11:54:53.500243Z","shell.execute_reply":"2021-07-27T11:55:09.465305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mmdet.core.bbox import demodata","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:55:09.468088Z","iopub.execute_input":"2021-07-27T11:55:09.468455Z","iopub.status.idle":"2021-07-27T11:55:09.473686Z","shell.execute_reply.started":"2021-07-27T11:55:09.468395Z","shell.execute_reply":"2021-07-27T11:55:09.47291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:55:09.475118Z","iopub.execute_input":"2021-07-27T11:55:09.475658Z","iopub.status.idle":"2021-07-27T11:55:09.508995Z","shell.execute_reply.started":"2021-07-27T11:55:09.475622Z","shell.execute_reply":"2021-07-27T11:55:09.508054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"level_df = pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')\nlevel_df","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:55:09.51052Z","iopub.execute_input":"2021-07-27T11:55:09.511149Z","iopub.status.idle":"2021-07-27T11:55:09.589201Z","shell.execute_reply.started":"2021-07-27T11:55:09.511109Z","shell.execute_reply":"2021-07-27T11:55:09.588412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta = pd.read_csv('../input/second/metadf.csv')\nmeta[\"id\"] += \"_image\"\nmeta","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:55:09.590418Z","iopub.execute_input":"2021-07-27T11:55:09.590793Z","iopub.status.idle":"2021-07-27T11:55:09.637872Z","shell.execute_reply.started":"2021-07-27T11:55:09.590756Z","shell.execute_reply":"2021-07-27T11:55:09.637036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_df = pd.merge(level_df, meta, on=\"id\", how=\"left\")\nmerge_df","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:55:09.639269Z","iopub.execute_input":"2021-07-27T11:55:09.639914Z","iopub.status.idle":"2021-07-27T11:55:09.673201Z","shell.execute_reply.started":"2021-07-27T11:55:09.639873Z","shell.execute_reply":"2021-07-27T11:55:09.672107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!conda install gdcm -c conda-forge -y","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:55:09.675976Z","iopub.execute_input":"2021-07-27T11:55:09.676367Z","iopub.status.idle":"2021-07-27T11:56:11.718866Z","shell.execute_reply.started":"2021-07-27T11:55:09.676327Z","shell.execute_reply":"2021-07-27T11:56:11.717812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.auto import tqdm\n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:56:11.721097Z","iopub.execute_input":"2021-07-27T11:56:11.721523Z","iopub.status.idle":"2021-07-27T11:56:11.726059Z","shell.execute_reply.started":"2021-07-27T11:56:11.721477Z","shell.execute_reply":"2021-07-27T11:56:11.725125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"anno_df = pd.DataFrame(columns = [[ 'image_id','bbox_name', 'bbox']])\nanno_df","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:56:11.727393Z","iopub.execute_input":"2021-07-27T11:56:11.727977Z","iopub.status.idle":"2021-07-27T11:56:11.752995Z","shell.execute_reply.started":"2021-07-27T11:56:11.727933Z","shell.execute_reply":"2021-07-27T11:56:11.752238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bbox_names = []\nbboxes = []\n\nimage_len = level_df.shape[0]\n\nfor i, row in tqdm(merge_df.iterrows(), total=merge_df.shape[0]):\n    image_id = row.id.split('_')[0]\n    boxes = row.label.split()\n    bbox_name = []\n    bbox = []\n    sum = []\n    width_ratio = 512/row.dim1\n    height_ratio = 512/row.dim0\n    if boxes[0] == 'none':\n        anno_df.loc[i] = [image_id, None, None]\n        continue\n    else:\n        for j in range(int(len(boxes)/6)):\n            label = boxes[j*6+0]\n            x1 = float(boxes[j*6+2])*width_ratio\n            y1 = float(boxes[j*6+3])*height_ratio\n            x2 = float(boxes[j*6+4])*width_ratio\n            y2 = float(boxes[j*6+5])*height_ratio\n\n            bbox_name.append(label)\n            bbox.append([float(x1), float(y1), float(x2), float(y2)])\n    sum.append(image_id)\n    sum.append(bbox_name)\n    sum.append(bbox)\n    anno_df.loc[i] = sum\nanno_df","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:56:11.755798Z","iopub.execute_input":"2021-07-27T11:56:11.756071Z","iopub.status.idle":"2021-07-27T11:56:36.819728Z","shell.execute_reply.started":"2021-07-27T11:56:11.756039Z","shell.execute_reply":"2021-07-27T11:56:36.818799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"anno_df = anno_df.dropna().reset_index(drop=True)\nprint(anno_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:56:36.821148Z","iopub.execute_input":"2021-07-27T11:56:36.82169Z","iopub.status.idle":"2021-07-27T11:56:36.831528Z","shell.execute_reply.started":"2021-07-27T11:56:36.821651Z","shell.execute_reply":"2021-07-27T11:56:36.830564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"anno_df","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:56:36.832863Z","iopub.execute_input":"2021-07-27T11:56:36.833256Z","iopub.status.idle":"2021-07-27T11:56:36.861002Z","shell.execute_reply.started":"2021-07-27T11:56:36.833217Z","shell.execute_reply":"2021-07-27T11:56:36.860072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_df = merge_df.dropna().reset_index(drop=True)\nprint(merge_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:56:36.862275Z","iopub.execute_input":"2021-07-27T11:56:36.862828Z","iopub.status.idle":"2021-07-27T11:56:36.875959Z","shell.execute_reply.started":"2021-07-27T11:56:36.862785Z","shell.execute_reply":"2021-07-27T11:56:36.87497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_df","metadata":{"execution":{"iopub.status.busy":"2021-07-27T11:56:36.87732Z","iopub.execute_input":"2021-07-27T11:56:36.877747Z","iopub.status.idle":"2021-07-27T11:56:36.901754Z","shell.execute_reply.started":"2021-07-27T11:56:36.877708Z","shell.execute_reply":"2021-07-27T11:56:36.900786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# merge_df.loc[0,\"boxes\"]","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:19:06.023809Z","iopub.execute_input":"2021-07-27T12:19:06.024218Z","iopub.status.idle":"2021-07-27T12:19:06.033144Z","shell.execute_reply.started":"2021-07-27T12:19:06.024185Z","shell.execute_reply":"2021-07-27T12:19:06.031871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# merge_df.loc[0,\"label\"]","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:18:55.669033Z","iopub.execute_input":"2021-07-27T12:18:55.669352Z","iopub.status.idle":"2021-07-27T12:18:55.674827Z","shell.execute_reply.started":"2021-07-27T12:18:55.669321Z","shell.execute_reply":"2021-07-27T12:18:55.673772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# anno_df.loc[0,\"bbox\"][0]","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:20:11.105354Z","iopub.execute_input":"2021-07-27T12:20:11.10575Z","iopub.status.idle":"2021-07-27T12:20:11.112795Z","shell.execute_reply.started":"2021-07-27T12:20:11.105716Z","shell.execute_reply":"2021-07-27T12:20:11.111838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# aaaa = anno_df.values.tolist()\n# aaaa[0][2]\n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:30:18.708689Z","iopub.execute_input":"2021-07-27T12:30:18.709006Z","iopub.status.idle":"2021-07-27T12:30:18.718149Z","shell.execute_reply.started":"2021-07-27T12:30:18.708976Z","shell.execute_reply":"2021-07-27T12:30:18.717204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import cv2\n# import matplotlib.pyplot as plt\n# image = cv2.imread('/kaggle/working/siim/image/000a312787f2_image.png')\n# draw_img = image.copy()\n# cv2.rectangle(draw_img, (94,85) , (218, 366), (0,255,0), 2)\n# cv2.rectangle(draw_img, (270,86) , (401, 345), (0,255,0), 2)\n# # cv2.putText(draw_img, caption, (94.95198315789473, 85.49436330275229-5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, red_color, 2)\n# plt.imshow(draw_img)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:32:05.832168Z","iopub.execute_input":"2021-07-27T12:32:05.832505Z","iopub.status.idle":"2021-07-27T12:32:06.013771Z","shell.execute_reply.started":"2021-07-27T12:32:05.832474Z","shell.execute_reply":"2021-07-27T12:32:06.012844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_df, val_df = train_test_split(merge_df, test_size=0.1,random_state=2021)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:02:16.757393Z","iopub.execute_input":"2021-07-27T12:02:16.757817Z","iopub.status.idle":"2021-07-27T12:02:16.766073Z","shell.execute_reply.started":"2021-07-27T12:02:16.757784Z","shell.execute_reply":"2021-07-27T12:02:16.765132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_df), len(val_df))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:02:19.504755Z","iopub.execute_input":"2021-07-27T12:02:19.505147Z","iopub.status.idle":"2021-07-27T12:02:19.510354Z","shell.execute_reply.started":"2021-07-27T12:02:19.505108Z","shell.execute_reply":"2021-07-27T12:02:19.509262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.mkdir('/kaggle/working/siim')","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:02:22.829706Z","iopub.execute_input":"2021-07-27T12:02:22.830056Z","iopub.status.idle":"2021-07-27T12:02:22.834239Z","shell.execute_reply.started":"2021-07-27T12:02:22.830026Z","shell.execute_reply":"2021-07-27T12:02:22.833316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['id'].to_csv('/kaggle/working/siim/train.txt', sep=' ', header=False, index=False)\nval_df['id'].to_csv('/kaggle/working/siim/val.txt', sep=' ', header=False, index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:02:34.002313Z","iopub.execute_input":"2021-07-27T12:02:34.002665Z","iopub.status.idle":"2021-07-27T12:02:34.021673Z","shell.execute_reply.started":"2021-07-27T12:02:34.002633Z","shell.execute_reply":"2021-07-27T12:02:34.020909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('/kaggle/working/siim/image')","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:02:37.242852Z","iopub.execute_input":"2021-07-27T12:02:37.243167Z","iopub.status.idle":"2021-07-27T12:02:37.246971Z","shell.execute_reply.started":"2021-07-27T12:02:37.243139Z","shell.execute_reply":"2021-07-27T12:02:37.246119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nfor i in range(len(merge_df)):\n    shutil.copy(f'../input/second/image/{merge_df.loc[i,\"id\"]}.png', \"/kaggle/working/siim/image\") ","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:02:48.936026Z","iopub.execute_input":"2021-07-27T12:02:48.936347Z","iopub.status.idle":"2021-07-27T12:03:23.624863Z","shell.execute_reply.started":"2021-07-27T12:02:48.936317Z","shell.execute_reply":"2021-07-27T12:03:23.623857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/siim","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:05:12.642307Z","iopub.execute_input":"2021-07-27T12:05:12.642665Z","iopub.status.idle":"2021-07-27T12:05:12.769558Z","shell.execute_reply.started":"2021-07-27T12:05:12.642633Z","shell.execute_reply":"2021-07-27T12:05:12.768707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# os.remove('/kaggle/working/siim/image')\n# shutil.rmtree('/kaggle/working/siim/image')","metadata":{"execution":{"iopub.status.busy":"2021-07-27T01:30:43.182735Z","iopub.status.idle":"2021-07-27T01:30:43.183633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# image = cv2.imread('/kaggle/working/siim/image/000a312787f2_image.png')\n# height, width = image.shape[:2]\n# print(height, width)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:05:54.63799Z","iopub.execute_input":"2021-07-27T12:05:54.638328Z","iopub.status.idle":"2021-07-27T12:05:54.649852Z","shell.execute_reply.started":"2021-07-27T12:05:54.638298Z","shell.execute_reply":"2021-07-27T12:05:54.64888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image_list = mmcv.list_from_file('/kaggle/working/siim/train.txt')\n# image_list","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import copy\nimport os.path as osp\n\nimport mmcv\nimport numpy as np\nimport cv2\n\nfrom mmdet.datasets.builder import DATASETS\nfrom mmdet.datasets.custom import CustomDataset\n\n\n@DATASETS.register_module(force=True)\nclass CovDataset(CustomDataset):\n    CLASSES = ['opacity']\n\n    def load_annotations(self, ann_file):\n        cat2label = {k:i for i, k in enumerate(self.CLASSES)}\n        image_list = mmcv.list_from_file(self.ann_file)\n\n        data_infos = []\n\n        for image_id in image_list:\n\n            filename = '{0:}/{1:}.png'.format(self.img_prefix, image_id)\n            image = cv2.imread(filename)\n            height, width = image.shape[:2]\n\n            data_info = {'filename': str(image_id)+'.png',\n                      'width': width, 'height': height}\n            df_list = anno_df.values.tolist()\n            bbox_names = df_list[i][1]\n            bboxes = df_list[i][2]\n\n            gt_bboxes = []\n            gt_labels = []\n            gt_bboxes_ignore = []\n            gt_labels_ignore = []\n\n\n            for bbox_name, bbox in zip(bbox_names, bboxes):\n                if bbox_name in cat2label:\n                    gt_bboxes.append(bbox)\n                    gt_labels.append(cat2label[bbox_name])\n                else:\n                    gt_bboxes_ignore.append(bbox)\n                    gt_labels_ignore.append(-1)\n\n\n            data_anno = {\n            'bboxes': np.array(gt_bboxes, dtype=np.float32).reshape(-1, 4),\n            'labels': np.array(gt_labels, dtype=np.long),\n            'bboxes_ignore': np.array(gt_bboxes_ignore, dtype=np.float32).reshape(-1, 4),\n            'labels_ignore': np.array(gt_labels_ignore, dtype=np.long)\n            }\n\n\n            data_info.update(ann=data_anno)\n            data_infos.append(data_info)\n        return data_infos\n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:09:37.159672Z","iopub.execute_input":"2021-07-27T12:09:37.160052Z","iopub.status.idle":"2021-07-27T12:09:37.172247Z","shell.execute_reply.started":"2021-07-27T12:09:37.16002Z","shell.execute_reply":"2021-07-27T12:09:37.17122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/mmdetection/","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:45:53.143391Z","iopub.execute_input":"2021-07-27T12:45:53.143735Z","iopub.status.idle":"2021-07-27T12:45:53.292516Z","shell.execute_reply.started":"2021-07-27T12:45:53.143703Z","shell.execute_reply":"2021-07-27T12:45:53.29165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config_file = \"/kaggle/working/mmdetection/configs/cascade_rcnn/cascade_rcnn_x101_32x4d_fpn_1x_coco.py\"\n# checkpoint_file = '/kaggle/working/mmdetection/checkpoints/cascade_rcnn_x101_64x4d_fpn_1x_coco_20200515_075702-43ce6a30.pth'","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:47:02.757528Z","iopub.execute_input":"2021-07-27T12:47:02.757875Z","iopub.status.idle":"2021-07-27T12:47:02.761653Z","shell.execute_reply.started":"2021-07-27T12:47:02.757841Z","shell.execute_reply":"2021-07-27T12:47:02.760804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cd mmdetection; mkdir checkpoints\n# !wget -O /kaggle/working/mmdetection/checkpoints/cascade_rcnn_x101_64x4d_fpn_1x_coco_20200515_075702-43ce6a30.pth \\\n# https://download.openmmlab.com/mmdetection/v2.0/cascade_rcnn/cascade_rcnn_x101_64x4d_fpn_1x_coco/cascade_rcnn_x101_64x4d_fpn_1x_coco_20200515_075702-43ce6a30.pth","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:47:04.213137Z","iopub.execute_input":"2021-07-27T12:47:04.213484Z","iopub.status.idle":"2021-07-27T12:48:01.835637Z","shell.execute_reply.started":"2021-07-27T12:47:04.213448Z","shell.execute_reply":"2021-07-27T12:48:01.834784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mmcv import Config\n\ncfg = Config.fromfile(config_file)\nprint(cfg.pretty_text)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T12:48:54.60734Z","iopub.execute_input":"2021-07-27T12:48:54.607808Z","iopub.status.idle":"2021-07-27T12:48:55.300652Z","shell.execute_reply.started":"2021-07-27T12:48:54.607763Z","shell.execute_reply":"2021-07-27T12:48:55.298956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\njob_folder = '/kaggle/working/siim_work_dir'\n\nos.mkdir(job_folder)\n\nprint(\"job_folder:\", job_folder)\n# !mkdir /kaggle/working/siim_work_dir","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mmdet.apis import set_random_seed\n\ncfg.dataset_type = 'CovDataset'\ncfg.data_root = '/kaggle/working/siim/'\n\ncfg.data.train.type = 'CovDataset'\ncfg.data.train.data_root = '/kaggle/working/siim/'\ncfg.data.train.ann_file = 'train.txt'\ncfg.data.train.img_prefix = 'image'\n\ncfg.data.val.type = 'CovDataset'\ncfg.data.val.data_root = '/kaggle/working/siim/'\ncfg.data.val.ann_file = 'val.txt'         \ncfg.data.val.img_prefix = 'image'\n\nfor head in cfg.model.roi_head.bbox_head:\n    head.num_classes = 1\n# cfg.model.roi_head.bbox_head[0][\"num_classes\"] = 1\n# cfg.load_from = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'\n\ncfg.work_dir = job_folder\n\ncfg.optimizer.lr = 0.02 / 8 \ncfg.lr_config.warmup = None #None => linear\ncfg.log_config.interval = 5\n\ncfg.runner.max_epochs = 10\n\n\ncfg.evaluation.metric = 'mAP'\ncfg.evaluation.interval = 5\ncfg.checkpoint_config.interval = 5\n\ncfg.data.samples_per_gpu = 4\n# cfg.evaluation.iou_thrs = [0.5]\n\ncfg.seed = 0\nset_random_seed(0, deterministic=False)\ncfg.gpu_ids = range(1)\ncfg.lr_config.policy='step' #step => CosineAnnealing\n\nprint(f'Config:\\n{cfg.pretty_text}')","metadata":{"execution":{"iopub.status.busy":"2021-07-27T13:08:25.046742Z","iopub.execute_input":"2021-07-27T13:08:25.047058Z","iopub.status.idle":"2021-07-27T13:08:25.526782Z","shell.execute_reply.started":"2021-07-27T13:08:25.047029Z","shell.execute_reply":"2021-07-27T13:08:25.525758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mmdet.datasets import build_dataset\nfrom mmdet.models import build_detector\nfrom mmdet.apis import train_detector\n\n# train용 Dataset 생성. \n\ndatasets = [build_dataset(cfg.data.train)]","metadata":{"execution":{"iopub.status.busy":"2021-07-27T13:08:30.184878Z","iopub.execute_input":"2021-07-27T13:08:30.185185Z","iopub.status.idle":"2021-07-27T13:09:06.864014Z","shell.execute_reply.started":"2021-07-27T13:08:30.185157Z","shell.execute_reply":"2021-07-27T13:09:06.863144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datasets","metadata":{"execution":{"iopub.status.busy":"2021-07-27T13:09:06.865327Z","iopub.execute_input":"2021-07-27T13:09:06.865674Z","iopub.status.idle":"2021-07-27T13:09:06.993135Z","shell.execute_reply.started":"2021-07-27T13:09:06.865638Z","shell.execute_reply":"2021-07-27T13:09:06.992405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pwd","metadata":{"execution":{"iopub.status.busy":"2021-07-27T13:09:06.994771Z","iopub.execute_input":"2021-07-27T13:09:06.99511Z","iopub.status.idle":"2021-07-27T13:09:07.161544Z","shell.execute_reply.started":"2021-07-27T13:09:06.995075Z","shell.execute_reply":"2021-07-27T13:09:07.160654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd mmdetection\n\nmodel = build_detector(cfg.model, train_cfg=cfg.get('train_cfg'), test_cfg=cfg.get('test_cfg'))\nmodel.CLASSES = datasets[0].CLASSES\n\nmmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))\ntrain_detector(model, datasets, cfg, distributed=False, validate=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T13:09:07.164477Z","iopub.execute_input":"2021-07-27T13:09:07.164734Z","iopub.status.idle":"2021-07-27T13:10:48.826904Z","shell.execute_reply.started":"2021-07-27T13:09:07.164708Z","shell.execute_reply":"2021-07-27T13:10:48.823328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !ls /kaggle/working/siim_work_dir","metadata":{"execution":{"iopub.status.busy":"2021-07-27T01:30:43.205942Z","iopub.status.idle":"2021-07-27T01:30:43.206843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %cd","metadata":{"execution":{"iopub.status.busy":"2021-07-27T01:30:43.208197Z","iopub.status.idle":"2021-07-27T01:30:43.209053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from mmdet.apis import show_result_pyplot\n# import matplotlib.pyplot as plt\n# checkpoint_file = '/kaggle/working/siim_work_dir/epoch_20.pth'\n\n# # checkpoint 저장된 model 파일을 이용하여 모델을 생성, 이때 Config는 위에서 update된 config 사용. \n# model_ckpt = init_detector(cfg, checkpoint_file, device='cuda:0')\n# # BGR Image 사용 \n# img = cv2.imread('/kaggle/working/siim/image/b8b0eda83a15_image.png')\n# # model_ckpt.cfg = cfg\n# print(img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T01:30:43.210431Z","iopub.status.idle":"2021-07-27T01:30:43.211258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# result = inference_detector(model_ckpt,img)\n# result\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-27T01:30:43.21261Z","iopub.status.idle":"2021-07-27T01:30:43.213444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show_result_pyplot(model_ckpt, img, result, score_thr=0.2)","metadata":{"execution":{"iopub.status.busy":"2021-07-27T01:30:43.214703Z","iopub.status.idle":"2021-07-27T01:30:43.215558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !echo 'val list #####'; cat /kaggle/working/siim/val.txt","metadata":{"execution":{"iopub.status.busy":"2021-07-27T01:30:43.217011Z","iopub.status.idle":"2021-07-27T01:30:43.217926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}