{"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":"I couldn't find a notebook using the new mmdet version 3.0.0, so I made one myself. Please let me know if there are any mistakes!\n\nthe annotation josn file is from [this great notebook](https://www.kaggle.com/code/ammarnassanalhajali/hubmap-2023-k-fold-cv-coco-dataset-generator).\n\ninference notebook is [here](https://www.kaggle.com/code/andtaichi/hubmap-mmdet-ver3-0-0-infer/notebook).","metadata":{}},{"cell_type":"code","source":"# !pip install -U -qqq openmim\n# !mim install -qqq mmengine\n# !mim install -qqq \"mmcv>=2.0.0\"\n# !mim install -qqq mmdet\n# !pip install -U -qqq wandb","metadata":{"id":"bMYDFqlgSpr7","outputId":"47c74ceb-c957-4de7-e00c-1fb209570236","execution":{"iopub.status.busy":"2023-06-28T12:41:15.467837Z","iopub.execute_input":"2023-06-28T12:41:15.468219Z","iopub.status.idle":"2023-06-28T12:41:15.473403Z","shell.execute_reply.started":"2023-06-28T12:41:15.468180Z","shell.execute_reply":"2023-06-28T12:41:15.472492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -qqq /kaggle/input/mmdet3-wheels-ando/addict-2.4.0-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmdet3-wheels-ando/mmengine-0.7.3-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmdet3-wheels-ando/mmcv-2.0.0-cp310-cp310-linux_x86_64.whl\n!pip install -qqq /kaggle/input/pycocotools-206/wheels/pycocotools-2.0.6-cp310-cp310-linux_x86_64.whl\n!pip install -qqq /kaggle/input/mmdet3-wheels-ando/terminaltables-3.1.10-py2.py3-none-any.whl\n!pip install -qqq /kaggle/input/mmdet3-wheels-ando/mmdet-3.0.0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-06-28T12:41:15.475193Z","iopub.execute_input":"2023-06-28T12:41:15.475784Z","iopub.status.idle":"2023-06-28T12:42:40.620156Z","shell.execute_reply.started":"2023-06-28T12:41:15.475752Z","shell.execute_reply":"2023-06-28T12:42:40.618773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nsecret_value = user_secrets.get_secret(\"wandb-api\")\n\nimport wandb\nwandb.login(key=secret_value)","metadata":{"execution":{"iopub.status.busy":"2023-06-28T12:42:40.622758Z","iopub.execute_input":"2023-06-28T12:42:40.623139Z","iopub.status.idle":"2023-06-28T12:42:44.639131Z","shell.execute_reply.started":"2023-06-28T12:42:40.623101Z","shell.execute_reply":"2023-06-28T12:42:44.638093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import mmdet, mmcv\nprint(mmdet.__version__)\nprint(mmcv.__version__)","metadata":{"id":"-V-93RXwT5UW","outputId":"74770888-ad8e-4148-cdd7-97ddd0aadc6f","execution":{"iopub.status.busy":"2023-06-28T12:42:44.640887Z","iopub.execute_input":"2023-06-28T12:42:44.641575Z","iopub.status.idle":"2023-06-28T12:42:48.337017Z","shell.execute_reply.started":"2023-06-28T12:42:44.641538Z","shell.execute_reply":"2023-06-28T12:42:48.336075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make config file","metadata":{"id":"uK78Si0e831C"}},{"cell_type":"code","source":"%mkdir /kaggle/working/configs/","metadata":{"id":"nZ5XYaL1B9vq","outputId":"59f10eed-a1c5-4fc2-8ab4-a2b80620d223","execution":{"iopub.status.busy":"2023-06-28T12:42:48.339409Z","iopub.execute_input":"2023-06-28T12:42:48.340822Z","iopub.status.idle":"2023-06-28T12:42:49.318663Z","shell.execute_reply.started":"2023-06-28T12:42:48.340786Z","shell.execute_reply":"2023-06-28T12:42:49.317228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile /kaggle/working/configs/custom_config.py\n\n# model settings\nmodel = dict(\n    type='MaskRCNN',  # The name of detector\n    data_preprocessor=dict(  # The config of data preprocessor, usually includes image normalization and padding\n        type='DetDataPreprocessor',  # The type of the data preprocessor, refer to https://mmdetection.readthedocs.io/en/latest/api.html#mmdet.models.data_preprocessors.DetDataPreprocessor\n        mean=[123.675, 116.28, 103.53],  # Mean values used to pre-training the pre-trained backbone models, ordered in R, G, B\n        std=[58.395, 57.12, 57.375],  # Standard variance used to pre-training the pre-trained backbone models, ordered in R, G, B\n        bgr_to_rgb=True,  # whether to convert image from BGR to RGB\n        pad_mask=True,  # whether to pad instance masks\n        pad_size_divisor=32),  # The size of padded image should be divisible by ``pad_size_divisor``\n    backbone=dict(  # The config of backbone\n        type='ResNet',  # The type of backbone network. Refer to https://mmdetection.readthedocs.io/en/latest/api.html#mmdet.models.backbones.ResNet\n        depth=50,  # The depth of backbone, usually it is 50 or 101 for ResNet and ResNext backbones.\n        num_stages=4,  # Number of stages of the backbone.\n        out_indices=(0, 1, 2, 3),  # The index of output feature maps produced in each stage\n        frozen_stages=1,  # The weights in the first stage are frozen\n        norm_cfg=dict(  # The config of normalization layers.\n            type='BN',  # Type of norm layer, usually it is BN or GN\n            requires_grad=True),  # Whether to train the gamma and beta in BN\n        norm_eval=True,  # Whether to freeze the statistics in BN\n        style='pytorch', # The style of backbone, 'pytorch' means that stride 2 layers are in 3x3 Conv, 'caffe' means stride 2 layers are in 1x1 Convs.\n    \tinit_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')),  # The ImageNet pretrained backbone to be loaded\n    neck=dict(\n        type='FPN',  # The neck of detector is FPN. We also support 'NASFPN', 'PAFPN', etc. Refer to https://mmdetection.readthedocs.io/en/latest/api.html#mmdet.models.necks.FPN for more details.\n        in_channels=[256, 512, 1024, 2048],  # The input channels, this is consistent with the output channels of backbone\n        out_channels=256,  # The output channels of each level of the pyramid feature map\n        num_outs=5),  # The number of output scales\n    rpn_head=dict(\n        type='RPNHead',  # The type of RPN head is 'RPNHead', we also support 'GARPNHead', etc. Refer to https://mmdetection.readthedocs.io/en/latest/api.html#mmdet.models.dense_heads.RPNHead for more details.\n        in_channels=256,  # The input channels of each input feature map, this is consistent with the output channels of neck\n        feat_channels=256,  # Feature channels of convolutional layers in the head.\n        anchor_generator=dict(  # The config of anchor generator\n            type='AnchorGenerator',  # Most of methods use AnchorGenerator, SSD Detectors uses `SSDAnchorGenerator`. Refer to https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/task_modules/prior_generators/anchor_generator.py#L18 for more details\n            scales=[8],  # Basic scale of the anchor, the area of the anchor in one position of a feature map will be scale * base_sizes\n            ratios=[0.5, 1.0, 2.0],  # The ratio between height and width.\n            strides=[4, 8, 16, 32, 64]),  # The strides of the anchor generator. This is consistent with the FPN feature strides. The strides will be taken as base_sizes if base_sizes is not set.\n        bbox_coder=dict(  # Config of box coder to encode and decode the boxes during training and testing\n            type='DeltaXYWHBBoxCoder',  # Type of box coder. 'DeltaXYWHBBoxCoder' is applied for most of the methods. Refer to https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py#L13 for more details.\n            target_means=[0.0, 0.0, 0.0, 0.0],  # The target means used to encode and decode boxes\n            target_stds=[1.0, 1.0, 1.0, 1.0]),  # The standard variance used to encode and decode boxes\n        loss_cls=dict(  # Config of loss function for the classification branch\n            type='CrossEntropyLoss',  # Type of loss for classification branch, we also support FocalLoss etc. Refer to https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/losses/cross_entropy_loss.py#L201 for more details\n            use_sigmoid=True,  # RPN usually performs two-class classification, so it usually uses the sigmoid function.\n            loss_weight=1.0),  # Loss weight of the classification branch.\n        loss_bbox=dict(  # Config of loss function for the regression branch.\n            type='L1Loss',  # Type of loss, we also support many IoU Losses and smooth L1-loss, etc. Refer to https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/losses/smooth_l1_loss.py#L56 for implementation.\n            loss_weight=1.0)),  # Loss weight of the regression branch.\n    roi_head=dict(  # RoIHead encapsulates the second stage of two-stage/cascade detectors.\n        type='StandardRoIHead',\n        bbox_roi_extractor=dict(  # RoI feature extractor for bbox regression.\n            type='SingleRoIExtractor',  # Type of the RoI feature extractor, most of methods uses SingleRoIExtractor. Refer to https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/roi_heads/roi_extractors/single_level_roi_extractor.py#L13 for details.\n            roi_layer=dict(  # Config of RoI Layer\n                type='RoIAlign',  # Type of RoI Layer, DeformRoIPoolingPack and ModulatedDeformRoIPoolingPack are also supported. Refer to https://mmcv.readthedocs.io/en/latest/api.html#mmcv.ops.RoIAlign for details.\n                output_size=7,  # The output size of feature maps.\n                sampling_ratio=0),  # Sampling ratio when extracting the RoI features. 0 means adaptive ratio.\n            out_channels=256,  # output channels of the extracted feature.\n            featmap_strides=[4, 8, 16, 32]),  # Strides of multi-scale feature maps. It should be consistent with the architecture of the backbone.\n        bbox_head=dict(  # Config of box head in the RoIHead.\n            type='Shared2FCBBoxHead',  # Type of the bbox head, Refer to https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/roi_heads/bbox_heads/convfc_bbox_head.py#L220 for implementation details.\n            in_channels=256,  # Input channels for bbox head. This is consistent with the out_channels in roi_extractor\n            fc_out_channels=1024,  # Output feature channels of FC layers.\n            roi_feat_size=7,  # Size of RoI features\n            num_classes=1,  # Number of classes for classification\n            bbox_coder=dict(  # Box coder used in the second stage.\n                type='DeltaXYWHBBoxCoder',  # Type of box coder. 'DeltaXYWHBBoxCoder' is applied for most of the methods.\n                target_means=[0.0, 0.0, 0.0, 0.0],  # Means used to encode and decode box\n                target_stds=[0.1, 0.1, 0.2, 0.2]),  # Standard variance for encoding and decoding. It is smaller since the boxes are more accurate. [0.1, 0.1, 0.2, 0.2] is a conventional setting.\n            reg_class_agnostic=False,  # Whether the regression is class agnostic.\n            loss_cls=dict(  # Config of loss function for the classification branch\n                type='CrossEntropyLoss',  # Type of loss for classification branch, we also support FocalLoss etc.\n                use_sigmoid=False,  # Whether to use sigmoid.\n                loss_weight=1.0),  # Loss weight of the classification branch.\n            loss_bbox=dict(  # Config of loss function for the regression branch.\n                type='L1Loss',  # Type of loss, we also support many IoU Losses and smooth L1-loss, etc.\n                loss_weight=1.0)),  # Loss weight of the regression branch.\n        mask_roi_extractor=dict(  # RoI feature extractor for mask generation.\n            type='SingleRoIExtractor',  # Type of the RoI feature extractor, most of methods uses SingleRoIExtractor.\n            roi_layer=dict(  # Config of RoI Layer that extracts features for instance segmentation\n                type='RoIAlign',  # Type of RoI Layer, DeformRoIPoolingPack and ModulatedDeformRoIPoolingPack are also supported\n                output_size=14,  # The output size of feature maps.\n                sampling_ratio=0),  # Sampling ratio when extracting the RoI features.\n            out_channels=256,  # Output channels of the extracted feature.\n            featmap_strides=[4, 8, 16, 32]),  # Strides of multi-scale feature maps.\n        mask_head=dict(  # Mask prediction head\n            type='FCNMaskHead',  # Type of mask head, refer to https://mmdetection.readthedocs.io/en/latest/api.html#mmdet.models.roi_heads.FCNMaskHead for implementation details.\n            num_convs=4,  # Number of convolutional layers in mask head.\n            in_channels=256,  # Input channels, should be consistent with the output channels of mask roi extractor.\n            conv_out_channels=256,  # Output channels of the convolutional layer.\n            num_classes=1,  # Number of class to be segmented.\n            loss_mask=dict(  # Config of loss function for the mask branch.\n                type='CrossEntropyLoss',  # Type of loss used for segmentation\n                use_mask=True,  # Whether to only train the mask in the correct class.\n                loss_weight=1.0))),  # Loss weight of mask branch.\n    train_cfg = dict(  # Config of training hyperparameters for rpn and rcnn\n        rpn=dict(  # Training config of rpn\n            assigner=dict(  # Config of assigner\n                type='MaxIoUAssigner',  # Type of assigner, MaxIoUAssigner is used for many common detectors. Refer to https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/task_modules/assigners/max_iou_assigner.py#L14 for more details.\n                pos_iou_thr=0.7,  # IoU >= threshold 0.7 will be taken as positive samples\n                neg_iou_thr=0.3,  # IoU < threshold 0.3 will be taken as negative samples\n                min_pos_iou=0.3,  # The minimal IoU threshold to take boxes as positive samples\n                match_low_quality=True,  # Whether to match the boxes under low quality (see API doc for more details).\n                ignore_iof_thr=-1),  # IoF threshold for ignoring bboxes\n            sampler=dict(  # Config of positive/negative sampler\n                type='RandomSampler',  # Type of sampler, PseudoSampler and other samplers are also supported. Refer to https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/task_modules/samplers/random_sampler.py#L14 for implementation details.\n                num=256,  # Number of samples\n                pos_fraction=0.5,  # The ratio of positive samples in the total samples.\n                neg_pos_ub=-1,  # The upper bound of negative samples based on the number of positive samples.\n                add_gt_as_proposals=False),  # Whether add GT as proposals after sampling.\n            allowed_border=-1,  # The border allowed after padding for valid anchors.\n            pos_weight=-1,  # The weight of positive samples during training.\n            debug=False),  # Whether to set the debug mode\n        rpn_proposal=dict(  # The config to generate proposals during training\n            nms_across_levels=False,  # Whether to do NMS for boxes across levels. Only work in `GARPNHead`, naive rpn does not support do nms cross levels.\n            nms_pre=2000,  # The number of boxes before NMS\n            nms_post=1000,  # The number of boxes to be kept by NMS. Only work in `GARPNHead`.\n            max_per_img=1000,  # The number of boxes to be kept after NMS.\n            nms=dict( # Config of NMS\n                type='nms',  # Type of NMS\n                iou_threshold=0.7 # NMS threshold\n                ),\n            min_bbox_size=0),  # The allowed minimal box size\n        rcnn=dict(  # The config for the roi heads.\n            assigner=dict(  # Config of assigner for second stage, this is different for that in rpn\n                type='MaxIoUAssigner',  # Type of assigner, MaxIoUAssigner is used for all roi_heads for now. Refer to https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/task_modules/assigners/max_iou_assigner.py#L14 for more details.\n                pos_iou_thr=0.6,  # IoU >= threshold 0.5 will be taken as positive samples\n                neg_iou_thr=0.6,  # IoU < threshold 0.5 will be taken as negative samples\n                min_pos_iou=0.6,  # The minimal IoU threshold to take boxes as positive samples\n                match_low_quality=False,  # Whether to match the boxes under low quality (see API doc for more details).\n                ignore_iof_thr=-1),  # IoF threshold for ignoring bboxes\n            sampler=dict(\n                type='RandomSampler',  # Type of sampler, PseudoSampler and other samplers are also supported. Refer to https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/task_modules/samplers/random_sampler.py#L14 for implementation details.\n                num=512,  # Number of samples\n                pos_fraction=0.25,  # The ratio of positive samples in the total samples.\n                neg_pos_ub=-1,  # The upper bound of negative samples based on the number of positive samples.\n                add_gt_as_proposals=True\n            ),  # Whether add GT as proposals after sampling.\n            mask_size=28,  # Size of mask\n            pos_weight=-1,  # The weight of positive samples during training.\n            debug=False)),  # Whether to set the debug mode\n    test_cfg = dict(  # Config for testing hyperparameters for rpn and rcnn\n        rpn=dict(  # The config to generate proposals during testing\n            nms_across_levels=False,  # Whether to do NMS for boxes across levels. Only work in `GARPNHead`, naive rpn does not support do nms cross levels.\n            nms_pre=1000,  # The number of boxes before NMS\n            nms_post=1000,  # The number of boxes to be kept by NMS. Only work in `GARPNHead`.\n            max_per_img=1000,  # The number of boxes to be kept after NMS.\n            nms=dict( # Config of NMS\n                type='nms',  #Type of NMS\n                iou_threshold=0.7 # NMS threshold\n                ),\n            min_bbox_size=0),  # The allowed minimal box size\n        rcnn=dict(  # The config for the roi heads.\n            score_thr=0.05,  # Threshold to filter out boxes\n            nms=dict(  # Config of NMS in the second stage\n                type='nms',  # Type of NMS\n                iou_thr=0.6),  # NMS threshold\n            max_per_img=100,  # Max number of detections of each image\n            mask_thr_binary=0.5)))  # Threshold of mask prediction\n\n# dataset settings\ndataset_type = 'CocoDataset'  # Dataset type, this will be used to define the dataset\ndata_root = ''  # Root path of data\nbackend_args = None # Arguments to instantiate the corresponding file backend\n\nmetainfo = {\n    \"classes\": (\"blood_vessel\",),\n    \"palette\": [(255, 0, 0)]\n}\n\ntrain_pipeline = [  # Training data processing pipeline\n    dict(type='LoadImageFromFile', backend_args=backend_args),  # First pipeline to load images from file path\n    dict(\n        type='LoadAnnotations',  # Second pipeline to load annotations for current image\n        with_bbox=True,  # Whether to use bounding box, True for detection\n        with_mask=True,  # Whether to use instance mask, True for instance segmentation\n        poly2mask=True),  # Whether to convert the polygon mask to instance mask, set False for acceleration and to save memory\n    dict(\n        type='Resize',  # Pipeline that resizes the images and their annotations\n        scale=(512, 512),  # The largest scale of the images\n        keep_ratio=True  # Whether to keep the ratio between height and width\n        ),\n    dict(\n        type='RandomFlip',\n        direction=[\"horizontal\", \"vertical\"],  # Augmentation pipeline that flips the images and their annotations\n        prob=0.5),  # The probability to flip\n    dict(type='PackDetInputs')  # Pipeline that formats the annotation data and decides which keys in the data should be packed into data_samples\n]\n\ntest_pipeline = [  # Testing data processing pipeline\n    dict(type='LoadImageFromFile', backend_args=backend_args),  # First pipeline to load images from file path\n    dict(type='Resize', scale=(512, 512), keep_ratio=True),  # Pipeline that resizes the images\n    dict(\n        type='PackDetInputs',  # Pipeline that formats the annotation data and decides which keys in the data should be packed into data_samples\n        meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',\n                   'scale_factor'))\n]\ntrain_dataloader = dict(   # Train dataloader config\n    batch_size=4,  # Batch size of a single GPU\n    num_workers=2,  # Worker to pre-fetch data for each single GPU\n    persistent_workers=True,  # If ``True``, the dataloader will not shut down the worker processes after an epoch end, which can accelerate training speed.\n    sampler=dict(  # training data sampler\n        type='DefaultSampler',  # DefaultSampler which supports both distributed and non-distributed training. Refer to https://mmengine.readthedocs.io/en/latest/api/generated/mmengine.dataset.DefaultSampler.html#mmengine.dataset.DefaultSampler\n        shuffle=True),  # randomly shuffle the training data in each epoch\n    batch_sampler=dict(type='AspectRatioBatchSampler'),  # Batch sampler for grouping images with similar aspect ratio into a same batch. It can reduce GPU memory cost.\n    dataset=dict(  # Train dataset config\n        type=dataset_type,\n        metainfo=metainfo,\n        data_root=\"\",\n        ann_file='/kaggle/input/hubmap-2023-k-fold-cv-coco-dataset-generator/coco_annotations_train_all_fold1.json',  # Path of annotation file\n        data_prefix=dict(img='/kaggle/input/hubmap-hacking-the-human-vasculature/train'),  # Prefix of image path\n        filter_cfg=dict(filter_empty_gt=True, min_size=32),  # Config of filtering images and annotations\n        pipeline=train_pipeline,\n        backend_args=backend_args))\nval_dataloader = dict(  # Validation dataloader config\n    batch_size=1,  # Batch size of a single GPU. If batch-size > 1, the extra padding area may influence the performance.\n    num_workers=2,  # Worker to pre-fetch data for each single GPU\n    persistent_workers=True,  # If ``True``, the dataloader will not shut down the worker processes after an epoch end, which can accelerate training speed.\n    drop_last=False,  # Whether to drop the last incomplete batch, if the dataset size is not divisible by the batch size\n    sampler=dict(\n        type='DefaultSampler',\n        shuffle=False),  # not shuffle during validation and testing\n    dataset=dict(\n        type=dataset_type,\n        metainfo=metainfo,\n        data_root=\"\",\n        ann_file='/kaggle/input/hubmap-2023-k-fold-cv-coco-dataset-generator/coco_annotations_valid_all_fold1.json',\n        data_prefix=dict(img='/kaggle/input/hubmap-hacking-the-human-vasculature/train'),\n        test_mode=True,  # Turn on the test mode of the dataset to avoid filtering annotations or images\n        pipeline=test_pipeline,\n        backend_args=backend_args))\ntest_dataloader = val_dataloader  # Testing dataloader config\n\n# setting evaluator\nval_evaluator = dict(  # Validation evaluator config\n    type='CocoMetric',  # The coco metric used to evaluate AR, AP, and mAP for detection and instance segmentation\n    ann_file='/kaggle/input/hubmap-2023-k-fold-cv-coco-dataset-generator/coco_annotations_valid_all_fold1.json',  # Annotation file path\n    metric=['segm'],  # Metrics to be evaluated, `bbox` for detection and `segm` for instance segmentation\n    format_only=False,\n    # backend_args=backend_args\n    backend_args=None,\n    # iou_thrs=[0.6]\n    )\ntest_evaluator = val_evaluator  # Testing evaluator config\n\n# setting optimizer\noptim_wrapper = dict(  # Optimizer wrapper config\n    type='OptimWrapper',  # Optimizer wrapper type, switch to AmpOptimWrapper to enable mixed precision training.\n    optimizer=dict(  # Optimizer config. Support all kinds of optimizers in PyTorch. Refer to https://pytorch.org/docs/stable/optim.html#algorithms\n        type='SGD',  # Stochastic gradient descent optimizer\n        lr=0.02,  # The base learning rate\n        momentum=0.9,  # Stochastic gradient descent with momentum\n        weight_decay=0.0001),  # Weight decay of SGD\n    clip_grad=None,  # Gradient clip option. Set None to disable gradient clip. Find usage in https://mmengine.readthedocs.io/en/latest/tutorials/optimizer.html\n    )\n\n# setting scheduler\nparam_scheduler = [\n    # Linear learning rate warm-up scheduler\n    dict(\n        type='LinearLR',  # Use linear policy to warmup learning rate\n        start_factor=0.001, # The ratio of the starting learning rate used for warmup\n        by_epoch=False,  # The warmup learning rate is updated by iteration\n        begin=0,  # Start from the first iteration\n        end=500),  # End the warmup at the 500th iteration\n    # The main LRScheduler\n    dict(\n        type='MultiStepLR',  # Use multi-step learning rate policy during training\n        by_epoch=True,  # The learning rate is updated by epoch\n        begin=0,   # Start from the first epoch\n        end=12,  # End at the 12th epoch\n        milestones=[8, 11],  # Epochs to decay the learning rate\n        gamma=0.1)  # The learning rate decay ratio\n]\n\n# setting hook\ndefault_hooks = dict(\n    timer=dict(type='IterTimerHook'),  # Update the time spent during iteration into message hub\n    logger=dict(type='LoggerHook', interval=50),  # Collect logs from different components of Runner and write them to terminal, JSON file, tensorboard and wandb .etc\n    param_scheduler=dict(type='ParamSchedulerHook'), # update some hyper-parameters of optimizer\n    checkpoint=dict(type='CheckpointHook', interval=1, save_best=\"coco/segm_mAP\"), # Save checkpoints periodically\n    sampler_seed=dict(type='DistSamplerSeedHook'),  # Ensure distributed Sampler shuffle is active\n    visualization=dict(type='DetVisualizationHook'))  # Detection Visualization Hook. Used to visualize validation and testing process prediction results\n\ncustom_hooks = [dict(\n        type='EarlyStoppingHook',\n        monitor='coco/segm_mAP',\n        rule='greater',\n        min_delta=0.005,\n        strict=False,\n        check_finite=True,\n        patience=2,\n        stopping_threshold=None)]\n\n# setting scope\ndefault_scope = 'mmdet'  # The default registry scope to find modules. Refer to https://mmengine.readthedocs.io/en/latest/advanced_tutorials/registry.html\n\n# setting env config\nenv_cfg = dict(\n    cudnn_benchmark=False,  # Whether to enable cudnn benchmark\n    mp_cfg=dict(  # Multi-processing config\n        mp_start_method='fork',  # Use fork to start multi-processing threads. 'fork' usually faster than 'spawn' but maybe unsafe. See discussion in https://github.com/pytorch/pytorch/issues/1355\n        opencv_num_threads=0),  # Disable opencv multi-threads to avoid system being overloaded\n    dist_cfg=dict(backend='nccl'),  # Distribution configs\n)\n\n# setting visualizer\nvis_backends = [dict(type='LocalVisBackend')]  # Visualization backends. Refer to https://mmengine.readthedocs.io/en/latest/advanced_tutorials/visualization.html\nvisualizer = dict(\n    # type='DetLocalVisualizer', vis_backends=vis_backends, name='visualizer'\n    type=\"Visualizer\", vis_backends=[dict(type=\"WandbVisBackend\")]\n    )\n\nlog_config = dict(\n    hooks = [\n    dict(type='TextLoggerHook'),\n        dict(type='MMDetWandbHook',\n            init_kwargs={'project': 'mmdetection'},\n            interval=10,\n            log_checkpoint=True,\n            log_checkpoint_metadata=True,\n            num_eval_images=100,\n            bbox_score_thr=0.3)\n    ]\n)\n\n# settiing logger\nlog_processor = dict(\n    type='LogProcessor',  # Log processor to process runtime logs\n    window_size=50,  # Smooth interval of log values\n    by_epoch=True)  # Whether to format logs with epoch type. Should be consistent with the train loop's type.\nlog_level = 'INFO'  # The level of logging.\nload_from = None  # Load model checkpoint as a pre-trained model from a given path. This will not resume training.\nresume = False  # Whether to resume from the checkpoint defined in `load_from`. If `load_from` is None, it will resume the latest checkpoint in the `work_dir`.\n\n# setting trian test cfg\ntrain_cfg = dict(\n    type='EpochBasedTrainLoop',  # The training loop type. Refer to https://github.com/open-mmlab/mmengine/blob/main/mmengine/runner/loops.py\n    max_epochs=12,  # Maximum training epochs\n    val_interval=1)  # Validation intervals. Run validation every epoch.\nval_cfg = dict(type='ValLoop')  # The validation loop type\ntest_cfg = dict(type='TestLoop')  # The testing loop type\n\n","metadata":{"id":"qfxn8mFVPNnT","outputId":"080cdf1a-caf4-4973-e679-e71c14ec525c","execution":{"iopub.status.busy":"2023-06-28T12:42:49.323880Z","iopub.execute_input":"2023-06-28T12:42:49.324269Z","iopub.status.idle":"2023-06-28T12:42:49.354237Z","shell.execute_reply.started":"2023-06-28T12:42:49.324225Z","shell.execute_reply":"2023-06-28T12:42:49.353261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train","metadata":{"id":"wkc1wmEA87LT"}},{"cell_type":"code","source":"%mkdir work_dir\nfrom mmengine.config import Config\nfrom mmengine.runner import Runner\n\nfrom mmdet.utils import register_all_modules\n\ncfg = Config.fromfile(\"/kaggle/working/configs/custom_config.py\")\n\ncfg.work_dir = \"/kaggle/working/work_dir\"\nrunner = Runner.from_cfg(cfg)","metadata":{"id":"RQ5Dk2ZZeh6r","outputId":"ec542c97-8e19-438e-aaa4-8d5c9830f23d","execution":{"iopub.status.busy":"2023-06-28T12:43:14.202303Z","iopub.execute_input":"2023-06-28T12:43:14.202670Z","iopub.status.idle":"2023-06-28T12:44:02.407620Z","shell.execute_reply.started":"2023-06-28T12:43:14.202639Z","shell.execute_reply":"2023-06-28T12:44:02.406582Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"runner.train()","metadata":{"id":"8JdOs8sde9d5","outputId":"9d909517-c530-425a-e797-e6c3407c8b81","execution":{"iopub.status.busy":"2023-06-28T12:44:02.409568Z","iopub.execute_input":"2023-06-28T12:44:02.410300Z","iopub.status.idle":"2023-06-28T13:00:53.610495Z","shell.execute_reply.started":"2023-06-28T12:44:02.410256Z","shell.execute_reply":"2023-06-28T13:00:53.609522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}