{"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":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# #import numpy as np # linear algebra\n# #import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n# import torch\n# import cv2 as cv\n# import numpy as np\n# import os\n# import pandas as pd\n# import pydicom\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n# '''\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n# '''\n# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-22T04:30:33.287007Z","iopub.execute_input":"2021-11-22T04:30:33.287596Z","iopub.status.idle":"2021-11-22T04:30:33.291582Z","shell.execute_reply.started":"2021-11-22T04:30:33.287558Z","shell.execute_reply":"2021-11-22T04:30:33.29085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Detection","metadata":{}},{"cell_type":"markdown","source":"## 1-1.Setup MMDetection Library for object Detection","metadata":{}},{"cell_type":"code","source":"# torch version is 1.7.0+cu110\n\n!pip install torch==1.7.0+cu110 torchvision==0.8.1+cu110 torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:52:21.219713Z","iopub.execute_input":"2021-12-05T07:52:21.219986Z","iopub.status.idle":"2021-12-05T07:54:21.708514Z","shell.execute_reply.started":"2021-12-05T07:52:21.219908Z","shell.execute_reply":"2021-12-05T07:54:21.707721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mmDetection version is 1.3.8\n\n!pip install mmcv-full==1.3.8 -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.html","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:54:21.710441Z","iopub.execute_input":"2021-12-05T07:54:21.7107Z","iopub.status.idle":"2021-12-05T07:54:41.18933Z","shell.execute_reply.started":"2021-12-05T07:54:21.710664Z","shell.execute_reply":"2021-12-05T07:54:41.188549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# install mmDetection in Kaggle env.\n\n!rm -rf mmdetection\n!git clone -b v2.18.1 https://github.com/open-mmlab/mmdetection.git\n!cd mmdetection && pip install -e .\n\n!pip install Pillow==7.0.0","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:54:41.190926Z","iopub.execute_input":"2021-12-05T07:54:41.191184Z","iopub.status.idle":"2021-12-05T07:55:18.113753Z","shell.execute_reply.started":"2021-12-05T07:54:41.191149Z","shell.execute_reply":"2021-12-05T07:55:18.112895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# setup sys.path for mmDetection in Kaggle env.\n\nimport sys\nprint(sys.path)\nsys.path.append('/kaggle/working/mmdetection/mmdet/models/detectors')\nsys.path.insert(0, \"./mmdetection\")\nprint(sys.path)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:18.120614Z","iopub.execute_input":"2021-12-05T07:55:18.120876Z","iopub.status.idle":"2021-12-05T07:55:18.141983Z","shell.execute_reply.started":"2021-12-05T07:55:18.120841Z","shell.execute_reply":"2021-12-05T07:55:18.141346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1-2 Setup wandb and Import wandb (A library which can track the training process and log our experiment results)\n\nI have saved my API token with \"wandb_key\" as Label. Please check the \"Add-ons->Secret\" button in the menu of this notebook. ","metadata":{}},{"cell_type":"code","source":"# Install wandb and Login wandb\n\n!pip install wandb --upgrade\n\nimport wandb\nfrom kaggle_secrets import UserSecretsClient\n\nuser_secrets = UserSecretsClient()\n\nwandb_api = user_secrets.get_secret(\"wandb_key\") \nwandb.login(key=wandb_api)\n\nwnb_username = 'sjs1999'\nwnb_project_name = 'siim-covid19-2'","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:18.146268Z","iopub.execute_input":"2021-12-05T07:55:18.147005Z","iopub.status.idle":"2021-12-05T07:55:31.929505Z","shell.execute_reply.started":"2021-12-05T07:55:18.146971Z","shell.execute_reply":"2021-12-05T07:55:31.928635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1-3 Import everything and Seed everything","metadata":{}},{"cell_type":"code","source":"# Import Everything\n\nimport sys\nimport os\nimport random\nimport numpy as np\nimport torch, torchvision\n\nfrom pathlib import Path\nfrom mmcv.ops import get_compiling_cuda_version, get_compiler_version\nfrom mmdet.apis import set_random_seed\n\nimport mmdet\nfrom mmdet.apis import set_random_seed\nfrom mmdet.datasets import build_dataset\nfrom mmdet.models import build_detector\nfrom mmdet.apis import train_detector\nfrom mmcv import Config","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:31.931016Z","iopub.execute_input":"2021-12-05T07:55:31.931255Z","iopub.status.idle":"2021-12-05T07:55:47.249117Z","shell.execute_reply.started":"2021-12-05T07:55:31.931217Z","shell.execute_reply":"2021-12-05T07:55:47.248202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Seed Everything\n\nglobal_seed = 20563228\n\ndef set_seed(seed=global_seed):\n    set_random_seed(seed, deterministic=True)  # mmdet random seed, deterministic=True to seed gpu/cudnn state\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    \nset_seed(global_seed)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:47.250803Z","iopub.execute_input":"2021-12-05T07:55:47.251402Z","iopub.status.idle":"2021-12-05T07:55:47.260619Z","shell.execute_reply.started":"2021-12-05T07:55:47.251357Z","shell.execute_reply":"2021-12-05T07:55:47.259343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2-1 Choose mm model","metadata":{}},{"cell_type":"code","source":"# Config Input/model/output \n\nbaseline_cfg_path = \"/kaggle/working/mmdetection/configs/cascade_rcnn/cascade_rcnn_x101_32x4d_fpn_1x_coco.py\"  # cascade-rcnn\nmodel_name = 'cascade_rcnn_x101_32x4d_fpn_1x'\nfold = 0  # fold can be set to 0-4\njob = 1\njob_folder = f'/kaggle/working/job{job}_{model_name}_fold{fold}'\n\nif not os.path.exists(job_folder):\n    os.makedirs(job_folder)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:47.261805Z","iopub.execute_input":"2021-12-05T07:55:47.262126Z","iopub.status.idle":"2021-12-05T07:55:47.272162Z","shell.execute_reply.started":"2021-12-05T07:55:47.262084Z","shell.execute_reply":"2021-12-05T07:55:47.271357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Config basic parameter    \n\ncfg = Config.fromfile(baseline_cfg_path)\ncfg.work_dir = job_folder\ncfg.seed = global_seed\ncfg.log_config.interval = 20 \ncfg.checkpoint_config.interval = 1 ","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:47.275439Z","iopub.execute_input":"2021-12-05T07:55:47.275634Z","iopub.status.idle":"2021-12-05T07:55:47.305618Z","shell.execute_reply.started":"2021-12-05T07:55:47.27561Z","shell.execute_reply":"2021-12-05T07:55:47.304992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Config wandb\n\ncfg.log_config.hooks = [dict(type='TextLoggerHook'),\n                        dict(type='WandbLoggerHook',\n                         init_kwargs=dict(project=wnb_project_name,\n                                          name=f'exp-{model_name}-fold{fold}-job{job}',\n                                          entity=wnb_username))\n                       ]","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:47.308485Z","iopub.execute_input":"2021-12-05T07:55:47.308739Z","iopub.status.idle":"2021-12-05T07:55:47.314563Z","shell.execute_reply.started":"2021-12-05T07:55:47.308707Z","shell.execute_reply":"2021-12-05T07:55:47.313891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2-2 Config the hyper-parameter","metadata":{}},{"cell_type":"code","source":"# Config pretrained/head\n\nfor head in cfg.model.roi_head.bbox_head:\n    head.num_classes = 1\n\ncfg.gpu_ids = [0]\ncfg.model.backbone.init_cfg=dict(type='Pretrained', checkpoint='open-mmlab://resnext101_32x4d')\ncfg.model.pop('pretrained', None)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:47.315599Z","iopub.execute_input":"2021-12-05T07:55:47.31579Z","iopub.status.idle":"2021-12-05T07:55:47.323865Z","shell.execute_reply.started":"2021-12-05T07:55:47.315767Z","shell.execute_reply":"2021-12-05T07:55:47.323142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Config hyper-parameters\n\ncfg.runner.max_epochs = 12\ncfg.total_epochs = 12\ncfg.optimizer.lr = 0.02/8\ncfg.lr_config = dict(\n    policy='CosineAnnealing', \n    by_epoch=False,\n    warmup='linear', \n    warmup_iters=500, \n    warmup_ratio=0.001, \n    min_lr=1e-07)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:47.325455Z","iopub.execute_input":"2021-12-05T07:55:47.325724Z","iopub.status.idle":"2021-12-05T07:55:47.33386Z","shell.execute_reply.started":"2021-12-05T07:55:47.325689Z","shell.execute_reply":"2021-12-05T07:55:47.333217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2-3 Config the training process and evaluation metrics\n\nOrganize the native dataset into (512x512) size and divide the data into 5-fold","metadata":{}},{"cell_type":"code","source":"# Config the dataset\n\ncfg.dataset_type = 'CocoDataset' \ncfg.classes = (\"Covid_Abnormality\",)\n\ncfg.data.train.img_prefix = '/kaggle/input/siim-covid19-512-images-and-metadata/train' \ncfg.data.train.classes = cfg.classes\ncfg.data.train.ann_file = f'/kaggle/input/siim-covid19-coco-512x512-groupkfold/train_annotations_fold{fold}.json'\ncfg.data.train.type='CocoDataset'\n\ncfg.data.val.img_prefix = '/kaggle/input/siim-covid19-512-images-and-metadata/train' \ncfg.data.val.classes = cfg.classes\ncfg.data.val.ann_file = f'/kaggle/input/siim-covid19-coco-512x512-groupkfold/val_annotations_fold{fold}.json'\ncfg.data.val.type='CocoDataset'\n\ncfg.data.test.img_prefix = '/kaggle/input/siim-covid19-512-images-and-metadata/train' \ncfg.data.test.classes = cfg.classes\ncfg.data.test.ann_file =  f'/kaggle/input/siim-covid19-coco-512x512-groupkfold/val_annotations_fold{fold}.json'\ncfg.data.test.type='CocoDataset'\n\ncfg.data.samples_per_gpu = 4 \ncfg.data.workers_per_gpu = 2 \n\ncfg.evaluation.metric = 'bbox' \ncfg.evaluation.interval = 1\ncfg.evaluation.iou_thrs = [0.5]","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:47.334892Z","iopub.execute_input":"2021-12-05T07:55:47.335181Z","iopub.status.idle":"2021-12-05T07:55:47.346279Z","shell.execute_reply.started":"2021-12-05T07:55:47.335156Z","shell.execute_reply":"2021-12-05T07:55:47.345402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2-4 Preprocess and augment the data","metadata":{}},{"cell_type":"code","source":"# Config the augmentation\n\nalbu_train_transforms = [\n    dict(type='ShiftScaleRotate', shift_limit=0.0625,\n         scale_limit=0.15, rotate_limit=15, p=0.4),\n    dict(type='RandomBrightnessContrast', brightness_limit=0.2,\n         contrast_limit=0.2, p=0.5),\n    dict(type='IAAAffine', shear=(-10.0, 10.0), p=0.4),\n    dict(type=\"Blur\", p=1.0, blur_limit=7),\n    dict(type='CLAHE', p=0.5),\n    dict(type='Equalize', mode='cv', p=0.4),\n    dict(\n        type=\"OneOf\",\n        transforms=[\n            dict(type=\"GaussianBlur\", p=1.0, blur_limit=7),\n            dict(type=\"MedianBlur\", p=1.0, blur_limit=7),\n        ],\n        p=0.4,\n    ),\n]","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:47.34771Z","iopub.execute_input":"2021-12-05T07:55:47.348233Z","iopub.status.idle":"2021-12-05T07:55:47.357184Z","shell.execute_reply.started":"2021-12-05T07:55:47.348196Z","shell.execute_reply":"2021-12-05T07:55:47.356456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Config the data pipeline\n\ncfg.train_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(type='LoadAnnotations', with_bbox=True, with_mask=True),\n    dict(type='Resize', img_scale=(1333, 800), keep_ratio=True),\n    dict(type='RandomFlip', flip_ratio=0.5),\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=dict(img='image', gt_bboxes='bboxes'),\n        update_pad_shape=False,\n        skip_img_without_anno=True),\n    dict(\n        type='Normalize',\n        mean=[123.675, 116.28, 103.53],\n        std=[58.395, 57.12, 57.375],\n        to_rgb=True),\n    dict(type='Pad', size_divisor=32),\n    dict(type='DefaultFormatBundle'),\n    dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks'])\n]\n\ncfg.test_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(\n        type='MultiScaleFlipAug',\n        img_scale=(1333, 800),\n        flip=False,\n        transforms=[\n            dict(type='Resize', keep_ratio=True),\n            dict(type='RandomFlip'),\n            dict(\n                type='Normalize',\n                mean=[123.675, 116.28, 103.53],\n                std=[58.395, 57.12, 57.375],\n                to_rgb=True),\n            dict(type='Pad', size_divisor=32),\n            dict(type='ImageToTensor', keys=['img']),\n            dict(type='Collect', keys=['img'])\n        ]\n    )\n]","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:47.358626Z","iopub.execute_input":"2021-12-05T07:55:47.359043Z","iopub.status.idle":"2021-12-05T07:55:47.371556Z","shell.execute_reply.started":"2021-12-05T07:55:47.359006Z","shell.execute_reply":"2021-12-05T07:55:47.370702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dump the config\n\ncfg_path = f'{job_folder}/job{job}_{Path(baseline_cfg_path).name}'\nprint(cfg_path)\n\ncfg.dump(cfg_path)\n# print(f'Config:\\n{cfg.pretty_text}')","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:47.372833Z","iopub.execute_input":"2021-12-05T07:55:47.373169Z","iopub.status.idle":"2021-12-05T07:55:48.666796Z","shell.execute_reply.started":"2021-12-05T07:55:47.373133Z","shell.execute_reply":"2021-12-05T07:55:48.665923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3 Build detector and Train","metadata":{}},{"cell_type":"code","source":"# Build the model\n\nmodel = build_detector(cfg.model,\n                       train_cfg=cfg.get('train_cfg'),\n                       test_cfg=cfg.get('test_cfg'))\nmodel.init_weights()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:55:48.668292Z","iopub.execute_input":"2021-12-05T07:55:48.668549Z","iopub.status.idle":"2021-12-05T07:56:16.64307Z","shell.execute_reply.started":"2021-12-05T07:55:48.668514Z","shell.execute_reply":"2021-12-05T07:56:16.642283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build the dataset\n\ndatasets = [build_dataset(cfg.data.train)]","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:56:16.644428Z","iopub.execute_input":"2021-12-05T07:56:16.644681Z","iopub.status.idle":"2021-12-05T07:56:16.731388Z","shell.execute_reply.started":"2021-12-05T07:56:16.644648Z","shell.execute_reply":"2021-12-05T07:56:16.730673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\n\ntrain_detector(model, datasets[0], cfg, distributed=False, validate=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T07:58:55.991478Z","iopub.execute_input":"2021-12-05T07:58:55.992248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the best epoch\n\nimport json\nfrom collections import defaultdict\n\nlog_file = f'{job_folder}/None.log.json'\n\ndef load_json_logs(json_logs):\n    log_dicts = [dict() for _ in json_logs]\n    for json_log, log_dict in zip(json_logs, log_dicts):\n        with open(json_log, 'r') as log_file:\n            for line in log_file:\n                log = json.loads(line.strip())\n                if 'epoch' not in log:\n                    continue\n                epoch = log.pop('epoch')\n                if epoch not in log_dict:\n                    log_dict[epoch] = defaultdict(list)\n                for k, v in log.items():\n                    log_dict[epoch][k].append(v)\n    return log_dicts\n\nlog_dict = load_json_logs([log_file])\nbest_epoch = np.argmax([item['bbox_mAP'][0] for item in log_dict[0].values()])+1\nbest_epoch","metadata":{"execution":{"iopub.status.busy":"2021-11-22T09:45:20.700597Z","iopub.execute_input":"2021-11-22T09:45:20.701181Z","iopub.status.idle":"2021-11-22T09:45:20.72568Z","shell.execute_reply.started":"2021-11-22T09:45:20.701143Z","shell.execute_reply":"2021-11-22T09:45:20.725002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Wandb visualization\n\nmodel_files = [f'{job_folder}/epoch_{best_epoch}.pth',\n               cfg_path\n              ]\n\nrun = wandb.init(project=wnb_project_name,\n                 name=f'models_files_{model_name}_fold{fold}_job{job}',\n                 entity=wnb_username,\n                 group='Artifact',\n                 job_type='model-files')\n\nartifact = wandb.Artifact(f'models_files_{model_name}_fold{fold}_job{job}', type='model')\n\nfor model_file in model_files:\n    artifact.add_file(model_file)\n\nrun.log_artifact(artifact)\nrun.finish()","metadata":{"execution":{"iopub.status.busy":"2021-11-22T10:26:39.937617Z","iopub.execute_input":"2021-11-22T10:26:39.938232Z","iopub.status.idle":"2021-11-22T10:26:55.113445Z","shell.execute_reply.started":"2021-11-22T10:26:39.938196Z","shell.execute_reply":"2021-11-22T10:26:55.11258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4 predict","metadata":{}},{"cell_type":"code","source":"# Import ALL\n\nimport mmcv\nfrom mmdet.models import build_detector\nfrom mmcv.runner import load_checkpoint\nfrom mmcv.parallel import MMDataParallel\nfrom mmdet.datasets import build_dataloader, build_dataset\nfrom mmdet.apis import single_gpu_test\nfrom mmdet.apis import init_detector, inference_detector, show_result_pyplot\n\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom pathlib import Path\nimport cv2\nimport json\nimport numpy as np\nimport os\nimport torch","metadata":{"execution":{"iopub.status.busy":"2021-11-22T10:56:42.279895Z","iopub.execute_input":"2021-11-22T10:56:42.280708Z","iopub.status.idle":"2021-11-22T10:56:42.291682Z","shell.execute_reply.started":"2021-11-22T10:56:42.280673Z","shell.execute_reply":"2021-11-22T10:56:42.290862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-1 Load data and model","metadata":{}},{"cell_type":"code","source":"# Load data\n\nwith open(\"../input/siim-covid19-coco-512x512-groupkfold/val_annotations_fold0.json\") as f:\n    val_ann = json.load(f)\nimagepaths = [item['file_name'] for item in val_ann['images'][:9]]\n\ndf_annotations = pd.read_csv('../input/siim-covid19-512-images-and-metadata/df_train_processed_meta.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-05T13:08:20.056665Z","iopub.execute_input":"2021-12-05T13:08:20.056944Z","iopub.status.idle":"2021-12-05T13:08:20.092852Z","shell.execute_reply.started":"2021-12-05T13:08:20.056914Z","shell.execute_reply":"2021-12-05T13:08:20.092147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Draw function\n\ndef draw_bbox(img, box, label, color, label_size=0.5, alpha_box=0.3, alpha_label=0.6):\n    \n    overlay_bbox = img.copy()\n    overlay_label = img.copy()\n    output = img.copy()\n\n    text_width, text_height = cv2.getTextSize(label.upper(), cv2.FONT_HERSHEY_SIMPLEX, label_size, 1)[0]\n    cv2.rectangle(overlay_bbox, (box[0], box[1]), (box[2], box[3]), color, -1)\n    cv2.addWeighted(overlay_bbox, alpha_box, output, 1-alpha_box, 0, output)\n    \n    cv2.rectangle(overlay_label, (box[0], box[1]-7-text_height), (box[0]+text_width+2, box[1]), (0, 0, 0), -1)\n    cv2.addWeighted(overlay_label, alpha_label, output, 1-alpha_label, 0, output)\n    output = cv2.rectangle(output, (box[0], box[1]), (box[2], box[3]), color, 2)\n    cv2.putText(output, label.upper(), (box[0], box[1]-5),\n            cv2.FONT_HERSHEY_SIMPLEX, label_size, (255, 255, 255), 1, cv2.LINE_AA)\n    return output","metadata":{"execution":{"iopub.status.busy":"2021-12-05T13:08:21.756762Z","iopub.execute_input":"2021-12-05T13:08:21.757496Z","iopub.status.idle":"2021-12-05T13:08:21.767162Z","shell.execute_reply.started":"2021-12-05T13:08:21.757457Z","shell.execute_reply":"2021-12-05T13:08:21.766122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load model\n\ncheckpoint = f'{job_folder}/epoch_{best_epoch}.pth'\n\nprint(\"Loading weights from:\", checkpoint)\ncfg = Config.fromfile(cfg_path)\nmodel = init_detector(cfg, checkpoint, device='cuda:0')","metadata":{"execution":{"iopub.status.busy":"2021-12-05T13:08:40.553840Z","iopub.execute_input":"2021-12-05T13:08:40.554558Z","iopub.status.idle":"2021-12-05T13:08:42.491966Z","shell.execute_reply.started":"2021-12-05T13:08:40.554524Z","shell.execute_reply":"2021-12-05T13:08:42.491192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-2 Model Visualization","metadata":{}},{"cell_type":"code","source":"# Heatmap Function\n\ndef featuremap_2_heatmap(feature_map):\n    assert isinstance(feature_map, torch.Tensor)\n    feature_map = feature_map.detach()\n    heatmap = feature_map[:,0,:,:]*0\n    heatmaps = []\n    for c in range(feature_map.shape[1]):\n        heatmap+=feature_map[:,c,:,:]\n    heatmap = heatmap.cpu().numpy()\n    heatmap = np.mean(heatmap, axis=0)\n\n    heatmap = np.maximum(heatmap, 0)\n    heatmap /= np.max(heatmap)\n    heatmaps.append(heatmap)\n\n    return heatmaps\n\ndef draw_feature_map(features,img,save_dir = 'feature_map',name = None):\n    i=0\n    if isinstance(features,torch.Tensor):\n        for heat_maps in features:\n            heat_maps = heat_maps.unsqueeze(0)\n            heatmaps = featuremap_2_heatmap(heat_maps)\n            for heatmap in heatmaps:\n                heatmap = np.uint8(255 * heatmap)\n                heatmap = cv2.resize(heatmap, (512, 512))  \n                heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)\n                superimposed_img = heatmap\n                plt.imshow(superimposed_img,cmap='gray')\n                plt.show()\n    else:\n        for featuremap in features:\n            heatmaps = featuremap_2_heatmap(featuremap)\n            for heatmap in heatmaps:\n                heatmap = np.uint8(255 * heatmap)\n                heatmap = cv2.resize(heatmap, (512, 512)) \n                \n                imGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n                superimposed_img =imGray / 255 + (heatmap)/255\n                plt.imshow(superimposed_img)\n                plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T13:08:45.317504Z","iopub.execute_input":"2021-12-05T13:08:45.317777Z","iopub.status.idle":"2021-12-05T13:08:45.328456Z","shell.execute_reply.started":"2021-12-05T13:08:45.317746Z","shell.execute_reply":"2021-12-05T13:08:45.327742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Heatmap Visualization\n\nfrom torchvision import transforms\n\nnew_size = (512, 512)\nimgs_path = \"/kaggle/input/siim-covid19-512-images-and-metadata/train\"\nthreshold = 0.45\n\n# fig, axes = plt.subplots(3,3, figsize=(19,21))\n# fig.subplots_adjust(hspace=0.2, wspace=0.2)\n# axes = axes.ravel()\n\nresults_list = []\n\nloader = transforms.Compose([\n    transforms.ToTensor()]) \n\nfor idx, img_id in enumerate(imagepaths):\n    img_path = os.path.join(imgs_path, img_id)\n    img = cv2.imread(img_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    \n    result = inference_detector(model, img_path)\n    feature_map = model.extract_feat(loader(img).unsqueeze(0).to('cuda:0'))\n    draw_feature_map(feature_map, img)\n    \n    results_filtered = result[0][result[0][:, 4]>threshold]\n    bboxes = results_filtered[:, :4]\n    scores = results_filtered[:, 4] \n    results_list.append(result[0])\n    \n    for box in bboxes:\n        img = draw_bbox(img, list(np.int_(box)), \"Covid_Abnormality\",\n                        (255, 243, 0))\n\n    axes[idx].imshow(img, cmap='gray')\n    axes[idx].set_title(img_id, size=18, pad=30)\n    axes[idx].set_xticklabels([])\n    axes[idx].set_yticklabels([])","metadata":{"execution":{"iopub.status.busy":"2021-12-05T13:16:48.008374Z","iopub.execute_input":"2021-12-05T13:16:48.009074Z","iopub.status.idle":"2021-12-05T13:17:00.093995Z","shell.execute_reply.started":"2021-12-05T13:16:48.009035Z","shell.execute_reply":"2021-12-05T13:17:00.093284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-3 Result Visualization","metadata":{}},{"cell_type":"code","source":"# Wandb Image Visualization\n\nrun = wandb.init(project=wnb_project_name,\n                 name=f'images-{model_name}-fold{fold}-job{job}',\n                 job_type='images')\n\nclass_id_to_label = {\n    1: \"pred_covid_abnormality\",\n    2: \"GT_covid_abnormality\"\n}\n\nwnb_images = []\n\nfor img_id, result in zip(imagepaths, results_list):\n    \n    bboxes = result[:, :4]\n    scores = result[:, 4]\n    ann_dict = {\"predictions\":{\n                        \"box_data\":[],\n                        \"class_labels\": class_id_to_label\n                        },\n                \"ground_truth\":{\n                        \"box_data\":[],\n                        \"class_labels\": class_id_to_label\n                        }\n                    }\n\n    for box, score in zip(bboxes, scores):\n        single_data = {\n            \"position\": {\n                \"minX\": round(float(box[0])/512, 3),\n                \"maxX\": round(float(box[2])/512, 3),\n                \"minY\": round(float(box[1])/512, 3),\n                \"maxY\": round(float(box[3])/512, 3),\n            },\n            \"class_id\" : 1,\n            \"box_caption\": class_id_to_label[1],\n            \"scores\" : {\n                \"confidence\": float(score),\n            }\n        }\n        ann_dict[\"predictions\"][\"box_data\"].append(single_data)\n\n    image_annotations = df_annotations[df_annotations.id==img_id.strip('.png')]\n\n    for idxx, row in image_annotations[['xmin', 'ymin', 'xmax', 'ymax']].iterrows():\n        single_data = {\n            \"position\": {\n                \"minX\": round(float(row[0])/512, 3),\n                \"maxX\": round(float(row[2])/512, 3),\n                \"minY\": round(float(row[1])/512, 3),\n                \"maxY\": round(float(row[3])/512, 3),\n            },\n            \"class_id\" : 2,\n            \"box_caption\": class_id_to_label[2],\n            \"scores\" : {\n                \"confidence\": 1.0,\n            }\n        }\n        ann_dict[\"ground_truth\"][\"box_data\"].append(single_data)\n\n    image = cv2.imread(os.path.join(imgs_path, img_id))\n    wnb_images.append(wandb.Image(image, boxes=ann_dict))\n    \nwandb.log({f'images-{model_name}-fold{fold}-job{job}': wnb_images})\n\nrun.finish()\nrun","metadata":{"execution":{"iopub.status.busy":"2021-12-05T13:08:59.969909Z","iopub.execute_input":"2021-12-05T13:08:59.970182Z","iopub.status.idle":"2021-12-05T13:09:12.029588Z","shell.execute_reply.started":"2021-12-05T13:08:59.970147Z","shell.execute_reply":"2021-12-05T13:09:12.028755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}