{"metadata":{"accelerator":"GPU","colab":{"gpuType":"T4","provenance":[]},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":67836,"databundleVersionId":7527339,"sourceType":"competition"},{"sourceId":7533149,"sourceType":"datasetVersion","datasetId":4387520},{"sourceId":7595623,"sourceType":"datasetVersion","datasetId":4419820}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Needs 24 GB of GPU memory for batch sizes of 1 ","metadata":{}},{"cell_type":"markdown","source":"## Install MMDetection\nInstalling mmdetection from wheel files.\n","metadata":{"id":"tJxJHruNLb7Y"}},{"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-11T18:51:08.146107Z","iopub.execute_input":"2024-02-11T18:51:08.146847Z","iopub.status.idle":"2024-02-11T18:52:52.673846Z","shell.execute_reply.started":"2024-02-11T18:51:08.146815Z","shell.execute_reply":"2024-02-11T18:52:52.672777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## checking if everyting matches with the wheel files\n!git clone https://github.com/open-mmlab/mmdetection.git\n%cd mmdetection\n!pip install -v -e .\n\ndisplay.clear_output()","metadata":{"id":"Wi4LPmsR66sy","outputId":"8a4d76d8-a420-4ca8-b338-f80e323d30ba","execution":{"iopub.status.busy":"2024-02-11T18:52:52.675903Z","iopub.execute_input":"2024-02-11T18:52:52.676225Z","iopub.status.idle":"2024-02-11T18:53:18.880968Z","shell.execute_reply.started":"2024-02-11T18:52:52.676199Z","shell.execute_reply":"2024-02-11T18:53:18.879867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2024-02-11T18:53:18.882318Z","iopub.execute_input":"2024-02-11T18:53:18.882652Z","iopub.status.idle":"2024-02-11T18:53:19.827946Z","shell.execute_reply.started":"2024-02-11T18:53:18.882613Z","shell.execute_reply":"2024-02-11T18:53:19.826917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-11T18:53:19.830326Z","iopub.execute_input":"2024-02-11T18:53:19.830635Z","iopub.status.idle":"2024-02-11T18:53:20.382404Z","shell.execute_reply.started":"2024-02-11T18:53:19.830607Z","shell.execute_reply":"2024-02-11T18:53:20.381597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mmengine.utils import get_git_hash\nfrom mmengine.utils.dl_utils import collect_env as collect_base_env\n\nimport mmdet\n\n\ndef collect_env():\n    \"\"\"Collect the information of the running environments.\"\"\"\n    env_info = collect_base_env()\n    env_info['MMDetection'] = f'{mmdet.__version__}+{get_git_hash()[:7]}'\n    return env_info\n\n\nif __name__ == '__main__':\n    for name, val in collect_env().items():\n        print(f'{name}: {val}')","metadata":{"id":"_YeUiqAoCaoV","outputId":"02f9cfe1-892a-43a5-cc9e-c1a4b21968ac","execution":{"iopub.status.busy":"2024-02-11T18:53:20.383508Z","iopub.execute_input":"2024-02-11T18:53:20.383921Z","iopub.status.idle":"2024-02-11T18:53:22.849440Z","shell.execute_reply.started":"2024-02-11T18:53:20.383894Z","shell.execute_reply":"2024-02-11T18:53:22.848513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2024-02-11T18:53:22.850650Z","iopub.execute_input":"2024-02-11T18:53:22.851171Z","iopub.status.idle":"2024-02-11T18:53:23.840331Z","shell.execute_reply.started":"2024-02-11T18:53:22.851121Z","shell.execute_reply":"2024-02-11T18:53:23.838983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Reorganize the dataset\nThe dataset should be in COCO format. The data and the annotation json are in the same root directory.","metadata":{}},{"cell_type":"code","source":"# Download the data and unzip it\n!mkdir data\n!mkdir data/dlenigma\n\n!cp -r /kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/train ./data/dlenigma/\n!cp  /kaggle/input/dlenigma-coco/train.json ./data/dlenigma/\n","metadata":{"id":"sbJhEsckU8UX","outputId":"8d52ad6d-e762-4179-f0fa-52c731267f86","execution":{"iopub.status.busy":"2024-02-11T18:53:23.841926Z","iopub.execute_input":"2024-02-11T18:53:23.842288Z","iopub.status.idle":"2024-02-11T18:54:12.492862Z","shell.execute_reply.started":"2024-02-11T18:53:23.842255Z","shell.execute_reply":"2024-02-11T18:54:12.491612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare a config\n\nWe need a config to describe the model we are training. In this case we use the config of Co-DINO with Swin-L backbone. The config can be found in this repo: \nhttps://github.com/open-mmlab/mmdetection/blob/main/projects/CO-DETR/configs/codino/co_dino_5scale_swin_l_16xb1_16e_o365tococo.py\n\n\n","metadata":{"id":"yc9UDp1vU8UZ"}},{"cell_type":"code","source":"config_balloon = \"\"\"\n# Inherit and overwrite part of the config based on this config\n_base_ = ['co_dino_5scale_r50_8xb2_1x_coco.py']\n\npretrained = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window12_384_22k.pth'  # noqa\nload_from = 'https://download.openmmlab.com/mmdetection/v3.0/codetr/co_dino_5scale_swin_large_16e_o365tococo-614254c9.pth'  # noqa\n\ntrain_batch_size_per_gpu = 1\ntrain_num_workers = 1\n# root directory where the data folder and annotation json are found\ndata_root = 'data/dlenigma/'\nmax_epochs = 20\nstage2_num_epochs = 1\nbase_lr = 0.00008\n\n# meta information about the number of classes and their annotation color in the inference output\nmetainfo = {\n    'classes': ('auto_rickshaw','bicycle', 'bus','car', 'cart_vehicle','construction_vehicle','motorbike','person','priority_vehicle','three_wheeler' ,'train', 'truck','wheelchair'),\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# model settings\n# number of classes must match the dataset's number of classes\nmodel = dict(\n    backbone=dict(\n        _delete_=True,\n        type='SwinTransformer',\n        pretrain_img_size=384,\n        embed_dims=192,\n        depths=[2, 2, 18, 2],\n        num_heads=[6, 12, 24, 48],\n        window_size=12,\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        # Please only add indices that would be used\n        # in FPN, otherwise some parameter will not be used\n        with_cp=True,\n        convert_weights=True,\n        init_cfg=dict(type='Pretrained', checkpoint=pretrained)),\n    neck=dict(in_channels=[192, 384, 768, 1536]),\n    query_head=dict(\n        num_classes=13,dn_cfg=dict(box_noise_scale=0.4, group_cfg=dict(num_dn_queries=500)),\n        transformer=dict(encoder=dict(with_cp=6))))\n\ntrain_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(type='LoadAnnotations', with_bbox=True),\n    dict(type='RandomFlip', prob=0.5),\n    dict(\n        type='RandomChoice',\n        transforms=[\n            [\n                dict(\n                    type='RandomChoiceResize',\n                    scales=[(480, 2048), (512, 2048), (544, 2048), (576, 2048),\n                            (608, 2048), (640, 2048), (672, 2048), (704, 2048),\n                            (736, 2048), (768, 2048), (800, 2048), (832, 2048),\n                            (864, 2048), (896, 2048), (928, 2048), (960, 2048),\n                            (992, 2048), (1024, 2048), (1056, 2048),\n                            (1088, 2048), (1120, 2048), (1152, 2048),\n                            (1184, 2048), (1216, 2048), (1248, 2048),\n                            (1280, 2048), (1312, 2048), (1344, 2048),\n                            (1376, 2048), (1408, 2048), (1440, 2048),\n                            (1472, 2048), (1504, 2048), (1536, 2048)],\n                    keep_ratio=True)\n            ],\n            [\n                dict(\n                    type='RandomChoiceResize',\n                    # The radio of all image in train dataset < 7\n                    # follow the original implement\n                    scales=[(400, 4200), (500, 4200), (600, 4200)],\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='RandomChoiceResize',\n                    scales=[(480, 2048), (512, 2048), (544, 2048), (576, 2048),\n                            (608, 2048), (640, 2048), (672, 2048), (704, 2048),\n                            (736, 2048), (768, 2048), (800, 2048), (832, 2048),\n                            (864, 2048), (896, 2048), (928, 2048), (960, 2048),\n                            (992, 2048), (1024, 2048), (1056, 2048),\n                            (1088, 2048), (1120, 2048), (1152, 2048),\n                            (1184, 2048), (1216, 2048), (1248, 2048),\n                            (1280, 2048), (1312, 2048), (1344, 2048),\n                            (1376, 2048), (1408, 2048), (1440, 2048),\n                            (1472, 2048), (1504, 2048), (1536, 2048)],\n                    keep_ratio=True)\n            ]\n        ]),\n    dict(type='PackDetInputs')\n]\n\ntrain_dataloader = dict(\n    batch_size=train_batch_size_per_gpu,\n    num_workers=train_num_workers,\n    dataset=dict(\n        data_root=data_root,\n        metainfo=metainfo,\n        data_prefix=dict(img='train/'),\n        ann_file='train.json'))\nprint(train_dataloader);\n\ntest_pipeline = [\n    dict(type='LoadImageFromFile'),\n    dict(type='Resize', scale=(2048, 1280), keep_ratio=True),\n    dict(type='LoadAnnotations', with_bbox=True),\n    dict(\n        type='PackDetInputs',\n        meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',\n                   'scale_factor'))\n]\n\n# This notebook uses the train data as validation. If you want to use a separate validation dataset, \n# you must put the paths for validation instead train in the val_dataloader\nval_dataloader = dict(\n    dataset=dict(\n        data_root=data_root,\n        metainfo=metainfo,\n        data_prefix=dict(img='train/'),\n        ann_file='train.json'))\ntest_dataloader = val_dataloader\n\n# Modify metric related settings\n# for validation set, change the train.json to the name of the validation json.\nval_evaluator = dict(ann_file=data_root + 'train.json')\ntest_evaluator = val_evaluator\n\noptim_wrapper = dict(optimizer=dict(lr=1e-4))\n\n\ntrain_cfg = dict(max_epochs=max_epochs)\n# out_dir is the directory where the checkpoints are saved\ndefault_hooks = dict(checkpoint=dict(type='CheckpointHook', save_best='auto',out_dir='../checkpoints_codetr_final'))\nparam_scheduler = [\n    dict(\n        type='MultiStepLR',\n        begin=0,\n        end=max_epochs,\n        by_epoch=True,\n        milestones=[8],\n        gamma=0.1)\n]\n\n\"\"\"\n\nwith open('./projects/CO-DETR/configs/codino/co_dino_5scale_swin_l_16xb1_16e_o365tococo.py', 'w') as f:\n    f.write(config_balloon)","metadata":{"id":"XjTW6XydU8Ua","execution":{"iopub.status.busy":"2024-02-11T19:24:37.176986Z","iopub.execute_input":"2024-02-11T19:24:37.177371Z","iopub.status.idle":"2024-02-11T19:24:37.188792Z","shell.execute_reply.started":"2024-02-11T19:24:37.177335Z","shell.execute_reply":"2024-02-11T19:24:37.188014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2024-02-11T18:57:39.283055Z","iopub.execute_input":"2024-02-11T18:57:39.283768Z","iopub.status.idle":"2024-02-11T18:57:40.246147Z","shell.execute_reply.started":"2024-02-11T18:57:39.283736Z","shell.execute_reply":"2024-02-11T18:57:40.244818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python tools/train.py ./projects/CO-DETR/configs/codino/co_dino_5scale_swin_l_16xb1_16e_o365tococo.py ","metadata":{"id":"5LNm7LxwZG2w","outputId":"5c77544f-ef00-4640-bdac-6719f5cf3b8d","scrolled":true,"execution":{"iopub.status.busy":"2024-02-11T18:57:40.248540Z","iopub.execute_input":"2024-02-11T18:57:40.248899Z","iopub.status.idle":"2024-02-11T19:00:43.720511Z","shell.execute_reply.started":"2024-02-11T18:57:40.248859Z","shell.execute_reply":"2024-02-11T19:00:43.719320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}