{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":67836,"databundleVersionId":7527339,"sourceType":"competition"},{"sourceId":7595623,"sourceType":"datasetVersion","datasetId":4419820}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# To Do\n* Add Your Private Data Source and Keep All The Images To Test In One Directory And Put The Path in \"**WHERE_DATA**\" Variable IN Next Code Block\n* Go To \"**Notebook options**\" and Select Accelerator as \"**GPU T4 x 2**\"\n* Click To \"**Run All**\" or \"**Save Version => Save & Run All**\"\n\n# Output\nIn the output section there will be \"**du_metrorail_submission.csv**\" waiting for you!\n\n# Don't Touch Any Block Except This","metadata":{}},{"cell_type":"code","source":"WHERE_DATA = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/test'","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:44:32.126164Z","iopub.execute_input":"2024-02-14T14:44:32.126528Z","iopub.status.idle":"2024-02-14T14:44:32.139866Z","shell.execute_reply.started":"2024-02-14T14:44:32.126497Z","shell.execute_reply":"2024-02-14T14:44:32.138958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Necessary Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nfrom collections import Counter\nfrom decimal import Decimal\nimport csv\nimport random\nfrom PIL import Image\nimport cv2\nimport tqdm \nimport shutil\nimport json\nimport pickle","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:44:32.141599Z","iopub.execute_input":"2024-02-14T14:44:32.141883Z","iopub.status.idle":"2024-02-14T14:44:34.280424Z","shell.execute_reply.started":"2024-02-14T14:44:32.141860Z","shell.execute_reply":"2024-02-14T14:44:34.279306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import IPython.display as display\n!pip install --no-index --no-deps /kaggle/input/du-metrorail-model/archive/pycocotools-2.0.6-cp310-cp310-linux_x86_64.whl\n!pip install --no-index --no-deps /kaggle/input/du-metrorail-model/archive/torch-1.12.1+cu116-cp310-cp310-linux_x86_64.whl\n!pip install --no-index --no-deps /kaggle/input/du-metrorail-model/archive/torchvision-0.13.1+cu116-cp310-cp310-linux_x86_64.whl\n!pip install --no-index --no-deps /kaggle/input/du-metrorail-model/archive/mmcv-2.0.1-cp310-cp310-manylinux1_x86_64.whl \n!pip install --no-index --no-deps /kaggle/input/du-metrorail-model/archive/openmim-0.3.9-py2.py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/du-metrorail-model/archive/mmengine-0.7.4-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/du-metrorail-model/archive/addict-2.4.0-py3-none-any.whl\n!pip install yapf==0.40.1\ndisplay.clear_output()","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:44:34.281774Z","iopub.execute_input":"2024-02-14T14:44:34.282310Z","iopub.status.idle":"2024-02-14T14:46:52.231533Z","shell.execute_reply.started":"2024-02-14T14:44:34.282274Z","shell.execute_reply":"2024-02-14T14:46:52.230243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/open-mmlab/mmdetection.git\n%cd mmdetection\n!pip install -v -e .\n\ndisplay.clear_output()","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:46:52.234568Z","iopub.execute_input":"2024-02-14T14:46:52.234987Z","iopub.status.idle":"2024-02-14T14:47:21.937433Z","shell.execute_reply.started":"2024-02-14T14:46:52.234951Z","shell.execute_reply":"2024-02-14T14:47:21.936255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch, torchvision,mmdet\nprint(\"torch=\",torch.__version__,torch.cuda.is_available())\nprint(\"mmdet=\",mmdet.__version__)\nfrom mmcv.ops import get_compiling_cuda_version, get_compiler_version\nprint(get_compiling_cuda_version())\nprint(get_compiler_version())","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:47:21.938899Z","iopub.execute_input":"2024-02-14T14:47:21.939210Z","iopub.status.idle":"2024-02-14T14:47:24.350385Z","shell.execute_reply.started":"2024-02-14T14:47:21.939179Z","shell.execute_reply":"2024-02-14T14:47:24.349230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:47:24.352018Z","iopub.execute_input":"2024-02-14T14:47:24.353049Z","iopub.status.idle":"2024-02-14T14:47:25.446415Z","shell.execute_reply.started":"2024-02-14T14:47:24.353007Z","shell.execute_reply":"2024-02-14T14:47:25.445231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Getting The Folder Ready","metadata":{}},{"cell_type":"code","source":"def yolo_to_coco(image_paths, yolo_labels_paths, coco_output_path, class_names, noannotation = 0):\n    coco_data = {\n        \"info\": {},\n        \"licenses\": [],\n        \"images\": [],\n        \"annotations\": [],\n        \"categories\": []\n    }\n    for class_id, class_name in class_names.items():\n        category = {\"id\": class_id, \"name\": class_name, \"supercategory\": \"none\"}\n        coco_data[\"categories\"].append(category)\n    for image_path in image_paths:\n        image = Image.open(image_path)\n        image_width, image_height = image.size\n        coco_image = {\n            \"id\": len(coco_data[\"images\"]) + 1,\n            \"width\": image_width,\n            \"height\": image_height,\n            \"file_name\": image_path.split(\"/\")[-1],\n            \"license\": 1,\n            \"flickr_url\": \"\",\n            \"coco_url\": \"\",\n            \"date_captured\": \"\"\n        }\n\n        coco_data[\"images\"].append(coco_image)\n    if noannotation == 1:\n        with open(coco_output_path, 'w') as json_file:\n            json.dump(coco_data, json_file, indent=2)\n        return\n    for image_path, yolo_labels_path in zip(image_paths, yolo_labels_paths):\n        try:\n            with open(yolo_labels_path, 'r') as f:\n                yolo_labels = f.readlines()\n\n            \n            for yolo_label in yolo_labels:\n                yolo_label = yolo_label.strip().split()\n                class_id, x_center, y_center, width, height = map(float, yolo_label)\n\n                x_min = max(0, int((x_center - width / 2) * image_width))\n                y_min = max(0, int((y_center - height / 2) * image_height))\n                x_max = min(image_width, int((x_center + width / 2) * image_width))\n                y_max = min(image_height, int((y_center + height / 2) * image_height))\n                annotation = {\n                    \"id\": len(coco_data[\"annotations\"]) + 1,\n                    \"image_id\": coco_image[\"id\"],\n                    \"category_id\": int(class_id),\n                    \"bbox\": [x_min, y_min, x_max - x_min, y_max - y_min],\n                    \"area\": (x_max - x_min) * (y_max - y_min),\n                    \"iscrowd\": 0,\n                    \"segmentation\": [],\n                    \"image_width\": image_width,\n                    \"image_height\": image_height\n                }\n\n                coco_data[\"annotations\"].append(annotation)\n\n        except FileNotFoundError:\n            continue\n\n    with open(coco_output_path, 'w') as json_file:\n        json.dump(coco_data, json_file, indent=2)\ndef create_list(path):\n    image_paths = []\n    for file in os.listdir(path):\n        image_paths.append(os.path.join(path, file))\n    return image_paths\ndef move_folder_contents(source_folder, destination_folder):\n    try:\n        os.makedirs(destination_folder, exist_ok=True)\n        shutil.rmtree(destination_folder, ignore_errors=True)\n        shutil.copytree(source_folder, destination_folder)\n    except Exception as e:\n        pass","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:47:25.448596Z","iopub.execute_input":"2024-02-14T14:47:25.449633Z","iopub.status.idle":"2024-02-14T14:47:25.603639Z","shell.execute_reply.started":"2024-02-14T14:47:25.449584Z","shell.execute_reply":"2024-02-14T14:47:25.602403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm data/dlenigma -r\n!mkdir data\n!mkdir data/dlenigma","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:47:25.605019Z","iopub.execute_input":"2024-02-14T14:47:25.605435Z","iopub.status.idle":"2024-02-14T14:47:28.896658Z","shell.execute_reply.started":"2024-02-14T14:47:25.605402Z","shell.execute_reply":"2024-02-14T14:47:28.895358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = {\n    0: \"auto_rickshaw\",\n    1: \"bicycle\",\n    2: \"bus\",\n    3: \"car\",\n    4: \"cart_vehicle\",\n    5: \"construction_vehicle\",\n    6: \"motorbike\",\n    7: \"person\",\n    8: \"priority_vehicle\",\n    9: \"three_wheeler\",\n    10: \"train\",\n    11: \"truck\",\n    12: \"wheelchair\"\n}\ntest_coco_output_path = \"data/dlenigma/test.json\"\n\nyolo_to_coco(create_list(WHERE_DATA),None, test_coco_output_path, class_names, noannotation=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:47:28.898407Z","iopub.execute_input":"2024-02-14T14:47:28.898794Z","iopub.status.idle":"2024-02-14T14:47:58.309006Z","shell.execute_reply.started":"2024-02-14T14:47:28.898757Z","shell.execute_reply":"2024-02-14T14:47:58.307820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir data\n!mkdir data/dlenigma\n!cp -r /kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/test  data/dlenigma\n!rm /testdir/test/_annotations.coco.json\n# !cp /kaggle/input/du-metrorail-special-validation-split-coco/dataset/dlenigma_coco/test/_annotations.coco.json data/dlenigma/test.json","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:47:58.312916Z","iopub.execute_input":"2024-02-14T14:47:58.313356Z","iopub.status.idle":"2024-02-14T14:48:08.932096Z","shell.execute_reply.started":"2024-02-14T14:47:58.313315Z","shell.execute_reply":"2024-02-14T14:48:08.930878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Write The Config","metadata":{}},{"cell_type":"code","source":"config=\"\"\"\nauto_scale_lr = dict(base_batch_size=16)\nbackend_args = None\nbase_lr = 8e-05\nbatch_augments = [\n    dict(pad_mask=True, size=(\n        1024,\n        1024,\n    ), type='BatchFixedSizePad'),\n]\ncustom_imports = dict(\n    allow_failed_imports=False, imports=[\n        'projects.CO-DETR.codetr',\n    ])\ndata_root = 'data/dlenigma/'\ndataset_type = 'CocoDataset'\ndefault_hooks = dict(\n    checkpoint=dict(\n        _scope_='mmdet',\n        by_epoch=True,\n        interval=1,\n        max_keep_ckpts=3,\n        type='CheckpointHook'),\n    logger=dict(_scope_='mmdet', interval=50, type='LoggerHook'),\n    param_scheduler=dict(_scope_='mmdet', type='ParamSchedulerHook'),\n    sampler_seed=dict(_scope_='mmdet', type='DistSamplerSeedHook'),\n    timer=dict(_scope_='mmdet', type='IterTimerHook'),\n    visualization=dict(_scope_='mmdet', type='DetVisualizationHook'))\ndefault_scope = 'mmdet'\nenv_cfg = dict(\n    cudnn_benchmark=False,\n    dist_cfg=dict(backend='nccl'),\n    mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0))\nimage_size = (\n    1024,\n    1024,\n)\nlauncher = 'none'\nload_from = 'https://download.openmmlab.com/mmdetection/v3.0/codetr/co_dino_5scale_swin_large_16e_o365tococo-614254c9.pth'\nload_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(type='LoadAnnotations', with_bbox=True, with_mask=True),\n    dict(\n        keep_ratio=True,\n        ratio_range=(\n            0.1,\n            2.0,\n        ),\n        scale=(\n            1024,\n            1024,\n        ),\n        type='RandomResize'),\n    dict(\n        allow_negative_crop=True,\n        crop_size=(\n            1024,\n            1024,\n        ),\n        crop_type='absolute_range',\n        recompute_bbox=True,\n        type='RandomCrop'),\n    dict(min_gt_bbox_wh=(\n        0.01,\n        0.01,\n    ), type='FilterAnnotations'),\n    dict(prob=0.5, type='RandomFlip'),\n    dict(pad_val=dict(img=(\n        114,\n        114,\n        114,\n    )), size=(\n        1024,\n        1024,\n    ), type='Pad'),\n]\nlog_level = 'INFO'\nlog_processor = dict(\n    _scope_='mmdet', by_epoch=True, type='LogProcessor', window_size=50)\nloss_lambda = 2.0\nmax_epochs = 1\nmax_iters = 270000\nmetainfo = dict(\n    classes=(\n        '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',\n    ),\n    palette=[\n        (\n            220,\n            20,\n            60,\n        ),\n        (\n            220,\n            20,\n            60,\n        ),\n        (\n            220,\n            20,\n            60,\n        ),\n        (\n            220,\n            20,\n            60,\n        ),\n        (\n            220,\n            20,\n            60,\n        ),\n        (\n            220,\n            20,\n            60,\n        ),\n    ])\nmodel = dict(\n    backbone=dict(\n        attn_drop_rate=0.0,\n        convert_weights=True,\n        depths=[\n            2,\n            2,\n            18,\n            2,\n        ],\n        drop_path_rate=0.3,\n        drop_rate=0.0,\n        embed_dims=192,\n        init_cfg=dict(\n            checkpoint=\n            'https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window12_384_22k.pth',\n            type='Pretrained'),\n        mlp_ratio=4,\n        num_heads=[\n            6,\n            12,\n            24,\n            48,\n        ],\n        out_indices=(\n            0,\n            1,\n            2,\n            3,\n        ),\n        patch_norm=True,\n        pretrain_img_size=384,\n        qk_scale=None,\n        qkv_bias=True,\n        type='SwinTransformer',\n        window_size=12,\n        with_cp=True),\n    bbox_head=[\n        dict(\n            anchor_generator=dict(\n                octave_base_scale=8,\n                ratios=[\n                    1.0,\n                ],\n                scales_per_octave=1,\n                strides=[\n                    4,\n                    8,\n                    16,\n                    32,\n                    64,\n                    128,\n                ],\n                type='AnchorGenerator'),\n            bbox_coder=dict(\n                target_means=[\n                    0.0,\n                    0.0,\n                    0.0,\n                    0.0,\n                ],\n                target_stds=[\n                    0.1,\n                    0.1,\n                    0.2,\n                    0.2,\n                ],\n                type='DeltaXYWHBBoxCoder'),\n            feat_channels=256,\n            in_channels=256,\n            loss_bbox=dict(loss_weight=24.0, type='GIoULoss'),\n            loss_centerness=dict(\n                loss_weight=12.0, type='CrossEntropyLoss', use_sigmoid=True),\n            loss_cls=dict(\n                alpha=0.25,\n                gamma=2.0,\n                loss_weight=12.0,\n                type='FocalLoss',\n                use_sigmoid=True),\n            num_classes=80,\n            stacked_convs=1,\n            type='CoATSSHead'),\n    ],\n    data_preprocessor=dict(\n        batch_augments=None,\n        bgr_to_rgb=True,\n        mean=[\n            123.675,\n            116.28,\n            103.53,\n        ],\n        pad_mask=False,\n        std=[\n            58.395,\n            57.12,\n            57.375,\n        ],\n        type='DetDataPreprocessor'),\n    eval_module='detr',\n    neck=dict(\n        act_cfg=None,\n        in_channels=[\n            192,\n            384,\n            768,\n            1536,\n        ],\n        kernel_size=1,\n        norm_cfg=dict(num_groups=32, type='GN'),\n        num_outs=5,\n        out_channels=256,\n        type='ChannelMapper'),\n    query_head=dict(\n        as_two_stage=True,\n        dn_cfg=dict(\n            box_noise_scale=0.4,\n            group_cfg=dict(dynamic=True, num_dn_queries=500, num_groups=None),\n            label_noise_scale=0.5),\n        in_channels=2048,\n        loss_bbox=dict(loss_weight=5.0, type='L1Loss'),\n        loss_cls=dict(\n            beta=2.0,\n            loss_weight=1.0,\n            type='QualityFocalLoss',\n            use_sigmoid=True),\n        loss_iou=dict(loss_weight=2.0, type='GIoULoss'),\n        num_classes=13,\n        num_query=900,\n        positional_encoding=dict(\n            normalize=True,\n            num_feats=128,\n            temperature=20,\n            type='SinePositionalEncoding'),\n        transformer=dict(\n            decoder=dict(\n                num_layers=6,\n                return_intermediate=True,\n                transformerlayers=dict(\n                    attn_cfgs=[\n                        dict(\n                            dropout=0.0,\n                            embed_dims=256,\n                            num_heads=8,\n                            type='MultiheadAttention'),\n                        dict(\n                            dropout=0.0,\n                            embed_dims=256,\n                            num_levels=5,\n                            type='MultiScaleDeformableAttention'),\n                    ],\n                    feedforward_channels=2048,\n                    ffn_dropout=0.0,\n                    operation_order=(\n                        'self_attn',\n                        'norm',\n                        'cross_attn',\n                        'norm',\n                        'ffn',\n                        'norm',\n                    ),\n                    type='DetrTransformerDecoderLayer'),\n                type='DinoTransformerDecoder'),\n            encoder=dict(\n                num_layers=6,\n                transformerlayers=dict(\n                    attn_cfgs=dict(\n                        dropout=0.0,\n                        embed_dims=256,\n                        num_levels=5,\n                        type='MultiScaleDeformableAttention'),\n                    feedforward_channels=2048,\n                    ffn_dropout=0.0,\n                    operation_order=(\n                        'self_attn',\n                        'norm',\n                        'ffn',\n                        'norm',\n                    ),\n                    type='BaseTransformerLayer'),\n                type='DetrTransformerEncoder',\n                with_cp=6),\n            num_co_heads=2,\n            num_feature_levels=5,\n            type='CoDinoTransformer',\n            with_coord_feat=False),\n        type='CoDINOHead'),\n    roi_head=[\n        dict(\n            bbox_head=dict(\n                bbox_coder=dict(\n                    target_means=[\n                        0.0,\n                        0.0,\n                        0.0,\n                        0.0,\n                    ],\n                    target_stds=[\n                        0.1,\n                        0.1,\n                        0.2,\n                        0.2,\n                    ],\n                    type='DeltaXYWHBBoxCoder'),\n                fc_out_channels=1024,\n                in_channels=256,\n                loss_bbox=dict(loss_weight=120.0, type='GIoULoss'),\n                loss_cls=dict(\n                    loss_weight=12.0,\n                    type='CrossEntropyLoss',\n                    use_sigmoid=False),\n                num_classes=80,\n                reg_class_agnostic=False,\n                reg_decoded_bbox=True,\n                roi_feat_size=7,\n                type='Shared2FCBBoxHead'),\n            bbox_roi_extractor=dict(\n                featmap_strides=[\n                    4,\n                    8,\n                    16,\n                    32,\n                    64,\n                ],\n                finest_scale=56,\n                out_channels=256,\n                roi_layer=dict(\n                    output_size=7, sampling_ratio=0, type='RoIAlign'),\n                type='SingleRoIExtractor'),\n            type='CoStandardRoIHead'),\n    ],\n    rpn_head=dict(\n        anchor_generator=dict(\n            octave_base_scale=4,\n            ratios=[\n                0.5,\n                1.0,\n                2.0,\n            ],\n            scales_per_octave=3,\n            strides=[\n                4,\n                8,\n                16,\n                32,\n                64,\n                128,\n            ],\n            type='AnchorGenerator'),\n        bbox_coder=dict(\n            target_means=[\n                0.0,\n                0.0,\n                0.0,\n                0.0,\n            ],\n            target_stds=[\n                1.0,\n                1.0,\n                1.0,\n                1.0,\n            ],\n            type='DeltaXYWHBBoxCoder'),\n        feat_channels=256,\n        in_channels=256,\n        loss_bbox=dict(loss_weight=12.0, type='L1Loss'),\n        loss_cls=dict(\n            loss_weight=12.0, type='CrossEntropyLoss', use_sigmoid=True),\n        type='RPNHead'),\n    test_cfg=[\n        dict(max_per_img=300, nms=dict(iou_threshold=0.8, type='soft_nms')),\n        dict(\n            rcnn=dict(\n                max_per_img=100,\n                nms=dict(iou_threshold=0.5, type='nms'),\n                score_thr=0.0),\n            rpn=dict(\n                max_per_img=1000,\n                min_bbox_size=0,\n                nms=dict(iou_threshold=0.7, type='nms'),\n                nms_pre=1000)),\n        dict(\n            max_per_img=100,\n            min_bbox_size=0,\n            nms=dict(iou_threshold=0.6, type='nms'),\n            nms_pre=1000,\n            score_thr=0.0),\n    ],\n    train_cfg=[\n        dict(\n            assigner=dict(\n                match_costs=[\n                    dict(type='FocalLossCost', weight=2.0),\n                    dict(box_format='xywh', type='BBoxL1Cost', weight=5.0),\n                    dict(iou_mode='giou', type='IoUCost', weight=2.0),\n                ],\n                type='HungarianAssigner')),\n        dict(\n            rcnn=dict(\n                assigner=dict(\n                    ignore_iof_thr=-1,\n                    match_low_quality=False,\n                    min_pos_iou=0.5,\n                    neg_iou_thr=0.5,\n                    pos_iou_thr=0.5,\n                    type='MaxIoUAssigner'),\n                debug=False,\n                pos_weight=-1,\n                sampler=dict(\n                    add_gt_as_proposals=True,\n                    neg_pos_ub=-1,\n                    num=512,\n                    pos_fraction=0.25,\n                    type='RandomSampler')),\n            rpn=dict(\n                allowed_border=-1,\n                assigner=dict(\n                    ignore_iof_thr=-1,\n                    match_low_quality=True,\n                    min_pos_iou=0.3,\n                    neg_iou_thr=0.3,\n                    pos_iou_thr=0.7,\n                    type='MaxIoUAssigner'),\n                debug=False,\n                pos_weight=-1,\n                sampler=dict(\n                    add_gt_as_proposals=False,\n                    neg_pos_ub=-1,\n                    num=256,\n                    pos_fraction=0.5,\n                    type='RandomSampler')),\n            rpn_proposal=dict(\n                max_per_img=1000,\n                min_bbox_size=0,\n                nms=dict(iou_threshold=0.7, type='nms'),\n                nms_pre=4000)),\n        dict(\n            allowed_border=-1,\n            assigner=dict(topk=9, type='ATSSAssigner'),\n            debug=False,\n            pos_weight=-1),\n    ],\n    type='CoDETR',\n    use_lsj=False)\nnum_classes = 80\nnum_dec_layer = 6\noptim_wrapper = dict(\n    clip_grad=dict(max_norm=0.1, norm_type=2),\n    optimizer=dict(lr=0.0001, type='AdamW', weight_decay=0.0001),\n    paramwise_cfg=dict(custom_keys=dict(backbone=dict(lr_mult=0.1))),\n    type='OptimWrapper')\nparam_scheduler = [\n    dict(\n        begin=0,\n        by_epoch=True,\n        end=1,\n        gamma=0.1,\n        milestones=[\n            8,\n        ],\n        type='MultiStepLR'),\n]\npretrained = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window12_384_22k.pth'\nresume = False\nstage2_num_epochs = 1\ntest_cfg = dict(_scope_='mmdet', type='TestLoop')\ntest_dataloader = dict(\n    batch_size=1,\n    dataset=dict(\n        _scope_='mmdet',\n        ann_file='test.json',\n        backend_args=None,\n        data_prefix=dict(img='test/'),\n        data_root='data/dlenigma/',\n        metainfo=dict(\n            classes=(\n                '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',\n            ),\n           palette = [\n    (220, 20, 60),  # Red\n    (255, 165, 0),  # Orange\n    (255, 255, 0),  # Yellow\n    (0, 128, 0),    # Green\n    (0, 0, 255),    # Blue\n    (128, 0, 128),  # Purple\n    (255, 0, 255),  # Magenta\n    (128, 128, 128),# Gray\n    (255, 192, 203),# Pink\n    (0, 255, 255),  # Cyan\n    (173, 216, 230),# Light Blue\n    (0, 255, 0),    # Lime Green\n    (139, 69, 19)   # Brown\n]\n),\n        pipeline=[\n            dict(backend_args=None, type='LoadImageFromFile'),\n            dict(keep_ratio=True, scale=(\n                1333,\n                800,\n            ), type='Resize'),\n            dict(type='LoadAnnotations', with_bbox=True),\n            dict(\n                meta_keys=(\n                    'img_id',\n                    'img_path',\n                    'ori_shape',\n                    'img_shape',\n                    'scale_factor',\n                ),\n                type='PackDetInputs'),\n        ],\n        test_mode=True,\n        type='CocoDataset'),\n    drop_last=False,\n    num_workers=2,\n    persistent_workers=True,\n    sampler=dict(_scope_='mmdet', shuffle=False, type='DefaultSampler'))\ntest_evaluator = dict(\n    _scope_='mmdet',\n    ann_file='data/dlenigma/test.json',\n    backend_args=None,\n    format_only=False,\n    metric='bbox',\n    type='CocoMetric')\ntest_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(keep_ratio=True, scale=(\n        2048,\n        1280,\n    ), type='Resize'),\n    dict(type='LoadAnnotations', with_bbox=True),\n    dict(\n        meta_keys=(\n            'img_id',\n            'img_path',\n            'ori_shape',\n            'img_shape',\n            'scale_factor',\n        ),\n        type='PackDetInputs'),\n]\ntrain_batch_size_per_gpu = 1\ntrain_cfg = dict(max_epochs=1, type='EpochBasedTrainLoop', val_interval=1)\ntrain_dataloader = dict(\n    batch_size=1,\n    dataset=dict(\n        ann_file='train.json',\n        backend_args=None,\n        data_prefix=dict(img='train/'),\n        data_root='data/dlenigma/',\n        filter_cfg=dict(filter_empty_gt=False, min_size=32),\n        metainfo=dict(\n            classes=(\n                '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',\n            ),\n            palette=[\n                (\n                    220,\n                    20,\n                    60,\n                ),\n                (\n                    220,\n                    20,\n                    60,\n                ),\n                (\n                    220,\n                    20,\n                    60,\n                ),\n                (\n                    220,\n                    20,\n                    60,\n                ),\n                (\n                    220,\n                    20,\n                    60,\n                ),\n                (\n                    220,\n                    20,\n                    60,\n                ),\n            ]),\n        pipeline=[\n            dict(backend_args=None, type='LoadImageFromFile'),\n            dict(type='LoadAnnotations', with_bbox=True),\n            dict(prob=0.5, type='RandomFlip'),\n            dict(\n                transforms=[\n                    [\n                        dict(\n                            keep_ratio=True,\n                            scales=[\n                                (\n                                    480,\n                                    1333,\n                                ),\n                                (\n                                    512,\n                                    1333,\n                                ),\n                                (\n                                    544,\n                                    1333,\n                                ),\n                                (\n                                    576,\n                                    1333,\n                                ),\n                                (\n                                    608,\n                                    1333,\n                                ),\n                                (\n                                    640,\n                                    1333,\n                                ),\n                                (\n                                    672,\n                                    1333,\n                                ),\n                                (\n                                    704,\n                                    1333,\n                                ),\n                                (\n                                    736,\n                                    1333,\n                                ),\n                                (\n                                    768,\n                                    1333,\n                                ),\n                                (\n                                    800,\n                                    1333,\n                                ),\n                            ],\n                            type='RandomChoiceResize'),\n                    ],\n                    [\n                        dict(\n                            keep_ratio=True,\n                            scales=[\n                                (\n                                    400,\n                                    4200,\n                                ),\n                                (\n                                    500,\n                                    4200,\n                                ),\n                                (\n                                    600,\n                                    4200,\n                                ),\n                            ],\n                            type='RandomChoiceResize'),\n                        dict(\n                            allow_negative_crop=True,\n                            crop_size=(\n                                384,\n                                600,\n                            ),\n                            crop_type='absolute_range',\n                            type='RandomCrop'),\n                        dict(\n                            keep_ratio=True,\n                            scales=[\n                                (\n                                    480,\n                                    1333,\n                                ),\n                                (\n                                    512,\n                                    1333,\n                                ),\n                                (\n                                    544,\n                                    1333,\n                                ),\n                                (\n                                    576,\n                                    1333,\n                                ),\n                                (\n                                    608,\n                                    1333,\n                                ),\n                                (\n                                    640,\n                                    1333,\n                                ),\n                                (\n                                    672,\n                                    1333,\n                                ),\n                                (\n                                    704,\n                                    1333,\n                                ),\n                                (\n                                    736,\n                                    1333,\n                                ),\n                                (\n                                    768,\n                                    1333,\n                                ),\n                                (\n                                    800,\n                                    1333,\n                                ),\n                            ],\n                            type='RandomChoiceResize'),\n                    ],\n                ],\n                type='RandomChoice'),\n            dict(type='PackDetInputs'),\n        ],\n        type='CocoDataset'),\n    num_workers=1,\n    persistent_workers=True,\n    sampler=dict(_scope_='mmdet', shuffle=True, type='DefaultSampler'))\ntrain_num_workers = 1\ntrain_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(type='LoadAnnotations', with_bbox=True),\n    dict(prob=0.5, type='RandomFlip'),\n    dict(\n        transforms=[\n            [\n                dict(\n                    keep_ratio=True,\n                    scales=[\n                        (\n                            480,\n                            2048,\n                        ),\n                        (\n                            512,\n                            2048,\n                        ),\n                        (\n                            544,\n                            2048,\n                        ),\n                        (\n                            576,\n                            2048,\n                        ),\n                        (\n                            608,\n                            2048,\n                        ),\n                        (\n                            640,\n                            2048,\n                        ),\n                        (\n                            672,\n                            2048,\n                        ),\n                        (\n                            704,\n                            2048,\n                        ),\n                        (\n                            736,\n                            2048,\n                        ),\n                        (\n                            768,\n                            2048,\n                        ),\n                        (\n                            800,\n                            2048,\n                        ),\n                        (\n                            832,\n                            2048,\n                        ),\n                        (\n                            864,\n                            2048,\n                        ),\n                        (\n                            896,\n                            2048,\n                        ),\n                        (\n                            928,\n                            2048,\n                        ),\n                        (\n                            960,\n                            2048,\n                        ),\n                        (\n                            992,\n                            2048,\n                        ),\n                        (\n                            1024,\n                            2048,\n                        ),\n                        (\n                            1056,\n                            2048,\n                        ),\n                        (\n                            1088,\n                            2048,\n                        ),\n                        (\n                            1120,\n                            2048,\n                        ),\n                        (\n                            1152,\n                            2048,\n                        ),\n                        (\n                            1184,\n                            2048,\n                        ),\n                        (\n                            1216,\n                            2048,\n                        ),\n                        (\n                            1248,\n                            2048,\n                        ),\n                        (\n                            1280,\n                            2048,\n                        ),\n                        (\n                            1312,\n                            2048,\n                        ),\n                        (\n                            1344,\n                            2048,\n                        ),\n                        (\n                            1376,\n                            2048,\n                        ),\n                        (\n                            1408,\n                            2048,\n                        ),\n                        (\n                            1440,\n                            2048,\n                        ),\n                        (\n                            1472,\n                            2048,\n                        ),\n                        (\n                            1504,\n                            2048,\n                        ),\n                        (\n                            1536,\n                            2048,\n                        ),\n                    ],\n                    type='RandomChoiceResize'),\n            ],\n            [\n                dict(\n                    keep_ratio=True,\n                    scales=[\n                        (\n                            400,\n                            4200,\n                        ),\n                        (\n                            500,\n                            4200,\n                        ),\n                        (\n                            600,\n                            4200,\n                        ),\n                    ],\n                    type='RandomChoiceResize'),\n                dict(\n                    allow_negative_crop=True,\n                    crop_size=(\n                        384,\n                        600,\n                    ),\n                    crop_type='absolute_range',\n                    type='RandomCrop'),\n                dict(\n                    keep_ratio=True,\n                    scales=[\n                        (\n                            480,\n                            2048,\n                        ),\n                        (\n                            512,\n                            2048,\n                        ),\n                        (\n                            544,\n                            2048,\n                        ),\n                        (\n                            576,\n                            2048,\n                        ),\n                        (\n                            608,\n                            2048,\n                        ),\n                        (\n                            640,\n                            2048,\n                        ),\n                        (\n                            672,\n                            2048,\n                        ),\n                        (\n                            704,\n                            2048,\n                        ),\n                        (\n                            736,\n                            2048,\n                        ),\n                        (\n                            768,\n                            2048,\n                        ),\n                        (\n                            800,\n                            2048,\n                        ),\n                        (\n                            832,\n                            2048,\n                        ),\n                        (\n                            864,\n                            2048,\n                        ),\n                        (\n                            896,\n                            2048,\n                        ),\n                        (\n                            928,\n                            2048,\n                        ),\n                        (\n                            960,\n                            2048,\n                        ),\n                        (\n                            992,\n                            2048,\n                        ),\n                        (\n                            1024,\n                            2048,\n                        ),\n                        (\n                            1056,\n                            2048,\n                        ),\n                        (\n                            1088,\n                            2048,\n                        ),\n                        (\n                            1120,\n                            2048,\n                        ),\n                        (\n                            1152,\n                            2048,\n                        ),\n                        (\n                            1184,\n                            2048,\n                        ),\n                        (\n                            1216,\n                            2048,\n                        ),\n                        (\n                            1248,\n                            2048,\n                        ),\n                        (\n                            1280,\n                            2048,\n                        ),\n                        (\n                            1312,\n                            2048,\n                        ),\n                        (\n                            1344,\n                            2048,\n                        ),\n                        (\n                            1376,\n                            2048,\n                        ),\n                        (\n                            1408,\n                            2048,\n                        ),\n                        (\n                            1440,\n                            2048,\n                        ),\n                        (\n                            1472,\n                            2048,\n                        ),\n                        (\n                            1504,\n                            2048,\n                        ),\n                        (\n                            1536,\n                            2048,\n                        ),\n                    ],\n                    type='RandomChoiceResize'),\n            ],\n        ],\n        type='RandomChoice'),\n    dict(type='PackDetInputs'),\n]\nval_cfg = dict(_scope_='mmdet', type='ValLoop')\nval_dataloader = dict(\n    batch_size=1,\n    dataset=dict(\n        _scope_='mmdet',\n        ann_file='val.json',\n        backend_args=None,\n        data_prefix=dict(img='val/'),\n        data_root='data/dlenigma/',\n        metainfo=dict(\n            classes=(\n                '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',\n            ),\n            palette=[\n                (\n                    220,\n                    20,\n                    60,\n                ),\n                (\n                    220,\n                    20,\n                    60,\n                ),\n                (\n                    220,\n                    20,\n                    60,\n                ),\n                (\n                    220,\n                    20,\n                    60,\n                ),\n                (\n                    220,\n                    20,\n                    60,\n                ),\n                (\n                    220,\n                    20,\n                    60,\n                ),\n            ]),\n        pipeline=[\n            dict(backend_args=None, type='LoadImageFromFile'),\n            dict(keep_ratio=True, scale=(\n                1333,\n                800,\n            ), type='Resize'),\n            dict(type='LoadAnnotations', with_bbox=True),\n            dict(\n                meta_keys=(\n                    'img_id',\n                    'img_path',\n                    'ori_shape',\n                    'img_shape',\n                    'scale_factor',\n                ),\n                type='PackDetInputs'),\n        ],\n        test_mode=True,\n        type='CocoDataset'),\n    drop_last=False,\n    num_workers=2,\n    persistent_workers=True,\n    sampler=dict(_scope_='mmdet', shuffle=False, type='DefaultSampler'))\nval_evaluator = dict(\n    _scope_='mmdet',\n    ann_file='data/dlenigma/val.json',\n    backend_args=None,\n    format_only=False,\n    metric='bbox',\n    type='CocoMetric')\n\nvis_backends = [\n    dict(_scope_='mmdet', type='LocalVisBackend'),\n]\nvisualizer = dict(\n    _scope_='mmdet',\n    name='visualizer',\n    type='DetLocalVisualizer',\n    vis_backends=[\n        dict(type='LocalVisBackend'),\n    ])\nwork_dir = './work_dirs/co_dino_5scale_swin_l_16xb1_16e_o365tococo'\n\n\"\"\"\nwith open('/kaggle/working/model_test.py', 'w') as f:\n    f.write(config)","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:48:08.934380Z","iopub.execute_input":"2024-02-14T14:48:08.934792Z","iopub.status.idle":"2024-02-14T14:48:08.985002Z","shell.execute_reply.started":"2024-02-14T14:48:08.934756Z","shell.execute_reply":"2024-02-14T14:48:08.983794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#multi gpu training\n# for single gpu you can change the 2 to a 1 \n!bash tools/dist_test.sh \\\n    /kaggle/working/model_test.py \\\n    /kaggle/input/du-metrorail-model/best_coco_bbox_mAP_epoch_9.pth \\\n    2 \\\n    --out dumetrorail.pkl ","metadata":{"execution":{"iopub.status.busy":"2024-02-14T14:48:08.986604Z","iopub.execute_input":"2024-02-14T14:48:08.986955Z","iopub.status.idle":"2024-02-14T15:11:28.207207Z","shell.execute_reply.started":"2024-02-14T14:48:08.986928Z","shell.execute_reply":"2024-02-14T15:11:28.205815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nwith open('/kaggle/working/mmdetection/dumetrorail.pkl', 'rb') as f:\n    data = pickle.load(f)","metadata":{"execution":{"iopub.status.busy":"2024-02-14T15:11:28.209210Z","iopub.execute_input":"2024-02-14T15:11:28.209695Z","iopub.status.idle":"2024-02-14T15:11:28.966309Z","shell.execute_reply.started":"2024-02-14T15:11:28.209651Z","shell.execute_reply":"2024-02-14T15:11:28.965366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalizeBbox(bbox,shape):\n    xmin, ymin, xmax, ymax = bbox\n    \n    image_width = shape[1] \n    image_height = shape[0]\n    x_center = (xmin + xmax) / 2\n    y_center = (ymin + ymax) / 2\n    width = xmax - xmin\n    height = ymax - ymin\n    x_center_norm = x_center / image_width\n    y_center_norm = y_center / image_height\n    width_norm = width / image_width\n    height_norm = height / image_height\n    \n    return [x_center_norm,y_center_norm,width_norm,height_norm];\n\ndef convertToYolo(result,shape):\n    if len(result['bboxes'])==0:\n        return [],[],[];\n    data=result\n    boxes=[];\n    scores=[];\n    classes=[];\n    for label, score, bbox in zip(data['labels'], data['scores'], data['bboxes']):\n        if score>0.40:\n            classes.append(label);\n            scores.append(score);\n            bbox_xywhn=normalizeBbox(bbox,shape)\n            boxes.append(bbox_xywhn);\n            \n    return boxes,scores,classes;\n            ","metadata":{"execution":{"iopub.status.busy":"2024-02-14T15:11:28.967590Z","iopub.execute_input":"2024-02-14T15:11:28.967888Z","iopub.status.idle":"2024-02-14T15:11:28.977852Z","shell.execute_reply.started":"2024-02-14T15:11:28.967862Z","shell.execute_reply":"2024-02-14T15:11:28.976943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Produce Submission","metadata":{}},{"cell_type":"code","source":"def get_prediction_string(boxes, scores, classes, image_path):\n    pred_strs = []\n\n    try:\n        # Read the image using OpenCV\n        image = cv2.imread(image_path)\n\n        for i, score in enumerate(scores):\n            single_pred_str = \"\"\n            single_pred_str += str(float(classes[i])) + \" \" + str(float(score)) + \" \"\n\n            x_center, y_center, width, height = boxes[i]\n            x1 = max(0, float(x_center) - (float(width) / 2))\n            y1 = max(0, float(y_center) - (float(height) / 2))\n            x2 = min(float(x_center) + (float(width) / 2), image.shape[1])\n            y2 = min(float(y_center) + (float(height) / 2), image.shape[0])\n\n            width = x2 - x1\n            height = y2 - y1\n\n            single_pred_str += f\"{x1} {y1} {width} {height}\"\n\n            pred_strs.append(single_pred_str)\n\n    except (ValueError, IndexError, TypeError, AttributeError) as e:\n        # Handle exceptions (e.g., invalid values, index errors, type errors, attribute errors)\n        print(f\"An exception occurred: {e}\")\n        return \"0 0 0 0 0 0\"\n\n    ans = ','.join(map(str, pred_strs))\n\n    if len(ans):\n        return ans\n\n    # The solution metrics failed in case of NaN or empty string.\n    # So, return \"0 0 0 0 0 0\" for NaN or empty string\n    return \"0 0 0 0 0 0\"\n\n\ndef get_prediction_entry(i, filename, boxes, scores, classes):\n    return {\n        \"id\": i, # strating from 0 ...\n        \"ImageID\": filename.split('.')[0], # before the extension ...\n        \"PredictionString_pred\": get_prediction_string(boxes, scores, classes,'/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/test/'+filename )\n    }\n\ndef predict_all_files(data):\n    predictions = []\n\n    for obj in data:\n        filename=obj['img_path'].split('/')[-1]\n        try:\n            if filename.endswith(\".jpg\"):\n                boxes, scores, classes = convertToYolo(obj['pred_instances'],obj['ori_shape'])\n                \n                prediction = get_prediction_entry(obj['img_id']-1, filename, boxes, scores, classes)\n                predictions.append(prediction)\n        except Exception as e:\n            print(f\"An exception occurred for {filename}: {e}\")\n    predictions_df = pd.DataFrame(predictions)\n    shutil.rmtree('/kaggle/working/mmdetection')\n    csv_path = \"/kaggle/working/du_metrorail_submission.csv\"\n\n    if not os.path.exists(csv_path):\n        open(csv_path, 'w').close()\n\n    predictions_df.to_csv(csv_path, index=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-14T15:11:28.979799Z","iopub.execute_input":"2024-02-14T15:11:28.980113Z","iopub.status.idle":"2024-02-14T15:11:28.997418Z","shell.execute_reply.started":"2024-02-14T15:11:28.980086Z","shell.execute_reply":"2024-02-14T15:11:28.996349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm '/kaggle/working/model_test.py'","metadata":{"execution":{"iopub.status.busy":"2024-02-14T15:11:28.998852Z","iopub.execute_input":"2024-02-14T15:11:28.999192Z","iopub.status.idle":"2024-02-14T15:11:30.096010Z","shell.execute_reply.started":"2024-02-14T15:11:28.999156Z","shell.execute_reply":"2024-02-14T15:11:30.094505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_all_files(data)","metadata":{"execution":{"iopub.status.busy":"2024-02-14T15:11:30.097963Z","iopub.execute_input":"2024-02-14T15:11:30.098399Z","iopub.status.idle":"2024-02-14T15:12:48.700809Z","shell.execute_reply.started":"2024-02-14T15:11:30.098362Z","shell.execute_reply":"2024-02-14T15:12:48.699748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}