{"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":"## Reference \n\n1. MMDetection Inspired Notebooks:\n    1. https://www.kaggle.com/awsaf49/sartorius-mmdetection-train\n    1. https://www.kaggle.com/its7171/mmdetection-for-segmentation-training\n    1. https://www.kaggle.com/vexxingbanana/sartorius-mmdetection-training\n    1. https://www.kaggle.com/remekkinas/yolox-training-pipeline-cots-dataset-lb-0-507\n    1. https://www.kaggle.com/superkevingit/faster-rcnn-with-mmdetection-without-internet\n    1. \n1. Yolox COCO Dataset : [Efficient Coco datasest generator](https://www.kaggle.com/coldfir3/simple-yolox-dataset-generator-coco-json)\n1. Torch Dataset\n    1. https://www.kaggle.com/ttjccc/pytorch-190","metadata":{}},{"cell_type":"markdown","source":"# LOGS\n1. [Inference Notebook](https://www.kaggle.com/mlneo07/mmdetection-swin-transfomer-frcnn-inference)\n1. Minor Bugs Fixes\n1. Changed Tiny --> Small transformer\n1. New loss_bbox=dict(type='GIoULoss', loss_weight=10.0)))) ; IoULoss --> GIoULoss\n1. FasterRCNN -- > CascadeRCNN\n1.    \n    train_cfg=dict(\n        rpn=dict(sampler=dict(neg_pos_ub=5), allowed_border=-1),\n        rcnn=dict(\n            sampler=dict(\n                _delete_=True,\n                type='CombinedSampler',\n                num=512,\n                pos_fraction=0.25,\n                add_gt_as_proposals=True,\n                pos_sampler=dict(type='InstanceBalancedPosSampler'),\n                neg_sampler=dict(\n                    type='IoUBalancedNegSampler',\n                    floor_thr=-1,\n                    floor_fraction=0,\n                    num_bins=3)))))","metadata":{}},{"cell_type":"markdown","source":"# **Install MMDetection and MMDetection-Compatible Torch**","metadata":{"papermill":{"duration":0.01534,"end_time":"2021-12-27T08:57:13.469303","exception":false,"start_time":"2021-12-27T08:57:13.453963","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!pip install '../input/pytorch-190/torch-1.9.0+cu111-cp37-cp37m-linux_x86_64.whl' --no-deps\n# !pip install '../input/pytorch-190/torchvision-0.10.0+cu111-cp37-cp37m-linux_x86_64.whl' --no-deps","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"papermill":{"duration":89.958783,"end_time":"2021-12-27T08:58:43.441953","exception":false,"start_time":"2021-12-27T08:57:13.48317","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:31:53.114286Z","iopub.execute_input":"2022-01-09T09:31:53.114628Z","iopub.status.idle":"2022-01-09T09:33:16.139045Z","shell.execute_reply.started":"2022-01-09T09:31:53.114538Z","shell.execute_reply":"2022-01-09T09:33:16.138086Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf mmdetection\n!git clone https://github.com/open-mmlab/mmdetection.git /kaggle/working/mmdetection","metadata":{"_kg_hide-output":true,"papermill":{"duration":6.754333,"end_time":"2021-12-27T08:58:50.210754","exception":false,"start_time":"2021-12-27T08:58:43.456421","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:33:16.140939Z","iopub.execute_input":"2022-01-09T09:33:16.141236Z","iopub.status.idle":"2022-01-09T09:33:20.067161Z","shell.execute_reply.started":"2022-01-09T09:33:16.141197Z","shell.execute_reply":"2022-01-09T09:33:20.066244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install openmim\n!mim install mmdet","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"papermill":{"duration":77.446822,"end_time":"2021-12-27T09:00:07.724044","exception":false,"start_time":"2021-12-27T08:58:50.277222","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:33:20.068802Z","iopub.execute_input":"2022-01-09T09:33:20.069323Z","iopub.status.idle":"2022-01-09T09:34:47.447187Z","shell.execute_reply.started":"2022-01-09T09:33:20.069284Z","shell.execute_reply":"2022-01-09T09:34:47.446358Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Import Libraries** ","metadata":{"papermill":{"duration":0.022144,"end_time":"2021-12-27T08:58:50.255188","exception":false,"start_time":"2021-12-27T08:58:50.233044","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# import sys\n# sys.path.append('./mmdetection')\n\nimport numpy as np\nimport os\nimport pandas as pd\n\nimport mmdet\nimport mmcv\n\nfrom mmdet.datasets import build_dataset\nfrom mmdet.models import build_detector\nfrom mmdet.apis import train_detector\nfrom mmcv import Config\nfrom mmdet.apis import inference_detector, init_detector, set_random_seed","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":18.085243,"end_time":"2021-12-27T09:00:25.911507","exception":false,"start_time":"2021-12-27T09:00:07.826264","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:34:47.451258Z","iopub.execute_input":"2022-01-09T09:34:47.451493Z","iopub.status.idle":"2022-01-09T09:35:05.594404Z","shell.execute_reply.started":"2022-01-09T09:34:47.451454Z","shell.execute_reply":"2022-01-09T09:35:05.593565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 3047\nset_random_seed(seed, deterministic=False)","metadata":{"papermill":{"duration":0.159391,"end_time":"2021-12-27T09:00:26.174434","exception":false,"start_time":"2021-12-27T09:00:26.015043","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:35:05.598936Z","iopub.execute_input":"2022-01-09T09:35:05.601270Z","iopub.status.idle":"2022-01-09T09:35:05.608765Z","shell.execute_reply.started":"2022-01-09T09:35:05.601228Z","shell.execute_reply":"2022-01-09T09:35:05.608139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Helper Functions**","metadata":{"papermill":{"duration":0.100518,"end_time":"2021-12-27T09:00:26.375843","exception":false,"start_time":"2021-12-27T09:00:26.275325","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import zipfile\n\nos.makedirs('./images') \nwith zipfile.ZipFile('../input/simple-yolox-dataset-generator-coco-json/train2017.zip', 'r') as zip_ref:\n    zip_ref.extractall('./images')\n    \nwith zipfile.ZipFile('../input/simple-yolox-dataset-generator-coco-json/val2017.zip', 'r') as zip_ref:\n    zip_ref.extractall('./images')","metadata":{"papermill":{"duration":41.756625,"end_time":"2021-12-27T09:01:08.234845","exception":false,"start_time":"2021-12-27T09:00:26.47822","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:35:05.614443Z","iopub.execute_input":"2022-01-09T09:35:05.615058Z","iopub.status.idle":"2022-01-09T09:35:55.262499Z","shell.execute_reply.started":"2022-01-09T09:35:05.614995Z","shell.execute_reply":"2022-01-09T09:35:55.261741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile labels.txt \ncots","metadata":{"papermill":{"duration":0.110613,"end_time":"2021-12-27T09:01:08.449002","exception":false,"start_time":"2021-12-27T09:01:08.338389","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:35:55.263645Z","iopub.execute_input":"2022-01-09T09:35:55.265708Z","iopub.status.idle":"2022-01-09T09:35:55.270728Z","shell.execute_reply.started":"2022-01-09T09:35:55.265676Z","shell.execute_reply":"2022-01-09T09:35:55.270073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Config","metadata":{"papermill":{"duration":0.100277,"end_time":"2021-12-27T09:01:08.649654","exception":false,"start_time":"2021-12-27T09:01:08.549377","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%%writefile ./mmdetection/configs/swin/TFGBR_swin_base_faster_rcnn_fp16.py\n\n_base_ = [\n    '../_base_/models/cascade_rcnn_r50_fpn.py',\n    '../_base_/datasets/coco_detection.py',\n    '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'\n]\npretrained = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_small_patch4_window7_224.pth'\nmodel = dict(\n    backbone=dict(\n        _delete_=True,\n        type='SwinTransformer',\n        embed_dims=96,\n        depths=[2, 2, 18, 2],\n        num_heads=[3, 6, 12, 24],\n        window_size=7,\n        mlp_ratio=4,\n        qkv_bias=True,\n        qk_scale=None,\n        drop_rate=0.,\n        attn_drop_rate=0.,\n        drop_path_rate=0.3,\n        patch_norm=True,\n        out_indices=(0, 1, 2, 3),\n        with_cp=False,\n        convert_weights=True,\n        init_cfg=dict(type='Pretrained', checkpoint=pretrained)),\n    neck=dict(in_channels=[96, 192, 384, 768]),\n    roi_head=dict(\n        type='CascadeRoIHead',\n        num_stages=3,\n        stage_loss_weights=[1, 0.5, 0.25],\n        bbox_roi_extractor=dict(\n            type='SingleRoIExtractor',\n            roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0),\n            out_channels=256,\n            featmap_strides=[4, 8, 16, 32]),\n        bbox_head=[\n            dict(\n                type='Shared2FCBBoxHead',\n                in_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=1,\n                bbox_coder=dict(\n                    type='DeltaXYWHBBoxCoder',\n                    target_means=[0., 0., 0., 0.],\n                    target_stds=[0.1, 0.1, 0.2, 0.2]),\n                reg_class_agnostic=True,\n                loss_cls=dict(\n                    type='CrossEntropyLoss',\n                    use_sigmoid=False,\n                    loss_weight=1.0),\n                reg_decoded_bbox=True,\n                loss_bbox=dict(type='GIoULoss', loss_weight=10.0)),\n            dict(\n                type='Shared2FCBBoxHead',\n                in_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=1,\n                bbox_coder=dict(\n                    type='DeltaXYWHBBoxCoder',\n                    target_means=[0., 0., 0., 0.],\n                    target_stds=[0.05, 0.05, 0.1, 0.1]),\n                reg_class_agnostic=True,\n                loss_cls=dict(\n                    type='CrossEntropyLoss',\n                    use_sigmoid=False,\n                    loss_weight=1.0),\n                reg_decoded_bbox=True,\n                loss_bbox=dict(type='GIoULoss', loss_weight=10.0)),\n            dict(\n                type='Shared2FCBBoxHead',\n                in_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=1,\n                bbox_coder=dict(\n                    type='DeltaXYWHBBoxCoder',\n                    target_means=[0., 0., 0., 0.],\n                    target_stds=[0.033, 0.033, 0.067, 0.067]),\n                reg_class_agnostic=True,\n                loss_cls=dict(\n                    type='CrossEntropyLoss',\n                    use_sigmoid=False,\n                    loss_weight=1.0),\n                reg_decoded_bbox=True,\n                loss_bbox=dict(type='GIoULoss', loss_weight=10.0))\n        ]))\n\noptimizer = dict(\n    _delete_=True,\n    type='AdamW',\n    lr=0.0004,\n    betas=(0.9, 0.999),\n    weight_decay=0.05,\n    paramwise_cfg=dict(\n        custom_keys={\n            'absolute_pos_embed': dict(decay_mult=0.),\n            'relative_position_bias_table': dict(decay_mult=0.),\n            'norm': dict(decay_mult=0.)\n        }))\nlr_config = dict(warmup_iters=500, step=[8, 11])\nrunner = dict(max_epochs=14)","metadata":{"papermill":{"duration":0.109025,"end_time":"2021-12-27T09:01:08.859858","exception":false,"start_time":"2021-12-27T09:01:08.750833","status":"completed"},"scrolled":true,"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:38:55.692809Z","iopub.execute_input":"2022-01-09T09:38:55.693141Z","iopub.status.idle":"2022-01-09T09:38:55.700526Z","shell.execute_reply.started":"2022-01-09T09:38:55.693091Z","shell.execute_reply":"2022-01-09T09:38:55.699697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = Config.fromfile('./mmdetection/configs/swin/TFGBR_swin_base_faster_rcnn_fp16.py')","metadata":{"papermill":{"duration":0.121235,"end_time":"2021-12-27T09:01:09.081797","exception":false,"start_time":"2021-12-27T09:01:08.960562","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:38:56.553362Z","iopub.execute_input":"2022-01-09T09:38:56.554176Z","iopub.status.idle":"2022-01-09T09:38:56.580922Z","shell.execute_reply.started":"2022-01-09T09:38:56.554128Z","shell.execute_reply":"2022-01-09T09:38:56.580208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_norm_cfg = dict(\n    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)\ntrain_pipeline = [\n    dict(type='LoadImageFromFile',to_float32=True),\n    dict(type='LoadAnnotations', with_bbox=True),\n    dict(\n        type='AutoAugment',\n        policies=[[\n            dict(\n                type='Resize',\n                img_scale=[(480, 1333), (512, 1333), (544, 1333), (576, 1333),\n                           (608, 1333), (640, 1333), (672, 1333), (704, 1333),\n                           (736, 1333), (768, 1333), (800, 1333)],\n                multiscale_mode='value',\n                keep_ratio=True)\n        ],\n                  [\n                      dict(\n                          type='Resize',\n                          img_scale=[(400, 1333), (500, 1333), (600, 1333)],\n                          multiscale_mode='value',\n                          keep_ratio=True),\n                      dict(\n                          type='RandomCrop',\n                          crop_type='absolute_range',\n                          crop_size=(384, 600),\n                          allow_negative_crop=True),\n                      dict(\n                          type='Resize',\n                          img_scale=[(480, 1333), (512, 1333), (544, 1333),\n                                     (576, 1333), (608, 1333), (640, 1333),\n                                     (672, 1333), (704, 1333), (736, 1333),\n                                     (768, 1333), (800, 1333)],\n                          multiscale_mode='value',\n                          override=True,\n                          keep_ratio=True),\n                      dict(\n                            type='PhotoMetricDistortion',\n                            brightness_delta=32,\n                            contrast_range=(0.5, 1.5),\n                            saturation_range=(0.5, 1.5),\n                            hue_delta=18),\n                    dict(\n                            type='MinIoURandomCrop',\n                            min_ious=(0.4, 0.5, 0.6, 0.7, 0.8, 0.9),\n                            min_crop_size=0.3),\n                    dict(\n                            type='CutOut',\n                            n_holes=(5, 10),\n                            cutout_shape=[(4, 4), (4, 8), (8, 4), (8, 8),\n                                          (16, 32), (32, 16), (32, 32),\n                                          (32, 48), (48, 32), (48, 48)]\n                            )\n                  ]]),\n    dict(type='RandomFlip', flip_ratio=0.5),\n    dict(type='Normalize', **img_norm_cfg),\n    dict(type='Pad', size_divisor=32),\n    dict(type='DefaultFormatBundle'),\n    dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']),\n]\n\ntest_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(type='Normalize', **img_norm_cfg),\n            dict(type='Pad', size_divisor=32),\n            dict(type='DefaultFormatBundle'),\n            dict(type='Collect', keys=['img'])\n        ])\n]","metadata":{"papermill":{"duration":0.120272,"end_time":"2021-12-27T09:01:09.303634","exception":false,"start_time":"2021-12-27T09:01:09.183362","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:38:57.469700Z","iopub.execute_input":"2022-01-09T09:38:57.469959Z","iopub.status.idle":"2022-01-09T09:38:57.485730Z","shell.execute_reply.started":"2022-01-09T09:38:57.469930Z","shell.execute_reply":"2022-01-09T09:38:57.485051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.classes = '/kaggle/working/labels.txt'\ncfg.work_dir = '/kaggle/working/model_output'\ncfg.data_root = '/kaggle/working'\n\n# for head in cfg.model.roi_head.bbox_head:\n#     head.num_classes = 1\n\ncfg.data.test.type = 'CocoDataset'\ncfg.data.test.classes = 'labels.txt'\ncfg.data.test.ann_file = '../input/simple-yolox-dataset-generator-coco-json/annotations_valid.json'\ncfg.data.test.img_prefix = './images'\n\ncfg.data.train.type = 'CocoDataset'\ncfg.data.train.ann_file = '../input/simple-yolox-dataset-generator-coco-json/annotations_train.json'\ncfg.data.train.img_prefix = './images'\ncfg.data.train.classes = 'labels.txt'\n\ncfg.data.val.type = 'CocoDataset'\ncfg.data.val.ann_file = '../input/simple-yolox-dataset-generator-coco-json/annotations_valid.json'\ncfg.data.val.img_prefix = './images'\ncfg.data.val.classes = 'labels.txt'\n\ncfg.data.samples_per_gpu = 2\ncfg.data.workers_per_gpu = 2\n\ncfg.train_pipeline = train_pipeline\ncfg.val_pipeline = test_pipeline\ncfg.test_pipeline = test_pipeline\n\ncfg.data.train.pipeline = cfg.train_pipeline\ncfg.data.val.pipeline = cfg.val_pipeline\ncfg.data.test.pipeline = cfg.test_pipeline \n\ncfg.lr_config = dict(\n    policy='CosineAnnealing', \n    by_epoch=False,\n    warmup='linear', \n    warmup_iters= 1000, \n    warmup_ratio= 1/10,\n    min_lr=1e-07)\n\ncfg.evaluation.interval = 2\ncfg.evaluation.save_best='auto'\n\ncfg.seed = seed\ncfg.gpu_ids = range(1)\n\ncfg.fp16 = dict(loss_scale=dict(init_scale=512.))\n\ncfg.log_config = dict(\n    interval=100,\n    hooks=[\n        dict(type='TextLoggerHook'),\n        dict(type='TensorboardLoggerHook')\n    ])\n\nmeta = dict()\nmeta['config'] = cfg.pretty_text","metadata":{"papermill":{"duration":6.262174,"end_time":"2021-12-27T09:01:15.666205","exception":false,"start_time":"2021-12-27T09:01:09.404031","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:39:44.674714Z","iopub.execute_input":"2022-01-09T09:39:44.674985Z","iopub.status.idle":"2022-01-09T09:39:49.949855Z","shell.execute_reply.started":"2022-01-09T09:39:44.674954Z","shell.execute_reply":"2022-01-09T09:39:49.949131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datasets = [build_dataset(cfg.data.train)]\nmodel = build_detector(cfg.model, train_cfg=cfg.get('train_cfg'), test_cfg=cfg.get('test_cfg'))\nmodel.init_weights()\nmodel.CLASSES = datasets[0].CLASSES","metadata":{"_kg_hide-output":true,"papermill":{"duration":37.293602,"end_time":"2021-12-27T09:01:53.271126","exception":false,"start_time":"2021-12-27T09:01:15.977524","status":"completed"},"scrolled":true,"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:39:49.951446Z","iopub.execute_input":"2022-01-09T09:39:49.951706Z","iopub.status.idle":"2022-01-09T09:40:03.438507Z","shell.execute_reply.started":"2022-01-09T09:39:49.951671Z","shell.execute_reply":"2022-01-09T09:40:03.437906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mmcv.mkdir_or_exist(os.path.abspath(cfg.work_dir))\ntrain_detector(model, datasets, cfg, distributed=False, validate=True, meta = meta)","metadata":{"_kg_hide-output":true,"papermill":{"duration":16811.293499,"end_time":"2021-12-27T13:42:04.724709","exception":false,"start_time":"2021-12-27T09:01:53.43121","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-09T09:40:03.439484Z","iopub.execute_input":"2022-01-09T09:40:03.439725Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nshutil.rmtree('./mmdetection')\nshutil.rmtree('./images')","metadata":{"papermill":{"duration":4.484601,"end_time":"2021-12-27T13:42:12.972606","exception":true,"start_time":"2021-12-27T13:42:08.488005","status":"failed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]}]}