prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Empirical attention with ResNext backbone
  In mmdetectiom v1 I used ResNext backbone with empirical attention 0010_dcn but in v2 I got an error.
  Any ideas how to fix this?
  
  My updated config file:
  ```
  _base_ = '../faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
  model = dict(
  
      pretrained='open-mmlab://resnext50_32x4d',
      backbone=dict(
          type='ResNeXt',
          depth=50,
          groups=32,
          base_width=4,
          num_stages=4,
          out_indices=(0, 1, 2, 3),
          frozen_stages=1,
          norm_cfg=dict(type='BN', requires_grad=True),
          style='pytorch',
          plugins=[
              dict(
                  cfg=dict(
                      type='GeneralizedAttention',
                      spatial_range=-1,
                      num_heads=8,
                      attention_type='0010',
                      kv_stride=2),
                  stages=(False, False, True, True),
                  position='after_conv2')
          ],
          dcn=dict(type='DCN', deform_groups=1, fallback_on_stride=False),
          stage_with_dcn=(False, True, True, True)))
  ```
  
  Error:
  ```
  RuntimeError: Given groups=1, weight of size [256, 256, 1, 1], expected input[2, 512, 41, 73] to have 256 channels, but got 512 channels instead
  ```
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `mmdet/models/backbones/resnext.py`
  Content:
  ```
  1 import math
  2 
  3 from mmcv.cnn import build_conv_layer, build_norm_layer
  4 
  5 from ..builder import BACKBONES
  6 from ..utils import ResLayer
  7 from .resnet import Bottleneck as _Bottleneck
  8 from .resnet import ResNet
  9 
  10 
  11 class Bottleneck(_Bottleneck):
  12     expansion = 4
  13 
  14     def __init__(self,
  15                  inplanes,
  16                  planes,
  17                  groups=1,
  18                  base_width=4,
  19                  base_channels=64,
  20                  **kwargs):
  21         """Bottleneck block for ResNeXt.
  22 
  23         If style is "pytorch", the stride-two layer is the 3x3 conv layer, if
  24         it is "caffe", the stride-two layer is the first 1x1 conv layer.
  25         """
  26         super(Bottleneck, self).__init__(inplanes, planes, **kwargs)
  27 
  28         if groups == 1:
  29             width = self.planes
  30         else:
  31             width = math.floor(self.planes *
  32                                (base_width / base_channels)) * groups
  33 
  34         self.norm1_name, norm1 = build_norm_layer(
  35             self.norm_cfg, width, postfix=1)
  36         self.norm2_name, norm2 = build_norm_layer(
  37             self.norm_cfg, width, postfix=2)
  38         self.norm3_name, norm3 = build_norm_layer(
  39             self.norm_cfg, self.planes * self.expansion, postfix=3)
  40 
  41         self.conv1 = build_conv_layer(
  42             self.conv_cfg,
  43             self.inplanes,
  44             width,
  45             kernel_size=1,
  46             stride=self.conv1_stride,
  47             bias=False)
  48         self.add_module(self.norm1_name, norm1)
  49         fallback_on_stride = False
  50         self.with_modulated_dcn = False
  51         if self.with_dcn:
  52             fallback_on_stride = self.dcn.pop('fallback_on_stride', False)
  53         if not self.with_dcn or fallback_on_stride:
  54             self.conv2 = build_conv_layer(
  55                 self.conv_cfg,
  56                 width,
  57                 width,
  58                 kernel_size=3,
  59                 stride=self.conv2_stride,
  60                 padding=self.dilation,
  61                 dilation=self.dilation,
  62                 groups=groups,
  63                 bias=False)
  64         else:
  65             assert self.conv_cfg is None, 'conv_cfg must be None for DCN'
  66             self.conv2 = build_conv_layer(
  67                 self.dcn,
  68                 width,
  69                 width,
  70                 kernel_size=3,
  71                 stride=self.conv2_stride,
  72                 padding=self.dilation,
  73                 dilation=self.dilation,
  74                 groups=groups,
  75                 bias=False)
  76 
  77         self.add_module(self.norm2_name, norm2)
  78         self.conv3 = build_conv_layer(
  79             self.conv_cfg,
  80             width,
  81             self.planes * self.expansion,
  82             kernel_size=1,
  83             bias=False)
  84         self.add_module(self.norm3_name, norm3)
  85 
  86 
  87 @BACKBONES.register_module()
  88 class ResNeXt(ResNet):
  89     """ResNeXt backbone.
  90 
  91     Args:
  92         depth (int): Depth of resnet, from {18, 34, 50, 101, 152}.
  93         in_channels (int): Number of input image channels. Default: 3.
  94         num_stages (int): Resnet stages. Default: 4.
  95         groups (int): Group of resnext.
  96         base_width (int): Base width of resnext.
  97         strides (Sequence[int]): Strides of the first block of each stage.
  98         dilations (Sequence[int]): Dilation of each stage.
  99         out_indices (Sequence[int]): Output from which stages.
  100         style (str): `pytorch` or `caffe`. If set to "pytorch", the stride-two
  101             layer is the 3x3 conv layer, otherwise the stride-two layer is
  102             the first 1x1 conv layer.
  103         frozen_stages (int): Stages to be frozen (all param fixed). -1 means
  104             not freezing any parameters.
  105         norm_cfg (dict): dictionary to construct and config norm layer.
  106         norm_eval (bool): Whether to set norm layers to eval mode, namely,
  107             freeze running stats (mean and var). Note: Effect on Batch Norm
  108             and its variants only.
  109         with_cp (bool): Use checkpoint or not. Using checkpoint will save some
  110             memory while slowing down the training speed.
  111         zero_init_residual (bool): whether to use zero init for last norm layer
  112             in resblocks to let them behave as identity.
  113     """
  114 
  115     arch_settings = {
  116         50: (Bottleneck, (3, 4, 6, 3)),
  117         101: (Bottleneck, (3, 4, 23, 3)),
  118         152: (Bottleneck, (3, 8, 36, 3))
  119     }
  120 
  121     def __init__(self, groups=1, base_width=4, **kwargs):
  122         self.groups = groups
  123         self.base_width = base_width
  124         super(ResNeXt, self).__init__(**kwargs)
  125 
  126     def make_res_layer(self, **kwargs):
  127         """Pack all blocks in a stage into a ``ResLayer``"""
  128         return ResLayer(
  129             groups=self.groups,
  130             base_width=self.base_width,
  131             base_channels=self.base_channels,
  132             **kwargs)
  133
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/mmdet/models/backbones/resnext.py b/mmdet/models/backbones/resnext.py
  --- a/mmdet/models/backbones/resnext.py
  +++ b/mmdet/models/backbones/resnext.py
  @@ -83,6 +83,27 @@
               bias=False)
           self.add_module(self.norm3_name, norm3)
   
  +        if self.with_plugins:
  +            self._del_block_plugins(self.after_conv1_plugin_names +
  +                                    self.after_conv2_plugin_names +
  +                                    self.after_conv3_plugin_names)
  +            self.after_conv1_plugin_names = self.make_block_plugins(
  +                width, self.after_conv1_plugins)
  +            self.after_conv2_plugin_names = self.make_block_plugins(
  +                width, self.after_conv2_plugins)
  +            self.after_conv3_plugin_names = self.make_block_plugins(
  +                self.planes * self.expansion, self.after_conv3_plugins)
  +
  +    def _del_block_plugins(self, plugin_names):
  +        """delete plugins for block if exist.
  +
  +        Args:
  +            plugin_names (list[str]): List of plugins name to delete.
  +        """
  +        assert isinstance(plugin_names, list)
  +        for plugin_name in plugin_names:
  +            del self._modules[plugin_name]
  +
   
   @BACKBONES.register_module()
   class ResNeXt(ResNet):
  
verification_info: {"golden_diff": "diff --git a/mmdet/models/backbones/resnext.py b/mmdet/models/backbones/resnext.py\n--- a/mmdet/models/backbones/resnext.py\n+++ b/mmdet/models/backbones/resnext.py\n@@ -83,6 +83,27 @@\n             bias=False)\n         self.add_module(self.norm3_name, norm3)\n \n+        if self.with_plugins:\n+            self._del_block_plugins(self.after_conv1_plugin_names +\n+                                    self.after_conv2_plugin_names +\n+                                    self.after_conv3_plugin_names)\n+            self.after_conv1_plugin_names = self.make_block_plugins(\n+                width, self.after_conv1_plugins)\n+            self.after_conv2_plugin_names = self.make_block_plugins(\n+                width, self.after_conv2_plugins)\n+            self.after_conv3_plugin_names = self.make_block_plugins(\n+                self.planes * self.expansion, self.after_conv3_plugins)\n+\n+    def _del_block_plugins(self, plugin_names):\n+        \"\"\"delete plugins for block if exist.\n+\n+        Args:\n+            plugin_names (list[str]): List of plugins name to delete.\n+        \"\"\"\n+        assert isinstance(plugin_names, list)\n+        for plugin_name in plugin_names:\n+            del self._modules[plugin_name]\n+\n \n @BACKBONES.register_module()\n class ResNeXt(ResNet):\n", "issue": "Empirical attention with ResNext backbone\nIn mmdetectiom v1 I used ResNext backbone with empirical attention 0010_dcn but in v2 I got an error.\r\nAny ideas how to fix this?\r\n\r\nMy updated config file:\r\n```\r\n_base_ = '../faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'\r\nmodel = dict(\r\n\r\n    pretrained='open-mmlab://resnext50_32x4d',\r\n    backbone=dict(\r\n        type='ResNeXt',\r\n        depth=50,\r\n        groups=32,\r\n        base_width=4,\r\n        num_stages=4,\r\n        out_indices=(0, 1, 2, 3),\r\n        frozen_stages=1,\r\n        norm_cfg=dict(type='BN', requires_grad=True),\r\n        style='pytorch',\r\n        plugins=[\r\n            dict(\r\n                cfg=dict(\r\n                    type='GeneralizedAttention',\r\n                    spatial_range=-1,\r\n                    num_heads=8,\r\n                    attention_type='0010',\r\n                    kv_stride=2),\r\n                stages=(False, False, True, True),\r\n                position='after_conv2')\r\n        ],\r\n        dcn=dict(type='DCN', deform_groups=1, fallback_on_stride=False),\r\n        stage_with_dcn=(False, True, True, True)))\r\n```\r\n\r\nError:\r\n```\r\nRuntimeError: Given groups=1, weight of size [256, 256, 1, 1], expected input[2, 512, 41, 73] to have 256 channels, but got 512 channels instead\r\n```\n", "before_files": [{"content": "import math\n\nfrom mmcv.cnn import build_conv_layer, build_norm_layer\n\nfrom ..builder import BACKBONES\nfrom ..utils import ResLayer\nfrom .resnet import Bottleneck as _Bottleneck\nfrom .resnet import ResNet\n\n\nclass Bottleneck(_Bottleneck):\n    expansion = 4\n\n    def __init__(self,\n                 inplanes,\n                 planes,\n                 groups=1,\n                 base_width=4,\n                 base_channels=64,\n                 **kwargs):\n        \"\"\"Bottleneck block for ResNeXt.\n\n        If style is \"pytorch\", the stride-two layer is the 3x3 conv layer, if\n        it is \"caffe\", the stride-two layer is the first 1x1 conv layer.\n        \"\"\"\n        super(Bottleneck, self).__init__(inplanes, planes, **kwargs)\n\n        if groups == 1:\n            width = self.planes\n        else:\n            width = math.floor(self.planes *\n                               (base_width / base_channels)) * groups\n\n        self.norm1_name, norm1 = build_norm_layer(\n            self.norm_cfg, width, postfix=1)\n        self.norm2_name, norm2 = build_norm_layer(\n            self.norm_cfg, width, postfix=2)\n        self.norm3_name, norm3 = build_norm_layer(\n            self.norm_cfg, self.planes * self.expansion, postfix=3)\n\n        self.conv1 = build_conv_layer(\n            self.conv_cfg,\n            self.inplanes,\n            width,\n            kernel_size=1,\n            stride=self.conv1_stride,\n            bias=False)\n        self.add_module(self.norm1_name, norm1)\n        fallback_on_stride = False\n        self.with_modulated_dcn = False\n        if self.with_dcn:\n            fallback_on_stride = self.dcn.pop('fallback_on_stride', False)\n        if not self.with_dcn or fallback_on_stride:\n            self.conv2 = build_conv_layer(\n                self.conv_cfg,\n                width,\n                width,\n                kernel_size=3,\n                stride=self.conv2_stride,\n                padding=self.dilation,\n                dilation=self.dilation,\n                groups=groups,\n                bias=False)\n        else:\n            assert self.conv_cfg is None, 'conv_cfg must be None for DCN'\n            self.conv2 = build_conv_layer(\n                self.dcn,\n                width,\n                width,\n                kernel_size=3,\n                stride=self.conv2_stride,\n                padding=self.dilation,\n                dilation=self.dilation,\n                groups=groups,\n                bias=False)\n\n        self.add_module(self.norm2_name, norm2)\n        self.conv3 = build_conv_layer(\n            self.conv_cfg,\n            width,\n            self.planes * self.expansion,\n            kernel_size=1,\n            bias=False)\n        self.add_module(self.norm3_name, norm3)\n\n\n@BACKBONES.register_module()\nclass ResNeXt(ResNet):\n    \"\"\"ResNeXt backbone.\n\n    Args:\n        depth (int): Depth of resnet, from {18, 34, 50, 101, 152}.\n        in_channels (int): Number of input image channels. Default: 3.\n        num_stages (int): Resnet stages. Default: 4.\n        groups (int): Group of resnext.\n        base_width (int): Base width of resnext.\n        strides (Sequence[int]): Strides of the first block of each stage.\n        dilations (Sequence[int]): Dilation of each stage.\n        out_indices (Sequence[int]): Output from which stages.\n        style (str): `pytorch` or `caffe`. If set to \"pytorch\", the stride-two\n            layer is the 3x3 conv layer, otherwise the stride-two layer is\n            the first 1x1 conv layer.\n        frozen_stages (int): Stages to be frozen (all param fixed). -1 means\n            not freezing any parameters.\n        norm_cfg (dict): dictionary to construct and config norm layer.\n        norm_eval (bool): Whether to set norm layers to eval mode, namely,\n            freeze running stats (mean and var). Note: Effect on Batch Norm\n            and its variants only.\n        with_cp (bool): Use checkpoint or not. Using checkpoint will save some\n            memory while slowing down the training speed.\n        zero_init_residual (bool): whether to use zero init for last norm layer\n            in resblocks to let them behave as identity.\n    \"\"\"\n\n    arch_settings = {\n        50: (Bottleneck, (3, 4, 6, 3)),\n        101: (Bottleneck, (3, 4, 23, 3)),\n        152: (Bottleneck, (3, 8, 36, 3))\n    }\n\n    def __init__(self, groups=1, base_width=4, **kwargs):\n        self.groups = groups\n        self.base_width = base_width\n        super(ResNeXt, self).__init__(**kwargs)\n\n    def make_res_layer(self, **kwargs):\n        \"\"\"Pack all blocks in a stage into a ``ResLayer``\"\"\"\n        return ResLayer(\n            groups=self.groups,\n            base_width=self.base_width,\n            base_channels=self.base_channels,\n            **kwargs)\n", "path": "mmdet/models/backbones/resnext.py"}], "after_files": [{"content": "import math\n\nfrom mmcv.cnn import build_conv_layer, build_norm_layer\n\nfrom ..builder import BACKBONES\nfrom ..utils import ResLayer\nfrom .resnet import Bottleneck as _Bottleneck\nfrom .resnet import ResNet\n\n\nclass Bottleneck(_Bottleneck):\n    expansion = 4\n\n    def __init__(self,\n                 inplanes,\n                 planes,\n                 groups=1,\n                 base_width=4,\n                 base_channels=64,\n                 **kwargs):\n        \"\"\"Bottleneck block for ResNeXt.\n\n        If style is \"pytorch\", the stride-two layer is the 3x3 conv layer, if\n        it is \"caffe\", the stride-two layer is the first 1x1 conv layer.\n        \"\"\"\n        super(Bottleneck, self).__init__(inplanes, planes, **kwargs)\n\n        if groups == 1:\n            width = self.planes\n        else:\n            width = math.floor(self.planes *\n                               (base_width / base_channels)) * groups\n\n        self.norm1_name, norm1 = build_norm_layer(\n            self.norm_cfg, width, postfix=1)\n        self.norm2_name, norm2 = build_norm_layer(\n            self.norm_cfg, width, postfix=2)\n        self.norm3_name, norm3 = build_norm_layer(\n            self.norm_cfg, self.planes * self.expansion, postfix=3)\n\n        self.conv1 = build_conv_layer(\n            self.conv_cfg,\n            self.inplanes,\n            width,\n            kernel_size=1,\n            stride=self.conv1_stride,\n            bias=False)\n        self.add_module(self.norm1_name, norm1)\n        fallback_on_stride = False\n        self.with_modulated_dcn = False\n        if self.with_dcn:\n            fallback_on_stride = self.dcn.pop('fallback_on_stride', False)\n        if not self.with_dcn or fallback_on_stride:\n            self.conv2 = build_conv_layer(\n                self.conv_cfg,\n                width,\n                width,\n                kernel_size=3,\n                stride=self.conv2_stride,\n                padding=self.dilation,\n                dilation=self.dilation,\n                groups=groups,\n                bias=False)\n        else:\n            assert self.conv_cfg is None, 'conv_cfg must be None for DCN'\n            self.conv2 = build_conv_layer(\n                self.dcn,\n                width,\n                width,\n                kernel_size=3,\n                stride=self.conv2_stride,\n                padding=self.dilation,\n                dilation=self.dilation,\n                groups=groups,\n                bias=False)\n\n        self.add_module(self.norm2_name, norm2)\n        self.conv3 = build_conv_layer(\n            self.conv_cfg,\n            width,\n            self.planes * self.expansion,\n            kernel_size=1,\n            bias=False)\n        self.add_module(self.norm3_name, norm3)\n\n        if self.with_plugins:\n            self._del_block_plugins(self.after_conv1_plugin_names +\n                                    self.after_conv2_plugin_names +\n                                    self.after_conv3_plugin_names)\n            self.after_conv1_plugin_names = self.make_block_plugins(\n                width, self.after_conv1_plugins)\n            self.after_conv2_plugin_names = self.make_block_plugins(\n                width, self.after_conv2_plugins)\n            self.after_conv3_plugin_names = self.make_block_plugins(\n                self.planes * self.expansion, self.after_conv3_plugins)\n\n    def _del_block_plugins(self, plugin_names):\n        \"\"\"delete plugins for block if exist.\n\n        Args:\n            plugin_names (list[str]): List of plugins name to delete.\n        \"\"\"\n        assert isinstance(plugin_names, list)\n        for plugin_name in plugin_names:\n            del self._modules[plugin_name]\n\n\n@BACKBONES.register_module()\nclass ResNeXt(ResNet):\n    \"\"\"ResNeXt backbone.\n\n    Args:\n        depth (int): Depth of resnet, from {18, 34, 50, 101, 152}.\n        in_channels (int): Number of input image channels. Default: 3.\n        num_stages (int): Resnet stages. Default: 4.\n        groups (int): Group of resnext.\n        base_width (int): Base width of resnext.\n        strides (Sequence[int]): Strides of the first block of each stage.\n        dilations (Sequence[int]): Dilation of each stage.\n        out_indices (Sequence[int]): Output from which stages.\n        style (str): `pytorch` or `caffe`. If set to \"pytorch\", the stride-two\n            layer is the 3x3 conv layer, otherwise the stride-two layer is\n            the first 1x1 conv layer.\n        frozen_stages (int): Stages to be frozen (all param fixed). -1 means\n            not freezing any parameters.\n        norm_cfg (dict): dictionary to construct and config norm layer.\n        norm_eval (bool): Whether to set norm layers to eval mode, namely,\n            freeze running stats (mean and var). Note: Effect on Batch Norm\n            and its variants only.\n        with_cp (bool): Use checkpoint or not. Using checkpoint will save some\n            memory while slowing down the training speed.\n        zero_init_residual (bool): whether to use zero init for last norm layer\n            in resblocks to let them behave as identity.\n    \"\"\"\n\n    arch_settings = {\n        50: (Bottleneck, (3, 4, 6, 3)),\n        101: (Bottleneck, (3, 4, 23, 3)),\n        152: (Bottleneck, (3, 8, 36, 3))\n    }\n\n    def __init__(self, groups=1, base_width=4, **kwargs):\n        self.groups = groups\n        self.base_width = base_width\n        super(ResNeXt, self).__init__(**kwargs)\n\n    def make_res_layer(self, **kwargs):\n        \"\"\"Pack all blocks in a stage into a ``ResLayer``\"\"\"\n        return ResLayer(\n            groups=self.groups,\n            base_width=self.base_width,\n            base_channels=self.base_channels,\n            **kwargs)\n", "path": "mmdet/models/backbones/resnext.py"}]}