{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<span style=\"color: #E45D00; font-family: Segoe UI; font-size: 1.9em; font-weight: 300;\">📐 Setup MMDetection</span>","metadata":{}},{"cell_type":"code","source":"%%bash\n# Check nvcc version\nnvcc -V\necho\n# Check GCC version\ngcc --version\necho\n# Check the version of torch and cuda packages\npip list | grep \"torch\\|cuda\"","metadata":{"id":"320cDWMGgsVb","outputId":"86ab66f9-6812-4ba0-80e4-de882ba8cca1","execution":{"iopub.status.busy":"2022-02-24T10:44:20.098465Z","iopub.execute_input":"2022-02-24T10:44:20.098929Z","iopub.status.idle":"2022-02-24T10:44:22.348954Z","shell.execute_reply.started":"2022-02-24T10:44:20.098832Z","shell.execute_reply":"2022-02-24T10:44:22.347749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install mmcv-full==1.3.17 -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.html","metadata":{"id":"RG3ZA4TYhFUx","outputId":"fd3c8d2b-da23-43aa-a8b1-4dab01cfdfcc","_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-24T10:44:22.350828Z","iopub.execute_input":"2022-02-24T10:44:22.351231Z","iopub.status.idle":"2022-02-24T10:44:39.62992Z","shell.execute_reply.started":"2022-02-24T10:44:22.351187Z","shell.execute_reply":"2022-02-24T10:44:39.628968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# install packages\n# !rsync -a ../input/mmdetection-v280/mmdetection ../\n# !pip install ../input/mmdetection-v280/src/mmdet-2.8.0/mmdet-2.8.0/\n# !pip install ../input/mmdetection-v280/src/mmpycocotools-12.0.3/mmpycocotools-12.0.3/\n# !pip install ../input/mmdetection-v280/src/addict-2.4.0-py3-none-any.whl\n# !pip install ../input/mmdetection-v280/src/yapf-0.30.0-py2.py3-none-any.whl\n# !pip install ../input/mmdetection-v280/src/mmcv_full-1.2.6-cp37-cp37m-manylinux1_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:44:39.632852Z","iopub.execute_input":"2022-02-24T10:44:39.633231Z","iopub.status.idle":"2022-02-24T10:44:39.639557Z","shell.execute_reply.started":"2022-02-24T10:44:39.633198Z","shell.execute_reply":"2022-02-24T10:44:39.638679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf mmdetection\n!git clone https://github.com/open-mmlab/mmdetection.git\n!cd mmdetection && pip install -e .\n\n!pip install Pillow==7.0.0","metadata":{"id":"iJshzji7hHop","outputId":"7893d0dc-9fb0-46d2-c2b0-d95834125ff6","_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-24T10:44:39.641889Z","iopub.execute_input":"2022-02-24T10:44:39.642346Z","iopub.status.idle":"2022-02-24T10:45:25.215896Z","shell.execute_reply.started":"2022-02-24T10:44:39.642309Z","shell.execute_reply":"2022-02-24T10:45:25.214958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #E45D00; font-family: Segoe UI; font-size: 1.9em; font-weight: 300;\">Setup Weights & Biases</span>","metadata":{}},{"cell_type":"code","source":"!pip install wandb --upgrade","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-24T10:45:25.219165Z","iopub.execute_input":"2022-02-24T10:45:25.21948Z","iopub.status.idle":"2022-02-24T10:45:36.220297Z","shell.execute_reply.started":"2022-02-24T10:45:25.219447Z","shell.execute_reply":"2022-02-24T10:45:36.219243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\nTo connect the Kaggle Notebook and log in to Weights & Biases, we need to create an API key:\n\n1. New users can sign up for a Free Weights & Biases account for Research and Personal use from the https://wandb.ai/site page. Sign up process takes around 1-2 minutes.\n2. Now get the API key from https://wandb.ai/authorize.\n\nLogin to Weights & Biases from the notebook with the API key by using any of the two methods below:\n\n* Interative:\n    1. Run a cell with wandb.login(). It will ask for the API key, which can be copied and pasted to authenticate.\n\n* Kaggle Secrets:\n    1. The recommended way to use the API key is to use Kaggle Secrets to store the API key. From the top Menu on the Notebook Editor, click on 'Add-ons' and then, select 'Secrets'.\n    2. Select 'Add a new secret' and provide 'wandb_key' for label and it's value as the API key obtained from the previous steps.","metadata":{}},{"cell_type":"code","source":"import wandb\nfrom kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nsecret_value_0 = user_secrets.get_secret(\"wandb_key\")\nwandb.login(key=secret_value_0)\n# wandb.login('must')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:36.22186Z","iopub.execute_input":"2022-02-24T10:45:36.222207Z","iopub.status.idle":"2022-02-24T10:45:38.025277Z","shell.execute_reply.started":"2022-02-24T10:45:36.222168Z","shell.execute_reply":"2022-02-24T10:45:38.02415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #E45D00; font-family: Segoe UI; font-size: 1.9em; font-weight: 300;\">Imports and Seed Everything</span>","metadata":{}},{"cell_type":"code","source":"import sys\nsys.path.insert(0, \"./mmdetection\")\n\nimport os\nimport skimage.io as io\nimport matplotlib.pyplot as plt\n# Check Pytorch installation\nimport torch, torchvision\nprint(torch.__version__, torch.cuda.is_available())\n\nfrom pycocotools.coco import COCO\n\n# Check mmcv installation\nfrom mmcv.ops import get_compiling_cuda_version, get_compiler_version\nprint(get_compiling_cuda_version())\nprint(get_compiler_version())\n\n# Check MMDetection installation\nfrom mmdet.apis import set_random_seed\n\n# Imports\nimport mmdet\nprint(mmdet.__version__)\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\n\nimport random\nimport numpy as np\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:38.027148Z","iopub.execute_input":"2022-02-24T10:45:38.027507Z","iopub.status.idle":"2022-02-24T10:45:53.471841Z","shell.execute_reply.started":"2022-02-24T10:45:38.027465Z","shell.execute_reply":"2022-02-24T10:45:53.470821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# I am visualizing some images in the 'val/' directory\n\ndataDir='/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/coco_512x512/train_annotations_fold0_512x512.json'\n# dataType='train'\nmul_dataType='train'\n# annFile='{}/{}.json'.format(dataDir,dataType)\nmul_annFile='{}/{}.json'.format(dataDir,mul_dataType)\nimg_dir = '/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/train_512x512/'","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.473293Z","iopub.execute_input":"2022-02-24T10:45:53.473647Z","iopub.status.idle":"2022-02-24T10:45:53.478008Z","shell.execute_reply.started":"2022-02-24T10:45:53.473608Z","shell.execute_reply":"2022-02-24T10:45:53.477123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mul_coco=COCO(dataDir)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.481364Z","iopub.execute_input":"2022-02-24T10:45:53.481975Z","iopub.status.idle":"2022-02-24T10:45:53.579623Z","shell.execute_reply.started":"2022-02-24T10:45:53.481931Z","shell.execute_reply":"2022-02-24T10:45:53.578766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Multi Class #Parts dataset\n\nmul_cats = mul_coco.loadCats(mul_coco.getCatIds())\nmul_nms=[cat['name'] for cat in mul_cats]\nprint('COCO categories for parts: \\n{}\\n'.format(', '.join(mul_nms)))\n\n# mul_nms = set([mul_cat['supercategory'] for mul_cat in mul_cats])\n# print('COCO supercategories for parts: \\n{}\\n'.format(', '.join(mul_nms)))","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.581643Z","iopub.execute_input":"2022-02-24T10:45:53.582038Z","iopub.status.idle":"2022-02-24T10:45:53.587466Z","shell.execute_reply.started":"2022-02-24T10:45:53.581995Z","shell.execute_reply":"2022-02-24T10:45:53.586392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes=['Lung Opacity']\n# # get all images containing  for all category, select one at random\nall_catIds=[]\nall_imgIds=[]\nfor cls in classes:\n    catIds = mul_coco.getCatIds(catNms=cls)\n    all_catIds.append(catIds[0])\n    imgIds = mul_coco.getImgIds(catIds=catIds)\n    print(\"{} images length:{}\".format(cls,len(imgIds)))\n    all_imgIds.append(imgIds)\nprint(\"{} cats:\".format(all_catIds))","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.588838Z","iopub.execute_input":"2022-02-24T10:45:53.589381Z","iopub.status.idle":"2022-02-24T10:45:53.598606Z","shell.execute_reply.started":"2022-02-24T10:45:53.589336Z","shell.execute_reply":"2022-02-24T10:45:53.597586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs=mul_coco.loadImgs(ids=[1,2])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.600057Z","iopub.execute_input":"2022-02-24T10:45:53.600554Z","iopub.status.idle":"2022-02-24T10:45:53.606294Z","shell.execute_reply.started":"2022-02-24T10:45:53.600517Z","shell.execute_reply":"2022-02-24T10:45:53.605156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"anns_ids=mul_coco.getAnnIds(imgIds=[1,5])\nanns=mul_coco.loadAnns(ids=anns_ids)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.607846Z","iopub.execute_input":"2022-02-24T10:45:53.608202Z","iopub.status.idle":"2022-02-24T10:45:53.614647Z","shell.execute_reply.started":"2022-02-24T10:45:53.608167Z","shell.execute_reply":"2022-02-24T10:45:53.613756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the image\nimgId = mul_coco.getImgIds(imgIds = [222])\nimg = mul_coco.loadImgs(imgId)[0]\nprint(\"Image details \\n\",img)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.616049Z","iopub.execute_input":"2022-02-24T10:45:53.61654Z","iopub.status.idle":"2022-02-24T10:45:53.625025Z","shell.execute_reply.started":"2022-02-24T10:45:53.616499Z","shell.execute_reply":"2022-02-24T10:45:53.623985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get parts annotations\nmul_annIds = mul_coco.getAnnIds(imgIds=[222] ,iscrowd=None)\nmul_anns = mul_coco.loadAnns(mul_annIds)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.626682Z","iopub.execute_input":"2022-02-24T10:45:53.627144Z","iopub.status.idle":"2022-02-24T10:45:53.632438Z","shell.execute_reply.started":"2022-02-24T10:45:53.627104Z","shell.execute_reply":"2022-02-24T10:45:53.631181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a dictionary between category_id and category name\ncategory_map = {}\n\nfor ele in list(mul_coco.cats.values()):\n    category_map.update({ele['id']:ele['name']})","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.633991Z","iopub.execute_input":"2022-02-24T10:45:53.634415Z","iopub.status.idle":"2022-02-24T10:45:53.641176Z","shell.execute_reply.started":"2022-02-24T10:45:53.634378Z","shell.execute_reply":"2022-02-24T10:45:53.639981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create a list of parts in the image\nparts = []\nfor region in mul_anns:\n    parts.append(category_map[region['category_id']])\n\nprint(\"Parts are:\", parts) \n\n#Plot Parts\nI = io.imread(img_dir + '/' + img['file_name'])\nplt.imshow(I,cmap='gray')\nplt.axis('off')\nmul_coco.showAnns(mul_anns, draw_bbox=True )","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.642386Z","iopub.execute_input":"2022-02-24T10:45:53.643014Z","iopub.status.idle":"2022-02-24T10:45:53.774336Z","shell.execute_reply.started":"2022-02-24T10:45:53.642955Z","shell.execute_reply":"2022-02-24T10:45:53.773528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"global_seed = 42\n\ndef set_seed(seed=global_seed):\n    \"\"\"Sets the random seeds.\"\"\"\n    set_random_seed(seed, deterministic=False)\n    random.seed(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    os.environ['PYTHONHASHSEED'] = str(seed)\n    \nset_seed()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.775566Z","iopub.execute_input":"2022-02-24T10:45:53.775913Z","iopub.status.idle":"2022-02-24T10:45:53.788557Z","shell.execute_reply.started":"2022-02-24T10:45:53.775876Z","shell.execute_reply":"2022-02-24T10:45:53.787758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #E45D00; font-family: Segoe UI; font-size: 1.9em; font-weight: 300;\">🔨 Prepare the MMDetection Config</span>","metadata":{}},{"cell_type":"code","source":"from mmcv import Config\n\n# cfg = Config.fromfile('/kaggle/working/mmdetection/configs/vfnet/vfnet_r50_fpn_mdconv_c3-c5_mstrain_2x_coco.py')\n# cfg = Config.fromfile(\"/kaggle/working/mmdetection/configs/vfnet/vfnet_r50_fpn_mstrain_2x_coco.py\")\n# cfg = Config.fromfile(\"/kaggle/working/mmdetection/configs/gfl/gfl_r50_fpn_mstrain_2x_coco.py\")\n# baseline_cfg_path = \"/kaggle/working/mmdetection/configs/cascade_rcnn/cascade_rcnn_r50_fpn_20e_coco.py\"\nbaseline_cfg_path = \"/kaggle/working/mmdetection/configs/retinanet/retinanet_x101_64x4d_fpn_2x_coco.py\"\ncfg = Config.fromfile(baseline_cfg_path)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.790162Z","iopub.execute_input":"2022-02-24T10:45:53.790497Z","iopub.status.idle":"2022-02-24T10:45:53.816878Z","shell.execute_reply.started":"2022-02-24T10:45:53.790463Z","shell.execute_reply":"2022-02-24T10:45:53.81604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cfg","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.818763Z","iopub.execute_input":"2022-02-24T10:45:53.819337Z","iopub.status.idle":"2022-02-24T10:45:53.823608Z","shell.execute_reply.started":"2022-02-24T10:45:53.819301Z","shell.execute_reply":"2022-02-24T10:45:53.822336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.6em; font-weight: 300;\">General Training Settings</span>\n","metadata":{}},{"cell_type":"code","source":"# model_name = 'vfnet_r50_fpn'\n# model_name = 'cascade_rcnn_r50_fpn'\nmodel_name = 'retinanet_x101_64x4d_fpn_2x_coco_H_s5'\nfold = 0\njob = 4\n\n# Folder to store model logs and weight files\njob_folder = f'/kaggle/working/job{job}_{model_name}_fold{fold}'\ncfg.work_dir = job_folder\n\n# Change the wnd username and project name below\nwnb_username = 'ynhuhu'\nwnb_project_name = 'RSNA-Pneumonia-Detection'\n\n# Set seed thus the results are more reproducible\ncfg.seed = global_seed\n\nif not os.path.exists(job_folder):\n    os.makedirs(job_folder)\n\nprint(\"Job folder:\", job_folder)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.825887Z","iopub.execute_input":"2022-02-24T10:45:53.827775Z","iopub.status.idle":"2022-02-24T10:45:53.835383Z","shell.execute_reply.started":"2022-02-24T10:45:53.827743Z","shell.execute_reply":"2022-02-24T10:45:53.834305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(f'Config:\\n{cfg.pretty_text}')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.83756Z","iopub.execute_input":"2022-02-24T10:45:53.837873Z","iopub.status.idle":"2022-02-24T10:45:53.844773Z","shell.execute_reply.started":"2022-02-24T10:45:53.837847Z","shell.execute_reply":"2022-02-24T10:45:53.843916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.resume_from='/kaggle/input/rsna-mmdete-retinanet-x101l1-0th/epoch_48.pth'\n# for head in cfg.model.roi_head.bbox_head:\n# cfg.model.roi_head.bbox_head.num_classes = 1\n# cfg.model.roi_head.bbox_head.num_classes = 1\ncfg.model.bbox_head.num_classes = 1\n\n# cfg.gpu_ids = range(1)\ncfg.gpu_ids = [0]\n\n# Setting pretrained model in the init_cfg which is required \n# for transfer learning as per the latest MMdetection update\n# cfg.model.backbone.init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')\n# cfg.model.backbone.init_cfg=dict(type='Pretrained', checkpoint='open-mmlab://resnext101_32x4d')\ncfg.model.pop('pretrained', None)\n\ncfg.runner.max_epochs = 60 # Epochs for the runner that runs the workflow \ncfg.total_epochs = 60\n\ncfg.optimizer_config = dict(grad_clip=dict(max_norm=55, norm_type=2))\n\n# Learning rate of optimizers. The LR is divided by 8 since the config file is originally for 8 GPUs\ncfg.optimizer.lr = 0.01\n\n## Learning rate scheduler config used to register LrUpdater hook\ncfg.lr_config = dict(\n    policy='CosineAnnealing', # The policy of scheduler, also support CosineAnnealing, Cyclic, etc. Refer to details of supported LrUpdater from https://github.com/open-mmlab/mmcv/blob/master/mmcv/runner/hooks/lr_updater.py#L9.\n    by_epoch=False,\n    warmup='linear', # The warmup policy, also support `exp` and `constant`.\n    warmup_iters=200, # The number of iterations for warmup\n    warmup_ratio=0.001, # The ratio of the starting learning rate used for warmup\n    min_lr=1e-06)\n\n# config to register logger hook\ncfg.log_config.interval = 20 # Interval to print the log\n\n# Config to set the checkpoint hook, Refer to https://github.com/open-mmlab/mmcv/blob/master/mmcv/runner/hooks/checkpoint.py for implementation.\ncfg.checkpoint_config.interval = 1 # The save interval is 1","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.846316Z","iopub.execute_input":"2022-02-24T10:45:53.846991Z","iopub.status.idle":"2022-02-24T10:45:53.855937Z","shell.execute_reply.started":"2022-02-24T10:45:53.846946Z","shell.execute_reply":"2022-02-24T10:45:53.855031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.6em; font-weight: 300;\">Configure Datasets for Training and Evaluation</span>","metadata":{}},{"cell_type":"code","source":"img_scale=(640,640)\nINF = 1e8\n# cfg.model.backbone.depth=50\n# cfg.model.backbone.init_cfg.checkpoint='open-mmlab://resnext50_32x4d'\n# cfg.model.backbone.num_stages=4\n# cfg.model.backbone.out_indices=(0, 1, 2,3)\n\n# cfg.model.neck.in_channels=[256, 512,1024,2048]\n# cfg.model.neck.num_outs=6\n# cfg.model.bbox_head.regress_ranges=((-1, 32), (32,64),(64, 128), (128, 256), (256, 512),(512,INF))\n# cfg.model.bbox_head.stacked_convs=3\n# cfg.model.bbox_head.strides=[4, 8, 16,32,64,128]\n\n# cfg.model.bbox_head.norm_on_bbox=True\n# cfg.model.bbox_head.loss_bbox.type='GIoULoss'\n# cfg.model.bbox_head.loss_bbox.loss_weight=1.0\n\n# cfg.data.train.pipeline[2].img_scale=[(640,640),(800,800)]\n# cfg.data.val.pipeline[1].img_scale=[(800,800)]\n# cfg.data.test.pipeline[1].img_scale=[(800,800)]\n\ncfg.dataset_type = 'CocoDataset' # Dataset type, this will be used to define the dataset\ncfg.classes = (\"Lung Opacity\",)\n\ncfg.data.train.img_prefix = '/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/train_512x512/' # Prefix of image path\ncfg.data.train.classes = cfg.classes\ncfg.data.train.ann_file = f'/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/coco_512x512/train_annotations_fold{fold}_512x512.json'\ncfg.data.train.type='CocoDataset'\n\ncfg.data.val.img_prefix = '/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/train_512x512/' # Prefix of image path\ncfg.data.val.classes = cfg.classes\ncfg.data.val.ann_file = f'/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/coco_512x512/val_annotations_fold{fold}_512x512.json'\ncfg.data.val.type='CocoDataset'\n\ncfg.data.test.img_prefix = '/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/train_512x512/' # Prefix of image path\ncfg.data.test.classes = cfg.classes\ncfg.data.test.ann_file =  f'/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/coco_512x512/val_annotations_fold{fold}_512x512.json'\ncfg.data.test.type='CocoDataset'\n\ncfg.data.samples_per_gpu = 8 # Batch size of a single GPU used in testing\ncfg.data.workers_per_gpu = 4 # Worker to pre-fetch data for each single GPU","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.857956Z","iopub.execute_input":"2022-02-24T10:45:53.858588Z","iopub.status.idle":"2022-02-24T10:45:53.869286Z","shell.execute_reply.started":"2022-02-24T10:45:53.858553Z","shell.execute_reply":"2022-02-24T10:45:53.868281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.6em; font-weight: 300;\">Setting Metric for Evaluation</span>","metadata":{}},{"cell_type":"code","source":"# The config to build the evaluation hook, refer to https://github.com/open-mmlab/mmdetection/blob/master/mmdet/core/evaluation/eval_hooks.py#L7 for more details.\ncfg.evaluation.metric = 'bbox' # Metrics used during evaluation\n\n# Set the epoch intervel to perform evaluation\ncfg.evaluation.interval = 1\n\n# Set the iou threshold of the mAP calculation during evaluation\ncfg.evaluation.iou_thrs = [0.5]\ncfg.evaluation.classwise = True\n# cfg.evaluation.save_best='bbox_mAP_50'","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.87049Z","iopub.execute_input":"2022-02-24T10:45:53.871075Z","iopub.status.idle":"2022-02-24T10:45:53.882363Z","shell.execute_reply.started":"2022-02-24T10:45:53.871027Z","shell.execute_reply":"2022-02-24T10:45:53.881325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.6em; font-weight: 300;\">Prepare the Pre-processing & Augmentation Pipelines</span>","metadata":{}},{"cell_type":"code","source":"albu_train_transforms = [\n#     dict(type='Flip',p=0.5),\n#     dict(type='RandomCrop',crop_size=(0.98,0.98),crop_type='relative'),\n    dict(type='ShiftScaleRotate', shift_limit=0.0625,\n         scale_limit=0.15, rotate_limit=5, 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='MixUp', p=0.2, lambd=0.5),\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#     dict(type='Mosaic'),\n#     dict(type='CutOut',n_holes=[5,10],cutout_ratio=[0.04,0.08]),\n#     dict(type='MixUp', p=0.2, lambd=0.5),\n#     dict(type='RandomRotate90', p=0.5),\n#     dict(type='CLAHE', p=0.5),\n#     dict(type='InvertImg', p=0.5),\n#     dict(type='Equalize', mode='cv', p=0.4),\n#     dict(type='MedianBlur', blur_limit=3, p=0.1)\n    ]\n\n\ncfg.train_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(type='LoadAnnotations', with_bbox=True, with_mask=False),\n    dict(type='Resize', img_scale=img_scale, keep_ratio=True),\n    dict(type='RandomCrop',crop_size=(0.96,0.96),crop_type='relative'),\n    dict(type='CutOut',n_holes=(5,10),cutout_ratio=(0.04,0.08)),\n#     dict(type='MixUp'),\n    dict(type='RandomFlip', direction='horizontal',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={'img':'image', \n#                  'gt_masks': 'masks',\n                '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'],\n         meta_keys=('filename', 'ori_shape', 'img_shape', 'img_norm_cfg',\n                   'pad_shape', 'scale_factor'))\n]\ncfg.test_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(\n        type='MultiScaleFlipAug',\n        img_scale=(800,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]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.885134Z","iopub.execute_input":"2022-02-24T10:45:53.885382Z","iopub.status.idle":"2022-02-24T10:45:53.905562Z","shell.execute_reply.started":"2022-02-24T10:45:53.885351Z","shell.execute_reply":"2022-02-24T10:45:53.904147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #000508; font-family: Segoe UI; font-size: 1.6em; font-weight: 300;\">Weights & Biases Integration for Experiment Tracking and Logging</span>","metadata":{}},{"cell_type":"code","source":"## 4, 8\n# cfg.log_level = 'DEBUG'\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":"2022-02-24T10:45:53.910738Z","iopub.execute_input":"2022-02-24T10:45:53.910979Z","iopub.status.idle":"2022-02-24T10:45:53.916358Z","shell.execute_reply.started":"2022-02-24T10:45:53.910955Z","shell.execute_reply":"2022-02-24T10:45:53.915259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.data.train.pipeline=cfg.train_pipeline\ncfg.data.val.pipeline=cfg.test_pipeline\ncfg.data.test.pipeline=cfg.test_pipeline","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:53.918657Z","iopub.execute_input":"2022-02-24T10:45:53.919218Z","iopub.status.idle":"2022-02-24T10:45:53.925431Z","shell.execute_reply.started":"2022-02-24T10:45:53.919183Z","shell.execute_reply":"2022-02-24T10:45:53.924681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #2220508; font-family: Segoe UI; font-size: 1.6em; font-weight: 300;\">Save Config File</span>","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"cfg_path = f'{job_folder}/job{job}_{Path(baseline_cfg_path).name}'\nprint(cfg_path)\n\n# Save config file for inference later\ncfg.dump(cfg_path)\nprint(f'Config:\\n{cfg.pretty_text}')","metadata":{"id":"9C8s78L_hY2P","outputId":"5c532360-7684-42e1-bb2b-e59377576c2e","_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-24T10:45:53.92719Z","iopub.execute_input":"2022-02-24T10:45:53.927825Z","iopub.status.idle":"2022-02-24T10:45:56.178188Z","shell.execute_reply.started":"2022-02-24T10:45:53.927734Z","shell.execute_reply":"2022-02-24T10:45:56.177185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #E45D00; font-family: Segoe UI; font-size: 1.9em; font-weight: 300;\">🚀 Build Dataset and Start Training</span>","metadata":{}},{"cell_type":"code","source":"model = build_detector(cfg.model,\n                       train_cfg=cfg.get('train_cfg'),\n                       test_cfg=cfg.get('test_cfg'))\n# model.init_weights()","metadata":{"id":"o6u_0gZuhcUy","outputId":"557284a4-a687-474a-aa4d-2abbb8fb403c","execution":{"iopub.status.busy":"2022-02-24T10:45:56.179997Z","iopub.execute_input":"2022-02-24T10:45:56.180547Z","iopub.status.idle":"2022-02-24T10:45:57.411776Z","shell.execute_reply.started":"2022-02-24T10:45:56.180505Z","shell.execute_reply":"2022-02-24T10:45:57.410859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-24T10:45:57.413184Z","iopub.execute_input":"2022-02-24T10:45:57.413565Z","iopub.status.idle":"2022-02-24T10:45:57.425612Z","shell.execute_reply.started":"2022-02-24T10:45:57.413527Z","shell.execute_reply":"2022-02-24T10:45:57.424561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.bbox_head.conv_reg=torch.nn.Conv2d(256, 3, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n# model.backbone=torch.nn.Sequential((*list(model.backbone.children())[:-1]))","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:57.427132Z","iopub.execute_input":"2022-02-24T10:45:57.427784Z","iopub.status.idle":"2022-02-24T10:45:57.431451Z","shell.execute_reply.started":"2022-02-24T10:45:57.427748Z","shell.execute_reply":"2022-02-24T10:45:57.430354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:57.432891Z","iopub.execute_input":"2022-02-24T10:45:57.433602Z","iopub.status.idle":"2022-02-24T10:45:57.439997Z","shell.execute_reply.started":"2022-02-24T10:45:57.433563Z","shell.execute_reply":"2022-02-24T10:45:57.439193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datasets = [build_dataset(cfg.data.train)]","metadata":{"id":"om_JbWv9heIQ","outputId":"0f264828-5b49-4a30-b035-9635424cd2d1","execution":{"iopub.status.busy":"2022-02-24T10:45:57.441413Z","iopub.execute_input":"2022-02-24T10:45:57.442289Z","iopub.status.idle":"2022-02-24T10:45:57.539568Z","shell.execute_reply.started":"2022-02-24T10:45:57.442246Z","shell.execute_reply":"2022-02-24T10:45:57.538365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datasets","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:57.540719Z","iopub.execute_input":"2022-02-24T10:45:57.541052Z","iopub.status.idle":"2022-02-24T10:45:57.818869Z","shell.execute_reply.started":"2022-02-24T10:45:57.541017Z","shell.execute_reply":"2022-02-24T10:45:57.817943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-size: 1.3em; font-weight: 300;\">📌 Visit the Run page link in the cell below Ex: \"https://wandb.ai/sreevishnu-damodaran/siim-covid19-1/runs/<run-id\\>\" as soon as the training starts to see the metrics live seamlessly in the Weights & Biases Dashboard.</span>\n    \n<span style=\"font-size: 1.3em; font-weight: 300;\">The projects page <a href=\"https://wandb.ai/sreevishnu-damodaran/siim-covid19-1\">https://wandb.ai/sreevishnu-damodaran/siim-covid19-1</a> compares it with other training jobs.</span>","metadata":{}},{"cell_type":"code","source":"# !ls ./job4_fcos_x101_64x4d_fpn_gn-head_mstrain_640-800_2x_coco_fold0","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:57.820168Z","iopub.execute_input":"2022-02-24T10:45:57.820507Z","iopub.status.idle":"2022-02-24T10:45:57.824157Z","shell.execute_reply.started":"2022-02-24T10:45:57.820471Z","shell.execute_reply":"2022-02-24T10:45:57.823314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !python mmdetection/tools/misc/browse_dataset.py job4_fcos_x101_64x4d_fpn_gn-head_mstrain_640-800_2x_coco_fold0/job4_fcos_x101_64x4d_fpn_gn-head_mstrain_640-800_2x_coco.py","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:45:57.825504Z","iopub.execute_input":"2022-02-24T10:45:57.826081Z","iopub.status.idle":"2022-02-24T10:45:57.833286Z","shell.execute_reply.started":"2022-02-24T10:45:57.826033Z","shell.execute_reply":"2022-02-24T10:45:57.832426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_detector(model, datasets[0], cfg, distributed=False, validate=True)","metadata":{"id":"anIjmmhVhgKE","outputId":"9a97104b-5d08-4aa7-ce9c-be376e789ea5","execution":{"iopub.status.busy":"2022-02-24T10:45:57.834391Z","iopub.execute_input":"2022-02-24T10:45:57.834808Z","iopub.status.idle":"2022-02-24T10:49:28.269438Z","shell.execute_reply.started":"2022-02-24T10:45:57.834766Z","shell.execute_reply":"2022-02-24T10:49:28.266281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n\n<span style=\"font-size: 1.3em; font-weight: 300;\">📌 Please visit the Weights and Biases Dashboard (<a href=\"https://wandb.ai/sreevishnu-damodaran/siim-covid19-1\">https://wandb.ai/sreevishnu-damodaran/siim-covid19-1</a>) to see the current training progress and the results of some experiments which I ran previously.</span>","metadata":{}},{"cell_type":"code","source":"# Get the best epoch number\nimport json\nfrom collections import defaultdict\n\nlog_file = f'{job_folder}/None.log.json'\n\n# Source: mmdetection/tools/analysis_tools/analyze_logs.py \ndef load_json_logs(json_logs):\n    # load and convert json_logs to log_dict, key is epoch, value is a sub dict\n    # keys of sub dict is different metrics, e.g. memory, bbox_mAP\n    # value of sub dict is a list of corresponding values of all iterations\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 idx,line in enumerate(log_file):\n                if idx==0:\n                    continue\n                else:\n                    log = json.loads(line.strip())\n                    # skip lines without `epoch` field\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])\n# [(print(inner['bbox_mAP']) for inner in item) for item in log_dict]\n# [print(item) for item in log_dict[0]]\nbest_epoch = np.argmax([item['bbox_mAP'][0] for item in log_dict[0].values()])+49\nbest_epoch","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:49:28.270661Z","iopub.status.idle":"2022-02-24T10:49:28.271145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# best_epoch=1","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:49:28.272192Z","iopub.status.idle":"2022-02-24T10:49:28.272641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_files = [f'{job_folder}/epoch_{best_epoch}.pth',\n               cfg_path\n              ]\n\n# Create a new wnb run for saving models as artifacts\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":"2022-02-24T10:49:28.273564Z","iopub.status.idle":"2022-02-24T10:49:28.274274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #E45D00; font-family: Segoe UI; font-size: 1.9em; font-weight: 300;\">📰 Inference and Visualize Output</span>","metadata":{}},{"cell_type":"code","source":"import 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","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:49:28.275382Z","iopub.status.idle":"2022-02-24T10:49:28.27611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(f'/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/coco_512x512/val_annotations_fold{fold}_512x512.json') as f:\n    val_ann = json.load(f)\nimagepaths = [item['file_name'] for item in val_ann['images'][:16]]\n\ndf_annotations = pd.read_csv('/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/images_metadata_256_512_768/df_train_processed_meta_512x512.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:49:28.277299Z","iopub.status.idle":"2022-02-24T10:49:28.277898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_bbox(image,\n              box,\n              label,\n              color,\n              label_size = 0.5,\n              alpha_box = 0.3,\n              alpha_label = 0.6):\n    \n    overlay_bbox = image.copy()\n    overlay_label = image.copy()\n    output = image.copy()\n\n    text_width, text_height = cv2.getTextSize(label.upper(),\n                                              cv2.FONT_HERSHEY_SIMPLEX, label_size, 1)[0]\n    cv2.rectangle(overlay_bbox, (box[0], box[1]), (box[2], box[3]),\n                  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),\n                  (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]),\n                           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":"2022-02-24T10:49:28.279045Z","iopub.status.idle":"2022-02-24T10:49:28.27968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = 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":"2022-02-24T10:49:28.281109Z","iopub.status.idle":"2022-02-24T10:49:28.281772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_size = (512, 512)\nimgs_path = '/kaggle/input/rsna-pneumonia-detection-process-coco-dataset/train_512x512/'\nthreshold = 0.45\n\nfig, axes = plt.subplots(4,4, figsize=(19,21))\nfig.subplots_adjust(hspace=0.2, wspace=0.2)\naxes = axes.ravel()\n\nresults_list = []\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    result = inference_detector(model, img_path)\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)), \"Lung_Opacity\",\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":"2022-02-24T10:49:28.284849Z","iopub.status.idle":"2022-02-24T10:49:28.285759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #E45D00; font-family: Segoe UI; font-size: 1.9em; font-weight: 300;\">Interactively Visualize & Analyze Output in Dashboard</span>","metadata":{}},{"cell_type":"code","source":"run = 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_lung_opacity\",\n    2: \"GT_lung_opacity\"\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            # one box expressed in the default relative/fractional domain\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.patientId==img_id.strip('.png')]\n\n    for idxx, row in image_annotations[['xmin', 'ymin', 'xmax', 'ymax']].iterrows():\n        single_data = {\n            # one box expressed in the default relative/fractional domain\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":"2022-02-24T10:49:28.287096Z","iopub.status.idle":"2022-02-24T10:49:28.287925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf mmdetection/","metadata":{"execution":{"iopub.status.busy":"2022-02-24T10:49:28.289154Z","iopub.status.idle":"2022-02-24T10:49:28.289953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #E45D00; font-family: Segoe UI; font-size: 1.9em; font-weight: 300;\">Additional Resources</span>\n\n&nbsp;&nbsp;🔖&nbsp;&nbsp;[MMDetection Documentation](https://mmdetection.readthedocs.io/en/latest/)\n\n&nbsp;&nbsp;🔖&nbsp;&nbsp;[MDetection Github Repository](https://github.com/open-mmlab/mmdetection)\n\n&nbsp;&nbsp;🔖&nbsp;&nbsp;[Weights & Biases Documentation](https://docs.wandb.ai/)\n\n&nbsp;&nbsp;🔖&nbsp;&nbsp;[Weights & Biases Github Repository](https://github.com/wandb/examples)\n","metadata":{}},{"cell_type":"markdown","source":"<p style='text-align: center;'><span style=\"color: #000508; font-family: Segoe UI; font-size: 1.4em; font-weight: 300;\">Let me know if you have any suggestions!</span></p>\n\n<p style='text-align: center;'><span style=\"color: #000508; font-family: Segoe UI; font-size: 2.0em; font-weight: 300;\">THANKS!</span></p>","metadata":{}}]}