{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":67836,"databundleVersionId":7527339,"sourceType":"competition"},{"sourceId":6063037,"sourceType":"datasetVersion","datasetId":3469773},{"sourceId":6063590,"sourceType":"datasetVersion","datasetId":3470137},{"sourceId":8521750,"sourceType":"datasetVersion","datasetId":5088299}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from IPython.display import clear_output\n\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/pycocotools-2.0.6-cp310-cp310-linux_x86_64.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/torch-1.12.1+cu116-cp310-cp310-linux_x86_64.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/torchvision-0.13.1+cu116-cp310-cp310-linux_x86_64.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/mmcv-2.0.1-cp310-cp310-manylinux1_x86_64.whl \n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/openmim-0.3.9-py2.py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/mmengine-0.7.4-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/addict-2.4.0-py3-none-any.whl\n!pip install yapf==0.40.1\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/einops-0.6.1-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/mat4py-0.5.0-py2.py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/ordered_set-4.1.0-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/model_index-0.1.11-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/modelindex-0.0.2-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/mmpretrain-1.0.0rc8-py2.py3-none-any.whl\nclear_output()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-29T13:36:14.078358Z","iopub.execute_input":"2024-05-29T13:36:14.078686Z","iopub.status.idle":"2024-05-29T13:38:15.120137Z","shell.execute_reply.started":"2024-05-29T13:36:14.078662Z","shell.execute_reply":"2024-05-29T13:38:15.119054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nfrom PIL import Image\n\nfrom glob import glob\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-05-29T13:38:15.122651Z","iopub.execute_input":"2024-05-29T13:38:15.123060Z","iopub.status.idle":"2024-05-29T13:38:16.134529Z","shell.execute_reply.started":"2024-05-29T13:38:15.123021Z","shell.execute_reply":"2024-05-29T13:38:16.133733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone -bv3.1.0 https://github.com/open-mmlab/mmdetection.git\n%cd /kaggle/working/mmdetection\n!pip install -v -e .\n\n\nclear_output()","metadata":{"execution":{"iopub.status.busy":"2024-05-29T13:38:16.135613Z","iopub.execute_input":"2024-05-29T13:38:16.136010Z","iopub.status.idle":"2024-05-29T13:38:41.644900Z","shell.execute_reply.started":"2024-05-29T13:38:16.135984Z","shell.execute_reply":"2024-05-29T13:38:41.643794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch, torchvision,mmdet\nprint(\"PyTorch version= \",torch.__version__,torch.cuda.is_available())\nprint(\"mmdet version= \",mmdet.__version__)\n\nfrom mmcv.ops import get_compiling_cuda_version\nprint(\"THE CUDA version \", get_compiling_cuda_version())","metadata":{"execution":{"iopub.status.busy":"2024-05-29T13:38:41.647759Z","iopub.execute_input":"2024-05-29T13:38:41.648253Z","iopub.status.idle":"2024-05-29T13:38:43.757161Z","shell.execute_reply.started":"2024-05-29T13:38:41.648214Z","shell.execute_reply":"2024-05-29T13:38:43.756226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U openmim\nclear_output()","metadata":{"execution":{"iopub.status.busy":"2024-05-29T13:38:43.758327Z","iopub.execute_input":"2024-05-29T13:38:43.758708Z","iopub.status.idle":"2024-05-29T13:39:15.156588Z","shell.execute_reply.started":"2024-05-29T13:38:43.758683Z","shell.execute_reply":"2024-05-29T13:39:15.155479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p configs/Enigma","metadata":{"execution":{"iopub.status.busy":"2024-05-29T13:39:15.160134Z","iopub.execute_input":"2024-05-29T13:39:15.160460Z","iopub.status.idle":"2024-05-29T13:39:16.109109Z","shell.execute_reply.started":"2024-05-29T13:39:15.160430Z","shell.execute_reply":"2024-05-29T13:39:16.107988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile configs/Enigma/custom_config.py\n\n_base_ = '/kaggle/working/mmdetection/configs/cascade_rcnn/cascade-rcnn_x101-32x4d_fpn_20e_coco.py'\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.2,\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     bbox_head=[\n             dict(\n                type='Shared2FCBBoxHead',\n                in_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=13,\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                loss_bbox=dict(type='SmoothL1Loss', beta=1.0,\n                               loss_weight=1.0)),\n         dict(\n                type='Shared2FCBBoxHead',\n                in_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=13,\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                loss_bbox=dict(type='SmoothL1Loss', beta=1.0,\n                               loss_weight=1.0)),\n          dict(\n                type='Shared2FCBBoxHead',\n                in_channels=256,\n                fc_out_channels=1024,\n                roi_feat_size=7,\n                num_classes=13,\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                loss_bbox=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0))\n            ]\n    ),\n    test_cfg=dict(\n        rcnn=dict(\n            score_thr=0.05,\n            nms=dict(type='soft_nms', iou_threshold=0.5, min_score=0.05),\n            max_per_img=100))\n)\n\nclasses = ('auto_rickshaw',\n'bicycle',\n'bus',\n'car',\n'cart_vehicle',\n'construction_vehicle',\n'motorbike',\n'person',\n'priority_vehicle',\n'three_wheeler',\n'train',\n'truck',\n'wheelchair') # Added\nbackend_args = None\nepoch =12 \nbth_size = 2\ndata_root = '/kaggle/input/dl-enigma-cocodataset/' # Modified\ntrain_cfg = dict(type='EpochBasedTrainLoop', max_epochs=epoch, val_interval=1)\nval_cfg = dict(type='ValLoop')\ntest_cfg = dict(type='TestLoop')\n\noptim_wrapper = dict(\n    type='OptimWrapper',\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        }),\n    optimizer=dict(\n        _delete_=True,\n        type='AdamW',\n        lr=0.0001,\n        betas=(0.9, 0.999),\n        weight_decay=0.05))\n\nparam_scheduler = [\n    dict(type='LinearLR', start_factor=0.001, by_epoch=False, begin=0, end=5000),\n    dict(\n        type='MultiStepLR',\n        begin=0,\n        end=epoch,\n        by_epoch=True,\n        milestones=[8, 10],\n        gamma=0.1)\n]\n\n\ntrain_pipeline = [\n    dict(type='LoadImageFromFile', backend_args=backend_args),\n    dict(type='LoadAnnotations', with_bbox=True),\n    dict(type='RandomFlip', prob=0.5),\n    dict(\n        type='Resize',  # Pipeline that resizes the images and their annotations\n        scale=(1333, 800),  # The largest scale of the images\n        keep_ratio=True  # Whether to keep the ratio between height and width\n        ),\n    dict(type='PackDetInputs')\n]\n\ntest_pipeline = [\n    dict(type='LoadImageFromFile', backend_args=backend_args),\n    dict(type='LoadAnnotations', with_bbox=True),\n    dict(\n        type='Resize',  # Pipeline that resizes the images and their annotations\n        scale=(1333, 800),  # The largest scale of the images\n        keep_ratio=True  # Whether to keep the ratio between height and width\n        ),\n    dict(\n        type='PackDetInputs',\n        meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',\n                   'scale_factor'))\n]\n\n\n\ntrain_dataloader = dict(\n    batch_size=bth_size,\n    num_workers=4,\n    persistent_workers=True,\n    dataset=dict(\n        type='CocoDataset',\n        data_root=data_root,\n        metainfo=dict(classes=classes),\n        ann_file=data_root + 'annotations_train.json',\n        data_prefix=dict(img='train2017/'),\n        pipeline=train_pipeline,\n        backend_args=backend_args))\nval_dataloader = dict(\n    batch_size=bth_size,\n    num_workers=4,\n    persistent_workers=True,\n    drop_last=False,\n    dataset=dict(\n        type='CocoDataset',\n        data_root=data_root,\n        metainfo=dict(classes=classes),\n        ann_file=data_root + 'annotations_valid.json',\n        data_prefix=dict(img='valid2017/'),\n        test_mode=True,\n        pipeline=test_pipeline,\n        backend_args=backend_args))\ntest_dataloader = val_dataloader\n\nval_evaluator = dict(\nann_file=data_root + 'annotations_valid.json', metric=['bbox'], classwise=True)\nload_from='https://download.openmmlab.com/mmdetection/v2.0/cascade_rcnn/cascade_rcnn_x101_32x4d_fpn_20e_coco/cascade_rcnn_x101_32x4d_fpn_20e_coco_20200906_134608-9ae0a720.pth'\ntest_evaluator = val_evaluator\nfp16=dict(loss_scale=512.)\nresume_from = None","metadata":{"execution":{"iopub.status.busy":"2024-05-29T13:41:31.245752Z","iopub.execute_input":"2024-05-29T13:41:31.246217Z","iopub.status.idle":"2024-05-29T13:41:31.257225Z","shell.execute_reply.started":"2024-05-29T13:41:31.246187Z","shell.execute_reply":"2024-05-29T13:41:31.256321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python tools/train.py /kaggle/working/mmdetection/configs/Enigma/custom_config.py","metadata":{"execution":{"iopub.status.busy":"2024-05-29T13:41:33.208998Z","iopub.execute_input":"2024-05-29T13:41:33.209377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}